Skip to content

Commit

Permalink
Add flag to optionally listen on all interfaces (#96)
Browse files Browse the repository at this point in the history
Server currently listens on `localhost:${port}` which is fine for most scenarios
This adds the ability to optionally listen on `:${port}` instead (all local IPs), an IPv6 address, etc..

Fixes #95
  • Loading branch information
keilin-anz authored Jun 2, 2022
1 parent a977568 commit e844635
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Cam Hutchison <camh@xdna.net>
Dave Goddard <dave.goddard@anz.com>
James Moriarty <jamespaulmoriarty@gmail.com>
Julia Ogris <julia.ogris@gmail.com>
Keilin Olsen <keilin.olsen@anz.com>
Keith Ferguson <keith.ferguson@anz.com>
Sam Uong <samuong@gmail.com>
Seng Ern Gan <sengern.gan@anz.com>
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/user"
"strconv"
)

var BuildVersion string
Expand All @@ -36,6 +38,7 @@ func whoAmI() string {

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
host := flag.String("l", "localhost", "address to listen on")
port := flag.Int("p", 3128, "port number to listen on")
pacurl := flag.String("C", "", "url of proxy auto-config (pac) file")
domain := flag.String("d", "", "domain of the proxy account (for NTLM auth)")
Expand Down Expand Up @@ -76,14 +79,14 @@ func main() {
os.Exit(0)
}

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

func createServer(port int, pacurl string, a *authenticator) *http.Server {
func createServer(host string, port int, pacurl string, a *authenticator) *http.Server {
pacWrapper := NewPACWrapper(PACData{Port: port})
proxyFinder := NewProxyFinder(pacurl, pacWrapper)
proxyHandler := NewProxyHandler(a, getProxyFromContext, proxyFinder.blockProxy)
Expand All @@ -98,8 +101,8 @@ func createServer(port int, pacurl string, a *authenticator) *http.Server {
handler = AddContextID(handler)

return &http.Server{
// Set the addr to localhost so that we only listen locally.
Addr: fmt.Sprintf("localhost:%d", port),
// Set the addr to host(defaults to localhost) : port(defaults to 3128)
Addr: net.JoinHostPort(host, strconv.Itoa(port)),
Handler: handler,
// TODO: Implement HTTP/2 support. In the meantime, set TLSNextProto to a non-nil
// value to disable HTTP/2.
Expand Down

0 comments on commit e844635

Please sign in to comment.