Prefer Ipv4 addresses
This commit is contained in:
parent
8e3e62a9de
commit
dfa70a56b5
1 changed files with 34 additions and 20 deletions
54
host.go
54
host.go
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018 Unrud <unrud@outlook.com>
|
* Copyright (c) 2018-2019 Unrud <unrud@outlook.com>
|
||||||
*
|
*
|
||||||
* This file is part of Remote-Touchpad.
|
* This file is part of Remote-Touchpad.
|
||||||
*
|
*
|
||||||
|
|
@ -22,11 +22,25 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindDefaultHost() (host string) {
|
const ipv4Rating int = +1
|
||||||
host = "localhost"
|
|
||||||
|
func FindDefaultHost() string {
|
||||||
|
type hostsValue struct {
|
||||||
|
prio int
|
||||||
|
host string
|
||||||
|
}
|
||||||
|
hosts := make([]hostsValue, 0)
|
||||||
|
hosts = append(hosts, hostsValue{0, "localhost"})
|
||||||
|
addIP := func(prio int, ip net.IP) {
|
||||||
|
if ip.To4() != nil {
|
||||||
|
prio += ipv4Rating
|
||||||
|
}
|
||||||
|
hosts = append(hosts, hostsValue{prio, ip.String()})
|
||||||
|
}
|
||||||
for _, publicIP := range []string{"2001:4860:4860::8888", "8.8.8.8"} {
|
for _, publicIP := range []string{"2001:4860:4860::8888", "8.8.8.8"} {
|
||||||
addr := fmt.Sprintf("[%s]:80", publicIP)
|
addr := fmt.Sprintf("[%s]:80", publicIP)
|
||||||
conn, err := net.Dial("udp", addr)
|
conn, err := net.Dial("udp", addr)
|
||||||
|
|
@ -34,42 +48,42 @@ func FindDefaultHost() (host string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conn.Close()
|
conn.Close()
|
||||||
host, _, err = net.SplitHostPort(conn.LocalAddr().String())
|
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return
|
ip := net.ParseIP(host)
|
||||||
}
|
if ip == nil {
|
||||||
interfaces, err := net.Interfaces()
|
panic("Invalid IP address: " + host)
|
||||||
if err != nil {
|
}
|
||||||
return
|
addIP(100, ip)
|
||||||
}
|
}
|
||||||
|
interfaces, _ := net.Interfaces()
|
||||||
for _, inter := range interfaces {
|
for _, inter := range interfaces {
|
||||||
if inter.Flags&net.FlagUp == 0 || inter.Flags&net.FlagLoopback != 0 {
|
if inter.Flags&net.FlagUp == 0 {
|
||||||
continue
|
|
||||||
}
|
|
||||||
addrs, err := inter.Addrs()
|
|
||||||
if err != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
addrs, _ := inter.Addrs()
|
||||||
addrs:
|
addrs:
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
ip, _, err := net.ParseCIDR(addr.String())
|
ip, _, err := net.ParseCIDR(addr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
panic(err)
|
||||||
}
|
}
|
||||||
if host == "localhost" {
|
if inter.Flags&net.FlagLoopback != 0 {
|
||||||
host = ip.String()
|
addIP(10, ip)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
for _, linkLocalPrefix := range []string{
|
for _, linkLocalPrefix := range []string{
|
||||||
"169.254.", "fe8", "fe9", "fea", "feb"} {
|
"169.254.", "fe8", "fe9", "fea", "feb"} {
|
||||||
if strings.HasPrefix(ip.String(), linkLocalPrefix) {
|
if strings.HasPrefix(ip.String(), linkLocalPrefix) {
|
||||||
|
addIP(20, ip)
|
||||||
continue addrs
|
continue addrs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ip.String()
|
addIP(30, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
sort.Slice(hosts, func(i, j int) bool { return hosts[i].prio > hosts[j].prio })
|
||||||
|
return hosts[0].host
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue