Skip to content

Commit 40b74c9

Browse files
committed
ipn/yegor: account for non-perma creds
1 parent 5b4424a commit 40b74c9

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

intra/ipn/rpn/regional.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
"fmt"
1313
"net"
1414
"strings"
15+
16+
"github.com/celzero/firestack/intra/log"
17+
"github.com/celzero/firestack/intra/settings"
1518
)
1619

1720
type RegionalWgConf struct {
@@ -135,13 +138,22 @@ func (rwg *RegionalWgConf) GenUapiConfigFrom(creds *WsWgCreds, port string) (str
135138
return "", false
136139
}
137140

138-
addr := creds.Address
139-
if len(addr) <= 0 {
140-
return "", false // not a perma config
141+
addr4 := rwg.ClientAddr4
142+
if len(creds.Address) > 0 {
143+
addr4 = creds.Address // perma address
144+
}
145+
dns4 := rwg.ClientDNS4
146+
if len(creds.DNS) > 0 {
147+
dns4 = creds.DNS // perma dns
141148
}
142-
dns := creds.DNS
143-
if len(dns) <= 0 {
144-
dns = cfdns4 // fallback
149+
150+
priv := creds.PrivateKey
151+
pub := creds.PublicKey
152+
psk := creds.PresharedKey
153+
154+
if len(priv) <= 0 || len(pub) <= 0 {
155+
log.E("rpn: regconf: cannot gen; empty priv (%t) or pub (%t) key", len(priv) <= 0, len(pub) <= 0)
156+
return "", false
145157
}
146158

147159
// github.com/WireGuard/wireguard-android/blob/4ba87947ae/tunnel/src/main/java/com/wireguard/config/Config.java#L179
@@ -164,17 +176,22 @@ func (rwg *RegionalWgConf) GenUapiConfigFrom(creds *WsWgCreds, port string) (str
164176
ipp6str := changeport(rwg.ServerIPPort6, port)
165177
domstr := changeport(rwg.ServerDomainPort, port)
166178

179+
if settings.Debug {
180+
log.V("rpn: regconf: gen for %s/%s (port? %s); endpoint: %s %s %s; psk? %t; allowed: %v",
181+
addr4, dns4, port, ipp4str, ipp6str, domstr, len(psk) > 0, allowedips)
182+
}
183+
167184
// not added: listen_port, persistent_keepalive_interval
168185
conf := fmt.Sprintf(`private_key=%s
169186
replace_peers=true
170187
address=%s
171188
dns=%s
172189
mtu=(auto)
173190
public_key=%s`,
174-
toHex(creds.PrivateKey),
175-
addr,
176-
dns,
177-
toHex(rwg.ServerPubKey),
191+
toHex(priv),
192+
addr4,
193+
dns4,
194+
toHex(pub),
178195
)
179196
if len(rwg.ServerIPPort4) > 0 {
180197
conf += "\nendpoint=" + ipp4str
@@ -185,8 +202,8 @@ public_key=%s`,
185202
if len(rwg.ServerDomainPort) > 0 {
186203
conf += "\nendpoint=" + domstr
187204
}
188-
if len(creds.PresharedKey) > 0 {
189-
conf += "\npreshared_key=" + toHex(creds.PresharedKey)
205+
if len(psk) > 0 {
206+
conf += "\npreshared_key=" + toHex(psk)
190207
}
191208
for _, ip := range allowedips {
192209
conf += fmt.Sprintf("\nallowed_ip=%s", ip)

0 commit comments

Comments
 (0)