Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 2798c78

Browse files
committed
Kelp GUI: Add server to upgrade connections to HTTPS
1 parent 345f680 commit 2798c78

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

cmd/server_amd64.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const downloadCcxtUpdateIntervalLogMillis = 1000
6060

6161
type serverInputOptions struct {
6262
port *uint16
63+
ports *uint16
6364
dev *bool
6465
devAPIPort *uint16
6566
horizonTestnetURI *string
@@ -81,9 +82,10 @@ func (o serverInputOptions) String() string {
8182

8283
func init() {
8384
options := serverInputOptions{}
84-
options.port = serverCmd.Flags().Uint16P("port", "p", 8000, "port on which to serve")
85+
options.port = serverCmd.Flags().Uint16P("port", "p", 8000, "port on which to serve HTTP")
86+
options.ports = serverCmd.Flags().Uint16P("ports", "P", 8001, "port on which to serve HTTPS (only applicable if tls cert and key provided)")
8587
options.dev = serverCmd.Flags().Bool("dev", false, "run in dev mode for hot-reloading of JS code")
86-
options.devAPIPort = serverCmd.Flags().Uint16("dev-api-port", 8001, "port on which to run API server when in dev mode")
88+
options.devAPIPort = serverCmd.Flags().Uint16("dev-api-port", 8002, "port on which to run API server when in dev mode")
8789
options.horizonTestnetURI = serverCmd.Flags().String("horizon-testnet-uri", "https://horizon-testnet.stellar.org", "URI to use for the horizon instance connected to the Stellar Test Network (must contain the word 'test')")
8890
options.horizonPubnetURI = serverCmd.Flags().String("horizon-pubnet-uri", "https://horizon.stellar.org", "URI to use for the horizon instance connected to the Stellar Public Network (must not contain the word 'test')")
8991
options.noHeaders = serverCmd.Flags().Bool("no-headers", false, "do not use Amplitude or set X-App-Name and X-App-Version headers on requests to horizon")
@@ -409,17 +411,32 @@ func init() {
409411
// gui.FS is automatically compiled based on whether this is a local or deployment build
410412
gui.FileServer(r, "/", gui.FS)
411413

412-
log.Printf("starting server on port %d\n", *options.port)
414+
isTLS := *options.tlsCertFile != "" && *options.tlsKeyFile != ""
413415
threadTracker := multithreading.MakeThreadTracker()
414416
e = threadTracker.TriggerGoroutine(func(inputs []interface{}) {
415-
e1 := networking.StartServer(r, *options.port, *options.tlsCertFile, *options.tlsKeyFile)
417+
port := *options.port
418+
if isTLS {
419+
port = *options.ports
420+
}
421+
log.Printf("starting server on port %d (TLS enabled = %v)\n", port, isTLS)
422+
e1 := networking.StartServer(r, port, *options.tlsCertFile, *options.tlsKeyFile)
416423
if e1 != nil {
417424
log.Fatal(e1)
418425
}
419426
}, nil)
420427
if e != nil {
421428
log.Fatal(e)
422429
}
430+
if isTLS {
431+
// we want a new server to redirect traffic from http to https
432+
httpRedirectMux := chi.NewRouter()
433+
networking.AddHTTPSUpgrade(httpRedirectMux, "/")
434+
log.Printf("starting server on port %d to upgrade HTTP requests on the root path '/' to HTTPS connections\n", *options.port)
435+
e1 := networking.StartServer(httpRedirectMux, *options.port, "", "")
436+
if e1 != nil {
437+
log.Fatal(e1)
438+
}
439+
}
423440

424441
log.Printf("sleeping for %d seconds before showing the ready string indicator...\n", sleepNumSecondsBeforeReadyString)
425442
time.Sleep(sleepNumSecondsBeforeReadyString * time.Second)

support/networking/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"time"
1212

13+
"github.com/go-chi/chi"
1314
"github.com/lechengfan/googleauth"
1415
)
1516

@@ -112,3 +113,11 @@ func (s *server) googleAuthHandler(h http.Handler) http.Handler {
112113
Handler: h,
113114
}
114115
}
116+
117+
// AddHTTPSUpgrade adds an entry on the passed in path to redirect to an https connection
118+
func AddHTTPSUpgrade(mux *chi.Mux, path string) {
119+
mux.HandleFunc(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120+
log.Printf("received request on http port, redirecting to https connection using a temporary redirect (http status code 307)")
121+
http.Redirect(w, r, fmt.Sprintf("https://%s%s", r.Host, path), http.StatusTemporaryRedirect)
122+
}))
123+
}

0 commit comments

Comments
 (0)