-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.go
124 lines (108 loc) · 2.59 KB
/
connect.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
package vpn
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/netip"
"os"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/google/uuid"
"tailscale.com/tsnet"
)
type ConnectOpt any
type connectOptOutputWriter struct {
out io.Writer
}
func ConnectOptOutputWriter(out io.Writer) ConnectOpt {
return connectOptOutputWriter{out: out}
}
type vpn struct {
net.Conn
server *tsnet.Server
tempDir string
TargetIP string
}
// Connect to the given target host with tailscale, controllerURL specifies the URL where the coordination server lives
// authKey is the key to authenticate to the vpn.
func Connect(ctx context.Context, target, controllerURL, authkey string, opts ...ConnectOpt) (*vpn, error) {
var out io.Writer
out = os.Stdout
for _, opt := range opts {
switch o := opt.(type) {
case connectOptOutputWriter:
out = o.out
default:
return nil, fmt.Errorf("unknown connect opt: %T", opt)
}
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
randomSuffix, _, _ := strings.Cut(uuid.NewString(), "-")
hostname = fmt.Sprintf("%s-%s", hostname, randomSuffix)
tempDir, err := os.MkdirTemp("", hostname)
if err != nil {
return nil, err
}
s := &tsnet.Server{
Hostname: hostname,
ControlURL: controllerURL,
AuthKey: authkey,
Dir: tempDir,
}
// now disable logging, maybe altogether later
if os.Getenv("DEBUG") == "" {
s.Logf = func(format string, args ...any) {}
}
start := time.Now()
lc, err := s.LocalClient()
if err != nil {
return nil, err
}
var firewallVPNIP netip.Addr
err = retry.Do(
func() error {
fmt.Fprintf(out, ".")
status, err := lc.Status(ctx)
if err != nil {
return err
}
if status.Self.Online {
for _, peer := range status.Peer {
if strings.HasPrefix(peer.HostName, target) {
firewallVPNIP = peer.TailscaleIPs[0]
fmt.Fprintf(out, " connected to %s (ip %s) took: %s\n", target, firewallVPNIP, time.Since(start))
return nil
}
}
}
return fmt.Errorf("did not get online")
},
retry.Attempts(50),
)
if err != nil {
return nil, err
}
// disable logging after successful connect
s.Logf = func(format string, args ...any) {}
conn, err := lc.DialTCP(ctx, firewallVPNIP.String(), 22)
return &vpn{Conn: conn, server: s, tempDir: tempDir, TargetIP: firewallVPNIP.String()}, err
}
// Close all open connections after vpn was used.
func (v *vpn) Close() error {
var errs []error
err := v.server.Close()
if err != nil {
errs = append(errs, err)
}
err = os.RemoveAll(v.tempDir)
if err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}