From cd0d993f1e7ffddcad76a2afac2e54340ad79bce Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Wed, 30 Sep 2015 11:11:29 +0800 Subject: [PATCH] support ipv6 format address --- parse.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/parse.go b/parse.go index 24356bf..85b3d32 100644 --- a/parse.go +++ b/parse.go @@ -7,6 +7,8 @@ import ( "github.com/miekg/dns" "io/ioutil" "log" + "net" + "strconv" "strings" ) @@ -24,19 +26,38 @@ func load_domain(f string) (Kv, error) { } func parse_addr(s string) (string, string, error) { - s2 := strings.Split(s, ":") - if len(s2) != 3 { + s2 := strings.SplitN(s, ":", 2) + + if len(s2) != 2 { msg := Sprintf("error %s not well formatted", s) err := errors.New(msg) return "", "", err } + if s2[0] != "tcp" && s2[0] != "udp" { msg := Sprintf("invalid %s, only tcp or udp allowed", s2[0]) err := errors.New(msg) return "", "", err } - t := Sprintf("%s:%s", s2[1], s2[2]) - return s2[0], t, nil + + host, port, err := net.SplitHostPort(s2[1]) + if err != nil { + return "", "", err + } + + /* check host */ + ip := net.ParseIP(host) + if ip == nil { + return "", "", errors.New(Sprintf("invalid host %s", host)) + } + + /* check port */ + _, err = strconv.Atoi(port) + if err != nil { + return "", "", err + } + + return s2[0], s2[1], nil } func parse_server(s string) (*UpstreamServer, error) {