parent
c30702a4bf
commit
8fb9008c95
|
@ -5,9 +5,7 @@
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
*.vscode
|
*.vscode
|
||||||
|
*.idea
|
||||||
# Test binary, build with `go test -c`
|
|
||||||
*.test
|
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
*.out
|
*.out
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -46,7 +47,6 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
|
||||||
// A d4 writer implements the io.Writer Interface by implementing Write() and Close()
|
// A d4 writer implements the io.Writer Interface by implementing Write() and Close()
|
||||||
// it accepts an io.Writer as sink
|
// it accepts an io.Writer as sink
|
||||||
d4Writer struct {
|
d4Writer struct {
|
||||||
|
@ -362,7 +362,6 @@ func setReaderWriters(d4 *d4S) bool {
|
||||||
isn, dstnet := isNet((*d4).conf.destination)
|
isn, dstnet := isNet((*d4).conf.destination)
|
||||||
if isn {
|
if isn {
|
||||||
dial := net.Dialer{
|
dial := net.Dialer{
|
||||||
DualStack: true,
|
|
||||||
Timeout: (*d4).ct,
|
Timeout: (*d4).ct,
|
||||||
KeepAlive: (*d4).cka,
|
KeepAlive: (*d4).cka,
|
||||||
FallbackDelay: 0,
|
FallbackDelay: 0,
|
||||||
|
@ -410,32 +409,42 @@ func setReaderWriters(d4 *d4S) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNet(host string) (bool, string) {
|
func isNet(host string) (bool, string) {
|
||||||
|
// DNS regex
|
||||||
|
validDNS := regexp.MustCompile(`^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
|
||||||
|
]{2,3})$`)
|
||||||
// Check ipv6
|
// Check ipv6
|
||||||
if strings.HasPrefix(host, "[") {
|
if strings.HasPrefix(host, "[") {
|
||||||
// Parse an IP-Literal in RFC 3986 and RFC 6874.
|
// Parse an IP-Literal in RFC 3986 and RFC 6874.
|
||||||
// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
|
// E.g., "[fe80::1]:80".
|
||||||
i := strings.LastIndex(host, "]")
|
i := strings.LastIndex(host, "]")
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
panic("Unmatched [ in destination config")
|
infof("Unmatched [ in destination config")
|
||||||
|
return false, ""
|
||||||
}
|
}
|
||||||
if !validPort(host[i+1:]) {
|
if !validPort(host[i+1:]) {
|
||||||
panic("No valid port specified")
|
infof("No valid port specified")
|
||||||
|
return false, ""
|
||||||
}
|
}
|
||||||
// trim brackets
|
// trim brackets
|
||||||
|
|
||||||
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
|
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
|
||||||
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", host[:i+1], host[i+1:]))
|
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", host[:i+1], host[i+1:]))
|
||||||
return true, host
|
return true, host
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Ipv4
|
// Ipv4 or DNS name
|
||||||
ss := strings.Split(string(host), ":")
|
ss := strings.Split(string(host), ":")
|
||||||
|
if len(ss) > 1 {
|
||||||
if !validPort(":" + ss[1]) {
|
if !validPort(":" + ss[1]) {
|
||||||
panic("No valid port specified")
|
infof("No valid port specified")
|
||||||
|
return false, ""
|
||||||
}
|
}
|
||||||
if net.ParseIP(ss[0]) != nil {
|
if net.ParseIP(ss[0]) != nil {
|
||||||
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
|
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
|
||||||
return true, host
|
return true, host
|
||||||
|
} else if validDNS.MatchString(ss[0]) {
|
||||||
|
infof(fmt.Sprintf("DNS: %s, Server Port: %s\n", ss[0], ss[1]))
|
||||||
|
return true, host
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false, host
|
return false, host
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testCases = []struct {
|
||||||
|
name string
|
||||||
|
str string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"Well-formed IPv4 with port", "127.0.0.1:4443", true},
|
||||||
|
{"Well-formed IPv4 without port", "127.0.0.1", false},
|
||||||
|
{"Malformed IPv4 with port", "127..0.1:4443", false},
|
||||||
|
{"Malformed IPv4 without port", "127..0.1", false},
|
||||||
|
{"Well-formed IPv6 with port - 2", "[::1]:4443", true},
|
||||||
|
{"Well-formed IPv6 without port", "[fe80::1%25en0]", false},
|
||||||
|
{"Malformed IPv6 with port", "[::::1]:4443", false},
|
||||||
|
{"Malformed IPv6 without port", "[::::::::1]", false},
|
||||||
|
{"Malformed IPv6 : missing square brackets", "::::::::1:4443", false},
|
||||||
|
{"Well-formed DNS name with port", "toto.circl.lu:4443", true},
|
||||||
|
{"Well-formed DNS name without port", "toto.circl.lu", false},
|
||||||
|
{"Malformed DNS name with port", ".:4443", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsNet(t *testing.T) {
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
b, _ := isNet(tc.str)
|
||||||
|
if b != tc.expected {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue