Skip to content

Commit

Permalink
support base url
Browse files Browse the repository at this point in the history
  • Loading branch information
cfal authored and elisescu committed Oct 30, 2024
1 parent 95ee2b9 commit 5fd0772
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (

var version string = "0.0.0"

func createServer(frontListenAddress string, frontendPath string, pty server.PTYHandler, sessionID string, allowTunneling bool, crossOrigin bool) *server.TTYServer {
func createServer(frontListenAddress string, frontendPath string, pty server.PTYHandler, sessionID string, allowTunneling bool, crossOrigin bool, baseUrlPath string) *server.TTYServer {
config := ttyServer.TTYServerConfig{
FrontListenAddress: frontListenAddress,
FrontendPath: frontendPath,
PTY: pty,
SessionID: sessionID,
AllowTunneling: allowTunneling,
CrossOrigin: crossOrigin,
BaseUrlPath: baseUrlPath,
}

server := ttyServer.NewTTYServer(config)
Expand Down Expand Up @@ -86,6 +87,7 @@ Flags:
allowTunneling := flag.Bool("A", false, "[s] Allow clients to create a TCP tunnel")
tunnelConfig := flag.String("L", "", "[c] TCP tunneling addresses: local_port:remote_host:remote_port. The client will listen on local_port for TCP connections, and will forward those to the from the server side to remote_host:remote_port")
crossOrgin := flag.Bool("cross-origin", false, "[s] Allow cross origin requests to the server")
baseUrlPath := flag.String("base-url-path", "", "[s] The base URL path on the serve")

verbose := flag.Bool("verbose", false, "Verbose logging")
flag.Usage = func() {
Expand Down Expand Up @@ -178,7 +180,15 @@ Flags:
fmt.Printf("public session: %s\n", publicURL)
}

fmt.Printf("local session: http://%s/s/local/\n", *listenAddress)
// Ensure the base URL path does not end with a forward slash,
// and that there are no excessive forward slashes at the beginning.
// A base URL of "/" will be trimmed to an empty string.
sanitizedBaseUrlPath := strings.Trim(*baseUrlPath, "/")
if sanitizedBaseUrlPath != "" {
sanitizedBaseUrlPath = "/" + sanitizedBaseUrlPath
}

fmt.Printf("local session: http://%s%s/s/local/\n", *listenAddress, sanitizedBaseUrlPath)

if !*noWaitEnter && !*headless {
fmt.Printf("Press Enter to continue!\n")
Expand All @@ -197,7 +207,7 @@ Flags:
pty = &nilPTY{}
}

server := createServer(*listenAddress, *frontendPath, pty, sessionID, *allowTunneling, *crossOrgin)
server := createServer(*listenAddress, *frontendPath, pty, sessionID, *allowTunneling, *crossOrgin, sanitizedBaseUrlPath)
if cols, rows, e := ptyMaster.GetWinSize(); e == nil {
server.WindowSize(cols, rows)
}
Expand Down
10 changes: 6 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TTYServerConfig struct {
SessionID string
AllowTunneling bool
CrossOrigin bool
BaseUrlPath string
}

// TTYServer represents the instance of a tty server
Expand Down Expand Up @@ -97,10 +98,11 @@ func NewTTYServer(config TTYServerConfig) (server *TTYServer) {
installHandlers := func(session string) {
// This function installs handlers for paths that contain the "session" passed as a
// parameter. The paths are for the static files, websockets, and other.
staticPath := "/s/" + session + "/static/"
ttyWsPath := "/s/" + session + "/ws"
tunnelWsPath := "/s/" + session + "/tws"
pathPrefix := "/s/" + session
baseUrlPath := config.BaseUrlPath
staticPath := baseUrlPath + "/s/" + session + "/static/"
ttyWsPath := baseUrlPath + "/s/" + session + "/ws/"
tunnelWsPath := baseUrlPath + "/s/" + session + "/tws"
pathPrefix := baseUrlPath + "/s/" + session

routesHandler.PathPrefix(staticPath).Handler(http.StripPrefix(staticPath,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 5fd0772

Please sign in to comment.