Skip to content

Commit

Permalink
fix the issue with closing auth connections, closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
ameshkov committed Jun 12, 2023
1 parent a1f6d4d commit 147e33e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func newNotAuthorizedResponse(session *Session) *http.Response {

// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authenticate.
res.Header.Set("Proxy-Authenticate", "Basic")

return res
}

Expand Down
6 changes: 6 additions & 0 deletions examples/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Auth proxy demo

Runs an HTTPS proxy on `localhost:3333` with authorization:

* Username: `user`
* Password: `pass`
46 changes: 46 additions & 0 deletions examples/auth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"net"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/gomitmproxy"
)

func main() {
log.SetLevel(log.DEBUG)

go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()

// Prepare the proxy.
addr := &net.TCPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: 3333,
}

proxy := gomitmproxy.NewProxy(gomitmproxy.Config{
ListenAddr: addr,

Username: "user",
Password: "pass",
APIHost: "gomitmproxy",
})

err := proxy.Start()
if err != nil {
log.Fatal(err)
}

signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
<-signalChannel

// Stop the proxy.
proxy.Close()
}
9 changes: 7 additions & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,14 @@ func (p *Proxy) handleLoop(ctx *Context) {

if err := p.handleRequest(ctx); err != nil {
log.Debug("id=%s: closing connection due to: %v", ctx.ID(), err)

return
}
}
}

// handleRequest reads an incoming request and processes it.
func (p *Proxy) handleRequest(ctx *Context) error {
func (p *Proxy) handleRequest(ctx *Context) (err error) {
origReq, err := p.readRequest(ctx)

defer log.OnCloserError(origReq.Body, log.DEBUG)
Expand Down Expand Up @@ -268,7 +269,11 @@ func (p *Proxy) handleRequest(ctx *Context) error {

_ = p.writeResponse(session)

return errClose
// Do not return any error here as we must keep the connection
// alive. When the client receives 407 error, it can write
// another request with user credentials to the same connection.
// See https://github.com/AdguardTeam/gomitmproxy/pull/19.
return nil
}
}

Expand Down

0 comments on commit 147e33e

Please sign in to comment.