@@ -7,10 +7,11 @@ package conf
77
88import (
99 "encoding/base64"
10- "net"
1110 "strconv"
1211 "strings"
1312
13+ "golang.zx2c4.com/go118/netip"
14+
1415 "golang.org/x/sys/windows"
1516 "golang.org/x/text/encoding/unicode"
1617
@@ -27,43 +28,16 @@ func (e *ParseError) Error() string {
2728 return l18n .Sprintf ("%s: %q" , e .why , e .offender )
2829}
2930
30- func parseIPCidr (s string ) (ipcidr * IPCidr , err error ) {
31- var addrStr , cidrStr string
32- var cidr int
33-
34- i := strings .IndexByte (s , '/' )
35- if i < 0 {
36- addrStr = s
37- } else {
38- addrStr , cidrStr = s [:i ], s [i + 1 :]
39- }
40-
41- err = & ParseError {l18n .Sprintf ("Invalid IP address" ), s }
42- addr := net .ParseIP (addrStr )
43- if addr == nil {
44- return
45- }
46- maybeV4 := addr .To4 ()
47- if maybeV4 != nil {
48- addr = maybeV4
31+ func parseIPCidr (s string ) (netip.Prefix , error ) {
32+ ipcidr , err := netip .ParsePrefix (s )
33+ if err == nil {
34+ return ipcidr , nil
4935 }
50- if len (cidrStr ) > 0 {
51- err = & ParseError {l18n .Sprintf ("Invalid network prefix length" ), s }
52- cidr , err = strconv .Atoi (cidrStr )
53- if err != nil || cidr < 0 || cidr > 128 {
54- return
55- }
56- if cidr > 32 && maybeV4 != nil {
57- return
58- }
59- } else {
60- if maybeV4 != nil {
61- cidr = 32
62- } else {
63- cidr = 128
64- }
36+ addr , err := netip .ParseAddr (s )
37+ if err != nil {
38+ return netip.Prefix {}, & ParseError {l18n .Sprintf ("Invalid IP address: " ), s }
6539 }
66- return & IPCidr { addr , uint8 ( cidr )} , nil
40+ return netip . PrefixFrom ( addr , addr . BitLen ()) , nil
6741}
6842
6943func parseEndpoint (s string ) (* Endpoint , error ) {
@@ -87,16 +61,16 @@ func parseEndpoint(s string) (*Endpoint, error) {
8761 if i := strings .LastIndexByte (host , '%' ); i > 1 {
8862 end = i
8963 }
90- maybeV6 := net . ParseIP (host [1 :end ])
91- if maybeV6 == nil || len ( maybeV6 ) != net . IPv6len {
64+ maybeV6 , err2 := netip . ParseAddr (host [1 :end ])
65+ if err2 != nil || ! maybeV6 . Is6 () {
9266 return nil , err
9367 }
9468 } else {
9569 return nil , err
9670 }
9771 host = host [1 : len (host )- 1 ]
9872 }
99- return & Endpoint {host , uint16 ( port ) }, nil
73+ return & Endpoint {host , port }, nil
10074}
10175
10276func parseMTU (s string ) (uint16 , error ) {
@@ -256,16 +230,16 @@ func FromWgQuick(s string, name string) (*Config, error) {
256230 if err != nil {
257231 return nil , err
258232 }
259- conf .Interface .Addresses = append (conf .Interface .Addresses , * a )
233+ conf .Interface .Addresses = append (conf .Interface .Addresses , a )
260234 }
261235 case "dns" :
262236 addresses , err := splitList (val )
263237 if err != nil {
264238 return nil , err
265239 }
266240 for _ , address := range addresses {
267- a := net . ParseIP (address )
268- if a = = nil {
241+ a , err := netip . ParseAddr (address )
242+ if err ! = nil {
269243 conf .Interface .DNSSearch = append (conf .Interface .DNSSearch , address )
270244 } else {
271245 conf .Interface .DNS = append (conf .Interface .DNS , a )
@@ -312,7 +286,7 @@ func FromWgQuick(s string, name string) (*Config, error) {
312286 if err != nil {
313287 return nil , err
314288 }
315- peer .AllowedIPs = append (peer .AllowedIPs , * a )
289+ peer .AllowedIPs = append (peer .AllowedIPs , a )
316290 }
317291 case "persistentkeepalive" :
318292 p , err := parsePersistentKeepalive (val )
@@ -399,7 +373,7 @@ func FromDriverConfiguration(interfaze *driver.Interface, existingConfig *Config
399373 }
400374 if p .Flags & driver .PeerHasEndpoint != 0 {
401375 peer .Endpoint .Port = p .Endpoint .Port ()
402- peer .Endpoint .Host = p .Endpoint .IP ().String ()
376+ peer .Endpoint .Host = p .Endpoint .Addr ().String ()
403377 }
404378 if p .Flags & driver .PeerHasPersistentKeepalive != 0 {
405379 peer .PersistentKeepalive = p .PersistentKeepalive
@@ -416,16 +390,13 @@ func FromDriverConfiguration(interfaze *driver.Interface, existingConfig *Config
416390 } else {
417391 a = a .NextAllowedIP ()
418392 }
419- var ip net. IP
393+ var ip netip. Addr
420394 if a .AddressFamily == windows .AF_INET {
421- ip = a .Address [:4 ]
395+ ip = netip . AddrFrom4 ( * ( * [ 4 ] byte )( a .Address [:4 ]))
422396 } else if a .AddressFamily == windows .AF_INET6 {
423- ip = a .Address [:16 ]
397+ ip = netip . AddrFrom16 ( * ( * [ 16 ] byte )( a .Address [:16 ]))
424398 }
425- peer .AllowedIPs = append (peer .AllowedIPs , IPCidr {
426- IP : ip ,
427- Cidr : a .Cidr ,
428- })
399+ peer .AllowedIPs = append (peer .AllowedIPs , netip .PrefixFrom (ip , int (a .Cidr )))
429400 }
430401 conf .Peers = append (conf .Peers , peer )
431402 }
0 commit comments