-
Notifications
You must be signed in to change notification settings - Fork 25
/
direct_connect.go
188 lines (162 loc) · 5.31 KB
/
direct_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"net"
"net/netip"
"os"
"sync"
"time"
"io"
"log"
"net/http"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/tun/netstack"
gtcp "gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
gudp "gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"wiretap/peer"
"wiretap/transport/icmp"
"wiretap/transport/tcp"
"wiretap/transport/udp"
"github.com/rainforestapp/rainforest-cli/rainforest"
"github.com/urfave/cli"
)
func launchDirectConnect(c cliContext, rfApi *rainforest.Client) error {
tunnelId := c.String("tunnel-id")
if tunnelId == "" {
log.Println("Starting Direct Connect Tunnel")
} else {
log.Println("Starting Direct Connect Tunnel for tunnel ID:", tunnelId)
}
// Generate a wireguard public/private keypair
privateKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to generate private key: %w", err), 1)
}
publicKey := privateKey.PublicKey()
// Ask the rainforest api to set up our connection, returning the server details
serverDetails, err := rfApi.SetupDirectConnectTunnel(tunnelId, publicKey.String())
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to configure direct connect: %w", err), 1)
}
var (
wg sync.WaitGroup
lock sync.Mutex
)
// Static configuration settings
listenPort := 51820
persistentKeepaliveInterval := 25
mtu := 1420
allowedIPs := []string{"198.18.18.1/32", "fd:16::1/128"}
ipv4Addr, err := netip.ParseAddr("198.18.18.2")
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to parse ipv4 address: %w", err), 1)
}
ipv6Addr, err := netip.ParseAddr("fd:16::2")
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to parse ipv6 address: %w", err), 1)
}
wireguardConfigArgs := peer.ConfigArgs{
PrivateKey: privateKey.String(),
ListenPort: listenPort,
Peers: []peer.PeerConfigArgs{
{
PublicKey: serverDetails.ServerPublicKey,
Endpoint: fmt.Sprintf("%s:%d", serverDetails.ServerEndpoint, serverDetails.ServerPort),
PersistentKeepaliveInterval: persistentKeepaliveInterval,
AllowedIPs: allowedIPs,
},
},
Addresses: []string{ipv4Addr.String() + "/32"},
}
wireguardConfig, err := peer.GetConfig(wireguardConfigArgs)
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to make wireguard configuration: %w", err), 1)
}
// Create virtual wireguard interface with these addresses and MTU.
tunnelAddrs := []netip.Addr{ipv4Addr, ipv6Addr}
tunWireguard, tnetWireguard, err := netstack.CreateNetTUN(
tunnelAddrs,
[]netip.Addr{},
mtu,
)
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create wireguard TUN: %w", err), 1)
}
// Set the interface to promiscuous mode so forwarding works properly
s := tnetWireguard.Stack()
s.SetPromiscuousMode(1, true)
// TCP Forwarding settings.
catchTimeout := 5 * 1000
connTimeout := 5 * 1000
keepaliveIdle := 60
keepaliveCount := 3
keepaliveInterval := 60
// TCP Forwarding mechanism.
tcpConfig := tcp.Config{
CatchTimeout: time.Duration(catchTimeout) * time.Millisecond,
ConnTimeout: time.Duration(connTimeout) * time.Millisecond,
KeepaliveIdle: time.Duration(keepaliveIdle) * time.Second,
KeepaliveInterval: time.Duration(keepaliveInterval) * time.Second,
KeepaliveCount: int(keepaliveCount),
Tnet: tnetWireguard,
StackLock: &lock,
}
tcpForwarder := gtcp.NewForwarder(s, 0, 65535, tcp.Handler(tcpConfig))
s.SetTransportProtocolHandler(gtcp.ProtocolNumber, tcpForwarder.HandlePacket)
// UDP Forwarding mechanism.
udpConfig := udp.Config{
Tnet: tnetWireguard,
StackLock: &lock,
}
s.SetTransportProtocolHandler(gudp.ProtocolNumber, udp.Handler(udpConfig))
// Configure the wireguard device with the wireguard details
var logger int = device.LogLevelError // Or device.LogLevelVerbose or device.LogLevelSilent
devWireguard := device.NewDevice(tunWireguard, conn.NewDefaultBind(), device.NewLogger(logger, ""))
// Configure wireguard.
err = devWireguard.IpcSet(wireguardConfig.AsIPC())
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to configure wireguard device: %w", err), 1)
}
err = devWireguard.Up()
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to bring up device: %w", err), 1)
}
// Handlers that require long-running routines:
// Start ICMP Handler.
wg.Add(1)
go func() {
icmp.Handle(tnetWireguard, &lock)
wg.Done()
}()
// Start API handler.
wg.Add(1)
go func() {
apiHandler(tnetWireguard, ipv4Addr, uint16(80))
wg.Done()
}()
log.Printf("Rainforest Direct Connect running (name: %s, PID: %d)! Press Ctrl+C to exit\n", serverDetails.Name, os.Getpid())
wg.Wait()
return nil
}
func apiHandler(tnet *netstack.Net, addr netip.Addr, port uint16) {
// Stand up API server.
listener, err := tnet.ListenTCP(&net.TCPAddr{IP: addr.AsSlice(), Port: int(port)})
if err != nil {
log.Panic(err)
}
http.HandleFunc("/ping", handlePing)
err = http.Serve(listener, nil)
if err != nil {
log.Panic(err)
}
}
// handlePing responds with pong message.
func handlePing(w http.ResponseWriter, r *http.Request) {
log.Printf("(client %s) - API: %s", r.RemoteAddr, r.RequestURI)
_, err := io.WriteString(w, "pong\n")
if err != nil {
log.Printf("API Error: %v", err)
}
}