|
9 | 9 | "io" |
10 | 10 | "net" |
11 | 11 | "net/http" |
| 12 | + "net/http/httputil" |
| 13 | + "net/url" |
12 | 14 | "os" |
13 | 15 | "strings" |
14 | 16 | "sync" |
|
33 | 35 | ErrLocalSetupIncomplete = fmt.Errorf("local setup incomplete") |
34 | 36 | ) |
35 | 37 |
|
| 38 | +type requestLogContextKey struct{} |
| 39 | + |
| 40 | +type requestLogData struct { |
| 41 | + id string |
| 42 | + request *http.Request |
| 43 | + body []byte |
| 44 | +} |
| 45 | + |
| 46 | +type singleConnListener struct { |
| 47 | + conn net.Conn |
| 48 | + accepted bool |
| 49 | + closed bool |
| 50 | + mu sync.Mutex |
| 51 | +} |
| 52 | + |
| 53 | +func (l *singleConnListener) Accept() (net.Conn, error) { |
| 54 | + l.mu.Lock() |
| 55 | + defer l.mu.Unlock() |
| 56 | + |
| 57 | + if l.closed || l.accepted { |
| 58 | + return nil, net.ErrClosed |
| 59 | + } |
| 60 | + |
| 61 | + l.accepted = true |
| 62 | + return l.conn, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (l *singleConnListener) Close() error { |
| 66 | + l.mu.Lock() |
| 67 | + defer l.mu.Unlock() |
| 68 | + |
| 69 | + l.closed = true |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (l *singleConnListener) Addr() net.Addr { |
| 74 | + return l.conn.LocalAddr() |
| 75 | +} |
| 76 | + |
36 | 77 | type SshClient struct { |
37 | 78 | config config.ClientConfig |
38 | 79 | listener net.Listener |
@@ -216,6 +257,141 @@ func (s *SshClient) startListenerForClient() error { |
216 | 257 | } |
217 | 258 |
|
218 | 259 | func (s *SshClient) httpTunnel(src net.Conn, localEndpoint string) { |
| 260 | + if s.config.EnableHttpReverseProxy { |
| 261 | + s.httpTunnelReverseProxy(src, localEndpoint) |
| 262 | + return |
| 263 | + } |
| 264 | + |
| 265 | + s.httpTunnelLegacy(src, localEndpoint) |
| 266 | +} |
| 267 | + |
| 268 | +func (s *SshClient) httpTunnelReverseProxy(src net.Conn, localEndpoint string) { |
| 269 | + defer src.Close() |
| 270 | + |
| 271 | + target := &url.URL{ |
| 272 | + Scheme: "http", |
| 273 | + Host: localEndpoint, |
| 274 | + } |
| 275 | + |
| 276 | + transport := &http.Transport{ |
| 277 | + Proxy: http.ProxyFromEnvironment, |
| 278 | + ForceAttemptHTTP2: false, |
| 279 | + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 280 | + var d net.Dialer |
| 281 | + return d.DialContext(ctx, network, localEndpoint) |
| 282 | + }, |
| 283 | + } |
| 284 | + defer transport.CloseIdleConnections() |
| 285 | + |
| 286 | + proxy := httputil.NewSingleHostReverseProxy(target) |
| 287 | + proxy.Transport = transport |
| 288 | + |
| 289 | + defaultDirector := proxy.Director |
| 290 | + proxy.Director = func(request *http.Request) { |
| 291 | + host := request.Host |
| 292 | + defaultDirector(request) |
| 293 | + request.Host = host |
| 294 | + } |
| 295 | + |
| 296 | + proxy.ModifyResponse = func(response *http.Response) error { |
| 297 | + if !s.config.EnableRequestLogging { |
| 298 | + return nil |
| 299 | + } |
| 300 | + |
| 301 | + if response.StatusCode == http.StatusSwitchingProtocols { |
| 302 | + return nil |
| 303 | + } |
| 304 | + |
| 305 | + if strings.Contains(response.Header.Get("Content-Type"), "text/event-stream") { |
| 306 | + return nil |
| 307 | + } |
| 308 | + |
| 309 | + logData, ok := response.Request.Context().Value(requestLogContextKey{}).(*requestLogData) |
| 310 | + if !ok || logData == nil || logData.request == nil { |
| 311 | + return nil |
| 312 | + } |
| 313 | + |
| 314 | + responseBody, err := io.ReadAll(response.Body) |
| 315 | + if err != nil { |
| 316 | + if s.config.Debug { |
| 317 | + s.logDebug("Failed to read response body from reverse proxy", err) |
| 318 | + } |
| 319 | + return err |
| 320 | + } |
| 321 | + response.Body.Close() |
| 322 | + response.Body = io.NopCloser(bytes.NewBuffer(responseBody)) |
| 323 | + |
| 324 | + s.logHttpRequest(logData.id, logData.request, logData.body, response, responseBody) |
| 325 | + return nil |
| 326 | + } |
| 327 | + |
| 328 | + proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) { |
| 329 | + if s.config.Debug { |
| 330 | + s.logDebug("HTTP reverse proxy failed", err) |
| 331 | + } |
| 332 | + |
| 333 | + htmlContent := utils.LocalServerNotOnline(localEndpoint) |
| 334 | + writer.Header().Set("X-Portr-Error", "true") |
| 335 | + writer.Header().Set("X-Portr-Error-Reason", "local-server-not-online") |
| 336 | + writer.Header().Set("Content-Type", "text/html") |
| 337 | + writer.WriteHeader(http.StatusServiceUnavailable) |
| 338 | + _, _ = writer.Write([]byte(htmlContent)) |
| 339 | + } |
| 340 | + |
| 341 | + handler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 342 | + if request.Header.Get("X-Portr-Ping-Request") == "true" { |
| 343 | + writer.WriteHeader(http.StatusOK) |
| 344 | + return |
| 345 | + } |
| 346 | + |
| 347 | + if !s.config.EnableRequestLogging { |
| 348 | + proxy.ServeHTTP(writer, request) |
| 349 | + return |
| 350 | + } |
| 351 | + |
| 352 | + requestBody, err := io.ReadAll(request.Body) |
| 353 | + if err != nil { |
| 354 | + if s.config.Debug { |
| 355 | + s.logDebug("Failed to read request body for reverse proxy logging", err) |
| 356 | + } |
| 357 | + http.Error(writer, "Bad Request", http.StatusBadRequest) |
| 358 | + return |
| 359 | + } |
| 360 | + request.Body.Close() |
| 361 | + request.Body = io.NopCloser(bytes.NewBuffer(requestBody)) |
| 362 | + |
| 363 | + requestForLog := request.Clone(context.Background()) |
| 364 | + requestForLog.Header = request.Header.Clone() |
| 365 | + requestForLog.Host = request.Host |
| 366 | + if request.URL != nil { |
| 367 | + clonedURL := *request.URL |
| 368 | + requestForLog.URL = &clonedURL |
| 369 | + } |
| 370 | + |
| 371 | + logCtx := context.WithValue(request.Context(), requestLogContextKey{}, &requestLogData{ |
| 372 | + id: ulid.Make().String(), |
| 373 | + request: requestForLog, |
| 374 | + body: requestBody, |
| 375 | + }) |
| 376 | + |
| 377 | + proxy.ServeHTTP(writer, request.WithContext(logCtx)) |
| 378 | + }) |
| 379 | + |
| 380 | + server := &http.Server{ |
| 381 | + Handler: handler, |
| 382 | + ReadHeaderTimeout: 15 * time.Second, |
| 383 | + } |
| 384 | + |
| 385 | + listener := &singleConnListener{conn: src} |
| 386 | + err := server.Serve(listener) |
| 387 | + if err != nil && err != net.ErrClosed { |
| 388 | + if s.config.Debug { |
| 389 | + s.logDebug("Reverse proxy tunnel closed with error", err) |
| 390 | + } |
| 391 | + } |
| 392 | +} |
| 393 | + |
| 394 | +func (s *SshClient) httpTunnelLegacy(src net.Conn, localEndpoint string) { |
219 | 395 | var dst net.Conn |
220 | 396 |
|
221 | 397 | defer src.Close() |
|
0 commit comments