Skip to content

Configurable cors #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ v = true # show debug logging
appName = CreateBridge
updateUrl = http://downloads.arduino.cc/
#updateUrl = http://localhost/
origins = http://webide.arduino.cc:8080
9 changes: 8 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func uploadHandler(c *gin.Context) {
extraInfo.authdata.UserName = c.PostForm("auth_user")
extraInfo.authdata.Password = c.PostForm("auth_pass")
commandline := c.PostForm("commandline")
if commandline == "undefined" {
commandline = ""
}
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
Expand All @@ -78,7 +81,11 @@ func uploadHandler(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
}

go spProgramRW(port, board, board_rewrite, path, commandline, extraInfo)
if board_rewrite != "" {
board = board_rewrite
}

go spProgramRW(port, board, path, commandline, extraInfo)
}
}

Expand Down
4 changes: 2 additions & 2 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (h *hub) run() {
c.send <- []byte("{\"Version\" : \"" + version + "\"} ")
c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud] [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"program [portName] [board:name] [$path/to/filename/without/extension]\", \"programfromurl [portName] [board:name] [urlToHexFile]\"]} ")
c.send <- []byte("{\"Hostname\" : \"" + *hostname + "\"} ")
c.send <- []byte("{\"OS\" : \"" + runtime.GOOS + "\"} ")
case c := <-h.unregister:
delete(h.connections, c)
// put close in func cuz it was creating panics and want
Expand Down Expand Up @@ -216,11 +217,10 @@ func checkCmd(m []byte) {
//log.Print("Done with checkCmd")
}

var multi_writer = io.MultiWriter(&logger_ws, os.Stderr)

func logAction(sl string) {
if strings.HasPrefix(sl, "log on") {
*logDump = "on"
multi_writer := io.MultiWriter(&logger_ws, os.Stderr)
log.SetOutput(multi_writer)
} else if strings.HasPrefix(sl, "log off") {
*logDump = "off"
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
tempToolsPath = createToolsDir()
port string
portSSL string
origins = flag.String("origins", "", "Allowed origin list for CORS")
)

type NullWriter int
Expand All @@ -53,7 +54,7 @@ type logWriter struct{}

func (u *logWriter) Write(p []byte) (n int, err error) {
h.broadcastSys <- p
return 0, nil
return len(p), nil
}

var logger_ws logWriter
Expand Down Expand Up @@ -109,6 +110,20 @@ func main() {
iniflags.Parse()
}

// move CORS to config file compatibility, Vagrant version
if *origins == "" {
log.Println("Patching config.ini for compatibility")
f, err := os.OpenFile(dest+"/"+*configIni, os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
_, err = f.WriteString("\norigins = http://webide.arduino.cc:8080\n")
if err != nil {
panic(err)
}
f.Close()
restart("")
}
//log.SetFormatter(&log.JSONFormatter{})

log.SetLevel(log.InfoLevel)
Expand Down Expand Up @@ -194,8 +209,14 @@ func main() {

socketHandler := wsHandler().ServeHTTP

extraOriginStr := "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://create-staging.arduino.cc, https://create-staging.arduino.cc,"

for i := 8990; i < 9001; i++ {
extraOriginStr = extraOriginStr + " http://localhost:" + strconv.Itoa(i) + ", https://localhost:" + strconv.Itoa(i)
}

r.Use(cors.Middleware(cors.Config{
Origins: "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://webide.arduino.cc:8080, http://create-staging.arduino.cc, https://create-staging.arduino.cc, http://localhost:8989, https://localhost:8990",
Origins: *origins + "," + extraOriginStr,
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "",
Expand Down Expand Up @@ -270,7 +291,7 @@ const homeTemplateHtml = `<!DOCTYPE html>
var log = document.getElementById('log');
var pause = document.getElementById('myCheck');
var messages = [];
var only_log = false;
var only_log = true;

function appendLog(msg) {

Expand Down
Loading