Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ func (ctx *Context) onAuth(authType string, user string, pass string) bool {
return ctx.Prx.OnAuth(ctx, authType, user, pass)
}

func (ctx *Context) onDial(network, addr string) (c net.Conn, err error) {
defer func() {
if err, ok := recover().(error); ok {
ctx.doError("Dial", ErrPanic, err)
}
}()
if ctx.Prx.OnDial != nil {
return ctx.Prx.OnDial(network, addr)
} else {
return net.Dial(network, addr)
}

}

func (ctx *Context) onConnect(host string) (ConnectAction ConnectAction,
newHost string) {
defer func() {
Expand Down Expand Up @@ -208,7 +222,8 @@ func (ctx *Context) doConnect(w http.ResponseWriter, r *http.Request) (b bool) {
ctx.ConnectHost = host
switch ctx.ConnectAction {
case ConnectProxy:
conn, err := net.Dial("tcp", host)

conn, err := ctx.onDial("tcp", host)
if err != nil {
hijConn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n"))
hijConn.Close()
Expand Down
3 changes: 3 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpproxy

import (
"crypto/tls"
"net"
"net/http"
"sync/atomic"
)
Expand Down Expand Up @@ -40,6 +41,8 @@ type Proxy struct {
OnConnect func(ctx *Context, host string) (ConnectAction ConnectAction,
newHost string)

OnDial func(network string, addr string) (c net.Conn, err error)

// Request callback. It greets remote request.
// If it returns non-nil response, stops processing remote request.
OnRequest func(ctx *Context, req *http.Request) (resp *http.Response)
Expand Down