Skip to content

Commit

Permalink
parseIP supports IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
koaiwu authored and ginuerzh committed Jan 19, 2023
1 parent c07cdef commit 847ee05
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions cmd/gost/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,25 @@ func parseIP(s string, port string) (ips []string) {
port = "8080" // default port
}

addrFn := func(s, port string) string {
c := strings.Count(s, ":")
if c == 0 || //ipv4 or domain
s[len(s)-1] == ']' { //[ipv6]
return s + ":" + port
}
if c > 1 && s[0] != '[' { // ipv6
return "[" + s + "]:" + port
}
return s //ipv4:port or [ipv6]:port
}

file, err := os.Open(s)
if err != nil {
ss := strings.Split(s, ",")
for _, s := range ss {
s = strings.TrimSpace(s)
if s != "" {
// TODO: support IPv6
if !strings.Contains(s, ":") {
s = s + ":" + port
}
ips = append(ips, s)
ips = append(ips, addrFn(s, port))
}

}
Expand All @@ -175,10 +183,7 @@ func parseIP(s string, port string) (ips []string) {
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if !strings.Contains(line, ":") {
line = line + ":" + port
}
ips = append(ips, line)
ips = append(ips, addrFn(line, port))
}
return
}
Expand Down

0 comments on commit 847ee05

Please sign in to comment.