@@ -139,7 +139,7 @@ type wgtun struct {
139139 dns atomic.Pointer [multihost.MH ] // dns resolver for this interface
140140 remote atomic.Pointer [multihost.MHMap ] // peer (remote endpoint) addrs
141141 amnezia atomic.Pointer [wg.Amnezia ] // amnezia/warp config, if any
142- allowed * core. Volatile [[]netip.Prefix ] // allowed ips by all peers
142+ allowed atomic. Pointer [[]netip.Prefix ] // allowed ips by all peers
143143
144144 rt x.IpTree // route table for this interface
145145
@@ -447,7 +447,7 @@ func (w *wgproxy) redoPeers() {
447447
448448func (h * wgtun ) ipcset (d * device.Device ) {
449449 if cfg := h .uapicfg .Load (); len (cfg ) > 0 {
450- cpcfg := cfg // copies string
450+ cpcfg := strings . Clone ( cfg ) // copies string
451451 _ , _ = wgIfConfigOf (h .id , & cpcfg ) // removes non-uapi fields
452452 ipcerr := d .IpcSet (cpcfg )
453453 logei (ipcerr )("proxy: wg: %s: ipcset: re-apply; err %v" , h .tag (), ipcerr )
@@ -535,9 +535,9 @@ func (w *wgproxy) update(id, txt string) (ok bool) {
535535
536536 // reusing existing tunnel (interface config unchanged)
537537 // but peer config may have changed!
538- log .I ("proxy: wg: update: (%s<>%s): reuse; mtu: %d=>%d, allowed: %d=>%d; peers: %d; dns: %d=>%d; endpoint: %d=>%d" ,
538+ log .I ("proxy: wg: update: (%s<>%s): reuse; mtu: %d=>%d, allowed: %d=>%d; peers: %d; dns: %d=>%d; endpoint: %d=>%d (%s => %s) " ,
539539 id , w .who (), w .ep .MTU (), maybeNewMtu , w .rt .Len (), len (opts .allowed ), len (opts .peers ), w .dns .Load ().Len (), opts .dns .Len (),
540- w .remote .Load ().Len () /*remote.Load may return nil*/ , opts .eps .Len ())
540+ w .remote .Load ().Len () /*remote.Load may return nil*/ , opts .eps .Len (), w . remote . Load (). Endpoints (), opts . eps . Endpoints () )
541541
542542 w .allowedIPs (opts .allowed )
543543 w .remote .Store (opts .eps ) // requires refresh (wg.Conn:ParseEndpoint must be re-called)
@@ -552,8 +552,9 @@ func (w *wgproxy) update(id, txt string) (ok bool) {
552552 log .W ("proxy: updating wg(%s<>%s) ipcset; err %v" , id , w .who (), ipcerr )
553553 return anew
554554 }
555- // w.Device is assumed to be Up
556- w .uapicfg .Store (txt ) // persist the updated UAPI peer config
555+ // w.Device is assumed to be Up? Send an EventDown first?
556+ w .events <- tun .EventUp // re-apply interface config to wg device
557+ w .uapicfg .Store (txt ) // persist the updated UAPI peer config
557558
558559 return reused
559560}
@@ -563,7 +564,7 @@ func (w *wgtun) allowedIPs(allowed []netip.Prefix) {
563564 for _ , ipnet := range allowed {
564565 w .rt .Set (ipnet .String (), w .id )
565566 }
566- w .allowed .Store (allowed )
567+ w .allowed .Store (& allowed )
567568 // TODO: remove IPs on peer replace
568569 // TODO: remove IPs on peer update
569570}
@@ -586,6 +587,7 @@ func wgIfConfigOf(id string, txtptr *string) (opts wgifopts, err error) {
586587 opts .peers = make (map [string ]device.NoisePublicKey )
587588 opts .amnezia = wg .NewAmnezia (id )
588589 opts .mtu = MAXMTU // auto
590+ opts .allowed = make ([]netip.Prefix , 0 )
589591
590592 var currentPeer * multihost.MH
591593 for r .Scan () {
@@ -774,7 +776,7 @@ func loadIPNets(id string, out *[]netip.Prefix, v string) (err error) {
774776
775777// ref: github.com/WireGuard/wireguard-android/blob/713947e432/tunnel/tools/libwg-go/api-android.go#L76
776778func NewWgProxy (pctx context.Context , id string , ctl protect.Controller , px ProxyProvider , lp LinkProps , cfg string ) (* wgproxy , error ) {
777- ogcfg := cfg
779+ ogcfg := strings . Clone ( cfg )
778780 opts , err := wgIfConfigOf (id , & cfg )
779781 if err != nil {
780782 log .E ("proxy: wg: %s failure getting opts from config %v" , id , err )
@@ -944,7 +946,6 @@ func makeWgTun(pctx context.Context, id, cfg string, ctl protect.Controller, px
944946 px : px ,
945947 via : core .NewZeroVolatile [* core.WeakRef [Proxy ]](),
946948 rev : core .NewVolatile (lp .rev ),
947- allowed : core .NewVolatile (ifopts .allowed ),
948949 rt : x .NewIpTree (), // must be set to allowedaddrs
949950 status : core .NewVolatile (TUP ),
950951 preferOffload : preferOffload (id ),
@@ -954,6 +955,7 @@ func makeWgTun(pctx context.Context, id, cfg string, ctl protect.Controller, px
954955 }
955956 t .dns .Store (ifopts .dns )
956957 t .remote .Store (ifopts .eps ) // may be nil
958+ t .allowed .Store (& ifopts .allowed )
957959 t .amnezia .Store (ifopts .amnezia )
958960 t .hdl = core .Loc (t )
959961 t .idhdl = id2 + ":" + core .LocStr (t )
@@ -1284,7 +1286,7 @@ func (w *wgproxy) Stat() (out *x.RouterStats) {
12841286 }
12851287
12861288 if settings .Debug {
1287- out .Extra = w .remote .Load ().String () + "\n " + w .dns .Load ().String () + "\n allowed:" + fmt .Sprintf ("%v" , w .allowed .Load ())
1289+ out .Extra = w .remote .Load ().String () + "\n " + w .dns .Load ().String () + "\n allowed:" + fmt .Sprintf ("%v" , * w .allowed .Load ())
12881290
12891291 log .VV ("proxy: wg: %s stats: rx: %d, tx: %d, r: %s (rlastok: %s), w: %s (wlastok: %s), lastok: %s" ,
12901292 w .tag (), out .Rx , out .Tx ,
0 commit comments