Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/tunnel.go
2498 views
1
// Copyright (c) 2021 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
"context"
9
"strconv"
10
"time"
11
12
"github.com/spf13/cobra"
13
14
"github.com/gitpod-io/gitpod/common-go/log"
15
"github.com/gitpod-io/gitpod/supervisor/api"
16
)
17
18
var tunnelCmd = &cobra.Command{
19
Use: "tunnel <localPort> [targetPort] [visibility]",
20
Short: "opens a new tunnel",
21
Args: cobra.RangeArgs(1, 3),
22
Run: func(cmd *cobra.Command, args []string) {
23
localPort, err := strconv.ParseUint(args[0], 10, 16)
24
if err != nil {
25
log.WithError(err).Fatal("invalid local port")
26
return
27
}
28
targetPort := localPort
29
if len(args) > 1 {
30
targetPort, err = strconv.ParseUint(args[1], 10, 16)
31
if err != nil {
32
log.WithError(err).Fatal("invalid target port")
33
}
34
}
35
visiblity := api.TunnelVisiblity_host
36
if len(args) > 2 {
37
visiblity = api.TunnelVisiblity(api.TunnelVisiblity_value[args[2]])
38
}
39
40
client := api.NewPortServiceClient(dialSupervisor())
41
42
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
43
defer cancel()
44
_, err = client.Tunnel(ctx, &api.TunnelPortRequest{
45
Port: uint32(localPort),
46
TargetPort: uint32(targetPort),
47
Visibility: visiblity,
48
})
49
if err != nil {
50
log.WithError(err).Fatal("cannot tunnel")
51
}
52
},
53
}
54
55
var closeTunnelCmd = &cobra.Command{
56
Use: "close <localPort>",
57
Short: "close the tunnel",
58
Args: cobra.ExactArgs(1),
59
Run: func(cmd *cobra.Command, args []string) {
60
localPort, err := strconv.ParseUint(args[0], 10, 16)
61
if err != nil {
62
log.WithError(err).Fatal("invalid local port")
63
return
64
}
65
66
client := api.NewPortServiceClient(dialSupervisor())
67
68
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
69
defer cancel()
70
_, err = client.CloseTunnel(ctx, &api.CloseTunnelRequest{
71
Port: uint32(localPort),
72
})
73
if err != nil {
74
log.WithError(err).Fatal("cannot close the tunnel")
75
}
76
},
77
}
78
79
var autoTunnelCmd = &cobra.Command{
80
Use: "auto <enablement>",
81
Short: "controls auto tunneling",
82
Args: cobra.ExactArgs(1),
83
Run: func(cmd *cobra.Command, args []string) {
84
enablement, err := strconv.ParseBool(args[0])
85
if err != nil {
86
log.WithError(err).Fatal("invalid enablement")
87
return
88
}
89
90
client := api.NewPortServiceClient(dialSupervisor())
91
92
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
93
defer cancel()
94
_, err = client.AutoTunnel(ctx, &api.AutoTunnelRequest{
95
Enabled: enablement,
96
})
97
if err != nil {
98
log.WithError(err).Fatal("cannot to update auto tunnelling enablement")
99
}
100
},
101
}
102
103
func init() {
104
rootCmd.AddCommand(tunnelCmd)
105
tunnelCmd.AddCommand(closeTunnelCmd)
106
tunnelCmd.AddCommand(autoTunnelCmd)
107
}
108
109