-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
112 lines (94 loc) · 1.89 KB
/
utils.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
package main
import (
"net"
"path"
"strings"
)
// Get preferred outbound ip of this machine
func getOutboundIP(targetaddr string) (net.IP, error) {
conn, err := net.Dial("udp", targetaddr)
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}
func getOutboundIface(targetaddr string) (*net.Interface, net.IP, error) {
outboundIP, err := getOutboundIP(targetaddr)
if err != nil {
return nil, nil, err
}
ifces, err := net.Interfaces()
if err != nil {
return nil, nil, err
}
for _, ifce := range ifces {
addrs, err := ifce.Addrs()
if err != nil {
continue
}
matched := false
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.Equal(outboundIP) {
matched = true
break
}
}
if matched {
return &ifce, outboundIP, nil
}
}
return nil, nil, err
}
func compareProcessNames(target, compares string) bool {
target = strings.ToLower(target)
comparesArr := strings.Split(compares, "|")
for _, compare := range comparesArr {
compare = strings.ReplaceAll(compare, "\\", "/")
compare = strings.ToLower(compare)
if target == compare || path.Base(target) == compare {
return true
}
}
return false
}
/*
func hasOutboundFlag(flags uint8) bool {
return flags&(1<<1) != 0
}
*/
func hasLoopbackFlag(flags uint8) bool {
return flags&(1<<2) != 0
}
func hasIPv6Flag(flags uint8) bool {
return flags&(1<<4) != 0
}
func setIPv6Flag(flags *uint8, on bool) {
if on {
*flags |= (1 << 4)
} else {
*flags &= 0xff ^ (1 << 4)
}
}
func convertDivertAddressToNetIP(ipv6 bool, addr [16]byte) net.IP {
ip := [16]byte{}
if ipv6 {
for i := 0; i < 16; i++ {
ip[i] = addr[15-i]
}
return net.IP(ip[:])
} else {
for i := 0; i < 4; i++ {
ip[i] = addr[3-i]
}
return net.IP(ip[:4])
}
}