croc/src/models/constants.go

53 lines
1.1 KiB
Go
Raw Normal View History

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)
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
DEFAULT_RELAY, err = lookupIP(DEFAULT_RELAY)
if err == nil {
DEFAULT_RELAY += ":" + DEFAULT_PORT
} else {
DEFAULT_RELAY = ""
}
DEFAULT_RELAY6, err = lookupIP(DEFAULT_RELAY6)
if err == nil {
DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
} else {
DEFAULT_RELAY6 = ""
}
2020-11-17 19:29:52 +01:00
}
func lookupIP(address string) (ipaddress string, err error) {
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),
}
2020-11-17 19:46:49 +01:00
return d.DialContext(ctx, "udp", "1.1.1.1: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
}