From 452a7e343fb17a01709d259f6fe8697500efb2e3 Mon Sep 17 00:00:00 2001 From: xvzc Date: Tue, 11 Jan 2022 02:11:30 +0900 Subject: [PATCH] add net module and fixed the other modules --- {proxy => net}/conn.go | 39 +++++++++++++++++++++++++++++++++----- net/dial.go | 23 ++++++++++++++++++++++ net/listener.go | 18 ++++++++++++++++++ net/net.go | 1 + proxy/http.go | 26 +------------------------ proxy/https.go | 43 ++++++++++++++++++++++++++++-------------- proxy/proxy.go | 4 ++-- util/util.go | 8 -------- 8 files changed, 108 insertions(+), 54 deletions(-) rename {proxy => net}/conn.go (52%) create mode 100644 net/dial.go create mode 100644 net/listener.go create mode 100644 net/net.go diff --git a/proxy/conn.go b/net/conn.go similarity index 52% rename from proxy/conn.go rename to net/conn.go index b5f5d9e..b14fad5 100644 --- a/proxy/conn.go +++ b/net/conn.go @@ -1,13 +1,42 @@ -package proxy +package net import ( "net" - // "github.com/xvzc/SpoofDPI/util" ) const BUF_SIZE = 1024 -func ReadBytes(conn net.Conn) ([]byte, error) { +type Conn struct { + Conn net.Conn +} + +func (conn *Conn) Close() { + conn.Conn.Close() +} + +func (conn *Conn) Read(b []byte) (n int, err error) { + return conn.Conn.Read(b) +} + +func (conn *Conn) Write(b []byte) (n int, err error) { + return conn.Conn.Write(b) +} + +func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { + total := 0 + for i := 0; i < len(c); i++ { + b, err := conn.Write(c[i]) + if err != nil { + return 0, nil + } + + b += total + } + + return total, nil +} + +func (conn *Conn) ReadBytes() ([]byte, error) { ret := make([]byte, 0) buf := make([]byte, BUF_SIZE) @@ -26,9 +55,9 @@ func ReadBytes(conn net.Conn) ([]byte, error) { return ret, nil } -func Serve(from net.Conn, to net.Conn, proto string) { +func (from *Conn) Serve(to Conn, proto string) { for { - buf, err := ReadBytes(from) + buf, err := from.ReadBytes() if err != nil { // util.Debug("["+proto+"]"+"Error reading from ", from.RemoteAddr()) // util.Debug(err, " Closing the connection.. ") diff --git a/net/dial.go b/net/dial.go new file mode 100644 index 0000000..6710397 --- /dev/null +++ b/net/dial.go @@ -0,0 +1,23 @@ +package net + +import ( + "net" +) + +func Listen(network, address string) (Listener, error) { + l, err := net.Listen(network, address) + if err != nil { + return Listener{}, err + } + + return Listener{Listener: l}, nil +} + +func Dial(network, address string) (Conn, error) { + conn, err := net.Dial(network, address) + if err != nil { + return Conn{}, err + } + + return Conn{Conn: conn}, nil +} diff --git a/net/listener.go b/net/listener.go new file mode 100644 index 0000000..a509fbf --- /dev/null +++ b/net/listener.go @@ -0,0 +1,18 @@ +package net + +import ( + "net" +) + +type Listener struct { + Listener net.Listener +} + +func (l *Listener) Accept() (Conn, error) { + conn, err := l.Listener.Accept() + if err != nil { + return Conn{}, err + } + + return Conn{Conn: conn}, nil +} diff --git a/net/net.go b/net/net.go new file mode 100644 index 0000000..9d9f1a1 --- /dev/null +++ b/net/net.go @@ -0,0 +1 @@ +package net diff --git a/proxy/http.go b/proxy/http.go index 5a8544a..3494d36 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -1,27 +1,3 @@ package proxy -import ( - "fmt" - "net" - - "github.com/xvzc/SpoofDPI/packet" - // "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) - return - } - defer remoteConn.Close() - - // util.Debug("[HTTP] Connected to the server.") - - go Serve(remoteConn, clientConn, "HTTP") - - // util.Debug("[HTTP] Sending request to the server") - fmt.Fprintf(remoteConn, string(*p.Raw)) - - Serve(clientConn, remoteConn, "HTTP") -} +// "github.com/xvzc/SpoofDPI/util" diff --git a/proxy/https.go b/proxy/https.go index 038a48a..90be871 100644 --- a/proxy/https.go +++ b/proxy/https.go @@ -2,12 +2,29 @@ package proxy import ( "fmt" - "net" + "github.com/xvzc/SpoofDPI/net" "github.com/xvzc/SpoofDPI/packet" - "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) + return + } + defer remoteConn.Close() + + // util.Debug("[HTTP] Connected to the server.") + + go remoteConn.Serve(clientConn, "HTTP") + + // util.Debug("[HTTP] Sending request to the server") + fmt.Fprintf(remoteConn.Conn, string(*p.Raw)) + + go clientConn.Serve(remoteConn, "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") @@ -20,11 +37,11 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) { // 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") + fmt.Fprintf(clientConn.Conn, "HTTP/1.1 200 Connection Established\r\n\r\n") // util.Debug("[HTTPS] Sent 200 Connection Estabalished") // Read client hello - clientHello, err := ReadBytes(clientConn) + clientHello, err := clientConn.ReadBytes() if err != nil { // util.Debug("[HTTPS] Error reading client hello: ", err) // util.Debug("Closing connection ", clientConn.RemoteAddr()) @@ -33,18 +50,16 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) { // util.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello", len(clientHello)) // Generate a go routine that reads from the server - go Serve(remoteConn, clientConn, "HTTPS") + go remoteConn.Serve(clientConn, "HTTPS") - // Send chunked request - chunks := util.BytesToChunks(clientHello) - 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) - break - } + pkt := packet.NewHttpsPacket(&clientHello) + + chunks := pkt.SplitInChunks() + + if _, err := remoteConn.WriteChunks(chunks); err != nil { + return } // Read from the client - Serve(clientConn, remoteConn, "HTTPS") + clientConn.Serve(remoteConn, "HTTPS") } diff --git a/proxy/proxy.go b/proxy/proxy.go index 5055bfd..3c89ab7 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -2,10 +2,10 @@ package proxy import ( "log" - "net" "os" "github.com/xvzc/SpoofDPI/doh" + "github.com/xvzc/SpoofDPI/net" "github.com/xvzc/SpoofDPI/packet" ) @@ -40,7 +40,7 @@ func (p *Proxy) Start() { go func() { defer clientConn.Close() - b, err := ReadBytes(clientConn) + b, err := clientConn.ReadBytes() if err != nil { return } diff --git a/util/util.go b/util/util.go index 80c7ce1..e2338c1 100644 --- a/util/util.go +++ b/util/util.go @@ -17,14 +17,6 @@ func ParseArgs() (string, string, bool) { return *port, *dns, *debug } -func BytesToChunks(buf []byte) [][]byte { - if len(buf) < 1 { - return [][]byte{buf} - } - - return [][]byte{buf[:1], buf[1:]} -} - func PrintWelcome(port string, dns string, debug bool) { cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta))