@@ -3,6 +3,7 @@ package config
33import (
44 "encoding/json"
55 "fmt"
6+ "net"
67 "net/url"
78 "os"
89 "path/filepath"
@@ -11,6 +12,7 @@ import (
1112 "time"
1213
1314 "github.com/ipfs/go-log/v2"
15+ "github.com/libp2p/go-libp2p/core/peer"
1416 "github.com/libp2p/go-libp2p/p2p/host/eventbus"
1517 "github.com/moby/sys/atomicwriter"
1618 "github.com/multiformats/go-multiaddr"
@@ -237,10 +239,11 @@ func setDefaults(conf *Config, bus awlevent.Bus) {
237239 if isEmptyConfig && conf .VPNConfig .IPNetV6 == "" {
238240 conf .VPNConfig .IPNetV6 = DefaultVPNNetworkSubnet6
239241 }
242+ conf .ensureIPv6AddressLocked ()
240243 if ip , _ := conf .VPNLocalIPMask (); ip == nil {
241244 conf .VPNConfig .IPNet = DefaultVPNNetworkSubnet
242245 }
243- if ip , _ := conf .VPNLocalIPMaskV6 (); ip == nil {
246+ if ip , _ := conf .VPNLocalIPMaskV6 (); conf . VPNConfig . IPNetV6 != "" && ip == nil {
244247 conf .VPNConfig .IPNetV6 = DefaultVPNNetworkSubnet6
245248 }
246249 if conf .VPNConfig .InterfaceName == "" {
@@ -355,3 +358,18 @@ func writeFileAtomic(path string, data []byte) error {
355358 ChownFileIfNeeded (path )
356359 return nil
357360}
361+
362+ func (c * Config ) ensureIPv6AddressLocked () {
363+ if c .VPNConfig .IPNetV6 == "" || c .P2pNode .PeerID == "" {
364+ return
365+ }
366+ localIP , ipNet , err := net .ParseCIDR (c .VPNConfig .IPNetV6 )
367+ if err == nil && localIP .Equal (ipNet .IP ) {
368+ if pid , err := peer .Decode (c .P2pNode .PeerID ); err == nil {
369+ if derived := DeriveIPv6FromPeerID (pid , ipNet ); derived != nil {
370+ maskLen , _ := ipNet .Mask .Size ()
371+ c .VPNConfig .IPNetV6 = fmt .Sprintf ("%s/%d" , derived .String (), maskLen )
372+ }
373+ }
374+ }
375+ }
0 commit comments