From 95c4199025db7f498104724e9ed753364c57262d Mon Sep 17 00:00:00 2001 From: xvzc Date: Mon, 3 Jan 2022 16:24:39 +0900 Subject: [PATCH] update --- cmd/spoof-dpi/main.go | 3 ++- {proxy => config}/config.go | 4 ++-- proxy/http.go | 11 +++++------ proxy/https.go | 30 +++++++++++++++--------------- proxy/proxy.go | 16 +++++++--------- util/util.go | 27 ++++++++++++++++++++------- 6 files changed, 51 insertions(+), 40 deletions(-) rename {proxy => config}/config.go (96%) diff --git a/cmd/spoof-dpi/main.go b/cmd/spoof-dpi/main.go index 1c92e9f..5fd39e3 100644 --- a/cmd/spoof-dpi/main.go +++ b/cmd/spoof-dpi/main.go @@ -5,6 +5,7 @@ import ( "os" "github.com/xvzc/SpoofDPI/proxy" + "github.com/xvzc/SpoofDPI/config" ) func main() { @@ -13,7 +14,7 @@ func main() { debug := flag.Bool("debug", false, "true | false") mtu := flag.Int("mtu", 100, "int") - err := proxy.InitConfig(*src, *dns, *mtu, *debug) + err := config.InitConfig(*src, *dns, *mtu, *debug) if err != nil { os.Exit(1) } diff --git a/proxy/config.go b/config/config.go similarity index 96% rename from proxy/config.go rename to config/config.go index 2eeed57..73f233d 100644 --- a/proxy/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package proxy +package config import ( "errors" @@ -64,6 +64,6 @@ func InitConfig(srcAddress string, dns string, mtu int, debug bool) error { return err } -func getConfig() (*Config) { +func GetConfig() (*Config) { return config } diff --git a/proxy/http.go b/proxy/http.go index 24ada97..bcf0d2e 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -2,7 +2,6 @@ package proxy import ( "fmt" - "log" "net" "github.com/xvzc/SpoofDPI/util" @@ -11,33 +10,33 @@ import ( func HandleHttp(clientConn net.Conn, ip string, message []byte) { remoteConn, err := net.Dial("tcp", ip+":80") // create connection to server if err != nil { - log.Fatal(err) + util.Debug(err) return } defer remoteConn.Close() _, write_err := remoteConn.Write(message) if write_err != nil { - log.Fatal("failed:", write_err) + util.Debug("failed:", write_err) return } defer remoteConn.(*net.TCPConn).CloseWrite() buf, err := util.ReadMessage(remoteConn) if err != nil { - log.Fatal("failed:", err) + util.Debug("failed:", err) return } fmt.Println() - log.Println() + util.Debug() fmt.Println("##### Response from the server: ") fmt.Println(string(buf)) // Write to client _, write_err = clientConn.Write(buf) if write_err != nil { - log.Fatal("failed:", write_err) + util.Debug("failed:", write_err) return } defer clientConn.(*net.TCPConn).CloseWrite() diff --git a/proxy/https.go b/proxy/https.go index 527bfde..a2be5c7 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -2,7 +2,6 @@ package proxy import ( "fmt" - "log" "net" "io" @@ -14,16 +13,14 @@ import ( func HandleHttps(clientConn net.Conn, ip string) { remoteConn, err := net.Dial("tcp", ip+":443") // create connection to server if err != nil { - log.Fatal(err) + util.Debug(err) return } defer remoteConn.Close() - log.Println("Connected to the server.") + util.Debug("Connected to the server.") - // established := []byte("HTTP/1.1 204 No Content\n\n") - - log.Println("Sending 200 Connection Estabalished") + util.Debug("Sending 200 Connection Estabalished") fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection Established\r\n\r\n") @@ -33,18 +30,20 @@ func HandleHttps(clientConn net.Conn, ip string) { buf, err := util.ReadMessage(remoteConn) if err != nil { if err != io.EOF { - log.Println("Error reading from the server:", err) + util.Debug("Error reading from the server:", err) } else { - log.Println("Remote connection Closed: ", err) + util.Debug("Remote connection Closed: ", err) } + + util.Debug("Closing connection: ", remoteConn.RemoteAddr()) return } - log.Println("Server Sent Data", len(buf)) + util.Debug(remoteConn.RemoteAddr(), "Server Sent Data", len(buf)) _, write_err := clientConn.Write(buf) if write_err != nil { - log.Println("Error writing to client:", write_err) + util.Debug("Error writing to client:", write_err) return } } @@ -55,19 +54,20 @@ func HandleHttps(clientConn net.Conn, ip string) { buf, err := util.ReadMessage(clientConn) if err != nil { if err != io.EOF { - log.Println("Error reading from the client:", err) + util.Debug("Error reading from the client:", err) } else { - log.Println("Client connection Closed: ", err) + util.Debug("Client connection Closed: ", err) } + + util.Debug("Closing connection: ", clientConn.RemoteAddr()) break } - log.Println("Client Sent Data", len(buf)) + util.Debug(clientConn.RemoteAddr(), "Client Sent Data", len(buf)) _, write_err := remoteConn.Write(buf) if write_err != nil { - log.Println("Error writing to client:", write_err) + util.Debug("Error writing to client:", write_err) break } } - } diff --git a/proxy/proxy.go b/proxy/proxy.go index 71d136a..fd8a5cc 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -2,14 +2,15 @@ package proxy import ( "log" - "fmt" "net" "os" - "github.com/xvzc/SpoofDPI/util" + + "github.com/xvzc/SpoofDPI/util" + "github.com/xvzc/SpoofDPI/config" ) func Start() { - listener, err := net.Listen("tcp", ":" + config.SrcPort) + listener, err := net.Listen("tcp", ":" + config.GetConfig().SrcPort) if err != nil { log.Fatal("Error creating listener: ", err) os.Exit(1) @@ -34,14 +35,11 @@ func Start() { return } - fmt.Println() - log.Println() - fmt.Println("##### Request from client : ") - fmt.Println(string(message)) + log.Println("Client sent data: ", len(message)) domain := util.ExtractDomain(&message) - ip, err := util.DnsLookupOverHttps(getConfig().DNS, domain) // Dns lookup over https + ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, domain) // Dns lookup over https if err != nil { return } @@ -49,7 +47,7 @@ func Start() { log.Println("ip: "+ ip) if util.ExtractMethod(&message) == "CONNECT" { - log.Println("HTTPS Requested.") + util.Debug("HTTPS Requested") HandleHttps(clientConn, ip) }else { log.Println("HTTP Requested.") diff --git a/util/util.go b/util/util.go index 15b26c5..d79a735 100644 --- a/util/util.go +++ b/util/util.go @@ -1,12 +1,16 @@ package util import ( - "net" - "log" - "strings" - "github.com/babolivier/go-doh-client" + "log" + "net" + "strings" + + "github.com/babolivier/go-doh-client" + "github.com/xvzc/SpoofDPI/config" ) +const BUF_SIZE = 1024 + func WriteAndRead(conn net.Conn, message []byte) ([]byte, error){ _, err := conn.Write(message) if err != nil { @@ -25,8 +29,9 @@ func WriteAndRead(conn net.Conn, message []byte) ([]byte, error){ } func ReadMessage(conn net.Conn)([]byte, error) { - buf := make([]byte, 0, 4096) // big buffer - tmp := make([]byte, 1024) // using small tmo buffer for demonstrating + buf := make([]byte, 0) // big buffer + tmp := make([]byte, BUF_SIZE) // using small tmo buffer for demonstrating + for { n, err := conn.Read(tmp) if err != nil { @@ -34,7 +39,7 @@ func ReadMessage(conn net.Conn)([]byte, error) { } buf = append(buf, tmp[:n]...) - if n < 1024 { + if n < BUF_SIZE { break } } @@ -102,3 +107,11 @@ func ExtractMethod(message *[]byte) (string) { return strings.ToUpper(method) } + +func Debug(v ...interface{}) { + if config.GetConfig().Debug == false { + return + } + + log.Println(v...) +}