From 8494054c11a0c2f6e0917c501e7831ff1b23efd3 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 13 Nov 2015 13:11:54 +0100 Subject: [PATCH 1/2] add configurable CORS allowed origins --- config.ini | 1 + main.go | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config.ini b/config.ini index f0639f2c6..dae4bce0e 100644 --- a/config.ini +++ b/config.ini @@ -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 \ No newline at end of file diff --git a/main.go b/main.go index 87a644dbd..c92bfe525 100755 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ var ( tempToolsPath = createToolsDir() port string portSSL string + origins = flag.String("origins", "", "Allowed origin list for CORS") ) type NullWriter int @@ -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) @@ -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: "", From aa45d8366a29a925ef3dda9aa1e77dd4cee6971e Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 27 Nov 2015 12:51:12 +0100 Subject: [PATCH 2/2] Fix cors by adding the correct commas --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index c92bfe525..a95038a96 100755 --- a/main.go +++ b/main.go @@ -209,10 +209,10 @@ 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," + 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) + extraOriginStr = extraOriginStr + ", http://localhost:" + strconv.Itoa(i) + ", https://localhost:" + strconv.Itoa(i) } r.Use(cors.Middleware(cors.Config{