Skip to content

Commit

Permalink
Add -p option to override the HTTP server port
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Jan 4, 2024
1 parent b3adc1f commit d2ec83b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Server struct {

func New(
c *core.Core,
port int,
stderrWriter io.Writer,
shortWriter *memorywriter.MemoryWriter,
longWriter *memorywriter.MemoryWriter,
Expand All @@ -36,7 +37,7 @@ func New(
longWriter.Log("starting")

https := &http.Server{
Addr: "127.0.0.1:21325",
Addr: fmt.Sprintf("127.0.0.1:%d", port),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
}
Expand All @@ -54,7 +55,7 @@ func New(
postRouter := r.Methods("POST").Subrouter()
redirectRouter := r.Methods("GET").Path("/").Subrouter()

status.ServeStatus(statusRouter, c, version, githash, shortWriter, longWriter)
status.ServeStatus(statusRouter, c, port, version, githash, shortWriter, longWriter)
api.ServeAPI(postRouter, c, version, githash, longWriter)

status.ServeStatusRedirect(redirectRouter)
Expand Down
9 changes: 6 additions & 3 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/trezor/trezord-go/core"
Expand All @@ -15,6 +16,7 @@ import (

type status struct {
core *core.Core
port int
version string
githash string
shortMemoryWriter, longMemoryWriter *memorywriter.MemoryWriter
Expand All @@ -30,12 +32,13 @@ func ServeStatusRedirect(r *mux.Router) {
}

func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://127.0.0.1:21325/status/", http.StatusMovedPermanently)
http.Redirect(w, r, "/status/", http.StatusMovedPermanently)
}

func ServeStatus(r *mux.Router, c *core.Core, v, h string, mw, dmw *memorywriter.MemoryWriter) {
func ServeStatus(r *mux.Router, c *core.Core, p int, v, h string, mw, dmw *memorywriter.MemoryWriter) {
status := &status{
core: c,
port: p,
version: v,
githash: h,
shortMemoryWriter: mw,
Expand All @@ -47,7 +50,7 @@ func ServeStatus(r *mux.Router, c *core.Core, v, h string, mw, dmw *memorywriter
r.Use(csrf.Protect([]byte(csrfkey), csrf.Secure(false)))
r.Use(OriginCheck(map[string]string{
"/status/": "",
"/status/log.gz": "http://127.0.0.1:21325",
"/status/log.gz": fmt.Sprintf("http://127.0.0.1:%d", status.port),
}))
}

Expand Down
15 changes: 11 additions & 4 deletions trezord.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func main() {
}

var logfile string
var port int
var ports udpPorts
var touples udpTouples
var withusb bool
Expand All @@ -122,6 +123,12 @@ func main() {
"",
"Log into a file, rotating after 20MB",
)
flag.IntVar(
&port,
"p",
21325,
"Use a different port for the HTTP server. Default is 21325.",
)
flag.Var(
&ports,
"e",
Expand Down Expand Up @@ -185,7 +192,7 @@ func main() {

longMemoryWriter := memorywriter.New(90000, 200, true, verboseWriter)

printWelcomeInfo(stderrLogger)
printWelcomeInfo(stderrLogger, port)
bus := initUsb(withusb, longMemoryWriter, stderrLogger)

longMemoryWriter.Log(fmt.Sprintf("UDP port count - %d", len(ports)))
Expand Down Expand Up @@ -213,7 +220,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, githash)
s, err := server.New(c, port, stderrWriter, shortMemoryWriter, longMemoryWriter, version, githash)

if err != nil {
stderrLogger.Fatalf("https: %s", err)
Expand All @@ -228,8 +235,8 @@ func main() {
longMemoryWriter.Log("Main ended successfully")
}

func printWelcomeInfo(stderrLogger *log.Logger) {
stderrLogger.Printf("trezord v%s (rev %s) is starting", version, githash)
func printWelcomeInfo(stderrLogger *log.Logger, port int) {
stderrLogger.Printf("trezord v%s (rev %s) is starting on port %d", version, githash, port)
if core.IsDebugBinary() {
stderrLogger.Print("!! DEBUG mode enabled! Please contact Trezor support in case you did not initiate this. !!")
}
Expand Down

0 comments on commit d2ec83b

Please sign in to comment.