From 539d25614633e23e9291a5c549ef8be2c05f9812 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 03:34:26 +0900 Subject: [PATCH] update refactor http request --- proxy/http.go | 5 +++-- proxy/proxy.go | 3 ++- request/http.go | 53 +++++++++++++++++++------------------------------ 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/proxy/http.go b/proxy/http.go index 3352b86..41d80a7 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -4,10 +4,11 @@ import ( "fmt" "net" + "github.com/xvzc/SpoofDPI/request" "github.com/xvzc/SpoofDPI/util" ) -func HandleHttp(clientConn net.Conn, ip string, message []byte) { +func HandleHttp(clientConn net.Conn, ip string, r *request.HttpRequest) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { util.Debug(err) @@ -20,7 +21,7 @@ func HandleHttp(clientConn net.Conn, ip string, message []byte) { go Serve(remoteConn, clientConn, "HTTP") util.Debug("[HTTP] Sending request to the server") - fmt.Fprintf(remoteConn, string(message)) + fmt.Fprintf(remoteConn, string(*r.Raw)) Serve(clientConn, remoteConn, "HTTP") } diff --git a/proxy/proxy.go b/proxy/proxy.go index 432bd22..f9b69e5 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -39,6 +39,7 @@ func Start() { util.Debug("Client sent data: ", len(b)) r := request.NewHttpRequest(&b) + util.Debug("Request: \n" + string(*r.Raw)) if !r.IsValidMethod() { log.Println("Unsupported method: ", r.Method) @@ -59,7 +60,7 @@ func Start() { HandleHttps(clientConn, ip) } else { util.Debug("HTTP Requested.") - HandleHttp(clientConn, ip, b) + HandleHttp(clientConn, ip, &r) } }() } diff --git a/request/http.go b/request/http.go index 283381b..c2c678d 100644 --- a/request/http.go +++ b/request/http.go @@ -5,16 +5,19 @@ import ( ) type HttpRequest struct { - Raw *[]byte - Method string - Domain string + Raw *[]byte + Method string + Domain string + Version string } func NewHttpRequest(raw *[]byte) HttpRequest { + method, domain, version := parse(raw) return HttpRequest{ - Raw: raw, - Method: extractMethod(raw), - Domain: extractDomain(raw), + Raw: raw, + Method: method, + Domain: domain, + Version: version, } } @@ -30,42 +33,26 @@ func (r *HttpRequest) ToChunks() { } -func extractDomain(request *[]byte) string { - i := 0 - for ; i < len(*request); i++ { - if (*request)[i] == ' ' { - i++ - break +func parse(raw *[]byte) (string, string, string) { + var firstLine string + for i := 0; i < len(*raw); i++ { + if (*raw)[i] == '\n' { + firstLine = string((*raw)[:i]) } } - j := i - for ; j < len(*request); j++ { - if (*request)[j] == ' ' { - break - } - } + tokens := strings.Split(firstLine, " ") + + method := strings.TrimSpace(tokens[0]) + domain := strings.TrimSpace(tokens[1]) + version := strings.TrimSpace(tokens[2]) - domain := string((*request)[i:j]) domain = strings.Replace(domain, "http://", "", 1) domain = strings.Replace(domain, "https://", "", 1) domain = strings.Split(domain, ":")[0] domain = strings.Split(domain, "/")[0] - return strings.TrimSpace(domain) -} - -func extractMethod(message *[]byte) string { - i := 0 - for ; i < len(*message); i++ { - if (*message)[i] == ' ' { - break - } - } - - method := strings.TrimSpace(string((*message)[:i])) - - return strings.ToUpper(method) + return method, domain, version } func getValidMethods() map[string]struct{} {