Skip to content

Commit

Permalink
Merge pull request #3 from saucelabs/dans-logging
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
waggledans authored Apr 11, 2022
2 parents e186761 + 70e82d8 commit 7bd573f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ jobs:
go-version: 1.17

- name: Setup golangci-lint
uses: golangci/golangci-lint-action@v2.5.2
uses: golangci/golangci-lint-action@v3.1.0
with:
version: v1.43.0
version: v1.45.0
args: "--timeout 5m -v -c .golangci.yml"

- name: Lint
run: /home/runner/golangci-lint-1.43.0-linux-amd64/golangci-lint run -v -c .golangci.yml
run: golangci-lint run -v -c .golangci.yml

- name: Test
run: make test coverage
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17
id: go

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.7.0
uses: goreleaser/goreleaser-action@v2
with:
args: release --rm-dist
env:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Load config from Config file, and from env vars. Use viper for that
- Automatically alocates a random port, if the specified one is in-use

## [0.1.16] - 2022-04-11
## Changed
- Reset upstream proxy configuration when PAC is used and a URL doesn't require proxy
- Upgraded golang-ci version (CI pipeline)

## [0.1.15] - 2022-02-22
## Changed
- Fix message logging level
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ doc:
ifndef HAS_GODOC
$(error You must install godoc, run "go get golang.org/x/tools/cmd/godoc")
endif
@echo "Open localhost:6060/pkg/github.com/saucelabs/forwarder/ in your browser\n"
@echo "Open http://localhost:6060/pkg/github.com/saucelabs/forwarder/ in your browser\n"
@godoc -http :6060

ci: lint test coverage
Expand Down
7 changes: 4 additions & 3 deletions pkg/proxy/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

// Package proxy provides a simple proxy. The proxy can be protected with basic
// auth. It can also forward connections to a parent proxy, and authorize
// Package proxy provides a simple forward proxy server. The proxy can be protected with
// HTTP basic authentication.
// It can also forward connections to a parent proxy, and authorize
// connections against that. Both local, and parent credentials can be set via
// environment variables. For local proxy credential, set `PROXY_CREDENTIAL`.
// For remote proxy credential, set `PROXY_PARENT_CREDENTIAL`.
// For parent proxy credential, set `PROXY_PARENT_CREDENTIAL`.
package proxy
7 changes: 4 additions & 3 deletions pkg/proxy/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func ExampleNew() {
// Target/end server.
//////

// Create a protected HTTP server. user1:pass1 base64-encoded is dXNlcjE6cGFzczE=.
targetServer := createMockedHTTPServer(http.StatusOK, "body", "dXNlcjE6cGFzczE=")

defer func() { targetServer.Close() }()
Expand Down Expand Up @@ -89,6 +90,7 @@ func ExampleNew() {
// PAC server.
//////

// Start a protected server (user:pass) serving PAC file.
pacServer := createMockedHTTPServer(http.StatusOK, pacText.String(), "dXNlcjpwYXNz")

defer func() { pacServer.Close() }()
Expand All @@ -115,9 +117,8 @@ func ExampleNew() {
//////
// Local proxy.
//
// It's protected with Basic Auth. Upstream proxy will be automatically, and
// dynamically setup via PAC, including credentials for proxies specified
// in the PAC content.
// It's protected with Basic Auth. Upstream proxy URL and credentials are determined
// per URL via PAC.
//////

localProxy, err := New(
Expand Down
15 changes: 9 additions & 6 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ func setupUpstreamProxyConnection(ctx *goproxy.ProxyCtx, uri *url.URL) {

ctx.Proxy.ConnectDial = ctx.Proxy.NewConnectDialToProxyWithHandler(uri.String(), connectReqHandler)

logger.Get().Debuglnf("Setup up forwarding connections to %s", uri.Redacted())
logger.Get().Debuglnf("Connection to the upstream proxy %s is set up", uri.Redacted())
}

// setupUpstreamProxyConnection dynamically forwards connections to an upstream
// proxy setup via PAC.
func setupPACUpstreamProxyConnection(p *Proxy, ctx *goproxy.ProxyCtx) error {
urlToFindProxyFor := ctx.Req.URL.String()

logger.Get().Debuglnf("Finding proxy for %s", urlToFindProxyFor)
logger.Get().Tracelnf("Finding proxy for %s", urlToFindProxyFor)

pacProxies, err := p.pacParser.Find(urlToFindProxyFor)
if err != nil {
Expand All @@ -345,15 +345,18 @@ func setupPACUpstreamProxyConnection(p *Proxy, ctx *goproxy.ProxyCtx) error {
pacProxy := pacProxies[0]
pacProxyURI := pacProxy.GetURI()

// Should only do something if there's a proxy for the given URL, not
// `DIRECT`.
// Should only set up upstream if there's a proxy and not `DIRECT`.
if pacProxyURI != nil {
setupUpstreamProxyConnection(ctx, pacProxyURI)

return nil
}
} else {
logger.Get().Debugln("Found no proxy for", urlToFindProxyFor)
}

logger.Get().Debugln("Found no proxy for", urlToFindProxyFor)
// Clear upstream proxy settings (if any) for this request.
resetUpstreamSettings(ctx)

return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func executeRequest(client *http.Client, uri string) (int, string, error) {
// Tests
//////

//nolint:maintidx
func TestNew(t *testing.T) {
//////
// Randomness automates port allocation, ensuring no collision happens
Expand Down

0 comments on commit 7bd573f

Please sign in to comment.