Skip to content

Commit

Permalink
Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
gallypette committed Apr 1, 2019
1 parent c30702a commit 8fb9008
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*.so
*.dylib
*.vscode

# Test binary, build with `go test -c`
*.test
*.idea

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
35 changes: 22 additions & 13 deletions d4-goclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -46,7 +47,6 @@ const (
)

type (

// A d4 writer implements the io.Writer Interface by implementing Write() and Close()
// it accepts an io.Writer as sink
d4Writer struct {
Expand Down Expand Up @@ -362,7 +362,6 @@ func setReaderWriters(d4 *d4S) bool {
isn, dstnet := isNet((*d4).conf.destination)
if isn {
dial := net.Dialer{
DualStack: true,
Timeout: (*d4).ct,
KeepAlive: (*d4).cka,
FallbackDelay: 0,
Expand Down Expand Up @@ -410,32 +409,42 @@ func setReaderWriters(d4 *d4S) bool {
}

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
if strings.HasPrefix(host, "[") {
// 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, "]")
if i < 0 {
panic("Unmatched [ in destination config")
infof("Unmatched [ in destination config")
return false, ""
}
if !validPort(host[i+1:]) {
panic("No valid port specified")
infof("No valid port specified")
return false, ""
}
// trim brackets

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:]))
return true, host
}
} else {
// Ipv4
// Ipv4 or DNS name
ss := strings.Split(string(host), ":")
if !validPort(":" + ss[1]) {
panic("No valid port specified")
}
if net.ParseIP(ss[0]) != nil {
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
return true, host
if len(ss) > 1 {
if !validPort(":" + ss[1]) {
infof("No valid port specified")
return false, ""
}
if net.ParseIP(ss[0]) != nil {
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
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
Expand Down
35 changes: 35 additions & 0 deletions d4-goclient_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
})
}
}

0 comments on commit 8fb9008

Please sign in to comment.