-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonnet.go
256 lines (187 loc) · 6.69 KB
/
commonnet.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package common
import (
"bufio"
"bytes"
"crypto/tls"
"errors"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
upnp "github.com/NebulousLabs/go-upnp"
)
var (
// ExtIPProviders - preset macro defining list of available external IP checking services
ExtIPProviders = []string{
"http://checkip.amazonaws.com/",
"http://icanhazip.com/",
"http://www.trackip.net/ip",
"http://bot.whatismyipaddress.com/",
"https://ipecho.net/plain",
"http://myexternalip.com/raw",
}
)
/* BEGIN EXPORTED METHODS */
// SendBytes - attempt to send specified bytes to given address
func SendBytes(b []byte, address string) error {
if strings.Count(address, ":") > 1 { // Check IPv6
address = "[" + address[:strings.LastIndex(address, ":")] + "]" + address[strings.LastIndex(address, ":"):] // Set address
}
connection, err := tls.Dial("tcp", address, GeneralTLSConfig) // Connect to given address
if err != nil { // Check for errors
return err // Return found error
}
writer := bufio.NewWriter(connection) // Initialize writer
_, err = writer.Write(append(b, byte('\r'))) // Write data
if err != nil { // Check for errors
return err // Return found errors
}
writer.Flush() // Flush writer
err = connection.Close() // Close connection
if err != nil { // Check for errors
return err // Return found error
}
return nil // No error occurred, return nil
}
// SendBytesResult - gop2pCommon.SendBytesResult performance-optimized wrapper
func SendBytesResult(b []byte, address string) ([]byte, error) {
if strings.Count(address, ":") > 1 { // Check IPv6
address = "[" + address[:strings.LastIndex(address, ":")] + "]" + address[strings.LastIndex(address, ":"):] // Set address
}
d := net.Dialer{Timeout: 5 * time.Second} // Init dialer with timeout
connection, err := tls.DialWithDialer(&d, "tcp", address, GeneralTLSConfig) // Connect to given address
if err != nil { // Check for errors
return nil, err // Return found error
}
readWriter := bufio.NewReadWriter(bufio.NewReader(connection), bufio.NewWriter(connection)) // Initialize read writer
_, err = readWriter.Write(append(b, byte('\r'))) // Write data to connection
if err != nil { // Check for errors
return nil, err // Return found error
}
readWriter.Flush() // Flush
response, err := readWriter.ReadBytes('\r') // Read up to delimiter
if err != nil { // Check for errors
return nil, err // Return found error
}
return bytes.Trim(response, "\r"), nil // Return response
}
// ReadConnectionWaitAsyncNoTLS - attempt to read from connection in an asynchronous fashion, after waiting for peer to write
func ReadConnectionWaitAsyncNoTLS(conn net.Conn) ([]byte, error) {
reader := bufio.NewReader(conn) // Initialize reader
readBytes, err := reader.ReadBytes('\r') // Read up to delimiter
if err != nil { // Check for errors
return nil, err // Return found error
}
return bytes.Trim(readBytes, "\r"), nil // Return read bytes w/trimmed delimiter
}
/*
BEGIN IP ADDR METHODS
*/
// GetExtIPAddrWithUPnP - retrieve the external IP address of the current machine via upnp
func GetExtIPAddrWithUPnP() (string, error) {
// connect to router
d, err := upnp.Discover()
if err != nil { // Check for errors
return "", err // return error
}
// discover external IP
ip, err := d.ExternalIP()
if err != nil { // Check for errors
return "", err // return error
}
return ip, nil
}
// GetExtIPAddrWithoutUPnP - retrieve the external IP address of the current machine w/o upnp
func GetExtIPAddrWithoutUPnP() (string, error) {
if len(ExtIPProviders) == 0 { // Check no providers
host, err := os.Hostname() // Get host
if err != nil { // Check for errors
return "localhost", err // Return found error
}
addrs, err := net.LookupIP(host) // Get IP
if err != nil { // Check for errors
return "localhost", err // Return found error
}
for _, addr := range addrs { // Iterate through addresses
if ipv4 := addr.To4(); ipv4 != nil { // Get IPv4 representation
return ipv4.String(), nil // Return IP
}
return addr.String(), nil // Return raw IP
}
return "localhost", nil // Return localhost
}
startTime := time.Now() // Get start time
addresses := []string{} // Init address buffer
finished := false // Init finished
for _, provider := range ExtIPProviders { // Iterate through providers
go getIPFromProviderAsync(provider, &addresses, &finished) // Fetch IP
}
for finished != true { // Wait until finished
if time.Now().Sub(startTime) > 10*time.Second { // Check timeout
return "localhost", errors.New("timed out") // Return timed out error
}
}
return getNonNilInStringSlice(addresses) // Return valid address
}
// Connected - check if connected to internet
func Connected() (connected bool) {
resp, err := http.Get("http://clients3.google.com/generate_204") // Perform request
if err != nil { // Check for errors
return false // Return result
}
_, err = ioutil.ReadAll(resp.Body) // Read response
if err != nil { // Check for errors
return false // Return result
}
resp.Body.Close() // Close
return true // Return connected
}
/*
END IP ADDR METHODS
*/
/* END EXPORTED METHODS */
/* BEGIN INTERNAL METHODS */
// getIPFromProvider - get IP address from given IP provider
func getIPFromProvider(provider string) (string, error) {
resp, err := http.Get(provider) // Attempt to check IP via provider
if err != nil { // Check for errors
return "", err // Return error
}
defer resp.Body.Close() // Close connection
ip, err := ioutil.ReadAll(resp.Body) // Read address
if err != nil { // Check for errors
return "", err // Return error
}
stringVal := string(ip[:]) // Fetch string value
return strings.TrimSpace(stringVal), nil // Return ip
}
// getIPFromProviderAsync - asynchronously get IP address from given IP provider
func getIPFromProviderAsync(provider string, buffer *[]string, finished *bool) {
if Connected() { // Check connected to internet
if len(*buffer) == 0 { // Check IP not already determined
resp, err := http.Get(provider) // Attempt to check IP via provider
if err != nil { // Check for errors
if len(*buffer) == 0 { // Double check IP not already determined
*buffer = append(*buffer, "") // Set IP
*finished = true // Set finished
}
} else {
defer resp.Body.Close() // Close connection
ip, _ := ioutil.ReadAll(resp.Body) // Read address
stringVal := string(ip[:]) // Fetch string value
if len(*buffer) == 0 { // Double check IP not already determined
*buffer = append(*buffer, strings.TrimSpace(stringVal)) // Set ip
*finished = true // Set finished
}
}
}
} else {
if len(*buffer) == 0 {
*buffer = append(*buffer, "localhost") // Set localhost
*finished = true // Set finished
}
}
}
/* END INTERNAL METHODS */