mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 14:26:31 +00:00
update refactor http request
This commit is contained in:
parent
b615f4aabc
commit
539d256146
@ -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")
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
@ -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{} {
|
||||
|
Loading…
Reference in New Issue
Block a user