Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Comment on lines +19 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Blank line before list to satisfy Markdownlint (MD032)

-**Examples:**
+**Examples:**  
+
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
**Examples:**
- `./onekeyd` - Uses default port 21320
- `./onekeyd -p 21325` - Uses port 21325
- `./onekeyd -p 21321` - Uses port 21321
**Examples:**
- `./onekeyd` - Uses default port 21320
- `./onekeyd -p 21325` - Uses port 21325
- `./onekeyd -p 21321` - Uses port 21321
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

20-20: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

🤖 Prompt for AI Agents
In README.md around lines 19 to 23, add a blank line before the list of examples
to satisfy Markdownlint rule MD032. This means inserting an empty line above the
line starting with "**Examples:**" to separate the preceding paragraph or
content from the list.

This is useful when:
Comment on lines +17 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Fix article and punctuation

-OneKey bridge runs an HTTP server on port 21320 by default. You can change this port using the `-p` parameter.
+OneKey Bridge runs an HTTP server on port 21320 by default; you can change the port with the `-p` parameter.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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:
OneKey Bridge runs an HTTP server on port 21320 by default; you can change the port with 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:
🧰 Tools
🪛 LanguageTool

[uncategorized] ~17-~17: Possible missing comma found.
Context: ...n OneKey bridge runs an HTTP server on port 21320 by default. You can change this p...

(AI_HYDRA_LEO_MISSING_COMMA)

🪛 markdownlint-cli2 (0.17.2)

20-20: Lists should be surrounded by blank lines
null

(MD032, blanks-around-lists)

🤖 Prompt for AI Agents
In README.md around lines 17 to 24, correct the article usage and punctuation in
the description about the OneKey bridge HTTP server port. Change "an HTTP
server" to "a HTTP server" if following the pronunciation rule or keep "an HTTP
server" if considering the 'H' sound, and ensure punctuation such as colons and
periods are properly placed after the examples and sentences for clarity and
grammatical correctness.

- 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
Expand All @@ -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:

Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.2.2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
4 changes: 3 additions & 1 deletion onekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Comment on lines +173 to 176
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Handle “address already in use” gracefully

Right now server.New returns an http.Server that will fail only when
Run() is called. Consider binding early so you can surface a clearer
error (e.g., “port 21320 busy”) before the heavy initialisation work.

🤖 Prompt for AI Agents
In onekey.go around lines 173 to 176, the current code initializes the server
but only detects port conflicts when Run() is called, which delays error
reporting. Modify the server.New function or its usage to perform an early bind
to the specified port before full initialization, so that if the port is already
in use, the error can be caught and reported immediately with a clear message
like "port 21320 busy." This will require attempting to listen on the port early
and returning an error if binding fails, allowing the caller to handle it
gracefully before proceeding with heavy setup.

Expand Down
9 changes: 6 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Server struct {
serverPrivate

writer io.Writer
port int
}
Comment on lines 24 to 26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

port field is never read

Server.port is assigned but not used elsewhere. Either:

  1. Remove the field to keep the struct lean, or
  2. Expose a getter so callers can ask the server which port it bound.

Leaning toward option 1 for now.

🤖 Prompt for AI Agents
In server/http.go around lines 24 to 26, the port field in the Server struct is
assigned but never read or used. To fix this, remove the port field entirely
from the Server struct to keep it lean and avoid unused code.


func New(
Expand All @@ -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)
Expand All @@ -43,20 +45,21 @@ func New(
Server: https,
},
writer: allWriter,
port: httpPort,
}

r := mux.NewRouter()
statusRouter := r.PathPrefix("/status").Subrouter()
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

Expand Down
17 changes: 11 additions & 6 deletions server/status/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package status

import (
"fmt"
"net/http"

"github.com/OneKeyHQ/onekey-bridge/core"
Expand All @@ -17,35 +18,39 @@ 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)
})
Comment on lines +26 to +29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Sanity-check httpPort before using it

ServeStatusRedirect trusts the incoming httpPort. If the caller passes 0 or a negative/invalid value you’ll generate a broken URL like http://127.0.0.1:0/status/.

Add a quick guard:

+if httpPort <= 0 || httpPort > 65535 {
+    httpPort = 21320 // fallback or return an error
+}
🤖 Prompt for AI Agents
In server/status/status.go around lines 26 to 29, the ServeStatusRedirect
function uses the httpPort parameter without validation, which can lead to
invalid URLs if httpPort is zero or negative. Add a guard clause at the start of
ServeStatusRedirect to check if httpPort is a positive integer; if not, set it
to a default valid port number or return early to avoid creating broken redirect
URLs.

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)
}
Comment on lines +35 to 37
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Use 307 or 308 for redirects that preserve method

For local tooling a permanent 301 works, but if clients might POST to / you could break them. Consider a 307/308 which keeps the HTTP method intact:

-http.Redirect(w, r, fmt.Sprintf("http://127.0.0.1:%d/status/", httpPort), http.StatusMovedPermanently)
+http.Redirect(w, r, fmt.Sprintf("http://127.0.0.1:%d/status/", httpPort), http.StatusTemporaryRedirect)
🤖 Prompt for AI Agents
In server/status/status.go around lines 35 to 37, the redirect uses HTTP status
301 which does not preserve the HTTP method and can break POST requests. Change
the status code in the http.Redirect call to either 307 or 308 to ensure the
HTTP method is preserved during the redirect.


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)

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),
}))
Comment on lines 52 to 54
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Origin map can be built once

fmt.Sprintf runs on every request middleware chain build. Not critical, but you could pre-compute the map for clarity:

allowed := map[string]string{
    "/status/":       "",
    "/status/log.gz": fmt.Sprintf("http://127.0.0.1:%d", httpPort),
}
r.Use(OriginCheck(allowed))
🤖 Prompt for AI Agents
In server/status/status.go around lines 52 to 54, the origin map is being built
with fmt.Sprintf inside the middleware setup, causing it to be recomputed on
every request. To fix this, precompute the allowed origins map once before
setting up the middleware by creating the map with the formatted string assigned
to "/status/log.gz" and then pass this precomputed map to the OriginCheck
middleware. This improves clarity and avoids unnecessary repeated computation.

}

Expand Down