Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find and listen on all available networks #105

Merged
merged 14 commits into from
Sep 17, 2022
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' ]
samuong marked this conversation as resolved.
Show resolved Hide resolved

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
}