fallback to icanhazip for external IP detection
This commit is contained in:
parent
335d232727
commit
06d1642b16
2
add.go
2
add.go
@ -17,7 +17,7 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
|
|||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
Description: description,
|
Description: description,
|
||||||
PublicKey: publicKey,
|
PublicKey: publicKey,
|
||||||
PrivateKey: privateKey, // omitted from server config JSON!
|
PrivateKey: privateKey, // omitted from server config JSON!
|
||||||
PresharedKey: GenerateJSONKey(),
|
PresharedKey: GenerateJSONKey(),
|
||||||
AllowedIPs: []JSONIPNet{
|
AllowedIPs: []JSONIPNet{
|
||||||
JSONIPNet{
|
JSONIPNet{
|
||||||
|
20
init.go
20
init.go
@ -2,8 +2,10 @@ package dsnet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -53,11 +55,27 @@ func getExternalIP() net.IP {
|
|||||||
|
|
||||||
localAddr := conn.LocalAddr().String()
|
localAddr := conn.LocalAddr().String()
|
||||||
IP := net.ParseIP(strings.Split(localAddr, ":")[0])
|
IP := net.ParseIP(strings.Split(localAddr, ":")[0])
|
||||||
|
IP = IP.To4()
|
||||||
|
|
||||||
if !(IP[0] == 10 || (IP[0] == 172 && IP[1] >= 16 && IP[1] <= 31) || (IP[0] == 192 && IP[1] == 168)) {
|
if !(IP[0] == 10 || (IP[0] == 172 && IP[1] >= 16 && IP[1] <= 31) || (IP[0] == 192 && IP[1] == 168)) {
|
||||||
// not private, so public
|
// not private, so public
|
||||||
return IP
|
return IP
|
||||||
}
|
}
|
||||||
// TODO detect private IP and use icanhazip.com instead
|
|
||||||
|
// detect private IP and use icanhazip.com instead
|
||||||
|
client := http.Client{
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
resp, err := client.Get("https://ipv4.icanhazip.com/")
|
||||||
|
check(err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
check(err)
|
||||||
|
IP = net.ParseIP(strings.TrimSpace(string(body)))
|
||||||
|
return IP.To4()
|
||||||
|
}
|
||||||
|
|
||||||
return net.IP{}
|
return net.IP{}
|
||||||
}
|
}
|
||||||
|
14
types.go
14
types.go
@ -20,7 +20,7 @@ type PeerConfig struct {
|
|||||||
Description string `validate:"required,gte=1,lte=255"`
|
Description string `validate:"required,gte=1,lte=255"`
|
||||||
|
|
||||||
PublicKey JSONKey `validate:"required,len=44"`
|
PublicKey JSONKey `validate:"required,len=44"`
|
||||||
PrivateKey JSONKey `json:"-"` // omitted from config!
|
PrivateKey JSONKey `json:"-"` // omitted from config!
|
||||||
PresharedKey JSONKey `validate:"required,len=44"`
|
PresharedKey JSONKey `validate:"required,len=44"`
|
||||||
// TODO endpoint support
|
// TODO endpoint support
|
||||||
//Endpoint net.UDPAddr `validate:"required,udp4_addr"`
|
//Endpoint net.UDPAddr `validate:"required,udp4_addr"`
|
||||||
@ -52,14 +52,14 @@ type Peer struct {
|
|||||||
type DsnetConfig struct {
|
type DsnetConfig struct {
|
||||||
// domain to append to hostnames. Relies on separate DNS server for
|
// domain to append to hostnames. Relies on separate DNS server for
|
||||||
// resolution. Informational only.
|
// resolution. Informational only.
|
||||||
ExternalIP net.IP `validate:"required,cidr"`
|
ExternalIP net.IP `validate:"required,cidr"`
|
||||||
ListenPort int `validate:"gte=1024,lte=65535"`
|
ListenPort int `validate:"gte=1024,lte=65535"`
|
||||||
Domain string `validate:"required,gte=1,lte=255"`
|
Domain string `validate:"required,gte=1,lte=255"`
|
||||||
// IP network from which to allocate automatic sequential addresses
|
// IP network from which to allocate automatic sequential addresses
|
||||||
// Network is chosen randomly when not specified
|
// Network is chosen randomly when not specified
|
||||||
Network JSONIPNet `validate:"required"`
|
Network JSONIPNet `validate:"required"`
|
||||||
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
|
||||||
ReportFile string `validate:"required"`
|
ReportFile string `validate:"required"`
|
||||||
PrivateKey JSONKey `validate:"required,len=44"`
|
PrivateKey JSONKey `validate:"required,len=44"`
|
||||||
|
Loading…
Reference in New Issue
Block a user