Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/ports-expose.go
2498 views
1
// Copyright (c) 2020 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/google/tcpproxy"
15
"github.com/gorilla/handlers"
16
"github.com/spf13/cobra"
17
"golang.org/x/xerrors"
18
)
19
20
var rewriteHostHeader bool
21
22
var portExposeCmd = &cobra.Command{
23
Use: "expose <local-port> [target-port]",
24
Short: "Makes a port available on 0.0.0.0 so that it can be exposed to the internet",
25
Long: ``,
26
Args: cobra.RangeArgs(1, 2),
27
RunE: func(cmd *cobra.Command, args []string) error {
28
srcp, err := strconv.ParseUint(args[0], 10, 16)
29
if err != nil {
30
return xerrors.Errorf("local-port cannot be parsed as int: %w", err)
31
}
32
33
trgp := srcp + 1
34
if len(args) > 1 {
35
var err error
36
trgp, err = strconv.ParseUint(args[1], 10, 16)
37
if err != nil {
38
return xerrors.Errorf("target-port cannot be parsed as int: %w", err)
39
}
40
}
41
42
if rewriteHostHeader {
43
remote, _ := url.Parse(fmt.Sprintf("http://localhost:%d", srcp))
44
host := fmt.Sprintf("localhost:%d", srcp) // Spec: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
45
proxy := httputil.NewSingleHostReverseProxy(remote)
46
originalDirector := proxy.Director
47
proxy.Director = func(r *http.Request) {
48
originalDirector(r)
49
r.Host = host
50
}
51
// we want both X-Forwarded-Proto AND X-Forwarded-Host to reach the backend
52
53
server := http.Server{
54
Addr: fmt.Sprintf(":%d", trgp),
55
Handler: handlers.ProxyHeaders(http.HandlerFunc(proxy.ServeHTTP)),
56
}
57
fmt.Printf("Proxying HTTP traffic: 0.0.0.0:%d -> 127.0.0.1:%d (with host rewriting)\n", trgp, srcp)
58
errchan := make(chan error)
59
go func() {
60
err := server.ListenAndServe()
61
errchan <- err
62
}()
63
64
select {
65
case <-cmd.Context().Done():
66
server.Close()
67
case err := <-errchan:
68
return xerrors.Errorf("reverse proxy failed: %w", err)
69
}
70
return nil
71
}
72
73
var p tcpproxy.Proxy
74
p.AddRoute(fmt.Sprintf(":%d", trgp), tcpproxy.To(fmt.Sprintf("127.0.0.1:%d", srcp)))
75
fmt.Printf("Forwarding traffic: 0.0.0.0:%d -> 127.0.0.1:%d\n", trgp, srcp)
76
errchan := make(chan error)
77
go func() {
78
err := p.Run()
79
errchan <- err
80
}()
81
select {
82
case <-cmd.Context().Done():
83
p.Close()
84
case err := <-errchan:
85
return xerrors.Errorf("reverse proxy failed: %w", err)
86
}
87
return nil
88
},
89
}
90
91
var portExposeCmdAlias = &cobra.Command{
92
Hidden: true,
93
Deprecated: "please use `ports expose` instead.",
94
Use: "forward-port <local-port> [target-port]",
95
Short: portExposeCmd.Short,
96
Long: portExposeCmd.Long,
97
Args: portExposeCmd.Args,
98
RunE: portExposeCmd.RunE,
99
}
100
101
func init() {
102
portsCmd.AddCommand(portExposeCmd)
103
portExposeCmd.Flags().BoolVarP(&rewriteHostHeader, "rewrite-host-header", "r", false, "rewrites the host header of passing HTTP requests to localhost")
104
105
rootCmd.AddCommand(portExposeCmdAlias)
106
portExposeCmdAlias.Flags().BoolVarP(&rewriteHostHeader, "rewrite-host-header", "r", false, "rewrites the host header of passing HTTP requests to localhost")
107
}
108
109