auto external IP detection

This commit is contained in:
Callan Bryant 2020-03-02 21:11:45 +00:00
parent c4cb15c45e
commit 994814b180
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 16 additions and 3 deletions

17
init.go
View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"strings"
"time" "time"
) )
@ -14,11 +15,12 @@ func Init() {
conf := DsnetConfig{ conf := DsnetConfig{
PrivateKey: privateKey, PrivateKey: privateKey,
PresharedKey: presharedKey, PresharedKey: presharedKey,
Port: DEFAULT_LISTEN_PORT, ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(), Network: getRandomNetwork(),
Peers: make([]PeerConfig, 0), Peers: make([]PeerConfig, 0),
Domain: "dsnet", Domain: "dsnet",
ReportFile: DEFAULT_REPORT_FILE, ReportFile: DEFAULT_REPORT_FILE,
ExternalIP: getExternalIP(),
} }
IP := conf.MustAllocateIP() IP := conf.MustAllocateIP()
@ -27,7 +29,7 @@ func Init() {
conf.MustSave() conf.MustSave()
fmt.Printf("Config written to %s. Please edit.", CONFIG_FILE) fmt.Printf("Config written to %s. Please check/edit.", CONFIG_FILE)
} }
// get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?) // get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?)
@ -43,3 +45,14 @@ func getRandomNetwork() JSONIPNet {
}, },
} }
} }
func getExternalIP() net.IP {
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
localAddr := conn.LocalAddr().String()
IP := net.ParseIP(strings.Split(localAddr, ":")[0])
// TODO detect private IP and use icanhazip.com instead
return IP
}

View File

@ -56,7 +56,7 @@ type DsnetConfig struct {
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet `validate:"required"` Network JSONIPNet `validate:"required"`
ExternalIP net.IP `validate:"required,cidr"` ExternalIP net.IP `validate:"required,cidr"`
ExternalPort int `validate:"gte=1024,lte=65535"` ListenPort int `validate:"gte=1024,lte=65535"`
InternalIP net.IP `validate:"required,cidr"` InternalIP net.IP `validate:"required,cidr"`
InternalDNS net.IP `validate:"required,cidr"` InternalDNS net.IP `validate:"required,cidr"`
// TODO Default subnets to route via VPN // TODO Default subnets to route via VPN