@@ -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
5151func (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
7870func (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
168169replace_peers=true
169170address=%s
170171dns=%s
171172mtu=(auto)
172173public_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 += "\n endpoint=" + rwg . ServerIPPort4
180+ conf += "\n endpoint=" + ipp4str
180181 }
181182 if len (rwg .ServerIPPort6 ) > 0 {
182- conf += "\n endpoint=" + rwg . ServerIPPort6
183+ conf += "\n endpoint=" + ipp6str
183184 }
184185 if len (rwg .ServerDomainPort ) > 0 {
185- conf += "\n endpoint=" + rwg . ServerDomainPort
186+ conf += "\n endpoint=" + domstr
186187 }
187- if len (perma .PresharedKey ) > 0 {
188- conf += "\n preshared_key=" + toHex (perma .PresharedKey )
188+ if len (creds .PresharedKey ) > 0 {
189+ conf += "\n preshared_key=" + toHex (creds .PresharedKey )
189190 }
190191 for _ , ip := range allowedips {
191192 conf += fmt .Sprintf ("\n allowed_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+ }
0 commit comments