Path: blob/main/components/supervisor/cmd/debug-proxy.go
2498 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"fmt"8"net/http"9"net/http/httputil"10"net/url"11"strconv"1213"github.com/gitpod-io/gitpod/common-go/log"14"github.com/spf13/cobra"15)1617func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {18director := func(req *http.Request) {19req.URL.Scheme = target.Scheme20req.URL.Host = target.Host21if _, ok := req.Header["User-Agent"]; !ok {22// explicitly disable User-Agent so it's not set to default value23req.Header.Set("User-Agent", "")24}25req.Header.Del("X-WS-Proxy-Debug-Port")26}27return &httputil.ReverseProxy{Director: director}28}2930var debugProxyCmd = &cobra.Command{31Use: "debug-proxy",32Short: "forward request to debug workspace",33Run: func(cmd *cobra.Command, args []string) {34log.Fatal(http.ListenAndServe(":23003", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {35portStr := r.Header.Get("X-WS-Proxy-Debug-Port")36port, err := strconv.Atoi(portStr)37if err != nil || port < 1 || port > 65535 {38w.WriteHeader(502)39return40}41dst, err := url.Parse("http://localhost:" + portStr)42if err != nil {43w.WriteHeader(502)44return45}46fmt.Printf("%+v\n", dst)47proxy := NewSingleHostReverseProxy(dst)48proxy.ServeHTTP(w, r)49})))50},51}5253func init() {54rootCmd.AddCommand(debugProxyCmd)55}565758