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

Serve /alpaca.pac to send non-DIRECT requests to alpaca #16

Merged
merged 6 commits into from
Sep 18, 2019

Conversation

camh-
Copy link
Collaborator

@camh- camh- commented Aug 22, 2019

Serve a PAC file on /alpaca.pac that only returns DIRECT or PROXY localhost:<listen-port> (usually 3128). If the PAC file that alpaca is using would return DIRECT for a URL, the wrapped PAC also returns DIRECT. For everything else, it returns a proxy directive to send traffic to alpaca. If alpaca does not have a PAC file (wrong network, etc), then the PAC function only returns DIRECT.

This allows the system (Mac OSX, Gnome, etc) to use alpaca as the proxy, and to configure alpaca with the network proxy URL. When the system evaluates a URL with the wrapped PAC file and gets a DIRECT response, it will not proxy through alpaca and go direct instead. This helps with the odd application that doesn't seem to handle going through a proxy at all. It also means that when you're off the network that serves the PAC file, no requests go through alpaca. This makes alpaca work well as a whole-system proxy instead of just for CLI apps as originally intended.

A bit of refactoring and lint fixes made it into this PR. The most significant change would be the use of http handler wrappers (also known in some circles as http middleware). Once the code went from 1 to >1 handlers, it made sense to factor out some functionality into a handler wrapper. Some additional functionality was added that way too.

Please review this commit-by-commit and read the commit messages. I hope that makes it clearer to understand the changes and their reason. I'm happy to entertain the most nit-picky requests/reviews, so don't hold back, no matter how trivial it seems.

Fixes: #14

@juliaogris
Copy link
Collaborator

juliaogris commented Aug 22, 2019

@samuong - IMO you should protect and "require pull request reviews before merging" for your master branch. Maybe ;)

Copy link
Collaborator

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty tired now @camh- sorry. I've reviewed your first two commits.
Will do the rest tomorrow.

Looks really neat what you guys have built!

pacfetcher_test.go Outdated Show resolved Hide resolved
contextid.go Outdated Show resolved Hide resolved
contextid.go Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
pacfetcher_test.go Outdated Show resolved Hide resolved
pacfetcher_test.go Outdated Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
contextid.go Show resolved Hide resolved
contextid.go Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
proxyfinder.go Show resolved Hide resolved
proxyfinder.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
pacwrapper_test.go Show resolved Hide resolved
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Remove space in `// nolint:*`

Addresses: samuong#16 (review)
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Use testify.require for test setup failures.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Remove space in ``// nolint:*`

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Use testify.require for test setup failures.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Move check of `pf.fetcher == nil` outside the handler.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Aug 27, 2019
Use testify.require for test setup failures.

Addresses: samuong#16 (comment)
@camh-
Copy link
Collaborator Author

camh- commented Aug 27, 2019

@juliaogris @anzdaddy Changes pushed as fixup commits. PTAL, and resolve all comments that you feel are now resolved. Or continue to argue as you see fit.

@samuong
Copy link
Owner

samuong commented Aug 28, 2019

@samuong - IMO you should protect and "require pull request reviews before merging" for your master branch. Maybe ;)

@juliaogris The only reason I haven't done that in the past is that I didn't have anyone else to review my own pull requests. You all are the only ones (other than me) who have commented on PRs, so if you're happy to be collaborators and review my code, then I'll add you to the repo.

Copy link
Collaborator

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per usual: reads really well, much fun to review.

Sadly I lack a bit of context to be more than an aspirationally intelligent linter for your PR.

I understand the high level (I think) - keep alpaca running on and off corp and if there is no pac file just DIRECT everything but I'm really missing context in the middle so I don't feel in the position to approve.

Peace&love

requestlogger.go Show resolved Hide resolved
Copy link

@anzdaddy anzdaddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I just noticed I had an old review pending. Lazily submitting possibly stale review.)

contextid_test.go Outdated Show resolved Hide resolved
pacwrapper_test.go Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
camh- added a commit to camh-/alpaca that referenced this pull request Aug 31, 2019
camh- added a commit to camh-/alpaca that referenced this pull request Aug 31, 2019
Rework handler to remove `//nolint` directive.

Addresses: samuong#16 (comment)
@camh-
Copy link
Collaborator Author

camh- commented Sep 1, 2019

@marcelocantos @juliaogris @samuong All comments resolved. I have forced-pushed my squashed series, so back to just the commits that matter without fixups. I have removed the //nolint directives and added a couple of TODOs.

PTAL

contextid_test.go Outdated Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
contextid_test.go Outdated Show resolved Hide resolved
proxy_test.go Show resolved Hide resolved
proxy_test.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
pacwrapper.go Outdated Show resolved Hide resolved
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
Add full stop to end of comment sentence.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
Reformat comment for 100 char line length.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
Use `net/http` `http.MethodGet` const for `"GET"`

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
Don't serve upstream PAC (`/proxy.pac`).

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Sep 14, 2019
@camh-
Copy link
Collaborator Author

camh- commented Sep 14, 2019

@samuong PTAL

I've pushed fixup commits and linked them to the comments so you can see how I have addressed your comments. When you've re-reviewed, I'll run git rebase -i --autosquash master to squash the fixups into the relevant commits and force push so there is still a clean series.

camh- added a commit to camh-/alpaca that referenced this pull request Sep 18, 2019
Change back to simpler test for consecutive IDs starting from zero.

