Skip to content

Commit

Permalink
Apply some extremely slapdash changes to move towards enabling ipv6 #51
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Oct 31, 2024
1 parent b2d0e2b commit 6f5d6b7
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 25 deletions.
12 changes: 11 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ func parseAddress(address string) ([]string, error) {
if addr.To4() != nil {
addedSomething = true
output = append(output, addr.String()+"/32")
continue
} else if addr.To16() != nil {
addedSomething = true
output = append(output, addr.String()+"/128")
continue
}
}

Expand All @@ -400,5 +405,10 @@ func parseAddress(address string) ([]string, error) {
return []string{cidr.String()}, nil
}

return []string{ip.To4().String() + "/32"}, nil
mask := "/32"
if ip.To16() == nil {
mask = "/128"
}

return []string{ip.String() + mask}, nil
}
29 changes: 22 additions & 7 deletions internal/router/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,35 @@ func (f *Firewall) setIp(c *netlink.Conn, name string, address net.IPNet) error
return fmt.Errorf("wireguard network iface %s does not exist: %s", name, err)
}

addrMsg := IfAddrmsg{
Family: unix.AF_INET,
Index: uint32(iface.Index),
Scope: unix.RT_SCOPE_LINK,
}
var (
IP net.IP
addrMsg = IfAddrmsg{

Index: uint32(iface.Index),
Scope: unix.RT_SCOPE_LINK,
}
)

preflen, _ := address.Mask.Size()

if address.IP.To4() == nil {
IP = address.IP.To4()
addrMsg.Family = unix.AF_INET
} else if address.IP.To16() == nil {
IP = address.IP.To16()
addrMsg.Family = unix.AF_INET6

} else {
return errors.New("unrecognised ip version")
}

addrMsg.Prefixlen = uint8(preflen)

req.Data = addrMsg.Serialize()

attrs := []netlink.Attribute{
{Type: unix.IFA_LOCAL, Data: address.IP.To4()},
{Type: unix.IFA_ADDRESS, Data: address.IP.To4()},
{Type: unix.IFA_LOCAL, Data: IP},
{Type: unix.IFA_ADDRESS, Data: IP},
}

msg, err := netlink.MarshalAttributes(attrs)
Expand Down
2 changes: 1 addition & 1 deletion internal/routetypes/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (l *Key) AsIPv6() net.IP {
}

func (l Key) String() string {
return fmt.Sprintf("%s/%d", net.IP(l.IP).To4().String(), l.Prefixlen)
return fmt.Sprintf("%s/%d", net.IP(l.IP).String(), l.Prefixlen)
}

func lookupProtocol(t uint16) string {
Expand Down
27 changes: 20 additions & 7 deletions internal/routetypes/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func parseKeys(address string) (keys []Key, err error) {
keys = append(keys,
Key{
Prefixlen: uint32(maskLength),
IP: []byte{ip.IP.To4()[0], ip.IP.To4()[1], ip.IP.To4()[2], ip.IP.To4()[3]},
IP: ip.IP,
},
)
}
Expand Down Expand Up @@ -387,11 +387,18 @@ func parseAddress(address string) (resultAddresses []net.IPNet, err error) {
if addr.To4() != nil {
addedSomething = true
resultAddresses = append(resultAddresses, net.IPNet{IP: addr.To4(), Mask: net.IPv4Mask(255, 255, 255, 255)})
continue
}

if addr.To16() != nil {
addedSomething = true
resultAddresses = append(resultAddresses, net.IPNet{IP: addr.To16(), Mask: net.CIDRMask(128, 128)})
}

}

if !addedSomething {
return nil, fmt.Errorf("no addresses for domain %s were added, potentially because they were all ipv6 which is unsupported", address)
return nil, fmt.Errorf("no addresses for domain %s were added", address)
}

dnsLock.Lock()
Expand All @@ -404,11 +411,17 @@ func parseAddress(address string) (resultAddresses []net.IPNet, err error) {
return []net.IPNet{*cidr}, nil
}

// /32
var resultIP net.IPNet

if ip.To4() == nil {
resultIP.IP = ip.To4()
resultIP.Mask = net.CIDRMask(32, 32)
} else if ip.To16() == nil {
resultIP.IP = ip.To16()
resultIP.Mask = net.CIDRMask(128, 128)
}

return []net.IPNet{
{
IP: ip.To4(),
Mask: net.IPv4Mask(255, 255, 255, 255),
},
resultIP,
}, nil
}
5 changes: 0 additions & 5 deletions internal/routetypes/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ func TestParseSimpleSingles(t *testing.T) {
}

for i := 0; i < len(br.Values); i++ {

if br.Values[i].PolicyType == STOP {
return
}

found := false
for _, v := range expectedValues {

Expand Down
2 changes: 1 addition & 1 deletion internal/users/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func GetUser(username string) (user, error) {
}

func GetUserFromAddress(address net.IP) (user, error) {
ud, err := data.GetUserDataFromAddress(address.To4().String())
ud, err := data.GetUserDataFromAddress(address.String())
if err != nil {
return user{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ func GetIPFromRequest(r *http.Request) net.IP {

if len(addresses)-config.Values.NumberProxies < 0 {
log.Println("WARNING XFF parsing may be broken: ", len(addresses)-config.Values.NumberProxies, " check config.Values.NumberProxies")
return net.ParseIP(strings.TrimSpace(addresses[len(addresses)-1])).To4()
return net.ParseIP(strings.TrimSpace(addresses[len(addresses)-1]))
}

return net.ParseIP(strings.TrimSpace(addresses[len(addresses)-config.Values.NumberProxies])).To4()
return net.ParseIP(strings.TrimSpace(addresses[len(addresses)-config.Values.NumberProxies]))
}
}

return net.ParseIP(GetIP(r.RemoteAddr)).To4()
return net.ParseIP(GetIP(r.RemoteAddr))
}

func GenerateRandomHex(n uint32) (string, error) {
Expand Down

0 comments on commit 6f5d6b7

Please sign in to comment.