add request module

This commit is contained in:
xvzc 2022-01-08 02:41:02 +09:00
parent 1581f9c4d1
commit 81938445a1
2 changed files with 118 additions and 15 deletions

View File

@ -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)
}
}()
}

107
request/http.go Normal file
View File

@ -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": {},
}
}