-
Notifications
You must be signed in to change notification settings - Fork 5
/
wireguard.go
76 lines (65 loc) · 1.91 KB
/
wireguard.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
package ray2sing
import (
"net/netip"
"strconv"
"strings"
T "github.com/sagernet/sing-box/option"
)
func WiregaurdSingbox(url string) (*T.Outbound, error) {
u, err := ParseUrl(url, 0)
if err != nil {
return nil, err
}
out := &T.Outbound{
Type: "wireguard",
Tag: u.Name,
WireGuardOptions: T.WireGuardOutboundOptions{
ServerOptions: u.GetServerOption(),
FakePackets: u.Params["ifp"],
},
}
if pk, err := getOneOf(u.Params, "privatekey", "pk"); err == nil {
out.WireGuardOptions.PrivateKey = pk
}
if pub, err := getOneOf(u.Params, "peerpublickey", "publickey", "pub", "peerpub"); err == nil {
out.WireGuardOptions.PeerPublicKey = pub
}
if psk, err := getOneOf(u.Params, "presharedkey", "psk"); err == nil {
out.WireGuardOptions.PreSharedKey = psk
}
// Parse Workers
if workerStr, ok := u.Params["workers"]; ok {
if workers, err := strconv.Atoi(workerStr); err == nil {
out.WireGuardOptions.Workers = workers
}
}
if mtuStr, ok := u.Params["mtu"]; ok {
if mtu, err := strconv.ParseUint(mtuStr, 10, 32); err == nil {
out.WireGuardOptions.MTU = uint32(mtu)
}
}
if reservedStr, ok := u.Params["reserved"]; ok {
reservedParts := strings.Split(reservedStr, ",")
for _, part := range reservedParts {
num, err := strconv.ParseUint(part, 10, 8)
if err != nil {
return nil, err // Handle the error appropriately
}
out.WireGuardOptions.Reserved = append(out.WireGuardOptions.Reserved, uint8(num))
}
}
if localAddress, err := getOneOf(u.Params, "localaddress", "ip"); err == nil {
localAddressParts := strings.Split(localAddress, ",")
for _, part := range localAddressParts {
if !strings.Contains(part, "/") {
part += "/24"
}
prefix, err := netip.ParsePrefix(part)
if err != nil {
return nil, err // Handle the error appropriately
}
out.WireGuardOptions.LocalAddress = append(out.WireGuardOptions.LocalAddress, prefix)
}
}
return out, nil
}