mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2025-01-03 04:50:11 +00:00
update refactor
This commit is contained in:
parent
d6ddc4abbf
commit
43017e7211
48
proxy/conn.go
Normal file
48
proxy/conn.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user