Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP proxy written in Go to replace Tinyproxy (#269)
- Loading branch information
Showing
29 changed files
with
623 additions
and
680 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package httpproxy | ||
|
||
import ( | ||
"encoding/base64" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func isAuthorized(responseWriter http.ResponseWriter, request *http.Request, | ||
username, password string) (authorized bool) { | ||
basicAuth := request.Header.Get("Proxy-Authorization") | ||
if len(basicAuth) == 0 { | ||
responseWriter.WriteHeader(http.StatusProxyAuthRequired) | ||
return false | ||
} | ||
b64UsernamePassword := strings.TrimPrefix(basicAuth, "Basic ") | ||
b, err := base64.StdEncoding.DecodeString(b64UsernamePassword) | ||
if err != nil { | ||
responseWriter.WriteHeader(http.StatusUnauthorized) | ||
return false | ||
} | ||
usernamePassword := strings.Split(string(b), ":") | ||
const expectedFields = 2 | ||
if len(usernamePassword) != expectedFields { | ||
responseWriter.WriteHeader(http.StatusBadRequest) | ||
return false | ||
} | ||
if username != usernamePassword[0] && password != usernamePassword[1] { | ||
responseWriter.WriteHeader(http.StatusUnauthorized) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package httpproxy | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/qdm12/golibs/logging" | ||
) | ||
|
||
func newHandler(ctx context.Context, wg *sync.WaitGroup, | ||
client *http.Client, logger logging.Logger, | ||
stealth, verbose bool, username, password string) http.Handler { | ||
const relayTimeout = 10 * time.Second | ||
return &handler{ | ||
ctx: ctx, | ||
wg: wg, | ||
client: client, | ||
logger: logger, | ||
relayTimeout: relayTimeout, | ||
verbose: verbose, | ||
stealth: stealth, | ||
username: username, | ||
password: password, | ||
} | ||
} | ||
|
||
type handler struct { | ||
ctx context.Context | ||
wg *sync.WaitGroup | ||
client *http.Client | ||
logger logging.Logger | ||
relayTimeout time.Duration | ||
verbose, stealth bool | ||
username, password string | ||
} | ||
|
||
func (h *handler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) { | ||
if len(h.username) > 0 && !isAuthorized(responseWriter, request, h.username, h.password) { | ||
h.logger.Info("%s unauthorized", request.RemoteAddr) | ||
return | ||
} | ||
switch request.Method { | ||
case http.MethodConnect: | ||
h.handleHTTPS(responseWriter, request) | ||
default: | ||
h.handleHTTP(responseWriter, request) | ||
} | ||
} | ||
|
||
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html | ||
var hopHeaders = [...]string{ //nolint:gochecknoglobals | ||
"Connection", | ||
"Keep-Alive", | ||
"Proxy-Authenticate", | ||
"Proxy-Authorization", | ||
"Te", // canonicalized version of "TE" | ||
"Trailers", | ||
"Transfer-Encoding", | ||
"Upgrade", | ||
} |
Oops, something went wrong.