Addresses: samuong#16 (comment)
camh- added a commit to camh-/alpaca that referenced this pull request Sep 18, 2019
Remove PACUrl, domain and username from PACData. They are not currently
used and were added when envisaging a more general PAC wrapping
feature. They can be added back when that feature is added.

Addresses: samuong#16 (comment)
@camh-
Copy link
Collaborator Author

camh- commented Sep 18, 2019

PTAL @samuong. Again, fixup commits pushed which I'll squash into the main series when you're happy with it.

@samuong
Copy link
Owner

samuong commented Sep 18, 2019

Thanks Cam! LGTM.

I'll let you squash everything before I actually approve it, since I think your squash will invalidate my approval anyway. But once you're done I'm happy to give the actual approval.

The proxyHandler was adding a strictly increasing ID value to the
request's context that is used to correlate logs. As the only http
handler in the application, this was fine. However, moving to serving
PAC files from alpaca directly will split up the handling of http
requests over different handler for that and the proxy function. In this
case, it makes sense to split off the code that adds the ID to the
context into a separate piece of http middleware. This will ensure that
all requests have an ID, not just those handled by the proxy handler.

Add tests for the context ID.

Issues: samuong#14
Add a `WrapHandler()` method to `ProxyHandler` that can be used to wrap
another `http.Handler` such that any proxy requests (CONNECT methods or
absolute-form URIs) are passed to `ProxyHandler.ServeHTTP` and the rest
are passed to the next `http.Handler` (in the standard way that http
"middleware" works).

Plug in an empty `http.ServeMux` as the next handler. Any origin-form
URIs requested will go there and result in a 404 at the moment. This
will allow us to add paths to the mux in future commits so that alpaca
can serve content itself rather than just proxy requests. This will be
used to serve the PAC file that alpaca is using as well as serve a
wrapped PAC file that will direct all non-direct requests to alpaca.

Update the existing test cases to use the `WrapHandler()` method and
ensure the next `http.Handler` does not get called (an empty mux that
always returns 404).

Add an additional test to ensure that the middleware passes through
non-proxy requests (origin-form URIs) to the mux.

Move a few imports around as a result of `goimports` way of ordering
imports.

Issues: samuong#14
Add a request logger handler wrapper around the `ServeMux` used to
handle non-proxy http requests. At the moment there are none of these -
this is in preparation for handling some origin-form requests for
serving PAC files.

Issues: samuong#14
Consolidate handing of the PAC URL into the `ProxyFinder` so that the
one function is used as the proxy function and all logging of DIRECT
proxy requests are in one place. This simplifies `main()` keeping the
proxy function out of it. It will also help when looking to consolidate
all request logging in one place.

Issues: samuong#14
Put ProxyFinder.checkForUpdates in the HTTP path by adding a
`WrapHandler` method to check for updates before passing the HTTP
request to the next handler. This makes little difference to the
`ProxyHandler` as it triggers a check for updates on every proxied
request, but we will also want to check for updates on requests to the
`ServeMux` when it is serving content later.

Issues: samuong#14
Serve a PAC file that is wrapped such that the `FindProxyForURL`
function returns either `DIRECT` or `PROXY localhost:port` pointing
at alpaca. `DIRECT` is returned when the PAC file that alpaca is using
would return `DIRECT`. `PROXY localhost:port` is returned otherwise.

This allows the OS to be set up to use `http://localhost:3128/alpaca.pac`
as the PAC file to use which will make it use alpaca for any URLs that
alpaca would proxy but otherwise go direct if that is all alpaca is
going to do anyway. This helps with the odd application that doesn't
seem to like going through a proxy at all. If the system were using
the same PAC file that alpaca is using, applications would make
non-proxied direct connections for these same URLs.

If alpaca does not have a PAC file (if the host is on a different
network for instance), then the `FindProxyForURL` function returned on
the `/alpaca.pac` path will just return `DIRECT` always, directing all
traffic away from alpaca.

Issues: samuong#14
@camh-
Copy link
Collaborator Author

camh- commented Sep 18, 2019

Ok. Squashed and force-pushed. If you click on the "force-pushed" link where it says "camh- force-pushed the camh-:pacwrap branch" you can verify the push did not alter anything.

Copy link
Owner

@samuong samuong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Cam!

@camh-
Copy link
Collaborator Author

camh- commented Sep 18, 2019

@samuong I wondered if this was going to happen. It looks like you're not a CODEOWNER so you cannot approve this merge. Only @juliaogris or @marcelocantos can :-)

If you want to raise a PR to add yourself to CODEOWNERS, I can approve it. (or you can just use your admin superpowers and merge anyway)

@samuong
Copy link
Owner

samuong commented Sep 18, 2019

#25

camh- pushed a commit that referenced this pull request Sep 18, 2019
* Serve wrapped PAC file pointing at alpaca
* Move ProxyFinder.checkForUpdates into request path
* Consolidate PAC URL, proxy finding and logging
* Add request logger around handler mux
* Make ProxyHandler HTTP middleware capable
* Split request context id adding to separate middleware
@camh- camh- merged commit 29fa6b8 into samuong:master Sep 18, 2019
@camh- camh- deleted the pacwrap branch September 18, 2019 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Served wrapped PAC file to point at alpaca
5 participants