Skip to content

Commit

Permalink
Simplify non proxy functionality and better verbose output.
Browse files Browse the repository at this point in the history
  • Loading branch information
williambailey committed Nov 15, 2015
1 parent 9ce5c1d commit bcb6bfc
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 219 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Apache License
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2015 William Bailey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ A no-frills local HTTP proxy server powered by a [proxy auto-config (PAC) file](

```
$ ./pacproxy -h
Usage of ./pacproxy:
-c="proxy.pac": PAC file to use
-l="127.0.0.1:12345": Interface and port to listen on
-v=false: send verbose output to STDERR
Usage of ./bin/pacproxy:
-c string
PAC file to use
-l string
Interface and port to listen on (default "127.0.0.1:8080")
-v send verbose output to STDERR
```

```bash
pacproxy &
export http_proxy="127.0.0.1:12345"
export https_proxy="127.0.0.1:12345"
export http_proxy="127.0.0.1:8080"
export https_proxy="127.0.0.1:8080"
curl -I "http://www.example.com"
```

Expand Down
158 changes: 48 additions & 110 deletions bindata_assetfs.go

Large diffs are not rendered by default.

74 changes: 12 additions & 62 deletions nonproxyhttphandler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"bytes"
"expvar"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"

"github.com/elazarl/go-bindata-assetfs"
"github.com/julienschmidt/httprouter"
)

Expand All @@ -35,49 +31,7 @@ func NewNonProxyHTTPHandler(pac *Pac) http.Handler {
router := httprouter.New()
router.RedirectFixedPath = false
router.RedirectTrailingSlash = false
router.Handler(
"GET",
"/pacproxy/*filepath",
http.StripPrefix(
"/pacproxy",
http.FileServer(
&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
Prefix: "htdocs",
},
),
),
)
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
d := struct {
Name string
Version string
PacFilename string
}{
Name: Name,
Version: Version,
PacFilename: pac.PacFilename(),
}
HTMLTemplate.ExecuteTemplate(w, "home", d)
})

router.GET("/stats", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
})

router.GET("/status", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
d := struct {
Name string
Expand All @@ -90,38 +44,34 @@ func NewNonProxyHTTPHandler(pac *Pac) http.Handler {
PacFilename: pac.PacFilename(),
KnownProxies: pac.ConnService.KnownProxies(),
}

HTMLTemplate.ExecuteTemplate(w, "status", d)
})
router.GET("/wpad.dat", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig; charset=utf-8")
w.Write(
bytes.Replace(
MustAsset("wpad.dat"),
[]byte("{{.HTTPHost}}"),
[]byte(r.Host),
-1,
),
)
HTMLTemplate.ExecuteTemplate(w, "home", d)
})
router.GET("/proxy.pac", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig; charset=utf-8")
t := r.URL.Query().Get("t")
if t == "1" {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
} else {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig; charset=utf-8")
}
w.Write(pac.PacConfiguration())
})
router.GET("/pac/find-proxy", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
router.GET("/lookup-proxy", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
urlParam := r.URL.Query().Get("url")
if urlParam == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if !strings.HasPrefix(urlParam, "http://") && !strings.HasPrefix(urlParam, "https://") {
urlParam = "http://" + urlParam
}
urlURL, err := url.Parse(urlParam)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
pacResult, err := pac.CallFindProxyForURLFromURL(urlURL)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
http.Error(w, http.StatusText(http.StatusInternalServerError)+"\n\n"+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
Expand Down
3 changes: 3 additions & 0 deletions pac.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func NewPac() (*Pac, error) {

// Unload any previously loaded pac configuration and reverts to default.
func (p *Pac) Unload() error {
log.Print("Unloading pac")
return p.Load(MustAsset("default.pac"))
}

Expand All @@ -75,6 +76,7 @@ func (p *Pac) Load(js interface{}) error {
p.mutex.Lock()
defer p.mutex.Unlock()
p.pacFile = ""
log.Print("Loading pac from string")
return p.initPacRuntime(js)
}

Expand All @@ -87,6 +89,7 @@ func (p *Pac) LoadFile(file string) error {
return err
}
p.pacFile, _ = filepath.Abs(f.Name())
log.Printf("Loading pac from file %s", p.pacFile)
return p.initPacRuntime(f)
}

Expand Down
5 changes: 3 additions & 2 deletions pacproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const Name = "pacproxy"
const Version = "0.8.1"
const Version = "1.0.0"

var (
fPac string
Expand All @@ -22,7 +22,7 @@ var (

func init() {
flag.StringVar(&fPac, "c", "", "PAC file to use")
flag.StringVar(&fListen, "l", "127.0.0.1:12345", "Interface and port to listen on")
flag.StringVar(&fListen, "l", "127.0.0.1:8080", "Interface and port to listen on")
flag.BoolVar(&fVerbose, "v", false, "send verbose output to STDERR")
}

Expand All @@ -35,6 +35,7 @@ func main() {
}
log.SetPrefix("")
log.SetFlags(log.Ldate | log.Lmicroseconds)
log.Printf("Starting %s v%s", Name, Version)

pac, err := NewPac()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions resource/bindata/default.pac
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
console.warn("Default pac configuration loaded")
console.warn("Unless another configuration is loaded only DIRECT connections will be specified");

function FindProxyForURL(url, host)
{
return "DIRECT";
Expand Down
6 changes: 0 additions & 6 deletions resource/bindata/htdocs/index.html

This file was deleted.

18 changes: 17 additions & 1 deletion resource/bindata/tmpl/html/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
</head>
<body>
<h1>{{ .Name }} <em>v{{ .Version }}</em></h1>
<p><code>{{ .PacFilename }}</code></p>
<p>Pac: <code><a href="/proxy.pac?t=1">{{ if .PacFilename }}{{ .PacFilename }}{{ else }}default{{ end }}</a></code></p>
<form action="/lookup-proxy" method="GET">
<label>Lookup proxy for <input type="text" name="url" size="50"/></label>
<button type="submit" value="">Go</button>
</form>
{{ if .KnownProxies }}
<h3>Known Proxies</h3>
<ul>
{{range .KnownProxies}}
<li>
<code>{{.Address}}</code> <em>- {{if .IsActive}}Active{{else}}Inactive{{end}}</em>
<br><small>Last updated {{.Updated.Format "2006-01-02T15:04:05Z07:00"}}</small>
{{if .Error }}<br><small>{{ .Error }}</small>{{end}}
</li>
{{ end }}
</ul>
{{ end }}
</body>
</html>
23 changes: 0 additions & 23 deletions resource/bindata/tmpl/html/status.tmpl

This file was deleted.

8 changes: 0 additions & 8 deletions resource/bindata/wpad.dat

This file was deleted.

0 comments on commit bcb6bfc

Please sign in to comment.