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