-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfo.go
106 lines (86 loc) · 2.1 KB
/
info.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
package qcon
import (
"net"
"sort"
)
// TODO: change const to allow bitmap operations
// pass to GetInfo() as a filter for only returning certain types
// List of all host/address types ordered by priority
// Lowest values are most preferred
const (
httpsSmartLanIPv4 uint8 = iota
httpsSmartLanIPv6
httpsLanIPv4
httpsLanIPv6
httpsFQDN
httpsDDNS
httpsSmartHost
httpsSmartWanIPv6
httpsSmartWanIPv4
httpsWanIPv6
httpsWanIPv4
httpLanIPv4
httpLanIPv6
httpFQDN
httpDDNS
httpWanIPv6
httpWanIPv4
httpsTun
httpTun
maxRecordType
)
func isHTTPS(t uint8) bool {
return t < httpLanIPv4 || t == httpsTun
}
// Record is a single QuickConnect redirect record indicating a
// URL that may be able to access the desired Synology service.
// Each record has a Type which is used to prioritize URLs and
// a State to indicate the result of the most recent connection
// test with that host.
type Record struct {
URL string
Type uint8
State ConnState
}
// ConnState indicates the connection state with a URL/host
type ConnState uint8
const (
StateUnknown ConnState = iota
StateOK
StateConnectFailed
StateInvalidServer
)
// Info contains information about a QuickConnect host
type Info struct {
ServerID string
Records []Record
}
// Add Record to Info, sorted by Record.Type
func (set *Info) add(r Record) {
s := set.Records
// Find insertion point
i := sort.Search(len(s), func(i int) bool { return s[i].Type >= r.Type })
if i == len(s) {
// append record to end of current set
s = append(s, r)
} else {
// insert at index i
// expand the slice if necessary using zero value placeholder
s = append(s, Record{})
// shift any items at insertion point or after one spot over
copy(s[i+1:], s[i:])
// insert new item
s[i] = r
}
set.Records = s
}
// inspired by / copied from https://go-review.googlesource.com/c/go/+/162998/7/src/net/ip.go
func isLocalIP(addr string) bool {
ip := net.ParseIP(addr)
if ip4 := ip.To4(); ip4 != nil {
return ip4[0] == 10 ||
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
(ip4[0] == 192 && ip4[1] == 168)
}
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
}