diff --git a/proxy/conn.go b/proxy/conn.go new file mode 100644 index 0000000..0966ea7 --- /dev/null +++ b/proxy/conn.go @@ -0,0 +1,48 @@ +package proxy + +import ( + "net" + + "github.com/xvzc/SpoofDPI/util" +) + +const BUF_SIZE = 1024 + +func ReadBytes(conn net.Conn) ([]byte, error) { + ret := make([]byte, 0) + buf := make([]byte, BUF_SIZE) + + for { + n, err := conn.Read(buf) + if err != nil { + return nil, err + } + ret = append(ret, buf[:n]...) + + if n < BUF_SIZE { + break + } + } + + return ret, nil +} + +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.. ") + break + } + + 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.. ") + break + } + } +} diff --git a/proxy/manager.go b/proxy/manager.go deleted file mode 100644 index b933f06..0000000 --- a/proxy/manager.go +++ /dev/null @@ -1,49 +0,0 @@ -package proxy - -import( - "net" - "github.com/xvzc/SpoofDPI/util" -) - -const BUF_SIZE = 1024 - -func ReadBytes(conn net.Conn)([]byte, error) { - 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 { - return nil, err - } - buf = append(buf, tmp[:n]...) - - if n < BUF_SIZE { - break - } - } - - return buf, nil -} - - -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.. ") - break - } - - 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.. ") - break - } - } -} - diff --git a/proxy/proxy.go b/proxy/proxy.go index c88ac57..65a0283 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -5,54 +5,54 @@ import ( "net" "os" - "github.com/xvzc/SpoofDPI/util" "github.com/xvzc/SpoofDPI/config" + "github.com/xvzc/SpoofDPI/util" ) func Start() { - listener, err := net.Listen("tcp", ":" + config.GetConfig().Port) + listener, err := net.Listen("tcp", ":"+config.GetConfig().Port) if err != nil { - log.Fatal("Error creating listener: ", err) - os.Exit(1) + log.Fatal("Error creating listener: ", err) + os.Exit(1) } - util.Debug("Created a listener") + util.Debug("Created a listener") - for { + for { clientConn, err := listener.Accept() if err != nil { - log.Fatal("Error accepting connection: ", err) + log.Fatal("Error accepting connection: ", err) continue } - util.Debug("Accepted a new connection.", clientConn.RemoteAddr()) + util.Debug("Accepted a new connection.", clientConn.RemoteAddr()) - go func() { - defer clientConn.Close() + go func() { + defer clientConn.Close() - message , err := ReadBytes(clientConn) - if err != nil { - return - } + message, err := ReadBytes(clientConn) + if err != nil { + return + } - util.Debug("Client sent data: ", len(message)) + util.Debug("Client sent data: ", len(message)) - domain := util.ExtractDomain(&message) + domain := util.ExtractDomain(&message) - ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, domain) // Dns lookup over https - if err != nil { - return - } + ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, domain) // Dns lookup over https + if err != nil { + return + } - util.Debug("ip: "+ ip) + util.Debug("ip: " + ip) - if util.ExtractMethod(&message) == "CONNECT" { - util.Debug("HTTPS Requested") - HandleHttps(clientConn, ip) - }else { - util.Debug("HTTP Requested.") - HandleHttp(clientConn, ip, message) - } - }() - } + if util.ExtractMethod(&message) == "CONNECT" { + util.Debug("HTTPS Requested") + HandleHttps(clientConn, ip) + } else { + util.Debug("HTTP Requested.") + HandleHttp(clientConn, ip, message) + } + }() + } }