-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
193 lines (176 loc) · 6.1 KB
/
config.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
189
190
191
192
193
package utils
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"github.com/go-ini/ini"
)
type Xray struct {
Protocol string `json:"protocol"`
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"`
} `json:"settings"`
Tag string `json:"tag"`
}
type singBox struct {
Type string `json:"type"`
Tag string `json:"tag"`
Server string `json:"server"`
ServerPort int `json:"server_port"`
LocalAddress []string `json:"local_address"`
PrivateKey string `json:"private_key"`
PeerPublicKey string `json:"peer_public_key"`
Reserved string `json:"reserved"`
MTU int `json:"mtu"`
}
func ConfigGenerate(generateType string, filePath string) (string, string, error) {
var err error
var content []byte
var config []byte
var fileType string
var ReadedFile Response
if fileType, err = GetFileType(filePath); err != nil {
panic(err)
}
if fileType == "json" {
content = ReadConfig(filePath)
if err = json.Unmarshal(content, &ReadedFile); err != nil {
panic(err)
}
} else {
var cfg *ini.File
if cfg, err = ini.Load(filePath); err != nil {
panic(err)
}
section_Config := cfg.Section("Config")
ReadedFile.Config.PrivateKey = section_Config.Key("PrivateKey").String()
ReadedFile.Config.Interface.Addresses.V4 = section_Config.Key("IPv4").String()
ReadedFile.Config.Interface.Addresses.V6 = section_Config.Key("IPv6").String()
ReadedFile.Config.ClientID = section_Config.Key("ClientID").String()
ReadedFile.Config.ReservedHex = section_Config.Key("ReservedHex").String()
var intSlice []int
re := regexp.MustCompile(`\d+`)
matches := re.FindAllString(section_Config.Key("ReservedDec").String(), -1)
for _, str := range matches {
var i int
if i, err = strconv.Atoi(str); err != nil {
panic(err)
}
intSlice = append(intSlice, i)
}
ReadedFile.Config.ReservedDec = intSlice
}
if generateType == "xray" {
input := 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: ReadedFile.Config.PrivateKey,
Address: []string{ReadedFile.Config.Interface.Addresses.V4 + "/32", ReadedFile.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: "162.159.192.1:2408",
},
},
Reserved: ReadedFile.Config.ReservedDec,
MTU: 1280,
},
Tag: "wireguard",
}
if config, err = json.MarshalIndent(input, "", " "); err != nil {
panic(err)
}
return fmt.Sprintf((string(config) + "\n")), "", nil
} else if generateType == "wireguard" {
config := fmt.Sprintf(`
[Interface]
PrivateKey = `+ReadedFile.Config.PrivateKey+`
Address = `+ReadedFile.Config.Interface.Addresses.V4+`
Address = `+ReadedFile.Config.Interface.Addresses.V6+`
MTU = 1280
PreUp = ip rule add oif %s lookup 300
PostDown = ip rule del oif %s lookup 300
PreUp = ip -6 rule add oif %s lookup 300
PostDown = ip -6 rule del oif %s 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 `+ReadedFile.Config.Interface.Addresses.V4+`/32 lookup 300
#PostDown = ip rule del from `+ReadedFile.Config.Interface.Addresses.V4+`/32 lookup 300
#PreUp = ip -6 rule add from `+ReadedFile.Config.Interface.Addresses.V6+`/128 lookup 300
#PostDown = ip -6 rule del from `+ReadedFile.Config.Interface.Addresses.V6+`/128 lookup 300
# Alternative
PostUp = iptables -t mangle -A OUTPUT -s `+ReadedFile.Config.Interface.Addresses.V4+` -j MARK --set-mark 32975
PreDown = iptables -t mangle -D OUTPUT -s `+ReadedFile.Config.Interface.Addresses.V4+` -j MARK --set-mark 32975
PostUp = ip6tables -t mangle -A OUTPUT -s `+ReadedFile.Config.Interface.Addresses.V6+` -j MARK --set-mark 32975
PreDown = ip6tables -t mangle -D OUTPUT -s `+ReadedFile.Config.Interface.Addresses.V6+` -j MARK --set-mark 32975
[Peer]
PublicKey = `+"bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="+`
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = 162.159.193.199:2408
`, "%i", "%i", "%i", "%i")
return config, ReadedFile.Config.ReservedHex, nil
} else if generateType == "sing-box" {
input := singBox{
Type: "wireguard",
Tag: "wireguard-out",
Server: "engage.cloudflareclient.com",
ServerPort: 2408,
LocalAddress: []string{ReadedFile.Config.Interface.Addresses.V4 + "/32", ReadedFile.Config.Interface.Addresses.V6 + "/128"},
PrivateKey: ReadedFile.Config.PrivateKey,
PeerPublicKey: "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
Reserved: ReadedFile.Config.ClientID,
MTU: 1280,
}
if config, err = json.MarshalIndent(input, "", " "); err != nil {
panic(err)
}
return fmt.Sprintf((string(config) + "\n")), "", nil
}
panic("unsupported generateType")
}
func NftConfigGenerate(reserved string) (string, error) {
config := fmt.Sprintf(`
#!/usr/bin/nft -f
# vim:set ts=4 sw=4 et:
define routing_id = ` + reserved + `
table inet wgcf
delete table inet wgcf
table inet wgcf {
chain output {
type filter hook output priority mangle - 1; policy accept;
ip daddr 162.159.193.199 udp dport 2408 @th,72,24 set $routing_id;
}
chain input {
type filter hook input priority mangle; policy accept;
ip saddr 162.159.193.199 udp sport 2408 @th,72,24 $routing_id @th,72,24 set 0;
}
}
`)
return config, nil
}