Skip to content

Commit

Permalink
Unrefactor networks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelocantos committed Sep 17, 2022
1 parent f829c90 commit c146bc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
37 changes: 33 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ func main() {
s := createServer(*host, *port, *pacurl, a)

for _, network := range networks(*host) {
go func(network string) error {
go func(network string) {
l, err := net.Listen(network, s.Addr)
if err != nil {
return err
errch <- err
} else {
log.Printf("Listening on %s %s", network, s.Addr)
errch <- s.Serve(l)
}
log.Printf("Listening on %s %s", network, s.Addr)
return s.Serve(l)
}(network)
}

Expand Down Expand Up @@ -120,3 +121,31 @@ func createServer(host string, port int, pacurl string, a *authenticator) *http.
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
}

func networks(hostname string) []string {
if hostname == "" {
return []string{"tcp"}
}
addrs, err := net.LookupIP(hostname)
if err != nil {
log.Fatal(err)
}
nets := make([]string, 0, 2)
ipv4 := false
ipv6 := false
for _, addr := range addrs {
// addr == net.IPv4len doesn't work because all addrs use IPv6 format.
if addr.To4() != nil {
ipv4 = true
} else {
ipv6 = true
}
}
if ipv4 {
nets = append(nets, "tcp4")
}
if ipv6 {
nets = append(nets, "tcp6")
}
return nets
}
48 changes: 0 additions & 48 deletions net.go

This file was deleted.

0 comments on commit c146bc3

Please sign in to comment.