Skip to content

Commit 8fb9008

Browse files
committed
Fix #4
1 parent c30702a commit 8fb9008

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*.so
66
*.dylib
77
*.vscode
8-
9-
# Test binary, build with `go test -c`
10-
*.test
8+
*.idea
119

1210
# Output of the go coverage tool, specifically when used with LiteIDE
1311
*.out

d4-goclient.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net"
1717
"os"
1818
"os/signal"
19+
"regexp"
1920
"strconv"
2021
"strings"
2122
"time"
@@ -46,7 +47,6 @@ const (
4647
)
4748

4849
type (
49-
5050
// A d4 writer implements the io.Writer Interface by implementing Write() and Close()
5151
// it accepts an io.Writer as sink
5252
d4Writer struct {
@@ -362,7 +362,6 @@ func setReaderWriters(d4 *d4S) bool {
362362
isn, dstnet := isNet((*d4).conf.destination)
363363
if isn {
364364
dial := net.Dialer{
365-
DualStack: true,
366365
Timeout: (*d4).ct,
367366
KeepAlive: (*d4).cka,
368367
FallbackDelay: 0,
@@ -410,32 +409,42 @@ func setReaderWriters(d4 *d4S) bool {
410409
}
411410

412411
func isNet(host string) (bool, string) {
412+
// DNS regex
413+
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
414+
]{2,3})$`)
413415
// Check ipv6
414416
if strings.HasPrefix(host, "[") {
415417
// Parse an IP-Literal in RFC 3986 and RFC 6874.
416-
// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
418+
// E.g., "[fe80::1]:80".
417419
i := strings.LastIndex(host, "]")
418420
if i < 0 {
419-
panic("Unmatched [ in destination config")
421+
infof("Unmatched [ in destination config")
422+
return false, ""
420423
}
421424
if !validPort(host[i+1:]) {
422-
panic("No valid port specified")
425+
infof("No valid port specified")
426+
return false, ""
423427
}
424428
// trim brackets
425-
426429
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
427430
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", host[:i+1], host[i+1:]))
428431
return true, host
429432
}
430433
} else {
431-
// Ipv4
434+
// Ipv4 or DNS name
432435
ss := strings.Split(string(host), ":")
433-
if !validPort(":" + ss[1]) {
434-
panic("No valid port specified")
435-
}
436-
if net.ParseIP(ss[0]) != nil {
437-
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
438-
return true, host
436+
if len(ss) > 1 {
437+
if !validPort(":" + ss[1]) {
438+
infof("No valid port specified")
439+
return false, ""
440+
}
441+
if net.ParseIP(ss[0]) != nil {
442+
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
443+
return true, host
444+
} else if validDNS.MatchString(ss[0]) {
445+
infof(fmt.Sprintf("DNS: %s, Server Port: %s\n", ss[0], ss[1]))
446+
return true, host
447+
}
439448
}
440449
}
441450
return false, host

d4-goclient_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var testCases = []struct {
8+
name string
9+
str string
10+
expected bool
11+
}{
12+
{"Well-formed IPv4 with port", "127.0.0.1:4443", true},
13+
{"Well-formed IPv4 without port", "127.0.0.1", false},
14+
{"Malformed IPv4 with port", "127..0.1:4443", false},
15+
{"Malformed IPv4 without port", "127..0.1", false},
16+
{"Well-formed IPv6 with port - 2", "[::1]:4443", true},
17+
{"Well-formed IPv6 without port", "[fe80::1%25en0]", false},
18+
{"Malformed IPv6 with port", "[::::1]:4443", false},
19+
{"Malformed IPv6 without port", "[::::::::1]", false},
20+
{"Malformed IPv6 : missing square brackets", "::::::::1:4443", false},
21+
{"Well-formed DNS name with port", "toto.circl.lu:4443", true},
22+
{"Well-formed DNS name without port", "toto.circl.lu", false},
23+
{"Malformed DNS name with port", ".:4443", false},
24+
}
25+
26+
func TestIsNet(t *testing.T) {
27+
for _, tc := range testCases {
28+
t.Run(tc.name, func(t *testing.T) {
29+
b, _ := isNet(tc.str)
30+
if b != tc.expected {
31+
t.Fail()
32+
}
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)