diff --git a/handler/handler.go b/handler/handler.go index a3c18f9..e69de29 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -1,74 +0,0 @@ -package handler - -import ( - "fmt" - "io/ioutil" - "log" - "net" - "SpoofDPI/util" - - - "github.com/babolivier/go-doh-client" -) - -var resolver = doh.Resolver{ - Host: "8.8.8.8", - Class: doh.IN, -} - -func HandleClientRequest(clientConn net.Conn) { - defer clientConn.Close() - - buf, err := util.ReadBytes(clientConn) - if err != nil { - return - } - - fmt.Println("\n##### Request from client : ") - fmt.Println(string(buf)) - - domain, port := util.ExtractDomainAndPort(string(buf)) - - log.Println("domain: "+ domain) - log.Println("port: " + port) - - ip, err := util.DnsLookupOverHttps(domain) // Dns lookup over https - if err != nil { - log.Fatal(err) - return - } - - remoteConn, err := net.Dial("tcp", ip+":"+port) // create connection to server - if err != nil { - fmt.Println(err) - return - } - defer remoteConn.Close() - - DoMitm(clientConn, remoteConn, buf) -} - -func DoMitm(clientConn net.Conn, remoteConn net.Conn, data []byte) { - _, write_err := remoteConn.Write(data) - if write_err != nil { - fmt.Println("failed:", write_err) - return - } - defer remoteConn.(*net.TCPConn).CloseWrite() - - buf, read_err := ioutil.ReadAll(remoteConn) - if read_err != nil { - fmt.Println("failed:", read_err) - return - } - - log.Println("\n##### Response from server: ") - log.Println(string(buf)) - - _, write_err = clientConn.Write(buf) - if write_err != nil { - fmt.Println("failed:", write_err) - return - } - defer clientConn.(*net.TCPConn).CloseWrite() -} diff --git a/mitm/mitm.go b/mitm/mitm.go new file mode 100644 index 0000000..7377386 --- /dev/null +++ b/mitm/mitm.go @@ -0,0 +1,38 @@ +package mitm + +import ( + "net" + "log" + "io/ioutil" + "fmt" +) + +func GoGoSing(clientConn net.Conn, remoteConn net.Conn, data []byte) { + _, write_err := remoteConn.Write(data) + if write_err != nil { + log.Fatal("failed:", write_err) + return + } + defer remoteConn.(*net.TCPConn).CloseWrite() + + // Read from the server + buf, read_err := ioutil.ReadAll(remoteConn) + if read_err != nil { + log.Fatal("failed:", read_err) + return + } + + fmt.Println() + log.Println() + 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) + return + } + defer clientConn.(*net.TCPConn).CloseWrite() +} diff --git a/spoof-dpi.go b/spoof-dpi.go index 7f2002e..461a2ca 100644 --- a/spoof-dpi.go +++ b/spoof-dpi.go @@ -1,28 +1,72 @@ package main import ( - "net" - "log" - "SpoofDPI/handler" + "SpoofDPI/mitm" + "SpoofDPI/util" + "fmt" + "log" + "net" ) +const ( + CLI_PORT = "8080" + DNS_ADDR = "1.1.1.1" +) + +type config struct { + port string + age int +} + func main() { log.Println("##### Listening 8080..") - listener, err := net.Listen("tcp", ":8080") + listener, err := net.Listen("tcp", ":" + CLI_PORT) if err != nil { panic(err) } + for { - connClient, err := listener.Accept() + clientConn, err := listener.Accept() if err != nil { log.Println("error accepting connection", err) continue } - log.Println("##### New connection", connClient.RemoteAddr()) + log.Println("##### New connection", clientConn.RemoteAddr()) - go handler.HandleClientRequest(connClient) + go func() { + defer clientConn.Close() + + buf, err := util.ReadBytes(clientConn) + if err != nil { + return + } + + fmt.Println() + log.Println() + fmt.Println("##### Request from client : ") + fmt.Println(string(buf)) + + domain, port := util.ExtractDomainAndPort(string(buf)) + + log.Println("domain: "+ domain) + log.Println("port: " + port) + + ip, err := util.DnsLookupOverHttps(DNS_ADDR, domain) // Dns lookup over https + if err != nil { + log.Fatal(err) + return + } + + remoteConn, err := net.Dial("tcp", ip+":"+port) // create connection to server + if err != nil { + log.Fatal(err) + return + } + defer remoteConn.Close() + + mitm.GoGoSing(clientConn, remoteConn, buf) + }() } } - diff --git a/util/util.go b/util/util.go index a185a9b..10b4c2b 100644 --- a/util/util.go +++ b/util/util.go @@ -41,10 +41,10 @@ func ReadBytes(conn net.Conn)([]byte, error) { return buf, nil } -func DnsLookupOverHttps(domain string)(string, error) { +func DnsLookupOverHttps(addr string, domain string)(string, error) { // Perform a A lookup on example.com resolver := doh.Resolver{ - Host: "8.8.8.8", // Change this with your favourite DoH-compliant resolver. + Host: addr, // Change this with your favourite DoH-compliant resolver. Class: doh.IN, }