Skip to content

Discover a free port for the bridge, starting from 49152 #20

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 2 commits 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Please use the current latest version:

## Submitting an issue

Please attach the output of the commands running at http://localhost:8989 if useful.
Please attach the output of the commands running at the debug console if useful.

## Submitting a pull request

Expand Down
1 change: 0 additions & 1 deletion config.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
addr = :8989 # http service address
configUpdateInterval = 0 # Update interval for re-reading config file set via -config flag. Zero disables config file re-reading.
gc = std # Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)
hostname = unknown-hostname # Override the hostname we get from the OS
Expand Down
2 changes: 1 addition & 1 deletion hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func restart(path string) {
hiberString = "-hibernate"
}

cmd = exec.Command(exePath, "-ls", "-addr", *addr, "-regex", *regExpFilter, "-gc", *gcType, hiberString)
cmd = exec.Command(exePath, "-ls", "-regex", *regExpFilter, "-gc", *gcType, hiberString)

fmt.Println(cmd)

Expand Down
14 changes: 14 additions & 0 deletions info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"github.com/gin-gonic/gin"
)

func infoHandler(c *gin.Context) {
c.JSON(200, gin.H{
"http": "http://localhost" + port,
"https": "https://localhost" + port,
"ws": "ws://localhost" + port,
"wss": "wss://localhost" + portSSL,
})
}
51 changes: 36 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os/user"
"path/filepath"
"runtime/debug"
"strconv"
"text/template"
"time"
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily
Expand All @@ -26,8 +27,6 @@ var (
embedded_autoupdate = true
embedded_autoextract = false
hibernate = flag.Bool("hibernate", false, "start hibernated")
addr = flag.String("addr", ":8989", "http service address")
addrSSL = flag.String("addrSSL", ":8990", "https service address")
verbose = flag.Bool("v", true, "show debug logging")
//verbose = flag.Bool("v", false, "show debug logging")
isLaunchSelf = flag.Bool("ls", false, "launch self 5 seconds later")
Expand All @@ -41,6 +40,8 @@ var (
appName = flag.String("appName", "", "")
globalToolsMap = make(map[string]string)
tempToolsPath = createToolsDir()
port string
portSSL string
)

type NullWriter int
Expand Down Expand Up @@ -137,7 +138,6 @@ func main() {
}
}

f := flag.Lookup("addr")
log.Println("Version:" + version)

// hostname
Expand All @@ -159,11 +159,6 @@ func main() {
debug.SetGCPercent(-1)
}

ip := "0.0.0.0"
log.Print("Starting server and websocket on " + ip + "" + f.Value.String())

log.Println("The Arduino Create Agent is now running")

// see if they provided a regex filter
if len(*regExpFilter) > 0 {
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
Expand Down Expand Up @@ -214,17 +209,43 @@ func main() {
r.POST("/socket.io/", socketHandler)
r.Handle("WS", "/socket.io/", socketHandler)
r.Handle("WSS", "/socket.io/", socketHandler)
r.GET("/info", infoHandler)
go func() {
if err := r.RunTLS(*addrSSL, filepath.Join(dest, "cert.pem"), filepath.Join(dest, "key.pem")); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
log.Fatal("Error ListenAndServe:", err)
start := 49152
end := 65535
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
}
}
}()

go func() {
start := 49152
end := 65535
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
}
}
}()

if err := r.Run(*addr); err != nil {
log.Printf("Error trying to bind to port: %v, so exiting...", err)
log.Fatal("Error ListenAndServe:", err)
}
}()
}
setupSysTray()
Expand Down
2 changes: 1 addition & 1 deletion trayicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func setupSysTrayReal() {
for {
<-mDebug.ClickedCh
logAction("log on")
open.Start("http://localhost:8989")
open.Start("http://localhost" + port)
}
}()

Expand Down