Skip to content

Commit

Permalink
Find and listen on all available networks (#105)
Browse files Browse the repository at this point in the history
This was motivated by npm being unable to proxy through alpaca via npm set config http://localhost:3128 since node 17+ favours IPv6, whereas Go's net.Listen favours IPv4 when a hostname is supplied.

This PR performs a hostname lookup and detects the networks (tcp4/tcp6) for all returned IPs.
  • Loading branch information
marcelocantos authored Sep 17, 2022
1 parent fbbad0d commit 54ebbda
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ on:
jobs:
format:
strategy:
matrix:
matrix:
os: [ 'ubuntu-latest' ]
go: [ '1.17' ]
go: [ '1.19' ]

runs-on: ${{ matrix.os }}

Expand All @@ -34,9 +34,9 @@ jobs:

test:
strategy:
matrix:
matrix:
os: [ 'macos-latest', 'ubuntu-latest', 'windows-latest' ]
go: [ '1.17' ]
go: [ '1.18', '1.19' ]

runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
- os: 'windows-latest'
goos: 'windows'
goarch: 'amd64'
go: [ '1.17' ]
go: [ '1.18', '1.19' ]

runs-on: ${{ matrix.target.os }}

Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ James Moriarty <[email protected]>
Julia Ogris <[email protected]>
Keilin Olsen <[email protected]>
Keith Ferguson <[email protected]>
Marcelo Cantos <[email protected]>
Sam Uong <[email protected]>
Seng Ern Gan <[email protected]>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/samuong/alpaca

go 1.17
go 1.19

require (
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e
Expand Down
46 changes: 43 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,23 @@ func main() {
os.Exit(0)
}

errch := make(chan error)

s := createServer(*host, *port, *pacurl, a)
log.Printf("Listening on %s", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)

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

log.Fatal(<-errch)
}

func createServer(host string, port int, pacurl string, a *authenticator) *http.Server {
Expand All @@ -109,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
}

0 comments on commit 54ebbda

Please sign in to comment.