fix(main.go) code refactoring and comments
This commit is contained in:
parent
a1eca3121e
commit
8cc80f41fc
69
main.go
69
main.go
|
@ -12,17 +12,21 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type ProxyServer struct {
|
||||||
PORT string
|
port string
|
||||||
AUTH_USERNAME string
|
username string
|
||||||
AUTH_PASSWORD string
|
password string
|
||||||
)
|
}
|
||||||
|
|
||||||
func checkProxyAuth(r *http.Request, username, password string) bool {
|
func (ps *ProxyServer) isAuthRequired() bool {
|
||||||
// If no username or password provided, always allow
|
return ps.username != "" && ps.password != ""
|
||||||
if AUTH_USERNAME == "" && AUTH_PASSWORD == "" {
|
}
|
||||||
|
|
||||||
|
func (ps *ProxyServer) checkProxyAuth(r *http.Request) bool {
|
||||||
|
if !ps.isAuthRequired() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
authHeader := r.Header.Get("Proxy-Authorization")
|
authHeader := r.Header.Get("Proxy-Authorization")
|
||||||
const prefix = "Basic "
|
const prefix = "Basic "
|
||||||
if !strings.HasPrefix(authHeader, prefix) {
|
if !strings.HasPrefix(authHeader, prefix) {
|
||||||
|
@ -39,11 +43,11 @@ func checkProxyAuth(r *http.Request, username, password string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return pair[0] == AUTH_USERNAME && pair[1] == AUTH_PASSWORD
|
return pair[0] == ps.username && pair[1] == ps.password
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainHandler(w http.ResponseWriter, r *http.Request) {
|
func (ps *ProxyServer) requestHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !checkProxyAuth(r, AUTH_USERNAME, AUTH_PASSWORD) {
|
if !ps.checkProxyAuth(r) {
|
||||||
w.Header().Set("Proxy-Authenticate", `Basic realm="Provide username and password"`)
|
w.Header().Set("Proxy-Authenticate", `Basic realm="Provide username and password"`)
|
||||||
http.Error(w, "Proxy authentication required", http.StatusProxyAuthRequired)
|
http.Error(w, "Proxy authentication required", http.StatusProxyAuthRequired)
|
||||||
return
|
return
|
||||||
|
@ -56,30 +60,33 @@ func mainHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func logRequest(r *http.Request) {
|
// Manages tunneling requests (used for HTTPS connections).
|
||||||
log.Printf("Method: %s, URL: %s", r.Method, r.URL.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleTunneling(w http.ResponseWriter, r *http.Request) {
|
func handleTunneling(w http.ResponseWriter, r *http.Request) {
|
||||||
logRequest(r)
|
logRequest(r)
|
||||||
|
|
||||||
dest_conn, err := net.DialTimeout("tcp", r.Host, 10*time.Second)
|
destConn, err := net.DialTimeout("tcp", r.Host, 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
// Hijack the connection to manage it at the TCP level
|
||||||
hijacker, ok := w.(http.Hijacker)
|
hijacker, ok := w.(http.Hijacker)
|
||||||
if !ok {
|
if !ok {
|
||||||
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
|
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client_conn, _, err := hijacker.Hijack()
|
clientConn, _, err := hijacker.Hijack()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
go transfer(dest_conn, client_conn)
|
|
||||||
go transfer(client_conn, dest_conn)
|
// Transfer data between client and destination
|
||||||
|
go transfer(destConn, clientConn)
|
||||||
|
go transfer(clientConn, destConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func transfer(destination io.WriteCloser, source io.ReadCloser) {
|
func transfer(destination io.WriteCloser, source io.ReadCloser) {
|
||||||
|
@ -97,6 +104,7 @@ func handleHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
copyHeader(w.Header(), resp.Header)
|
copyHeader(w.Header(), resp.Header)
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
io.Copy(w, resp.Body)
|
io.Copy(w, resp.Body)
|
||||||
|
@ -110,22 +118,29 @@ func copyHeader(dst, src http.Header) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logRequest(r *http.Request) {
|
||||||
|
log.Printf("Method: %s, URL: %s", r.Method, r.URL.String())
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&PORT, "port", "3000", "Specify the port the proxy will run on")
|
var port, username, password string
|
||||||
flag.StringVar(&AUTH_USERNAME, "username", "", "Username for proxy authentication")
|
|
||||||
flag.StringVar(&AUTH_PASSWORD, "password", "", "Password for proxy authentication")
|
flag.StringVar(&port, "port", "3000", "Specify the port the proxy will run on")
|
||||||
|
flag.StringVar(&username, "username", "", "Username for proxy authentication")
|
||||||
|
flag.StringVar(&password, "password", "", "Password for proxy authentication")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if (AUTH_USERNAME == "" && AUTH_PASSWORD != "") || (AUTH_USERNAME != "" && AUTH_PASSWORD == "") {
|
if (username == "" && password != "") || (username != "" && password == "") {
|
||||||
log.Fatal("Error: Both username and password must be provided, or neither should be.")
|
log.Fatal("Error: Both username and password must be provided, or neither should be.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
proxy := &ProxyServer{port: port, username: username, password: password}
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: ":" + PORT,
|
Addr: ":" + proxy.port,
|
||||||
Handler: http.HandlerFunc(mainHandler),
|
Handler: http.HandlerFunc(proxy.requestHandler),
|
||||||
// Disable HTTP/2.
|
// Disable HTTP/2 (https://github.com/golang/go/issues/14797#issuecomment-196103814)
|
||||||
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
||||||
}
|
}
|
||||||
log.Printf("Starting proxy server on :%s\n", PORT)
|
log.Printf("Starting proxy server on :%s\n", proxy.port)
|
||||||
log.Fatal(server.ListenAndServe())
|
log.Fatal(server.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue