forked from WireGuard/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathwg.go
139 lines (117 loc) · 3.12 KB
/
wg.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
package app
import (
"bufio"
"bytes"
"context"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
"github.com/bepass-org/warp-plus/wireguard/conn"
"github.com/bepass-org/warp-plus/wireguard/device"
wgtun "github.com/bepass-org/warp-plus/wireguard/tun"
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
"github.com/bepass-org/warp-plus/wiresocks"
)
func usermodeTunTest(ctx context.Context, l *slog.Logger, tnet *netstack.Net, url string) error {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Second))
defer cancel()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
client := http.Client{Transport: &http.Transport{
DialContext: tnet.DialContext,
ResponseHeaderTimeout: 5 * time.Second,
}}
resp, err := client.Head(url)
if err != nil {
l.Error("connection test failed")
continue
}
if resp.StatusCode != http.StatusOK {
l.Error("connection test failed")
continue
}
l.Info("connection test successful")
break
}
return nil
}
func waitHandshake(ctx context.Context, l *slog.Logger, dev *device.Device) error {
lastHandshakeSecs := "0"
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
get, err := dev.IpcGet()
if err != nil {
continue
}
scanner := bufio.NewScanner(strings.NewReader(get))
for scanner.Scan() {
line := scanner.Text()
if line == "" {
break
}
key, value, ok := strings.Cut(line, "=")
if !ok {
continue
}
if key == "last_handshake_time_sec" {
lastHandshakeSecs = value
break
}
}
if lastHandshakeSecs != "0" {
l.Debug("handshake complete")
break
}
l.Debug("waiting on handshake")
time.Sleep(1 * time.Second)
}
return nil
}
func establishWireguard(l *slog.Logger, conf *wiresocks.Configuration, tunDev wgtun.Device, fwmark uint32, t string) error {
// create the IPC message to establish the wireguard conn
var request bytes.Buffer
request.WriteString(fmt.Sprintf("private_key=%s\n", conf.Interface.PrivateKey))
if fwmark != 0 {
request.WriteString(fmt.Sprintf("fwmark=%d\n", fwmark))
}
for _, peer := range conf.Peers {
request.WriteString(fmt.Sprintf("public_key=%s\n", peer.PublicKey))
request.WriteString(fmt.Sprintf("persistent_keepalive_interval=%d\n", peer.KeepAlive))
request.WriteString(fmt.Sprintf("preshared_key=%s\n", peer.PreSharedKey))
request.WriteString(fmt.Sprintf("endpoint=%s\n", peer.Endpoint))
request.WriteString(fmt.Sprintf("trick=%s\n", t))
request.WriteString(fmt.Sprintf("reserved=%d,%d,%d\n", peer.Reserved[0], peer.Reserved[1], peer.Reserved[2]))
for _, cidr := range peer.AllowedIPs {
request.WriteString(fmt.Sprintf("allowed_ip=%s\n", cidr))
}
}
dev := device.NewDevice(
tunDev,
conn.NewDefaultBind(),
device.NewSLogger(l.With("subsystem", "wireguard-go")),
)
if err := dev.IpcSet(request.String()); err != nil {
return err
}
if err := dev.Up(); err != nil {
return err
}
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(15*time.Second))
defer cancel()
if err := waitHandshake(ctx, l, dev); err != nil {
dev.BindClose()
dev.Close()
return err
}
return nil
}