Skip to content

Commit

Permalink
Fix parser error causing failure of all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Oct 31, 2024
1 parent d1888c7 commit d214440
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
9 changes: 5 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net"
"net/netip"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -365,9 +366,9 @@ func Load(path string) error {
}

func parseAddress(address string) ([]string, error) {
ip := net.ParseIP(address)
if ip == nil {

addr, err := netip.ParseAddr(address)
if err != nil {
_, cidr, err := net.ParseCIDR(address)
if err != nil {

Expand Down Expand Up @@ -406,9 +407,9 @@ func parseAddress(address string) ([]string, error) {
}

mask := "/32"
if ip.To16() == nil {
if addr.Is6() {
mask = "/128"
}

return []string{ip.String() + mask}, nil
return []string{addr.String() + mask}, nil
}
7 changes: 5 additions & 2 deletions internal/router/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,13 @@ func (f *Firewall) setupUsers(users []data.UserModel) error {
}

func (f *Firewall) hostIPWithMask(s string) string {
ip := net.ParseIP(s)
addr, err := netip.ParseAddr(s)
if err != nil {
return s + "/32"
}

mask := "/32"
if ip.To16() == nil {
if addr.Is6() {
mask = "/128"
}

Expand Down
4 changes: 4 additions & 0 deletions internal/routetypes/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (l *Key) AsIPv6() net.IP {
return net.IP(l.IP).To16()
}

func (l *Key) AsIP() net.IP {
return net.IP(l.IP)
}

func (l Key) String() string {
return fmt.Sprintf("%s/%d", net.IP(l.IP).String(), l.Prefixlen)
}
Expand Down
15 changes: 8 additions & 7 deletions internal/routetypes/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -359,8 +360,8 @@ var (

func parseAddress(address string) (resultAddresses []net.IPNet, err error) {

ip := net.ParseIP(address)
if ip == nil {
addr, err := netip.ParseAddr(address)
if err != nil {

_, cidr, err := net.ParseCIDR(address)
if err != nil {
Expand Down Expand Up @@ -412,13 +413,13 @@ func parseAddress(address string) (resultAddresses []net.IPNet, err error) {
}

var resultIP net.IPNet

if ip.To4() == nil {
resultIP.IP = ip.To4()
resultIP.IP = addr.AsSlice()
if addr.Is4() {
resultIP.Mask = net.CIDRMask(32, 32)
} else if ip.To16() == nil {
resultIP.IP = ip.To16()
} else if addr.Is6() {
resultIP.Mask = net.CIDRMask(128, 128)
} else {
return nil, errors.New("invalid ip")
}

return []net.IPNet{
Expand Down
4 changes: 2 additions & 2 deletions internal/routetypes/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func checkKey(reality Key, expectedKey Key) error {

if !net.IP.Equal(reality.AsIPv4(), expectedKey.AsIPv4()) {
return fmt.Errorf("key had incorrect ip: expected: %s got: %s", expectedKey.IP, reality.IP)
if !net.IP.Equal(reality.AsIP(), expectedKey.AsIP()) {
return fmt.Errorf("key had incorrect ip: expected: %s got: %s", expectedKey.AsIP(), reality.AsIP())
}

if reality.Prefixlen != expectedKey.Prefixlen {
Expand Down

0 comments on commit d214440

Please sign in to comment.