From 81938445a11099ac216a2ab2d1e6323b58f85a5f Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 02:41:02 +0900 Subject: [PATCH] add request module --- proxy/proxy.go | 26 +++++------- request/http.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 request/http.go diff --git a/proxy/proxy.go b/proxy/proxy.go index 5eb4892..dc0c76f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,6 +6,7 @@ import ( "os" "github.com/xvzc/SpoofDPI/config" + "github.com/xvzc/SpoofDPI/request" "github.com/xvzc/SpoofDPI/util" ) @@ -30,40 +31,35 @@ func Start() { go func() { defer clientConn.Close() - message, err := ReadBytes(clientConn) + b, err := ReadBytes(clientConn) if err != nil { return } - util.Debug("Client sent data: ", len(message)) + util.Debug("Client sent data: ", len(b)) - util.Debug("") - util.Debug("Request : ") - util.Debug("\n" + string(message)) + r := request.New(&b) - method := util.ExtractMethod(&message) - - if !util.IsValidMethod(method) { - util.Debug("Not a valid method: " + method) + if !r.IsValidMethod() { + log.Println("Unsupported method: ", r.Method) return } - domain := util.ExtractDomain(&message) - - ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, domain) // Dns lookup over https + // Dns lookup over https + ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, r.Domain) if err != nil { - log.Println("Error looking up dns: "+domain, err) + log.Println("Error looking up dns: "+r.Domain, err) return } util.Debug("ip: " + ip) - if util.ExtractMethod(&message) == "CONNECT" { + if r.Method == "CONNECT" { util.Debug("HTTPS Requested") HandleHttps(clientConn, ip) } else { util.Debug("HTTP Requested.") - HandleHttp(clientConn, ip, message) + HandleHttp(clientConn, ip, b) } }() } diff --git a/request/http.go b/request/http.go new file mode 100644 index 0000000..a109e1b --- /dev/null +++ b/request/http.go @@ -0,0 +1,107 @@ +package request + +import ( + "strings" +) + +type Request struct { + Raw *[]byte + Method string + Domain string +} + +func (r *Request) IsValidMethod() bool { + if _, exists := getValidMethods()[r.Method]; exists { + return true + } + + return false +} + +func New(raw *[]byte) Request { + return Request{ + Raw: raw, + Method: extractMethod(raw), + Domain: extractDomain(raw), + } +} + +func (r *Request) ToChunks() { + +} + +func extractDomain(request *[]byte) string { + i := 0 + for ; i < len(*request); i++ { + if (*request)[i] == ' ' { + i++ + break + } + } + + j := i + for ; j < len(*request); j++ { + if (*request)[j] == ' ' { + break + } + } + + 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) +} + +func getValidMethods() map[string]struct{} { + return map[string]struct{}{ + "DELETE": {}, + "GET": {}, + "HEAD": {}, + "POST": {}, + "PUT": {}, + "CONNECT": {}, + "OPTIONS": {}, + "TRACE": {}, + "COPY": {}, + "LOCK": {}, + "MKCOL": {}, + "MOVE": {}, + "PROPFIND": {}, + "PROPPATCH": {}, + "SEARCH": {}, + "UNLOCK": {}, + "BIND": {}, + "REBIND": {}, + "UNBIND": {}, + "ACL": {}, + "REPORT": {}, + "MKACTIVITY": {}, + "CHECKOUT": {}, + "MERGE": {}, + "M-SEARCH": {}, + "NOTIFY": {}, + "SUBSCRIBE": {}, + "UNSUBSCRIBE": {}, + "PATCH": {}, + "PURGE": {}, + "MKCALENDAR": {}, + "LINK": {}, + "UNLINK": {}, + } +}