Skip to content

Commit 5b4424a

Browse files
committed
ipn/yegor: generate wg config everytime
1 parent 2a0c752 commit 5b4424a

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

intra/ipn/rpn/regional.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/base64"
1111
"encoding/hex"
1212
"fmt"
13+
"net"
1314
"strings"
1415
)
1516

@@ -44,8 +45,7 @@ type RegionalWgConf struct {
4445
ServerDomainPort string `json:"ServerDomainPort"`
4546
AllowedIPs []string `json:"AllowedIPs"` // csv
4647

47-
WgConf string `json:"wgconf"` // generated
48-
UapiWgConf string `json:"uapiwgconf"` // generated
48+
UapiWgConf string `json:"uapiwgconf,omitempty"` // generated
4949
}
5050

5151
func (rwg *RegionalWgConf) String() string {
@@ -67,14 +67,6 @@ func (rwg *RegionalWgConf) GenUapiConfig() (didGenerate bool) {
6767
return rwg.genUapiConfig()
6868
}
6969

70-
func (rwg *RegionalWgConf) genUapiConfigIfNeeded() (hasConfig bool) {
71-
if len(rwg.UapiWgConf) <= 0 {
72-
return rwg.genUapiConfig()
73-
}
74-
return true
75-
}
76-
77-
// TODO: genWgConf github.com/celzero/firestack/blob/31633dc6f3/intra/ipn/warp/id.go#L260
7870
func (rwg *RegionalWgConf) genUapiConfig() (didGenerate bool) {
7971
// github.com/WireGuard/wireguard-android/blob/4ba87947ae/tunnel/src/main/java/com/wireguard/config/Config.java#L179
8072
// github.com/WireGuard/wireguard-android/blob/4ba87947ae/tunnel/src/main/java/com/wireguard/config/Interface.java#L257
@@ -138,24 +130,27 @@ func (rwg *RegionalWgConf) addrCsv() string {
138130
// preshared key, allowed IPs) onto this regional config's server endpoints.
139131
// rwg.UapiWgConf is NOT modified; the generated string is returned directly.
140132
// Returns ("", false) when rwg/perma is nil or PrivateKey/Address is absent.
141-
func (rwg *RegionalWgConf) GenUapiConfigFrom(perma *WsWgCreds) (string, bool) {
142-
if rwg == nil || perma == nil || len(perma.PrivateKey) <= 0 {
133+
func (rwg *RegionalWgConf) GenUapiConfigFrom(creds *WsWgCreds, port string) (string, bool) {
134+
if rwg == nil || creds == nil || len(creds.PrivateKey) <= 0 {
143135
return "", false
144136
}
145137

146-
addr := perma.Address
138+
addr := creds.Address
147139
if len(addr) <= 0 {
148140
return "", false // not a perma config
149141
}
150-
dns := perma.DNS
142+
dns := creds.DNS
151143
if len(dns) <= 0 {
152144
dns = cfdns4 // fallback
153145
}
154146

155-
// AllowedIPs from the permanent config may be comma-separated ("0.0.0.0/0, ::/0").
147+
// github.com/WireGuard/wireguard-android/blob/4ba87947ae/tunnel/src/main/java/com/wireguard/config/Config.java#L179
148+
// github.com/WireGuard/wireguard-android/blob/4ba87947ae/tunnel/src/main/java/com/wireguard/config/Interface.java#L257
149+
// allowedips must be individual entries in uapi, but our custom impl can handle csv
150+
// see: wgproxy.go:wgIfConfigOf => wgproxy.go:loadIPNets
156151
allowedips := []string{gw4}
157-
if len(perma.AllowedIPs) > 0 {
158-
parts := strings.Split(perma.AllowedIPs, ",")
152+
if len(creds.AllowedIPs) > 0 {
153+
parts := strings.Split(creds.AllowedIPs, ",")
159154
allowedips = make([]string, 0, len(parts))
160155
for _, p := range parts {
161156
if t := strings.TrimSpace(p); len(t) > 0 {
@@ -164,32 +159,49 @@ func (rwg *RegionalWgConf) GenUapiConfigFrom(perma *WsWgCreds) (string, bool) {
164159
}
165160
}
166161

162+
// port may be empty
163+
ipp4str := changeport(rwg.ServerIPPort4, port)
164+
ipp6str := changeport(rwg.ServerIPPort6, port)
165+
domstr := changeport(rwg.ServerDomainPort, port)
166+
167+
// not added: listen_port, persistent_keepalive_interval
167168
conf := fmt.Sprintf(`private_key=%s
168169
replace_peers=true
169170
address=%s
170171
dns=%s
171172
mtu=(auto)
172173
public_key=%s`,
173-
toHex(perma.PrivateKey),
174+
toHex(creds.PrivateKey),
174175
addr,
175176
dns,
176177
toHex(rwg.ServerPubKey),
177178
)
178179
if len(rwg.ServerIPPort4) > 0 {
179-
conf += "\nendpoint=" + rwg.ServerIPPort4
180+
conf += "\nendpoint=" + ipp4str
180181
}
181182
if len(rwg.ServerIPPort6) > 0 {
182-
conf += "\nendpoint=" + rwg.ServerIPPort6
183+
conf += "\nendpoint=" + ipp6str
183184
}
184185
if len(rwg.ServerDomainPort) > 0 {
185-
conf += "\nendpoint=" + rwg.ServerDomainPort
186+
conf += "\nendpoint=" + domstr
186187
}
187-
if len(perma.PresharedKey) > 0 {
188-
conf += "\npreshared_key=" + toHex(perma.PresharedKey)
188+
if len(creds.PresharedKey) > 0 {
189+
conf += "\npreshared_key=" + toHex(creds.PresharedKey)
189190
}
190191
for _, ip := range allowedips {
191192
conf += fmt.Sprintf("\nallowed_ip=%s", ip)
192193
}
193194

194195
return conf, true
195196
}
197+
198+
func changeport(endpoint, newPort string) string {
199+
if len(endpoint) <= 0 || len(newPort) <= 0 || newPort == "0" {
200+
return endpoint
201+
}
202+
host, _, err := net.SplitHostPort(endpoint)
203+
if err != nil {
204+
return endpoint // malformed, return as is
205+
}
206+
return net.JoinHostPort(host, newPort)
207+
}

intra/ipn/rpn/yegor.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,10 @@ func (a *WsClient) Conf(cc string) (string, error) {
10191019
usePerma = false
10201020
log.E("ws: conf: permacreds requested but nil; using dynamic creds")
10211021
}
1022+
portstr := ""
1023+
if port := a.Ops().Port(); port > 0 {
1024+
portstr = fmt.Sprintf("%d", port) // port may be 0
1025+
}
10221026
city := ""
10231027
if cccsv := strings.Split(cc, confKeySep); len(cccsv) >= 2 {
10241028
city = cccsv[0]
@@ -1068,10 +1072,9 @@ func (a *WsClient) Conf(cc string) (string, error) {
10681072
var confstr string
10691073
var confok bool
10701074
if usePerma && cfg.PermaCreds != nil {
1071-
confstr, confok = rc.GenUapiConfigFrom(cfg.PermaCreds)
1075+
confstr, confok = rc.GenUapiConfigFrom(cfg.PermaCreds, portstr)
10721076
} else {
1073-
confok = rc.genUapiConfigIfNeeded()
1074-
confstr = rc.UapiWgConf
1077+
confstr, confok = rc.GenUapiConfigFrom(cfg.Creds, portstr)
10751078
}
10761079
if confok {
10771080
out = append(out, confstr)
@@ -1083,7 +1086,7 @@ func (a *WsClient) Conf(cc string) (string, error) {
10831086
}
10841087
if len(out) > 0 {
10851088
r := rand.IntN(len(out))
1086-
log.I("ws: conf: cc %s(%s): %d/%d => chosen (any? %t): %d[%s]", cc, city, c, len(out), chooseAny, r, ids[r])
1089+
log.I("ws: conf: cc %s(%s): %d/%d => chosen (any? %t): %d[%s] (port: %s)", cc, city, c, len(out), chooseAny, r, ids[r], portstr)
10871090
return out[r], nil
10881091
}
10891092
log.E("ws: conf: cc %s(%s) not found (tot: %d)", cc, city, tot)

0 commit comments

Comments
 (0)