-
Notifications
You must be signed in to change notification settings - Fork 224
/
proxy.go
149 lines (115 loc) · 3.37 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cmd
import (
"context"
"fmt"
"net"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/proxy"
"github.com/superfly/flyctl/terminal"
)
func newProxyCommand(client *client.Client) *Command {
proxyDocStrings := docstrings.Get("proxy")
cmd := BuildCommandKS(nil, runProxy, proxyDocStrings, client, requireSession, requireAppName)
cmd.Args = cobra.ExactArgs(1)
cmd.AddBoolFlag(BoolFlagOpts{
Name: "select",
Shorthand: "s",
Default: false,
Description: "select available instances",
})
return cmd
}
func runProxy(cmdCtx *cmdctx.CmdContext) error {
ctx := createCancellableContext()
ports := strings.Split(cmdCtx.Args[0], ":")
var local, remote string
if len(ports) < 2 {
local, remote = ports[0], ports[0]
} else {
local, remote = ports[0], ports[1]
}
client := cmdCtx.Client.API()
terminal.Debugf("Retrieving app info for %s\n", cmdCtx.AppName)
app, err := client.GetApp(cmdCtx.AppName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
return err
}
dialer, err := agentclient.Dialer(ctx, &app.Organization)
if err != nil {
return err
}
cmdCtx.IO.StartProgressIndicatorMsg("Connecting to tunnel")
if err := agentclient.WaitForTunnel(ctx, &app.Organization); err != nil {
return errors.Wrapf(err, "tunnel unavailable")
}
cmdCtx.IO.StopProgressIndicator()
if cmdCtx.Config.GetBool("select") {
instances, err := agentclient.Instances(ctx, &app.Organization, cmdCtx.AppName)
if err != nil {
return fmt.Errorf("look up %s: %w", cmdCtx.AppName, err)
}
selected := 0
prompt := &survey.Select{
Message: "Select instance:",
Options: instances.Labels,
PageSize: 15,
}
if err := survey.AskOne(prompt, &selected); err != nil {
return fmt.Errorf("selecting instance: %w", err)
}
if err := survey.AskOne(prompt, &selected); err != nil {
return fmt.Errorf("selecting instance: %w", err)
}
remote = fmt.Sprintf("[%s]:%s", instances.Addresses[selected], remote)
} else {
remote = fmt.Sprintf("%s.internal:%s", cmdCtx.AppName, remote)
}
if !agent.IsIPv6(remote) {
cmdCtx.IO.StartProgressIndicatorMsg("Waiting for host")
if err := agentclient.WaitForHost(ctx, &app.Organization, remote); err != nil {
return errors.Wrapf(err, "host unavailable")
}
cmdCtx.IO.StopProgressIndicator()
}
params := &ProxyParams{
LocalAddr: local,
RemoteAddr: remote,
Dialer: dialer,
}
return proxyConnect(ctx, params)
}
type ProxyParams struct {
RemoteAddr string
LocalAddr string
Dialer agent.Dialer
}
func proxyConnect(ctx context.Context, params *ProxyParams) error {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%s", params.LocalAddr))
if err != nil {
return err
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
}
fmt.Printf("Proxy listening on: %s\n", listener.Addr().String())
proxy := proxy.Server{
Addr: params.RemoteAddr,
Listener: listener,
Dial: params.Dialer.DialContext,
}
terminal.Debug("Starting proxy on: ", params.LocalAddr)
terminal.Debug("Connecting to ", params.RemoteAddr)
return proxy.Proxy(ctx)
}