update refactor

This commit is contained in:
xvzc 2022-01-05 01:47:18 +09:00
parent d6ddc4abbf
commit 43017e7211
3 changed files with 78 additions and 79 deletions

48
proxy/conn.go Normal file
View File

@ -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
}
}
}

View File

@ -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
}
}
}

View File

@ -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)
}
}()
}
}