-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener.go
More file actions
74 lines (63 loc) · 1.79 KB
/
Copy pathlistener.go
File metadata and controls
74 lines (63 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
)
// StartSessionListener binds the given port and starts an HTTP server
// that routes WebSocket upgrades to WsProxyHandler and other HTTP requests
// to HttpProxyHandler. If port is 0, the OS assigns a free port.
// Returns the actual bound port and any error.
func StartSessionListener(session *Session, port int) (int, error) {
addr := fmt.Sprintf(":%d", port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return 0, fmt.Errorf("failed to bind port %d: %w", port, err)
}
actualPort := listener.Addr().(*net.TCPAddr).Port
session.mu.Lock()
session.listener = listener
session.Port = actualPort
session.mu.Unlock()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Check if this is a WebSocket upgrade request
if isWebSocketUpgrade(r) {
HandleWsProxy(session, w, r)
return
}
// Otherwise treat as HTTP proxy
HandleHttpProxy(session, w, r)
})
server := &http.Server{Handler: mux}
go func() {
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Printf("session %s listener on port %d closed: %v", session.ID, actualPort, err)
}
}()
// Store server reference so we can shut it down later
session.mu.Lock()
session.Server = server
session.mu.Unlock()
return actualPort, nil
}
// StopSessionListener gracefully shuts down the per-session HTTP server and closes the listener.
func StopSessionListener(session *Session) {
session.mu.Lock()
server := session.Server
session.Server = nil
session.mu.Unlock()
if server != nil {
server.Shutdown(context.Background())
}
}
func isWebSocketUpgrade(r *http.Request) bool {
for _, v := range r.Header["Upgrade"] {
if v == "websocket" {
return true
}
}
return false
}