Skip to content

Commit

Permalink
fixup! Serve wrapped PAC file pointing at alpaca
Browse files Browse the repository at this point in the history
Don't serve upstream PAC (`/proxy.pac`).

Addresses: samuong#16 (comment)
  • Loading branch information
camh- committed Sep 14, 2019
1 parent 72d31e7 commit 93581e2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
15 changes: 3 additions & 12 deletions pacwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,16 @@ func (pw *PACWrapper) Wrap(pacjs []byte) {
}

func (pw *PACWrapper) SetupHandlers(mux *http.ServeMux) {
mux.HandleFunc("/proxy.pac", pw.handleProxyPAC)
mux.HandleFunc("/alpaca.pac", pw.handleAlpacaPAC)
mux.HandleFunc("/alpaca.pac", pw.handlePAC)
}

func (pw *PACWrapper) handleProxyPAC(w http.ResponseWriter, req *http.Request) {
pw.handlePAC(w, req, pw.data.PAC)
}

func (pw *PACWrapper) handleAlpacaPAC(w http.ResponseWriter, req *http.Request) {
pw.handlePAC(w, req, pw.alpacaPAC)
}

func (pw *PACWrapper) handlePAC(w http.ResponseWriter, req *http.Request, pac string) {
func (pw *PACWrapper) handlePAC(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
if _, err := w.Write([]byte(pac)); err != nil {
if _, err := w.Write([]byte(pw.alpacaPAC)); err != nil {
log.Printf("Error writing PAC to response: %v\n", err)
}
}
22 changes: 1 addition & 21 deletions pacwrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,7 @@ func TestWrapEmptyPAC(t *testing.T) {
assert.Contains(t, pw.alpacaPAC, `return "DIRECT"`)
}

func TestProxyPACServe(t *testing.T) {
pw := NewPACWrapper(PACData{1234, "http://pacserver/proxy.pac", "domain", "username"})
pac := `function FindProxyForURL(url, host) { return "DIRECT" }`
pw.Wrap([]byte(pac))
mux := http.NewServeMux()
pw.SetupHandlers(mux)
server := httptest.NewServer(mux)
defer server.Close()

resp, err := http.Get(server.URL + "/proxy.pac")
require.NoError(t, err)

assert.Equal(t, resp.StatusCode, http.StatusOK)
assert.Equal(t, "application/x-ns-proxy-autoconfig", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, string(body), pac)
resp.Body.Close()
}

func TestAlpacaPACServe(t *testing.T) {
func TestPACServe(t *testing.T) {
pw := NewPACWrapper(PACData{1234, "http://pacserver/proxy.pac", "domain", "username"})
pac := `function FindProxyForURL(url, host) { return "DIRECT" }`
pw.Wrap([]byte(pac))
Expand Down

0 comments on commit 93581e2

Please sign in to comment.