Skip to content

Stricter CORS headers #58

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 3 commits into from
Jan 23, 2016
Merged
Changes from 1 commit
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
73 changes: 41 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package main

import (
"errors"
"flag"
"net"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -75,6 +77,21 @@ func launchSelfLater() {
log.Println("Done waiting 2 secs. Now launching...")
}

// getBindPort returns the first bindable port in the given range
func getBindPort(minPort, maxPort int) (int, error) {

for i := minPort; i < maxPort; i++ {
ln, _ := net.Listen("tcp", ":"+strconv.Itoa(i))
if ln != nil {
ln.Close()
return i, nil
}
}

return -1, errors.New("Unable to bind any port in the range [" + strconv.Itoa(minPort) + "," + strconv.Itoa(maxPort) + ")")

}

func main() {

flag.Parse()
Expand Down Expand Up @@ -215,11 +232,18 @@ 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)
portPlain, err := getBindPort(8990, 9001)
if err != nil {
panic(err)
}
portSSL, err := getBindPort(portPlain+1, 9001)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can maybe add a comment here to explain why you're passing portPlain+1. I heard people confused by it in the office. But of course if you find a free port, the previous are all occupied, and there's no reason to cycle through them again.

if err != nil {
panic(err)
}

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 += ", http://localhost:" + strconv.Itoa(portPlain) + ", https://localhost:" + strconv.Itoa(portPlain)
extraOriginStr += ", http://localhost:" + strconv.Itoa(portSSL) + ", https://localhost:" + strconv.Itoa(portSSL)

r.Use(cors.Middleware(cors.Config{
Origins: *origins + ", " + extraOriginStr,
Expand Down Expand Up @@ -247,38 +271,23 @@ func main() {
return
}

start := 8990
end := 9000
i := start
for i < end {
i = i + 1
portSSL = ":" + strconv.Itoa(i)
if err := r.RunTLS(portSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
continue
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
break
}
portStr := ":" + strconv.Itoa(portSSL)
if err := r.RunTLS(portStr, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
}
}()

go func() {
start := 8990
end := 9000
i := start
for i < end {
i = i + 1
port = ":" + strconv.Itoa(i)
if err := r.Run(port); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
continue
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket on " + ip + "" + port)
break
}

portStr := ":" + strconv.Itoa(portPlain)
if err := r.Run(portStr); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
} else {
ip := "0.0.0.0"
log.Print("Starting server and websocket on " + ip + "" + port)
}
}()

Expand Down