From 1581f9c4d1887fa2742be68edc24d997e28807fe Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 02:03:46 +0900 Subject: [PATCH 01/10] refactor --- config/config.go | 38 +++++++++++++++++++------------------- proxy/http.go | 24 ++++++++++++------------ util/welcome.go | 15 +++++++-------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/config/config.go b/config/config.go index b1cefbb..f2e3646 100644 --- a/config/config.go +++ b/config/config.go @@ -1,15 +1,15 @@ -package config +package config import ( - "sync" - "runtime" + "runtime" + "sync" ) type Config struct { - Port string - DNS string - OS string - Debug bool + Port string + DNS string + OS string + Debug bool } var config *Config @@ -17,21 +17,21 @@ var once sync.Once var err error func InitConfig(port string, dns string, debug bool) error { - err = nil + err = nil - once.Do(func() { + once.Do(func() { - config = &Config{ - Port : port, - DNS : dns, - OS : runtime.GOOS, - Debug : debug, - } - }) + config = &Config{ + Port: port, + DNS: dns, + OS: runtime.GOOS, + Debug: debug, + } + }) - return err + return err } -func GetConfig() (*Config) { - return config +func GetConfig() *Config { + return config } diff --git a/proxy/http.go b/proxy/http.go index 193265d..3352b86 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -7,20 +7,20 @@ import ( "github.com/xvzc/SpoofDPI/util" ) -func HandleHttp(clientConn net.Conn, ip string, message []byte) { - remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server - if err != nil { - util.Debug(err) - return - } - defer remoteConn.Close() +func HandleHttp(clientConn net.Conn, ip string, message []byte) { + remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server + if err != nil { + util.Debug(err) + return + } + defer remoteConn.Close() - util.Debug("[HTTP] Connected to the server.") + util.Debug("[HTTP] Connected to the server.") - go Serve(remoteConn, clientConn, "HTTP") + go Serve(remoteConn, clientConn, "HTTP") - util.Debug("[HTTP] Sending request to the server") - fmt.Fprintf(remoteConn, string(message)) + util.Debug("[HTTP] Sending request to the server") + fmt.Fprintf(remoteConn, string(message)) - Serve(clientConn, remoteConn, "HTTP") + Serve(clientConn, remoteConn, "HTTP") } diff --git a/util/welcome.go b/util/welcome.go index 32e4bdb..4016d26 100644 --- a/util/welcome.go +++ b/util/welcome.go @@ -5,15 +5,14 @@ import ( "github.com/xvzc/SpoofDPI/config" ) - func PrintWelcome() { - cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) - purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta)) - pterm.DefaultBigText.WithLetters(cyan, purple).Render() + cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) + purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta)) + pterm.DefaultBigText.WithLetters(cyan, purple).Render() - pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ - {Level: 0, Text: "PORT : " + config.GetConfig().Port}, - {Level: 0, Text: "DNS : " + config.GetConfig().DNS}, - }).Render() + pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ + {Level: 0, Text: "PORT : " + config.GetConfig().Port}, + {Level: 0, Text: "DNS : " + config.GetConfig().DNS}, + }).Render() } From 81938445a11099ac216a2ab2d1e6323b58f85a5f Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 02:41:02 +0900 Subject: [PATCH 02/10] 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": {}, + } +} From b615f4aabcf8131e029f8c5f81ad104b3bec8e59 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 03:01:54 +0900 Subject: [PATCH 03/10] add https request --- proxy/proxy.go | 2 +- request/http.go | 22 +++++++++++----------- request/https.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 request/https.go diff --git a/proxy/proxy.go b/proxy/proxy.go index dc0c76f..432bd22 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -38,7 +38,7 @@ func Start() { util.Debug("Client sent data: ", len(b)) - r := request.New(&b) + r := request.NewHttpRequest(&b) if !r.IsValidMethod() { log.Println("Unsupported method: ", r.Method) diff --git a/request/http.go b/request/http.go index a109e1b..283381b 100644 --- a/request/http.go +++ b/request/http.go @@ -4,13 +4,21 @@ import ( "strings" ) -type Request struct { +type HttpRequest struct { Raw *[]byte Method string Domain string } -func (r *Request) IsValidMethod() bool { +func NewHttpRequest(raw *[]byte) HttpRequest { + return HttpRequest{ + Raw: raw, + Method: extractMethod(raw), + Domain: extractDomain(raw), + } +} + +func (r *HttpRequest) IsValidMethod() bool { if _, exists := getValidMethods()[r.Method]; exists { return true } @@ -18,15 +26,7 @@ func (r *Request) IsValidMethod() bool { return false } -func New(raw *[]byte) Request { - return Request{ - Raw: raw, - Method: extractMethod(raw), - Domain: extractDomain(raw), - } -} - -func (r *Request) ToChunks() { +func (r *HttpRequest) ToChunks() { } diff --git a/request/https.go b/request/https.go new file mode 100644 index 0000000..2ff34bc --- /dev/null +++ b/request/https.go @@ -0,0 +1,19 @@ +package request + +type HttpsRequest struct { + Raw *[]byte +} + +func NewHttpsRequest(raw *[]byte) HttpsRequest { + return HttpsRequest{ + Raw: raw, + } +} + +func (r HttpsRequest) SplitInChunks() [][]byte { + if len(*r.Raw) < 1 { + return [][]byte{*r.Raw} + } + + return [][]byte{(*r.Raw)[:1], (*r.Raw)[1:]} +} From 539d25614633e23e9291a5c549ef8be2c05f9812 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 03:34:26 +0900 Subject: [PATCH 04/10] 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{} { From 11ed1fd6812a96701f1c9c3e57d774947cd7d0ff Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 15:35:32 +0900 Subject: [PATCH 05/10] rename request to packet --- {request => packet}/http.go | 6 +++--- {request => packet}/https.go | 2 +- proxy/http.go | 6 +++--- proxy/https.go | 3 ++- proxy/proxy.go | 8 ++++---- 5 files changed, 13 insertions(+), 12 deletions(-) rename {request => packet}/http.go (95%) rename {request => packet}/https.go (94%) diff --git a/request/http.go b/packet/http.go similarity index 95% rename from request/http.go rename to packet/http.go index c2c678d..7757481 100644 --- a/request/http.go +++ b/packet/http.go @@ -1,4 +1,4 @@ -package request +package packet import ( "strings" @@ -29,8 +29,8 @@ func (r *HttpRequest) IsValidMethod() bool { return false } -func (r *HttpRequest) ToChunks() { - +func (r *HttpRequest) IsConnectMethod() bool { + return r.Method == "CONNECT" } func parse(raw *[]byte) (string, string, string) { diff --git a/request/https.go b/packet/https.go similarity index 94% rename from request/https.go rename to packet/https.go index 2ff34bc..9d9af71 100644 --- a/request/https.go +++ b/packet/https.go @@ -1,4 +1,4 @@ -package request +package packet type HttpsRequest struct { Raw *[]byte diff --git a/proxy/http.go b/proxy/http.go index 41d80a7..f9343aa 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -4,11 +4,11 @@ import ( "fmt" "net" - "github.com/xvzc/SpoofDPI/request" + "github.com/xvzc/SpoofDPI/packet" "github.com/xvzc/SpoofDPI/util" ) -func HandleHttp(clientConn net.Conn, ip string, r *request.HttpRequest) { +func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpRequest) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { util.Debug(err) @@ -21,7 +21,7 @@ func HandleHttp(clientConn net.Conn, ip string, r *request.HttpRequest) { go Serve(remoteConn, clientConn, "HTTP") util.Debug("[HTTP] Sending request to the server") - fmt.Fprintf(remoteConn, string(*r.Raw)) + fmt.Fprintf(remoteConn, string(*p.Raw)) Serve(clientConn, remoteConn, "HTTP") } diff --git a/proxy/https.go b/proxy/https.go index 5835f6c..5f38b4a 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -4,10 +4,11 @@ import ( "fmt" "net" + "github.com/xvzc/SpoofDPI/packet" "github.com/xvzc/SpoofDPI/util" ) -func HandleHttps(clientConn net.Conn, ip string) { +func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpRequest) { // Create a connection to the requested server remoteConn, err := net.Dial("tcp", ip+":443") if err != nil { diff --git a/proxy/proxy.go b/proxy/proxy.go index f9b69e5..e921aae 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,7 +6,7 @@ import ( "os" "github.com/xvzc/SpoofDPI/config" - "github.com/xvzc/SpoofDPI/request" + "github.com/xvzc/SpoofDPI/packet" "github.com/xvzc/SpoofDPI/util" ) @@ -38,7 +38,7 @@ func Start() { util.Debug("Client sent data: ", len(b)) - r := request.NewHttpRequest(&b) + r := packet.NewHttpRequest(&b) util.Debug("Request: \n" + string(*r.Raw)) if !r.IsValidMethod() { @@ -55,9 +55,9 @@ func Start() { util.Debug("ip: " + ip) - if r.Method == "CONNECT" { + if r.IsConnectMethod() { util.Debug("HTTPS Requested") - HandleHttps(clientConn, ip) + HandleHttps(clientConn, ip, &r) } else { util.Debug("HTTP Requested.") HandleHttp(clientConn, ip, &r) From 5e1222054c8e31e285b9476ddbb13e047b692509 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sat, 8 Jan 2022 15:38:16 +0900 Subject: [PATCH 06/10] update structure names in module packet --- packet/http.go | 10 +++++----- packet/https.go | 8 ++++---- proxy/http.go | 2 +- proxy/https.go | 2 +- proxy/proxy.go | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packet/http.go b/packet/http.go index 7757481..f1ea4ac 100644 --- a/packet/http.go +++ b/packet/http.go @@ -4,16 +4,16 @@ import ( "strings" ) -type HttpRequest struct { +type Http struct { Raw *[]byte Method string Domain string Version string } -func NewHttpRequest(raw *[]byte) HttpRequest { +func NewHttp(raw *[]byte) Http { method, domain, version := parse(raw) - return HttpRequest{ + return Http{ Raw: raw, Method: method, Domain: domain, @@ -21,7 +21,7 @@ func NewHttpRequest(raw *[]byte) HttpRequest { } } -func (r *HttpRequest) IsValidMethod() bool { +func (r *Http) IsValidMethod() bool { if _, exists := getValidMethods()[r.Method]; exists { return true } @@ -29,7 +29,7 @@ func (r *HttpRequest) IsValidMethod() bool { return false } -func (r *HttpRequest) IsConnectMethod() bool { +func (r *Http) IsConnectMethod() bool { return r.Method == "CONNECT" } diff --git a/packet/https.go b/packet/https.go index 9d9af71..ec5ad01 100644 --- a/packet/https.go +++ b/packet/https.go @@ -1,16 +1,16 @@ package packet -type HttpsRequest struct { +type Https struct { Raw *[]byte } -func NewHttpsRequest(raw *[]byte) HttpsRequest { - return HttpsRequest{ +func NewHttps(raw *[]byte) Https { + return Https{ Raw: raw, } } -func (r HttpsRequest) SplitInChunks() [][]byte { +func (r Https) SplitInChunks() [][]byte { if len(*r.Raw) < 1 { return [][]byte{*r.Raw} } diff --git a/proxy/http.go b/proxy/http.go index f9343aa..c8c584e 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -8,7 +8,7 @@ import ( "github.com/xvzc/SpoofDPI/util" ) -func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpRequest) { +func HandleHttp(clientConn net.Conn, ip string, p *packet.Http) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { util.Debug(err) diff --git a/proxy/https.go b/proxy/https.go index 5f38b4a..3d30d5e 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -8,7 +8,7 @@ import ( "github.com/xvzc/SpoofDPI/util" ) -func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpRequest) { +func HandleHttps(clientConn net.Conn, ip string, r *packet.Http) { // Create a connection to the requested server remoteConn, err := net.Dial("tcp", ip+":443") if err != nil { diff --git a/proxy/proxy.go b/proxy/proxy.go index e921aae..bfc07cd 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -38,7 +38,7 @@ func Start() { util.Debug("Client sent data: ", len(b)) - r := packet.NewHttpRequest(&b) + r := packet.NewHttp(&b) util.Debug("Request: \n" + string(*r.Raw)) if !r.IsValidMethod() { From c376d62ea4470942af93260ea88ac8d069dec14f Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 9 Jan 2022 00:09:01 +0900 Subject: [PATCH 07/10] update packet module --- io/io.go | 1 + packet/http.go | 10 +++--- packet/https.go | 8 ++--- proxy/http.go | 2 +- proxy/https.go | 2 +- proxy/proxy.go | 2 +- util/util.go | 84 ------------------------------------------------- 7 files changed, 13 insertions(+), 96 deletions(-) create mode 100644 io/io.go diff --git a/io/io.go b/io/io.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/io/io.go @@ -0,0 +1 @@ +package main diff --git a/packet/http.go b/packet/http.go index f1ea4ac..e2975e2 100644 --- a/packet/http.go +++ b/packet/http.go @@ -4,16 +4,16 @@ import ( "strings" ) -type Http struct { +type HttpPacket struct { Raw *[]byte Method string Domain string Version string } -func NewHttp(raw *[]byte) Http { +func NewHttpPacket(raw *[]byte) HttpPacket { method, domain, version := parse(raw) - return Http{ + return HttpPacket{ Raw: raw, Method: method, Domain: domain, @@ -21,7 +21,7 @@ func NewHttp(raw *[]byte) Http { } } -func (r *Http) IsValidMethod() bool { +func (r *HttpPacket) IsValidMethod() bool { if _, exists := getValidMethods()[r.Method]; exists { return true } @@ -29,7 +29,7 @@ func (r *Http) IsValidMethod() bool { return false } -func (r *Http) IsConnectMethod() bool { +func (r *HttpPacket) IsConnectMethod() bool { return r.Method == "CONNECT" } diff --git a/packet/https.go b/packet/https.go index ec5ad01..151e250 100644 --- a/packet/https.go +++ b/packet/https.go @@ -1,16 +1,16 @@ package packet -type Https struct { +type HttpsPacket struct { Raw *[]byte } -func NewHttps(raw *[]byte) Https { - return Https{ +func NewHttpsPacket(raw *[]byte) HttpsPacket { + return HttpsPacket{ Raw: raw, } } -func (r Https) SplitInChunks() [][]byte { +func (r HttpsPacket) SplitInChunks() [][]byte { if len(*r.Raw) < 1 { return [][]byte{*r.Raw} } diff --git a/proxy/http.go b/proxy/http.go index c8c584e..835e73e 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -8,7 +8,7 @@ import ( "github.com/xvzc/SpoofDPI/util" ) -func HandleHttp(clientConn net.Conn, ip string, p *packet.Http) { +func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { util.Debug(err) diff --git a/proxy/https.go b/proxy/https.go index 3d30d5e..d9919c0 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -8,7 +8,7 @@ import ( "github.com/xvzc/SpoofDPI/util" ) -func HandleHttps(clientConn net.Conn, ip string, r *packet.Http) { +func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) { // Create a connection to the requested server remoteConn, err := net.Dial("tcp", ip+":443") if err != nil { diff --git a/proxy/proxy.go b/proxy/proxy.go index bfc07cd..08e0c4c 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -38,7 +38,7 @@ func Start() { util.Debug("Client sent data: ", len(b)) - r := packet.NewHttp(&b) + r := packet.NewHttpPacket(&b) util.Debug("Request: \n" + string(*r.Raw)) if !r.IsValidMethod() { diff --git a/util/util.go b/util/util.go index 4c9f04b..dfd4577 100644 --- a/util/util.go +++ b/util/util.go @@ -2,94 +2,10 @@ package util import ( "log" - "strings" "github.com/xvzc/SpoofDPI/config" ) -var validMethod = 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": {}, -} - -func IsValidMethod(name string) bool { - if _, exists := validMethod[name]; exists { - return true - } - - return false -} - -func ExtractDomain(message *[]byte) string { - i := 0 - for ; i < len(*message); i++ { - if (*message)[i] == ' ' { - i++ - break - } - } - - j := i - for ; j < len(*message); j++ { - if (*message)[j] == ' ' { - break - } - } - - domain := string((*message)[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])) - Debug(method) - - return strings.ToUpper(method) -} - func Debug(v ...interface{}) { if config.GetConfig().Debug == false { return From cd3e46355b2dfddffe098f4be08719c916575a75 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 9 Jan 2022 00:48:19 +0900 Subject: [PATCH 08/10] remove config module --- cmd/spoof-dpi/main.go | 24 ++++++++------------ config/config.go | 37 ------------------------------ config/os-proxy.go | 53 ------------------------------------------- proxy/conn.go | 13 +++++------ proxy/http.go | 8 +++---- proxy/https.go | 14 ++++++------ proxy/os-settings.go | 52 ++++++++++++++++++++++++++++++++++++++++++ proxy/proxy.go | 51 ++++++++++++++++++++++++++++++++--------- util/doh.go | 1 - util/util.go | 16 ++++++------- util/welcome.go | 18 --------------- 11 files changed, 126 insertions(+), 161 deletions(-) delete mode 100644 config/config.go delete mode 100644 config/os-proxy.go create mode 100644 proxy/os-settings.go delete mode 100644 util/welcome.go diff --git a/cmd/spoof-dpi/main.go b/cmd/spoof-dpi/main.go index 3ed51b5..f8b0d53 100644 --- a/cmd/spoof-dpi/main.go +++ b/cmd/spoof-dpi/main.go @@ -1,38 +1,32 @@ package main import ( - "flag" + "fmt" "log" "os" "os/signal" + "runtime" "syscall" - "github.com/xvzc/SpoofDPI/config" "github.com/xvzc/SpoofDPI/proxy" "github.com/xvzc/SpoofDPI/util" ) func main() { - port := flag.String("port", "8080", "port") - dns := flag.String("dns", "8.8.8.8", "DNS server") - debug := flag.Bool("debug", false, "true | false") + port, dns, debug := util.ParseArgs() - flag.Parse() + p := proxy.New(port, dns, runtime.GOOS, debug) + fmt.Println(*p) - err := config.InitConfig(*port, *dns, *debug) - if err != nil { - os.Exit(1) - } + p.PrintWelcome() - util.PrintWelcome() - - err = config.SetOsProxy() + err := p.SetOsProxy() if err != nil { log.Fatal(err) os.Exit(1) } - go proxy.Start() + go p.Start() sigs := make(chan os.Signal, 1) done := make(chan bool, 1) @@ -51,7 +45,7 @@ func main() { }() <-done - err = config.UnSetOsProxy() + err = p.UnsetOsProxy() if err != nil { log.Fatal(err) os.Exit(1) diff --git a/config/config.go b/config/config.go deleted file mode 100644 index f2e3646..0000000 --- a/config/config.go +++ /dev/null @@ -1,37 +0,0 @@ -package config - -import ( - "runtime" - "sync" -) - -type Config struct { - Port string - DNS string - OS string - Debug bool -} - -var config *Config -var once sync.Once -var err error - -func InitConfig(port string, dns string, debug bool) error { - err = nil - - once.Do(func() { - - config = &Config{ - Port: port, - DNS: dns, - OS: runtime.GOOS, - Debug: debug, - } - }) - - return err -} - -func GetConfig() *Config { - return config -} diff --git a/config/os-proxy.go b/config/os-proxy.go deleted file mode 100644 index bfb827e..0000000 --- a/config/os-proxy.go +++ /dev/null @@ -1,53 +0,0 @@ -package config - -import ( - "os/exec" - "strings" -) - - -func SetOsProxy() error { - if GetConfig().OS != "darwin" { - return nil - } - - network, err:= exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f2").Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setwebproxy " + strings.TrimSpace(string(network)) + " 127.0.0.1 " + GetConfig().Port).Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy " + strings.TrimSpace(string(network)) + " 127.0.0.1 " + GetConfig().Port).Output() - if err != nil { - return err - } - - return nil -} - -func UnSetOsProxy() (error) { - if GetConfig().OS != "darwin" { - return nil - } - - network, err:= exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f2").Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate " + strings.TrimSpace(string(network)) + " off").Output() - if err != nil { - return err - } - - _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate " + strings.TrimSpace(string(network)) + " off").Output() - if err != nil { - return err - } - - return nil -} diff --git a/proxy/conn.go b/proxy/conn.go index 0966ea7..b5f5d9e 100644 --- a/proxy/conn.go +++ b/proxy/conn.go @@ -2,8 +2,7 @@ package proxy import ( "net" - - "github.com/xvzc/SpoofDPI/util" + // "github.com/xvzc/SpoofDPI/util" ) const BUF_SIZE = 1024 @@ -31,17 +30,17 @@ func Serve(from net.Conn, to net.Conn, proto string) { for { buf, err := ReadBytes(from) if err != nil { - util.Debug("["+proto+"]"+"Error reading from ", from.RemoteAddr()) - util.Debug(err, " Closing the connection.. ") + // util.Debug("["+proto+"]"+"Error reading from ", from.RemoteAddr()) + // util.Debug(err, " Closing the connection.. ") break } - util.Debug(from.RemoteAddr(), "sent data", len(buf)) + // util.Debug(from.RemoteAddr(), "sent data", len(buf)) _, write_err := to.Write(buf) if write_err != nil { - util.Debug("["+proto+"]"+"Error reading from ", to.RemoteAddr()) - util.Debug(err, " Closing the connection.. ") + // util.Debug("["+proto+"]"+"Error reading from ", to.RemoteAddr()) + // util.Debug(err, " Closing the connection.. ") break } } diff --git a/proxy/http.go b/proxy/http.go index 835e73e..5a8544a 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -5,22 +5,22 @@ import ( "net" "github.com/xvzc/SpoofDPI/packet" - "github.com/xvzc/SpoofDPI/util" + // "github.com/xvzc/SpoofDPI/util" ) func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { - util.Debug(err) + // util.Debug(err) return } defer remoteConn.Close() - util.Debug("[HTTP] Connected to the server.") + // util.Debug("[HTTP] Connected to the server.") go Serve(remoteConn, clientConn, "HTTP") - util.Debug("[HTTP] Sending request to the server") + // util.Debug("[HTTP] Sending request to the server") fmt.Fprintf(remoteConn, string(*p.Raw)) Serve(clientConn, remoteConn, "HTTP") diff --git a/proxy/https.go b/proxy/https.go index d9919c0..038a48a 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -12,25 +12,25 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) { // Create a connection to the requested server remoteConn, err := net.Dial("tcp", ip+":443") if err != nil { - util.Debug(err) + // util.Debug(err) return } defer remoteConn.Close() - util.Debug("[HTTPS] Connected to the server.") + // util.Debug("[HTTPS] Connected to the server.") // Send self generated response for connect request fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection Established\r\n\r\n") - util.Debug("[HTTPS] Sent 200 Connection Estabalished") + // util.Debug("[HTTPS] Sent 200 Connection Estabalished") // Read client hello clientHello, err := ReadBytes(clientConn) if err != nil { - util.Debug("[HTTPS] Error reading client hello: ", err) - util.Debug("Closing connection ", clientConn.RemoteAddr()) + // util.Debug("[HTTPS] Error reading client hello: ", err) + // util.Debug("Closing connection ", clientConn.RemoteAddr()) } - util.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello", len(clientHello)) + // util.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello", len(clientHello)) // Generate a go routine that reads from the server go Serve(remoteConn, clientConn, "HTTPS") @@ -40,7 +40,7 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) { for i := 0; i < len(chunks); i++ { _, write_err := remoteConn.Write(chunks[i]) if write_err != nil { - util.Debug("[HTTPS] Error writing to the client:", write_err) + // util.Debug("[HTTPS] Error writing to the client:", write_err) break } } diff --git a/proxy/os-settings.go b/proxy/os-settings.go new file mode 100644 index 0000000..e08d695 --- /dev/null +++ b/proxy/os-settings.go @@ -0,0 +1,52 @@ +package proxy + +import ( + "os/exec" + "strings" +) + +func (p *Proxy) SetOsProxy() error { + if p.OS != "darwin" { + return nil + } + + network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f2").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+strings.TrimSpace(string(network))+" 127.0.0.1 "+p.Port).Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+strings.TrimSpace(string(network))+" 127.0.0.1 "+p.Port).Output() + if err != nil { + return err + } + + return nil +} + +func (p *Proxy) UnsetOsProxy() error { + if p.OS != "darwin" { + return nil + } + + network, err := exec.Command("sh", "-c", "networksetup -listnetworkserviceorder | grep `route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2` -B 1 | head -n 1 | cut -d ' ' -f2").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+strings.TrimSpace(string(network))+" off").Output() + if err != nil { + return err + } + + _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+strings.TrimSpace(string(network))+" off").Output() + if err != nil { + return err + } + + return nil +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 08e0c4c..9d27533 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,23 +1,52 @@ package proxy import ( + "fmt" "log" "net" "os" - "github.com/xvzc/SpoofDPI/config" + "github.com/pterm/pterm" "github.com/xvzc/SpoofDPI/packet" "github.com/xvzc/SpoofDPI/util" ) -func Start() { - listener, err := net.Listen("tcp", ":"+config.GetConfig().Port) +type Proxy struct { + Port string + DNS string + OS string + Debug bool +} + +func New(port string, dns string, os string, debug bool) *Proxy { + return &Proxy{ + Port: port, + DNS: dns, + OS: os, + Debug: debug, + } +} + +func (p *Proxy) PrintWelcome() { + cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) + purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta)) + pterm.DefaultBigText.WithLetters(cyan, purple).Render() + + pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ + {Level: 0, Text: "PORT : " + p.Port}, + {Level: 0, Text: "DNS : " + p.DNS}, + {Level: 0, Text: "DEBUG : " + fmt.Sprint(p.Debug)}, + }).Render() +} + +func (p *Proxy) Start() { + listener, err := net.Listen("tcp", ":"+p.Port) if err != nil { log.Fatal("Error creating listener: ", err) os.Exit(1) } - util.Debug("Created a listener") + // util.Debug("Created a listener") for { clientConn, err := listener.Accept() @@ -26,7 +55,7 @@ func Start() { continue } - util.Debug("Accepted a new connection.", clientConn.RemoteAddr()) + // util.Debug("Accepted a new connection.", clientConn.RemoteAddr()) go func() { defer clientConn.Close() @@ -36,10 +65,10 @@ func Start() { return } - util.Debug("Client sent data: ", len(b)) + // util.Debug("Client sent data: ", len(b)) r := packet.NewHttpPacket(&b) - util.Debug("Request: \n" + string(*r.Raw)) + // util.Debug("Request: \n" + string(*r.Raw)) if !r.IsValidMethod() { log.Println("Unsupported method: ", r.Method) @@ -47,19 +76,19 @@ func Start() { } // Dns lookup over https - ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, r.Domain) + ip, err := util.DnsLookupOverHttps(p.DNS, r.Domain) if err != nil { log.Println("Error looking up dns: "+r.Domain, err) return } - util.Debug("ip: " + ip) + // util.Debug("ip: " + ip) if r.IsConnectMethod() { - util.Debug("HTTPS Requested") + // util.Debug("HTTPS Requested") HandleHttps(clientConn, ip, &r) } else { - util.Debug("HTTP Requested.") + // util.Debug("HTTP Requested.") HandleHttp(clientConn, ip, &r) } }() diff --git a/util/doh.go b/util/doh.go index d0252ec..2a7ebcb 100644 --- a/util/doh.go +++ b/util/doh.go @@ -11,7 +11,6 @@ func DnsLookupOverHttps(dns string, domain string) (string, error) { Class: doh.IN, } - Debug(domain) a, _, err := resolver.LookupA(domain) if err != nil { return "", err diff --git a/util/util.go b/util/util.go index dfd4577..a0452d2 100644 --- a/util/util.go +++ b/util/util.go @@ -1,17 +1,17 @@ package util import ( - "log" - - "github.com/xvzc/SpoofDPI/config" + "flag" ) -func Debug(v ...interface{}) { - if config.GetConfig().Debug == false { - return - } +func ParseArgs() (string, string, bool) { + port := flag.String("port", "8080", "port") + dns := flag.String("dns", "8.8.8.8", "DNS server") + debug := flag.Bool("debug", false, "true | false") - log.Println(v...) + flag.Parse() + + return *port, *dns, *debug } func BytesToChunks(buf []byte) [][]byte { diff --git a/util/welcome.go b/util/welcome.go deleted file mode 100644 index 4016d26..0000000 --- a/util/welcome.go +++ /dev/null @@ -1,18 +0,0 @@ -package util - -import ( - "github.com/pterm/pterm" - "github.com/xvzc/SpoofDPI/config" -) - -func PrintWelcome() { - cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) - purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta)) - pterm.DefaultBigText.WithLetters(cyan, purple).Render() - - pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ - {Level: 0, Text: "PORT : " + config.GetConfig().Port}, - {Level: 0, Text: "DNS : " + config.GetConfig().DNS}, - }).Render() - -} From 1a55f159d1395c480208eeefdaf838a90975dcc3 Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 9 Jan 2022 01:02:30 +0900 Subject: [PATCH 09/10] move doh.go into proxy module --- proxy/doh.go | 14 ++++++++++++++ proxy/proxy.go | 10 +++++----- util/doh.go | 22 ---------------------- 3 files changed, 19 insertions(+), 27 deletions(-) create mode 100644 proxy/doh.go delete mode 100644 util/doh.go diff --git a/proxy/doh.go b/proxy/doh.go new file mode 100644 index 0000000..ef7a105 --- /dev/null +++ b/proxy/doh.go @@ -0,0 +1,14 @@ +package proxy + +func (p *Proxy) DnsLookupOverHttps(domain string) (string, error) { + // Perform a A lookup on example.com + + a, _, err := p.DNS.LookupA(domain) + if err != nil { + return "", err + } + + ip := a[0].IP4 + + return ip, nil +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 9d27533..8d41af1 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,14 +6,14 @@ import ( "net" "os" + "github.com/babolivier/go-doh-client" "github.com/pterm/pterm" "github.com/xvzc/SpoofDPI/packet" - "github.com/xvzc/SpoofDPI/util" ) type Proxy struct { Port string - DNS string + DNS doh.Resolver OS string Debug bool } @@ -21,7 +21,7 @@ type Proxy struct { func New(port string, dns string, os string, debug bool) *Proxy { return &Proxy{ Port: port, - DNS: dns, + DNS: doh.Resolver{Host: dns, Class: doh.IN}, OS: os, Debug: debug, } @@ -34,7 +34,7 @@ func (p *Proxy) PrintWelcome() { pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{ {Level: 0, Text: "PORT : " + p.Port}, - {Level: 0, Text: "DNS : " + p.DNS}, + {Level: 0, Text: "DNS : " + p.DNS.Host}, {Level: 0, Text: "DEBUG : " + fmt.Sprint(p.Debug)}, }).Render() } @@ -76,7 +76,7 @@ func (p *Proxy) Start() { } // Dns lookup over https - ip, err := util.DnsLookupOverHttps(p.DNS, r.Domain) + ip, err := p.DnsLookupOverHttps(r.Domain) if err != nil { log.Println("Error looking up dns: "+r.Domain, err) return diff --git a/util/doh.go b/util/doh.go deleted file mode 100644 index 2a7ebcb..0000000 --- a/util/doh.go +++ /dev/null @@ -1,22 +0,0 @@ -package util - -import ( - "github.com/babolivier/go-doh-client" -) - -func DnsLookupOverHttps(dns string, domain string) (string, error) { - // Perform a A lookup on example.com - resolver := doh.Resolver{ - Host: dns, // Change this with your favourite DoH-compliant resolver. - Class: doh.IN, - } - - a, _, err := resolver.LookupA(domain) - if err != nil { - return "", err - } - - ip := a[0].IP4 - - return ip, nil -} From 3739c8b712f0cc54f04cc780c8c057457347747b Mon Sep 17 00:00:00 2001 From: xvzc Date: Sun, 9 Jan 2022 02:40:05 +0900 Subject: [PATCH 10/10] refactor main.go --- cmd/spoof-dpi/main.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/cmd/spoof-dpi/main.go b/cmd/spoof-dpi/main.go index f8b0d53..9026499 100644 --- a/cmd/spoof-dpi/main.go +++ b/cmd/spoof-dpi/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "os/signal" @@ -16,18 +15,15 @@ func main() { port, dns, debug := util.ParseArgs() p := proxy.New(port, dns, runtime.GOOS, debug) - fmt.Println(*p) - p.PrintWelcome() - err := p.SetOsProxy() - if err != nil { + if err := p.SetOsProxy(); err != nil { log.Fatal(err) - os.Exit(1) } go p.Start() + // Handle signals sigs := make(chan os.Signal, 1) done := make(chan bool, 1) @@ -45,9 +41,7 @@ func main() { }() <-done - err = p.UnsetOsProxy() - if err != nil { + if err := p.UnsetOsProxy(); err != nil { log.Fatal(err) - os.Exit(1) } }