-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate.go
103 lines (89 loc) · 3.43 KB
/
generate.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
package utils
import (
"encoding/json"
"fmt"
C "github.com/ArchiveNetwork/wgcf-cli/constant"
)
func GenXray(resStruct C.Response) (body []byte, err error) {
in_struct := C.Xray{
Protocol: "wireguard",
Settings: struct {
SecretKey string `json:"secretKey"`
Address []string `json:"address"`
Peers []struct {
PublicKey string `json:"publicKey"`
AllowedIPs []string `json:"allowedIPs"`
Endpoint string `json:"endpoint"`
} `json:"peers"`
Reserved []int `json:"reserved"`
MTU int `json:"mtu"`
}{
SecretKey: resStruct.Config.PrivateKey,
Address: []string{resStruct.Config.Interface.Addresses.V4 + "/32", resStruct.Config.Interface.Addresses.V6 + "/128"},
Peers: []struct {
PublicKey string `json:"publicKey"`
AllowedIPs []string `json:"allowedIPs"`
Endpoint string `json:"endpoint"`
}{
{
PublicKey: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
AllowedIPs: []string{"0.0.0.0/0", "::/0"},
Endpoint: resStruct.Config.Peers[0].Endpoint.Host,
},
},
Reserved: resStruct.Config.ReservedDec,
MTU: 1280,
},
Tag: "wireguard",
}
body, err = json.MarshalIndent(in_struct, "", " ")
return
}
func GenSing(resStruct C.Response) (body []byte, err error) {
in_struct := C.Sing{
Type: "wireguard",
Tag: "wireguard-out",
Server: resStruct.Config.Peers[0].Endpoint.Host,
ServerPort: 2408,
LocalAddress: []string{resStruct.Config.Interface.Addresses.V4 + "/32", resStruct.Config.Interface.Addresses.V6 + "/128"},
PrivateKey: resStruct.Config.PrivateKey,
PeerPublicKey: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
Reserved: resStruct.Config.ClientID,
MTU: 1280,
}
body, err = json.MarshalIndent(in_struct, "", " ")
return
}
func GenWgQuick(resStruct C.Response) (body []byte, err error) {
in_str := fmt.Sprint(`
[Interface]
PrivateKey = ` + resStruct.Config.PrivateKey + `
Address = ` + resStruct.Config.Interface.Addresses.V4 + `/32
Address = ` + resStruct.Config.Interface.Addresses.V6 + `/128
MTU = 1280
Table = 300
PreUp = ip rule add oif %i lookup 300
PostDown = ip rule del oif %i lookup 300
PreUp = ip -6 rule add oif %i lookup 300
PostDown = ip -6 rule del oif %i lookup 300
PreUp = ip rule add fwmark 32975 lookup 300
PostDown = ip rule del fwmark 32975 lookup 300
PreUp = ip -6 rule add fwmark 32975 lookup 300
PostDown = ip -6 rule del fwmark 32975 lookup 300
#PreUp = ip rule add from ` + resStruct.Config.Interface.Addresses.V4 + `/32 lookup 300
#PostDown = ip rule del from ` + resStruct.Config.Interface.Addresses.V4 + `/32 lookup 300
#PreUp = ip -6 rule add from ` + resStruct.Config.Interface.Addresses.V6 + `/128 lookup 300
#PostDown = ip -6 rule del from ` + resStruct.Config.Interface.Addresses.V6 + `/128 lookup 300
# Alternative
PostUp = iptables -t mangle -A OUTPUT -s ` + resStruct.Config.Interface.Addresses.V4 + ` -j MARK --set-mark 32975
PreDown = iptables -t mangle -D OUTPUT -s ` + resStruct.Config.Interface.Addresses.V4 + ` -j MARK --set-mark 32975
PostUp = ip6tables -t mangle -A OUTPUT -s ` + resStruct.Config.Interface.Addresses.V6 + ` -j MARK --set-mark 32975
PreDown = ip6tables -t mangle -D OUTPUT -s ` + resStruct.Config.Interface.Addresses.V6 + ` -j MARK --set-mark 32975
[Peer]
PublicKey = ` + resStruct.Config.Peers[0].PublicKey + `
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = ` + resStruct.Config.Peers[0].Endpoint.V4 + `
`)
body = []byte(in_str)
return
}