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
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.PHONY: all
all: build lint

.PHONY: build
build:
go build

# Build all compiling os-arch combinations just to check for regressions. Output
# binaries get deleted.
.PHONY: all-osarchs
all-osarchs:
TMPDIR=$${TMPDIR:-/tmp}
GOOSNOT="android|ios|js|plan9"; \
for dist in $$(go tool dist list | egrep -v "^($$GOOSNOT)/|^darwin/amd64$$"); do \
marcelocantos marked this conversation as resolved.
Show resolved Hide resolved
echo " GOOS=$${dist%/*} GOARCH=$${dist#*/}"; \
GOOS=$${dist%/*} GOARCH=$${dist#*/} go build -o $$TMPDIR/alpaca && \
printf '\e[A\e[1;32m✔\e[0m\n'; \
done; \
rm $$TMPDIR/alpaca

.PHONY: test
test:
go test ./...

.PHONY: lint
lint:
golangci-lint run
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
18 changes: 18 additions & 0 deletions gogroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

// Run a group of goroutines and report the first error.
func gogroup[T any](args []T, f func(T) error) error {
errch := make(chan error)

gopher := func(arg T) {
if err := f(arg); err != nil {
errch <- err
}
}

for _, arg := range args {
go gopher(arg)
}

return <-errch
}
marcelocantos marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ func main() {
}

s := createServer(*host, *port, *pacurl, a)
log.Printf("Listening on %s", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
log.Fatal(gogroup(networks(*host), func(network string) error {
listener, err := net.Listen(network, s.Addr)
if err != nil {
return err
}
log.Printf("Listening on %s %s", network, s.Addr)
return s.Serve(listener)
}))
}

func createServer(host string, port int, pacurl string, a *authenticator) *http.Server {
Expand Down
34 changes: 34 additions & 0 deletions net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"log"
"net"
)

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
}
marcelocantos marked this conversation as resolved.
Show resolved Hide resolved