Skip to content

Commit 0deb11b

Browse files
committed
add configurable CORS allowed origins
1 parent 01ca85f commit 0deb11b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

config.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ v = true # show debug logging
77
appName = CreateBridge
88
updateUrl = http://downloads.arduino.cc/
99
#updateUrl = http://localhost/
10+
origins = http://webide.arduino.cc:8080

main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
tempToolsPath = createToolsDir()
4444
port string
4545
portSSL string
46+
origins = flag.String("origins", "", "Allowed origin list for CORS")
4647
)
4748

4849
type NullWriter int
@@ -109,6 +110,20 @@ func main() {
109110
iniflags.Parse()
110111
}
111112

113+
// move CORS to config file compatibility, Vagrant version
114+
if *origins == "" {
115+
log.Println("Patching config.ini for compatibility")
116+
f, err := os.OpenFile(dest+"/"+*configIni, os.O_APPEND|os.O_WRONLY, 0666)
117+
if err != nil {
118+
panic(err)
119+
}
120+
_, err = f.WriteString("\norigins = http://webide.arduino.cc:8080\n")
121+
if err != nil {
122+
panic(err)
123+
}
124+
f.Close()
125+
restart("")
126+
}
112127
//log.SetFormatter(&log.JSONFormatter{})
113128

114129
log.SetLevel(log.InfoLevel)
@@ -194,8 +209,14 @@ func main() {
194209

195210
socketHandler := wsHandler().ServeHTTP
196211

212+
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"
213+
214+
for i := 8990; i < 9001; i++ {
215+
extraOriginStr = extraOriginStr + ", http://localhost:" + strconv.Itoa(i) + ", https://localhost:" + strconv.Itoa(i)
216+
}
217+
197218
r.Use(cors.Middleware(cors.Config{
198-
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",
219+
Origins: *origins + ", " + extraOriginStr,
199220
Methods: "GET, PUT, POST, DELETE",
200221
RequestHeaders: "Origin, Authorization, Content-Type",
201222
ExposedHeaders: "",

0 commit comments

Comments
 (0)