@@ -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
4849type (
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
412411func 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
0 commit comments