Skip to content

Commit 4e498c5

Browse files
committed
Discover ports on which to listen
1 parent 1613570 commit 4e498c5

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

main.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ package main
3333
import (
3434
"flag"
3535
"log"
36+
"net"
3637
"os"
3738
"os/exec"
39+
"strconv"
3840
"strings"
3941
"time"
4042

@@ -88,16 +90,52 @@ func main() {
8890
os.Exit(0)
8991
}
9092

93+
http, https := findPorts()
94+
address := "http://localhost:" + strconv.Itoa(http)
9195
if !*hibernate {
96+
// Start http service
9297
go func() {
93-
// Start service
94-
if err := service.ListenAndServe(":9000"); err != nil {
98+
if err := service.ListenAndServe(":" + strconv.Itoa(http)); err != nil {
9599
service.LogError("startup", "err", err)
96100
}
97101
}()
102+
// Start https service
103+
go func() {
104+
if err := service.ListenAndServeTLS(":"+strconv.Itoa(https), "cert.pem", "key.pem"); err != nil {
105+
service.LogError("startup", "err", err)
106+
}
107+
}()
108+
}
109+
110+
setupSystray(*hibernate, version, revision, address, restart, shutdown)
111+
}
112+
113+
// findPorts returns the first two available ports for http and https listening
114+
func findPorts() (http, https int) {
115+
start := 8990
116+
end := 9000
117+
118+
// http
119+
for i := start; i < end; i++ {
120+
ln, err := net.Listen("tcp", ":"+strconv.Itoa(i))
121+
if err == nil {
122+
ln.Close()
123+
http = i
124+
break
125+
}
126+
}
127+
128+
// https
129+
for j := http + 1; j < end; j++ {
130+
ln, err := net.Listen("tcp", ":"+strconv.Itoa(j))
131+
if err == nil {
132+
ln.Close()
133+
https = j
134+
break
135+
}
98136
}
99137

100-
setupSystray(*hibernate, version, revision, restart, shutdown)
138+
return http, https
101139
}
102140

103141
// RestartFunc launches itself before exiting. It works because we pass an option to tell it to wait for 5 seconds, which gives us time to exit and unbind from serial ports and TCP/IP

systray.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ import (
3737
"github.com/skratchdot/open-golang/open"
3838
)
3939

40-
func setupSystray(hibernate bool, version, revision string, restart, shutdown func()) {
40+
func setupSystray(hibernate bool, version, revision, address string, restart, shutdown func()) {
4141
runtime.LockOSThread()
4242
if !hibernate {
43-
systray.Run(setupSystrayReal(version, revision, restart))
43+
systray.Run(setupSystrayReal(version, revision, address, restart))
4444
} else {
4545
systray.Run(setupSysTrayHibernate(restart, shutdown))
4646
}
4747
}
4848

49-
func setupSystrayReal(version, revision string, restart func()) func() {
49+
func setupSystrayReal(version, revision, address string, restart func()) func() {
5050
return func() {
5151
systray.SetIcon(icon.GetIcon())
5252
mURL := systray.AddMenuItem("Go to Arduino Create", "Arduino Create")
@@ -64,7 +64,7 @@ func setupSystrayReal(version, revision string, restart func()) func() {
6464
systray.Quit()
6565
restart()
6666
case <-mDebug.ClickedCh:
67-
open.Start("http://localhost:9000/debug")
67+
open.Start(address + "/debug")
6868
case <-mURL.ClickedCh:
6969
open.Start("https://create.arduino.cc")
7070
}

0 commit comments

Comments
 (0)