Skip to content

Add configurable CORS allowed origins #31

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 22 additions & 1 deletion 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 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