MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / NewWebSocketProxyWithSessionAndLogger

Function NewWebSocketProxyWithSessionAndLogger

internal/vnc/proxy.go:69–113  ·  view source on GitHub ↗

NewWebSocketProxyWithSessionAndLogger creates a new WebSocket proxy with session notifications and shared logger.

(config *ProxyConfig, session SessionNotifier, sharedLogger *logger.Logger)

Source from the content-addressed store, hash-verified

67
68// NewWebSocketProxyWithSessionAndLogger creates a new WebSocket proxy with session notifications and shared logger.
69func NewWebSocketProxyWithSessionAndLogger(config *ProxyConfig, session SessionNotifier, sharedLogger *logger.Logger) *WebSocketProxy {
70 var proxyLogger *logger.Logger
71
72 if sharedLogger != nil {
73 proxyLogger = sharedLogger
74 } else {
75 // Use the global logger system for unified logging
76 proxyLogger = logger.GetPackageLoggerConcrete("vnc-proxy")
77 }
78
79 proxyLogger.Info("Creating new WebSocket proxy for %s (Type: %s, Node: %s)",
80 getTargetName(config), config.VMType, config.NodeName)
81 proxyLogger.Debug("Proxy config - Port: %s, Proxmox Host: %s", config.Port, config.ProxmoxHost)
82
83 return &WebSocketProxy{
84 config: config,
85 logger: proxyLogger,
86 session: session,
87 upgrader: websocket.Upgrader{
88 CheckOrigin: func(r *http.Request) bool {
89 // Allow connections from localhost only for security
90 origin := r.Header.Get("Origin")
91 if origin == "" {
92 proxyLogger.Debug("WebSocket connection with no origin header (non-browser)")
93
94 return true // Allow non-browser connections
95 }
96 u, err := url.Parse(origin)
97 if err != nil {
98 proxyLogger.Error("Failed to parse origin header: %s", origin)
99
100 return false
101 }
102 allowed := u.Hostname() == "localhost" || u.Hostname() == "127.0.0.1"
103 if !allowed {
104 proxyLogger.Error("WebSocket connection rejected from origin: %s", origin)
105 } else {
106 proxyLogger.Debug("WebSocket connection allowed from origin: %s", origin)
107 }
108
109 return allowed
110 },
111 },
112 }
113}
114
115// getTargetName returns a descriptive name for the target (VM name or node name).
116func getTargetName(config *ProxyConfig) string {

Calls 6

InfoMethod · 0.95
DebugMethod · 0.95
ErrorMethod · 0.95
GetPackageLoggerConcreteFunction · 0.92
getTargetNameFunction · 0.85
GetMethod · 0.65

Tested by

no test coverage detected