|
4 | 4 | package main
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "flag"
|
| 9 | + "net" |
8 | 10 | "os"
|
9 | 11 | "os/user"
|
10 | 12 | "path/filepath"
|
@@ -75,6 +77,21 @@ func launchSelfLater() {
|
75 | 77 | log.Println("Done waiting 2 secs. Now launching...")
|
76 | 78 | }
|
77 | 79 |
|
| 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 | + |
78 | 95 | func main() {
|
79 | 96 |
|
80 | 97 | flag.Parse()
|
@@ -215,11 +232,18 @@ func main() {
|
215 | 232 |
|
216 | 233 | socketHandler := wsHandler().ServeHTTP
|
217 | 234 |
|
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) |
222 | 238 | }
|
| 239 | + portSSL, err := getBindPort(portPlain+1, 9001) |
| 240 | + if err != nil { |
| 241 | + panic(err) |
| 242 | + } |
| 243 | + |
| 244 | + 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" |
| 245 | + extraOriginStr += ", http://localhost:" + strconv.Itoa(portPlain) + ", https://localhost:" + strconv.Itoa(portPlain) |
| 246 | + extraOriginStr += ", http://localhost:" + strconv.Itoa(portSSL) + ", https://localhost:" + strconv.Itoa(portSSL) |
223 | 247 |
|
224 | 248 | r.Use(cors.Middleware(cors.Config{
|
225 | 249 | Origins: *origins + ", " + extraOriginStr,
|
@@ -247,38 +271,23 @@ func main() {
|
247 | 271 | return
|
248 | 272 | }
|
249 | 273 |
|
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 |
| - } |
| 274 | + portStr := ":" + strconv.Itoa(portSSL) |
| 275 | + if err := r.RunTLS(portStr, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil { |
| 276 | + log.Printf("Error trying to bind to port: %v, so exiting...", err) |
| 277 | + } else { |
| 278 | + ip := "0.0.0.0" |
| 279 | + log.Print("Starting server and websocket (SSL) on " + ip + "" + port) |
264 | 280 | }
|
265 | 281 | }()
|
266 | 282 |
|
267 | 283 | 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 |
| - } |
| 284 | + |
| 285 | + portStr := ":" + strconv.Itoa(portPlain) |
| 286 | + if err := r.Run(portStr); err != nil { |
| 287 | + log.Printf("Error trying to bind to port: %v, so exiting...", err) |
| 288 | + } else { |
| 289 | + ip := "0.0.0.0" |
| 290 | + log.Print("Starting server and websocket on " + ip + "" + port) |
282 | 291 | }
|
283 | 292 | }()
|
284 | 293 |
|
|
0 commit comments