-
Notifications
You must be signed in to change notification settings - Fork 5
/
net.go
79 lines (73 loc) · 1.46 KB
/
net.go
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
package main
import (
"errors"
"log"
"net"
"strconv"
)
func getIPs() ([]net.IP, error) {
var ips []net.IP
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
ips = append(ips, ip)
}
}
if len(ips) > 0 {
return ips, nil
}
return nil, errors.New("are you connected to the network?")
}
func getOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}
func checkIPs() {
ips, err := getIPs()
if err != nil {
log.Println(err)
} else {
log.Println("The Proxy will be listening on these IP-Addresses:")
for index, ip := range ips {
log.Println("#" + strconv.Itoa(index+1) + ": " + ip.String())
}
ip, err := getOutboundIP()
if err != nil {
log.Println(err)
} else {
log.Println("The most likely IP-Address to use for Plex should be: " + ip.String())
}
}
}