2018-09-23 15:28:34 +02:00
|
|
|
package models
|
|
|
|
|
2020-11-17 19:29:52 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
2020-10-13 21:26:11 +02:00
|
|
|
|
2019-09-07 18:46:04 +02:00
|
|
|
// TCP_BUFFER_SIZE is the maximum packet size
|
2019-04-30 15:58:57 +02:00
|
|
|
const TCP_BUFFER_SIZE = 1024 * 64
|
2019-09-07 18:46:04 +02:00
|
|
|
|
|
|
|
// DEFAULT_RELAY is the default relay used (can be set using --relay)
|
2020-10-22 08:37:32 +02:00
|
|
|
var (
|
|
|
|
DEFAULT_RELAY = "croc.schollz.com"
|
|
|
|
DEFAULT_RELAY6 = "croc6.schollz.com"
|
|
|
|
DEFAULT_PORT = "9009"
|
|
|
|
DEFAULT_PASSPHRASE = "pass123"
|
|
|
|
)
|
2020-10-13 21:26:11 +02:00
|
|
|
|
|
|
|
func init() {
|
2020-11-17 19:46:49 +01:00
|
|
|
var err error
|
2021-02-19 19:16:58 +01:00
|
|
|
DEFAULT_RELAY, err = lookupIPs(DEFAULT_RELAY)
|
2020-11-17 19:46:49 +01:00
|
|
|
if err == nil {
|
|
|
|
DEFAULT_RELAY += ":" + DEFAULT_PORT
|
|
|
|
} else {
|
|
|
|
DEFAULT_RELAY = ""
|
|
|
|
}
|
2021-02-19 19:16:58 +01:00
|
|
|
DEFAULT_RELAY6, err = lookupIPs(DEFAULT_RELAY6)
|
2020-11-17 19:46:49 +01:00
|
|
|
if err == nil {
|
|
|
|
DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
|
|
|
|
} else {
|
|
|
|
DEFAULT_RELAY6 = ""
|
|
|
|
}
|
2020-11-17 19:29:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 19:16:58 +01:00
|
|
|
func lookupIPs(address string) (ipaddress string, err error) {
|
|
|
|
var publicDns = []string{"1.1.1.1", "8.8.8.8", "8.8.4.4", "1.0.0.1", "8.26.56.26", "208.67.222.222", "208.67.220.220"}
|
|
|
|
result := make(chan string, len(publicDns))
|
|
|
|
for _, dns := range publicDns {
|
|
|
|
go func(dns string) {
|
|
|
|
s, _ := lookupIP(address, dns)
|
|
|
|
result <- s
|
|
|
|
}(dns)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(publicDns); i++ {
|
|
|
|
ipaddress = <-result
|
|
|
|
if ipaddress != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupIP(address, dns string) (ipaddress string, err error) {
|
2020-11-17 19:29:52 +01:00
|
|
|
r := &net.Resolver{
|
|
|
|
PreferGo: true,
|
|
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{
|
|
|
|
Timeout: time.Millisecond * time.Duration(10000),
|
|
|
|
}
|
2021-02-19 19:16:58 +01:00
|
|
|
return d.DialContext(ctx, "udp", dns+":53")
|
2020-11-17 19:29:52 +01:00
|
|
|
},
|
2020-10-13 21:26:11 +02:00
|
|
|
}
|
2020-11-17 19:29:52 +01:00
|
|
|
ip, err := r.LookupHost(context.Background(), address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-10-13 21:26:11 +02:00
|
|
|
}
|
2020-11-17 19:29:52 +01:00
|
|
|
ipaddress = ip[0]
|
|
|
|
return
|
2020-10-13 21:26:11 +02:00
|
|
|
}
|