-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathhost.go
More file actions
88 lines (84 loc) · 2.19 KB
/
host.go
File metadata and controls
88 lines (84 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) 2018-2019 Unrud <unrud@outlook.com>
*
* This file is part of Remote-Touchpad.
*
* Remote-Touchpad is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remote-Touchpad is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Remote-Touchpad. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"net"
"sort"
"strings"
)
const ipv4Rating int = +1
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"} {
addr := fmt.Sprintf("[%s]:80", publicIP)
conn, err := net.Dial("udp", addr)
if err != nil {
continue
}
conn.Close()
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
panic(err)
}
ip := net.ParseIP(host)
if ip != nil {
addIP(100, ip)
}
}
interfaces, _ := net.Interfaces()
for _, inter := range interfaces {
if inter.Flags&net.FlagUp == 0 {
continue
}
addrs, _ := inter.Addrs()
addrs:
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
panic(err)
}
if inter.Flags&net.FlagLoopback != 0 {
addIP(10, ip)
continue
}
for _, linkLocalPrefix := range []string{
"169.254.", "fe8", "fe9", "fea", "feb"} {
if strings.HasPrefix(ip.String(), linkLocalPrefix) {
addIP(20, ip)
continue addrs
}
}
addIP(30, ip)
}
}
sort.Slice(hosts, func(i, j int) bool { return hosts[i].prio > hosts[j].prio })
return hosts[0].host
}