-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
location.go
113 lines (94 loc) Β· 2.17 KB
/
location.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package netenv
import (
"fmt"
"log"
"net"
"os"
"time"
"github.com/safing/portmaster/network/netutils"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
// TODO: Create IPv6 version of GetApproximateInternetLocation
// GetApproximateInternetLocation returns the IP-address of the nearest ping-answering internet node
//nolint:gocognit // TODO
func GetApproximateInternetLocation() (net.IP, error) {
// TODO: first check if we have a public IP
// net.InterfaceAddrs()
// Traceroute example
dst := net.IPAddr{
IP: net.IPv4(1, 1, 1, 1),
}
c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") // ICMP for IPv4
if err != nil {
return nil, err
}
defer c.Close()
p := ipv4.NewPacketConn(c)
err = p.SetControlMessage(ipv4.FlagTTL|ipv4.FlagSrc|ipv4.FlagDst|ipv4.FlagInterface, true)
if err != nil {
return nil, err
}
wm := icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: os.Getpid() & 0xffff,
Data: []byte{0},
},
}
rb := make([]byte, 1500)
next:
for i := 1; i <= 64; i++ { // up to 64 hops
repeat:
for j := 1; j <= 5; j++ {
wm.Body.(*icmp.Echo).Seq = i
wb, err := wm.Marshal(nil)
if err != nil {
return nil, err
}
err = p.SetTTL(i)
if err != nil {
return nil, err
}
_, err = p.WriteTo(wb, nil, &dst)
if err != nil {
return nil, err
}
err = p.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
if err != nil {
return nil, err
}
// n, cm, peer, err := p.ReadFrom(rb)
// readping:
for {
n, _, peer, err := p.ReadFrom(rb)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
continue repeat
}
return nil, err
}
rm, err := icmp.ParseMessage(1, rb[:n])
if err != nil {
log.Fatal(err)
}
switch rm.Type {
case ipv4.ICMPTypeTimeExceeded:
ip := net.ParseIP(peer.String())
if ip == nil {
return nil, fmt.Errorf("failed to parse IP: %s", peer.String())
}
if !netutils.IPIsLAN(ip) {
return ip, nil
}
continue next
case ipv4.ICMPTypeEchoReply:
continue next
default:
// log.Tracef("unknown ICMP message: %+v\n", rm)
}
}
}
}
return nil, nil
}