Skip to content

Commit

Permalink
Update to support netip use in Nebula 1.9.4 (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire authored Oct 16, 2024
1 parent 494f071 commit 4705788
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
23 changes: 19 additions & 4 deletions lib/models/HostInfo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HostInfo {

factory HostInfo.fromJson(Map<String, dynamic> json) {
UDPAddress? currentRemote;
if (json['currentRemote'] != null) {
if (json['currentRemote'] != "") {
currentRemote = UDPAddress.fromJson(json['currentRemote']);
}

Expand Down Expand Up @@ -52,6 +52,11 @@ class UDPAddress {
String ip;
int port;

UDPAddress({
required this.ip,
required this.port,
});

@override
String toString() {
// Simple check on if nebula told us about a v4 or v6 ip address
Expand All @@ -62,7 +67,17 @@ class UDPAddress {
return '$ip:$port';
}

UDPAddress.fromJson(Map<String, dynamic> json)
: ip = json['ip'],
port = json['port'];
factory UDPAddress.fromJson(String json) {
// IPv4 Address
if (json.contains('.')) {
var ip = json.split(':')[0];
var port = int.parse(json.split(':')[1]);
return UDPAddress(ip: ip, port: port);
}

// IPv6 Address
var ip = json.split(']')[0].substring(1);
var port = int.parse(json.split(']')[1].split(':')[1]);
return UDPAddress(ip: ip, port: port);
}
}
33 changes: 21 additions & 12 deletions nebula/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/netip"
"os"
"runtime"
"runtime/debug"

"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
nc "github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/iputil"
"github.com/slackhq/nebula/overlay"
"github.com/slackhq/nebula/udp"
"github.com/slackhq/nebula/util"
)

Expand Down Expand Up @@ -109,7 +107,12 @@ func (n *Nebula) ListHostmap(pending bool) (string, error) {
}

func (n *Nebula) GetHostInfoByVpnIp(vpnIp string, pending bool) (string, error) {
b, err := json.Marshal(n.c.GetHostInfoByVpnIp(stringIpToInt(vpnIp), pending))
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return "", err
}

b, err := json.Marshal(n.c.GetHostInfoByVpnIp(netVpnIp, pending))
if err != nil {
return "", err
}
Expand All @@ -118,16 +121,26 @@ func (n *Nebula) GetHostInfoByVpnIp(vpnIp string, pending bool) (string, error)
}

func (n *Nebula) CloseTunnel(vpnIp string) bool {
return n.c.CloseTunnel(stringIpToInt(vpnIp), false)
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return false
}

return n.c.CloseTunnel(netVpnIp, false)
}

func (n *Nebula) SetRemoteForTunnel(vpnIp string, addr string) (string, error) {
udpAddr := udp.NewAddrFromString(addr)
if udpAddr == nil {
udpAddr, err := netip.ParseAddrPort(addr)
if err != nil {
return "", errors.New("could not parse udp address")
}

b, err := json.Marshal(n.c.SetRemoteForTunnel(stringIpToInt(vpnIp), *udpAddr))
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return "", errors.New("could not parse vpnIp")
}

b, err := json.Marshal(n.c.SetRemoteForTunnel(netVpnIp, udpAddr))
if err != nil {
return "", err
}
Expand All @@ -140,7 +153,3 @@ func (n *Nebula) Sleep() {
n.l.WithField("tunnels", closed).Info("Sleep called, closed non lighthouse tunnels")
}
}

func stringIpToInt(ip string) iputil.VpnIp {
return iputil.Ip2VpnIp(net.ParseIP(ip))
}
1 change: 1 addition & 0 deletions nebula/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
github.com/vishvananda/netlink v1.3.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/mobile v0.0.0-20241016134751-7ff83004ec2c // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions nebula/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20241016134751-7ff83004ec2c h1:zuNS/LWsEpPTLfrmBkis6Xofw3nieAqB4hYLn8+uswk=
golang.org/x/mobile v0.0.0-20241016134751-7ff83004ec2c/go.mod h1:snk1Mn2ZpdKCt90JPEsDh4sL3ReK520U2t0d7RHBnSU=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down

0 comments on commit 4705788

Please sign in to comment.