Skip to content

Commit d9eca8e

Browse files
committed
Merge pull request arduino#58 from atosatto/strictercors
Stricter CORS headers
2 parents 4ad1c58 + 1beed74 commit d9eca8e

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

main.go

+46-33
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package main
55

66
import (
7+
"errors"
78
"flag"
9+
"net"
810
"os"
911
"os/user"
1012
"path/filepath"
@@ -14,9 +16,9 @@ import (
1416
"time"
1517

1618
log "github.com/Sirupsen/logrus"
19+
"github.com/itsjamie/gin-cors"
1720
"github.com/carlescere/scheduler"
1821
"github.com/gin-gonic/gin"
19-
"github.com/itsjamie/gin-cors"
2022
"github.com/kardianos/osext"
2123
"github.com/vharitonsky/iniflags"
2224
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily
@@ -75,6 +77,21 @@ func launchSelfLater() {
7577
log.Println("Done waiting 2 secs. Now launching...")
7678
}
7779

80+
// getBindPort returns the first bindable port in the given range
81+
func getBindPort(minPort, maxPort int) (int, error) {
82+
83+
for i := minPort; i < maxPort; i++ {
84+
ln, _ := net.Listen("tcp", ":"+strconv.Itoa(i))
85+
if ln != nil {
86+
ln.Close()
87+
return i, nil
88+
}
89+
}
90+
91+
return -1, errors.New("Unable to bind any port in the range [" + strconv.Itoa(minPort) + "," + strconv.Itoa(maxPort) + ")")
92+
93+
}
94+
7895
func main() {
7996

8097
flag.Parse()
@@ -215,12 +232,23 @@ func main() {
215232

216233
socketHandler := wsHandler().ServeHTTP
217234

218-
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"
219-
220-
for i := 8990; i < 9001; i++ {
221-
extraOriginStr = extraOriginStr + ", http://localhost:" + strconv.Itoa(i) + ", https://localhost:" + strconv.Itoa(i)
235+
portPlain, err := getBindPort(8990, 9001)
236+
if err != nil {
237+
panic(err)
238+
}
239+
// All the ports p in the range 8990 <= p <= portPlain
240+
// has already been scanned and results not free.
241+
// Thus we can restrict the search range for portSSL
242+
// to [portPlain+1, 9001).
243+
portSSL, err := getBindPort(portPlain+1, 9001)
244+
if err != nil {
245+
panic(err)
222246
}
223247

248+
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"
249+
extraOriginStr += ", http://localhost:" + strconv.Itoa(portPlain) + ", https://localhost:" + strconv.Itoa(portPlain)
250+
extraOriginStr += ", http://localhost:" + strconv.Itoa(portSSL) + ", https://localhost:" + strconv.Itoa(portSSL)
251+
224252
r.Use(cors.Middleware(cors.Config{
225253
Origins: *origins + ", " + extraOriginStr,
226254
Methods: "GET, PUT, POST, DELETE",
@@ -247,38 +275,23 @@ func main() {
247275
return
248276
}
249277

250-
start := 8990
251-
end := 9000
252-
i := start
253-
for i < end {
254-
i = i + 1
255-
portSSL = ":" + strconv.Itoa(i)
256-
if err := r.RunTLS(portSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
257-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
258-
continue
259-
} else {
260-
ip := "0.0.0.0"
261-
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
262-
break
263-
}
278+
portStr := ":" + strconv.Itoa(portSSL)
279+
if err := r.RunTLS(portStr, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
280+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
281+
} else {
282+
ip := "0.0.0.0"
283+
log.Print("Starting server and websocket (SSL) on " + ip + "" + port)
264284
}
265285
}()
266286

267287
go func() {
268-
start := 8990
269-
end := 9000
270-
i := start
271-
for i < end {
272-
i = i + 1
273-
port = ":" + strconv.Itoa(i)
274-
if err := r.Run(port); err != nil {
275-
log.Printf("Error trying to bind to port: %v, so exiting...", err)
276-
continue
277-
} else {
278-
ip := "0.0.0.0"
279-
log.Print("Starting server and websocket on " + ip + "" + port)
280-
break
281-
}
288+
289+
portStr := ":" + strconv.Itoa(portPlain)
290+
if err := r.Run(portStr); err != nil {
291+
log.Printf("Error trying to bind to port: %v, so exiting...", err)
292+
} else {
293+
ip := "0.0.0.0"
294+
log.Print("Starting server and websocket on " + ip + "" + port)
282295
}
283296
}()
284297

0 commit comments

Comments
 (0)