From a2f4ecc9bf67f622a28c02742ab2e1285ed07351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=87=AF=E5=8D=87?= Date: Wed, 4 Jun 2025 19:39:15 +0800 Subject: [PATCH 1/5] feat: set http port --- .gitignore | 3 ++- README.md | 31 ++++++++++++++++++++++++++----- onekey.go | 4 +++- server/http.go | 9 ++++++--- server/status/status.go | 17 +++++++++++------ 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 9fb25a5e..1bbfd0cf 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ onekey # misc garbage .DS_Store -.vscode \ No newline at end of file +.vscode +onekeyd \ No newline at end of file diff --git a/README.md b/README.md index eaaf0dbd..27761118 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 20321` - Uses port 20321 + +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 20321 -u=false` - HTTP server on port 20321, USB disabled +- `./onekeyd -p 20321 -e 54935 -u=false` - HTTP server on port 20321,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/onekey.go b/onekey.go index 0b7aa321..710adb61 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", 21320, "HTTP server port. Default is 21320. 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 30012455..476ca9f6 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 fac48255..583542ed 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), })) } From 45e29853d390d146757a59ce0c68b1267902a23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=87=AF=E5=8D=87?= Date: Fri, 6 Jun 2025 15:19:42 +0800 Subject: [PATCH 2/5] fix: fix readme --- Dockerfile.bridge | 61 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 6 ++--- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 Dockerfile.bridge diff --git a/Dockerfile.bridge b/Dockerfile.bridge new file mode 100644 index 00000000..26cbd89a --- /dev/null +++ b/Dockerfile.bridge @@ -0,0 +1,61 @@ +FROM golang:1.19-alpine AS builder + +# 安装构建依赖 +RUN apk add --no-cache git make gcc musl-dev libusb-dev eudev-dev + +# 设置工作目录 +WORKDIR /app + +# 复制 go mod 文件 +COPY go.mod go.sum ./ + +# 下载依赖 +RUN go mod download + +# 复制源代码 +COPY . . + +# 构建应用 +RUN go build -o onekeyd . + +# 运行阶段 +FROM alpine:latest + +# 安装运行时依赖 +RUN apk add --no-cache ca-certificates curl libusb eudev netcat-openbsd + +# 创建非root用户 +RUN adduser -D -s /bin/sh onekey + +# 设置工作目录 +WORKDIR /app + +# 从构建阶段复制二进制文件 +COPY --from=builder /app/onekeyd . + +# 修改权限 +RUN chown onekey:onekey /app/onekeyd +RUN chmod +x /app/onekeyd + +# 创建启动脚本 +RUN echo '#!/bin/sh\n\ +set -e\n\ +echo "Starting OneKey Bridge..."\n\ +echo "HTTP Port: ${BRIDGE_PORT:-21321}"\n\ +echo "Emulator Host: ${EMULATOR_HOST:-localhost}"\n\ +echo "Emulator Port: ${EMULATOR_PORT:-54935}"\n\ +./onekeyd -p ${BRIDGE_PORT:-21321} -e ${EMULATOR_PORT:-54935} -u=false\n\ +' > /start-bridge.sh && chmod +x /start-bridge.sh + +# 切换到非root用户 +USER onekey + +# 暴露端口 +EXPOSE 21321 + +# 健康检查 +HEALTHCHECK --interval=10s --timeout=5s --start-period=20s --retries=3 \ + CMD nc -z localhost ${BRIDGE_PORT:-21321} || exit 1 + +# 启动命令 +CMD ["/start-bridge.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 27761118..fdd26e4e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ OneKey bridge runs an HTTP server on port 21320 by default. You can change this **Examples:** - `./onekeyd` - Uses default port 21320 - `./onekeyd -p 21325` - Uses port 21325 -- `./onekeyd -p 20321` - Uses port 20321 +- `./onekeyd -p 21321` - Uses port 21321 This is useful when: - Running multiple bridge instances simultaneously @@ -36,8 +36,8 @@ To disable all USB in order to run on some virtual environments,launch with `-u= **Combined Examples:** - `./onekeyd -p 21325 -e 54935` - HTTP server on port 21325, emulator on port 54935 -- `./onekeyd -p 20321 -u=false` - HTTP server on port 20321, USB disabled -- `./onekeyd -p 20321 -e 54935 -u=false` - HTTP server on port 20321,emulator on port 54935, USB disabled +- `./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 diff --git a/go.mod b/go.mod index 05c17712..1ee93a91 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 be019436..f69c6da4 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= From f65c37810d40136f118d7844c23e3b2e771af898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=87=AF=E5=8D=87?= Date: Tue, 17 Jun 2025 20:46:23 +0800 Subject: [PATCH 3/5] fix: update version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index c043eea7..b1b25a5f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.1 +2.2.2 From 224ddfee7cb216e7ca0c6f303b2ad213c304e2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=87=AF=E5=8D=87?= Date: Tue, 17 Jun 2025 21:06:02 +0800 Subject: [PATCH 4/5] fix: fix gitignore --- .gitignore | 3 +-- Dockerfile.bridge | 61 ----------------------------------------------- 2 files changed, 1 insertion(+), 63 deletions(-) delete mode 100644 Dockerfile.bridge diff --git a/.gitignore b/.gitignore index 1bbfd0cf..9fb25a5e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,4 @@ onekey # misc garbage .DS_Store -.vscode -onekeyd \ No newline at end of file +.vscode \ No newline at end of file diff --git a/Dockerfile.bridge b/Dockerfile.bridge deleted file mode 100644 index 26cbd89a..00000000 --- a/Dockerfile.bridge +++ /dev/null @@ -1,61 +0,0 @@ -FROM golang:1.19-alpine AS builder - -# 安装构建依赖 -RUN apk add --no-cache git make gcc musl-dev libusb-dev eudev-dev - -# 设置工作目录 -WORKDIR /app - -# 复制 go mod 文件 -COPY go.mod go.sum ./ - -# 下载依赖 -RUN go mod download - -# 复制源代码 -COPY . . - -# 构建应用 -RUN go build -o onekeyd . - -# 运行阶段 -FROM alpine:latest - -# 安装运行时依赖 -RUN apk add --no-cache ca-certificates curl libusb eudev netcat-openbsd - -# 创建非root用户 -RUN adduser -D -s /bin/sh onekey - -# 设置工作目录 -WORKDIR /app - -# 从构建阶段复制二进制文件 -COPY --from=builder /app/onekeyd . - -# 修改权限 -RUN chown onekey:onekey /app/onekeyd -RUN chmod +x /app/onekeyd - -# 创建启动脚本 -RUN echo '#!/bin/sh\n\ -set -e\n\ -echo "Starting OneKey Bridge..."\n\ -echo "HTTP Port: ${BRIDGE_PORT:-21321}"\n\ -echo "Emulator Host: ${EMULATOR_HOST:-localhost}"\n\ -echo "Emulator Port: ${EMULATOR_PORT:-54935}"\n\ -./onekeyd -p ${BRIDGE_PORT:-21321} -e ${EMULATOR_PORT:-54935} -u=false\n\ -' > /start-bridge.sh && chmod +x /start-bridge.sh - -# 切换到非root用户 -USER onekey - -# 暴露端口 -EXPOSE 21321 - -# 健康检查 -HEALTHCHECK --interval=10s --timeout=5s --start-period=20s --retries=3 \ - CMD nc -z localhost ${BRIDGE_PORT:-21321} || exit 1 - -# 启动命令 -CMD ["/start-bridge.sh"] \ No newline at end of file From 1300dddf93f6257ae1ed6db4d7f856af66fb633e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=87=AF=E5=8D=87?= Date: Tue, 17 Jun 2025 21:15:30 +0800 Subject: [PATCH 5/5] fix: emulator port 21321 --- onekey.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onekey.go b/onekey.go index 710adb61..5dadfde3 100644 --- a/onekey.go +++ b/onekey.go @@ -111,7 +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", 21320, "HTTP server port. Default is 21320. Example: onekey-go -p 21325") + flag.IntVar(&httpPort, "p", 21321, "HTTP server port. Emulator default is 21321. Example: onekey-go -p 21325") flag.Parse() if versionFlag {