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,12 +5,12 @@ import (
"net" "net"
"os" "os"
"github.com/xvzc/SpoofDPI/util"
"github.com/xvzc/SpoofDPI/config" "github.com/xvzc/SpoofDPI/config"
"github.com/xvzc/SpoofDPI/util"
) )
func Start() { func Start() {
listener, err := net.Listen("tcp", ":" + config.GetConfig().Port) listener, err := net.Listen("tcp", ":"+config.GetConfig().Port)
if err != nil { if err != nil {
log.Fatal("Error creating listener: ", err) log.Fatal("Error creating listener: ", err)
os.Exit(1) os.Exit(1)
@ -30,7 +30,7 @@ func Start() {
go func() { go func() {
defer clientConn.Close() defer clientConn.Close()
message , err := ReadBytes(clientConn) message, err := ReadBytes(clientConn)
if err != nil { if err != nil {
return return
} }
@ -44,12 +44,12 @@ func Start() {
return return
} }
util.Debug("ip: "+ ip) util.Debug("ip: " + ip)
if util.ExtractMethod(&message) == "CONNECT" { if util.ExtractMethod(&message) == "CONNECT" {
util.Debug("HTTPS Requested") util.Debug("HTTPS Requested")
HandleHttps(clientConn, ip) HandleHttps(clientConn, ip)
}else { } else {
util.Debug("HTTP Requested.") util.Debug("HTTP Requested.")
HandleHttp(clientConn, ip, message) HandleHttp(clientConn, ip, message)
} }