diff --git a/README.md b/README.md index eaaf0db..fdd26e4 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,35 @@ All version numbers are controlled by `VERSION` at root level, no need to change For things work properly , be sure to follow build methods documented in [Build.md](Build.md) -## Emulator support +## Launch Parameters + +### HTTP Server Port Configuration + +OneKey bridge runs an HTTP server on port 21320 by default. You can change this port using the `-p` parameter. + +**Examples:** +- `./onekeyd` - Uses default port 21320 +- `./onekeyd -p 21325` - Uses port 21325 +- `./onekeyd -p 21321` - Uses port 21321 + +This is useful when: +- Running multiple bridge instances simultaneously +- Port 21320 is already in use by another application +- You need to use a specific port for network configuration + +### Emulator Support OneKey bridge has emulator support, but it's disabled by default. -To enable emulator support, launch with `-e` parameter followed by port, for example `./onekeyd -e 21324` +To enable emulator support, launch with `-e` parameter followed by port, for example `./onekeyd -e 54935` To disable all USB in order to run on some virtual environments,launch with `-u=false` parameter, for example `./onekeyd -u=false` +**Combined Examples:** +- `./onekeyd -p 21325 -e 54935` - HTTP server on port 21325, emulator on port 54935 +- `./onekeyd -p 21321 -u=false` - HTTP server on port 21321, USB disabled +- `./onekeyd -p 21321 -e 54935 -u=false` - HTTP server on port 21321,emulator on port 54935, USB disabled + If you want change default launch options, you may have to change service accordingly ## Edit Default Service Launch Options @@ -30,7 +51,7 @@ On Windows, open `shell:startup` folder, then edit the `OneKey Bridge.lnk` file ## API documentation -`onekey-bridge` starts a HTTP server on `http://localhost:21320`. AJAX calls are only enabled from onekey.so subdomains. +`onekey-bridge` starts a HTTP server on `http://localhost:21320` by default. The port can be configured using the `-p` parameter (see Launch Parameters section above). AJAX calls are only enabled from onekey.so subdomains. Server supports following API calls: @@ -51,9 +72,9 @@ OneKey Bridge has support for debug link. To support an emulator with debug link, run -`./onekeyd -ed 21324:21320 -u=false` +`./onekeyd -ed 54935:21320 -u=false` -this will detect emulator debug link on port 21320, with regular device on 21324. +this will detect emulator debug link on port 21320, with regular device on 54935. To support WebUSB devices with debug link, no option is needed, just run onekey-bridge. diff --git a/VERSION b/VERSION index c043eea..b1b25a5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.1 +2.2.2 diff --git a/go.mod b/go.mod index 05c1771..1ee93a9 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( ) require ( - github.com/BurntSushi/toml v1.2.1 // indirect + github.com/BurntSushi/toml v1.5.0 // indirect github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index be01943..f69c6da 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= -github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= +github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= github.com/gorilla/csrf v1.5.1 h1:UASc2+EB0T51tvl6/2ls2ciA8/qC7KdTO7DsOEKbttQ= diff --git a/onekey.go b/onekey.go index 0b7aa32..5dadfde 100644 --- a/onekey.go +++ b/onekey.go @@ -102,6 +102,7 @@ func main() { var verbose bool var reset bool var versionFlag bool + var httpPort int flag.StringVar(&logfile, "l", "", "Log into a file, rotating after 20MB") flag.Var(&ports, "e", "Use UDP port for emulator. Can be repeated for more ports. Example: onekey-go -e 21324 -e 21326") @@ -110,6 +111,7 @@ func main() { flag.BoolVar(&verbose, "v", false, "Write verbose logs to either stderr or logfile") flag.BoolVar(&versionFlag, "version", false, "Write version") flag.BoolVar(&reset, "r", true, "Reset USB device on session acquiring. Enabled by default (to prevent wrong device states); set to false if you plan to connect to debug link outside of bridge.") + flag.IntVar(&httpPort, "p", 21321, "HTTP server port. Emulator default is 21321. Example: onekey-go -p 21325") flag.Parse() if versionFlag { @@ -168,7 +170,7 @@ func main() { longMemoryWriter.Log("Creating core") c := core.New(b, longMemoryWriter, allowCancel(), reset) longMemoryWriter.Log("Creating HTTP server") - s, err := server.New(c, stderrWriter, shortMemoryWriter, longMemoryWriter, version) + s, err := server.New(c, stderrWriter, shortMemoryWriter, longMemoryWriter, version, httpPort) if err != nil { stderrLogger.Fatalf("https: %s", err) diff --git a/server/http.go b/server/http.go index 3001245..476ca9f 100644 --- a/server/http.go +++ b/server/http.go @@ -22,6 +22,7 @@ type Server struct { serverPrivate writer io.Writer + port int } func New( @@ -30,11 +31,12 @@ func New( shortWriter *memorywriter.MemoryWriter, longWriter *memorywriter.MemoryWriter, version string, + httpPort int, ) (*Server, error) { longWriter.Log("starting") https := &http.Server{ - Addr: "127.0.0.1:21320", + Addr: fmt.Sprintf("127.0.0.1:%d", httpPort), } allWriter := io.MultiWriter(stderrWriter, shortWriter, longWriter) @@ -43,6 +45,7 @@ func New( Server: https, }, writer: allWriter, + port: httpPort, } r := mux.NewRouter() @@ -50,13 +53,13 @@ func New( postRouter := r.Methods("POST").Subrouter() redirectRouter := r.Methods("GET").Path("/").Subrouter() - status.ServeStatus(statusRouter, c, version, shortWriter, longWriter) + status.ServeStatus(statusRouter, c, version, shortWriter, longWriter, httpPort) err := api.ServeAPI(postRouter, c, version, longWriter) if err != nil { panic(err) // only error is an error from originValidator regexp constructor } - status.ServeStatusRedirect(redirectRouter) + status.ServeStatusRedirect(redirectRouter, httpPort) var h http.Handler = r diff --git a/server/status/status.go b/server/status/status.go index fac4825..583542e 100644 --- a/server/status/status.go +++ b/server/status/status.go @@ -1,6 +1,7 @@ package status import ( + "fmt" "net/http" "github.com/OneKeyHQ/onekey-bridge/core" @@ -17,27 +18,31 @@ type status struct { core *core.Core version string shortMemoryWriter, longMemoryWriter *memorywriter.MemoryWriter + port int } const csrfkey = "slk0118h51w2qiw4fhrfyd84f59j81ln" -func ServeStatusRedirect(r *mux.Router) { - r.HandleFunc("/", redirect) +func ServeStatusRedirect(r *mux.Router, httpPort int) { + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + redirect(w, r, httpPort) + }) r.Use(OriginCheck(map[string]string{ "": "", })) } -func redirect(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "http://127.0.0.1:21320/status/", http.StatusMovedPermanently) +func redirect(w http.ResponseWriter, r *http.Request, httpPort int) { + http.Redirect(w, r, fmt.Sprintf("http://127.0.0.1:%d/status/", httpPort), http.StatusMovedPermanently) } -func ServeStatus(r *mux.Router, c *core.Core, v string, mw, dmw *memorywriter.MemoryWriter) { +func ServeStatus(r *mux.Router, c *core.Core, v string, mw, dmw *memorywriter.MemoryWriter, httpPort int) { status := &status{ core: c, version: v, shortMemoryWriter: mw, longMemoryWriter: dmw, + port: httpPort, } r.Methods("GET").Path("/").HandlerFunc(status.statusPage) r.Methods("POST").Path("/log.gz").HandlerFunc(status.statusGzip) @@ -45,7 +50,7 @@ func ServeStatus(r *mux.Router, c *core.Core, v string, mw, dmw *memorywriter.Me r.Use(csrf.Protect([]byte(csrfkey), csrf.Secure(false))) r.Use(OriginCheck(map[string]string{ "/status/": "", - "/status/log.gz": "http://127.0.0.1:21320", + "/status/log.gz": fmt.Sprintf("http://127.0.0.1:%d", httpPort), })) }