add net module and fixed the other modules

This commit is contained in:
xvzc 2022-01-11 02:11:30 +09:00
parent 00b22acc25
commit 452a7e343f
8 changed files with 108 additions and 54 deletions

View File

@ -1,13 +1,42 @@
package proxy package net
import ( import (
"net" "net"
// "github.com/xvzc/SpoofDPI/util"
) )
const BUF_SIZE = 1024 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) ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE) buf := make([]byte, BUF_SIZE)
@ -26,9 +55,9 @@ func ReadBytes(conn net.Conn) ([]byte, error) {
return ret, nil return ret, nil
} }
func Serve(from net.Conn, to net.Conn, proto string) { func (from *Conn) Serve(to Conn, proto string) {
for { for {
buf, err := ReadBytes(from) buf, err := from.ReadBytes()
if err != nil { if err != nil {
// util.Debug("["+proto+"]"+"Error reading from ", from.RemoteAddr()) // util.Debug("["+proto+"]"+"Error reading from ", from.RemoteAddr())
// util.Debug(err, " Closing the connection.. ") // util.Debug(err, " Closing the connection.. ")

23
net/dial.go Normal file
View File

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

18
net/listener.go Normal file
View File

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

1
net/net.go Normal file
View File

@ -0,0 +1 @@
package net

View File

@ -1,27 +1,3 @@
package proxy package proxy
import ( // "github.com/xvzc/SpoofDPI/util"
"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")
}

View File

@ -2,12 +2,29 @@ package proxy
import ( import (
"fmt" "fmt"
"net"
"github.com/xvzc/SpoofDPI/net"
"github.com/xvzc/SpoofDPI/packet" "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) { func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) {
// Create a connection to the requested server // Create a connection to the requested server
remoteConn, err := net.Dial("tcp", ip+":443") 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.") // util.Debug("[HTTPS] Connected to the server.")
// Send self generated response for connect request // 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") // util.Debug("[HTTPS] Sent 200 Connection Estabalished")
// Read client hello // Read client hello
clientHello, err := ReadBytes(clientConn) clientHello, err := clientConn.ReadBytes()
if err != nil { if err != nil {
// util.Debug("[HTTPS] Error reading client hello: ", err) // util.Debug("[HTTPS] Error reading client hello: ", err)
// util.Debug("Closing connection ", clientConn.RemoteAddr()) // 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)) // util.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello", len(clientHello))
// Generate a go routine that reads from the server // Generate a go routine that reads from the server
go Serve(remoteConn, clientConn, "HTTPS") go remoteConn.Serve(clientConn, "HTTPS")
// Send chunked request pkt := packet.NewHttpsPacket(&clientHello)
chunks := util.BytesToChunks(clientHello)
for i := 0; i < len(chunks); i++ { chunks := pkt.SplitInChunks()
_, write_err := remoteConn.Write(chunks[i])
if write_err != nil { if _, err := remoteConn.WriteChunks(chunks); err != nil {
// util.Debug("[HTTPS] Error writing to the client:", write_err) return
break
}
} }
// Read from the client // Read from the client
Serve(clientConn, remoteConn, "HTTPS") clientConn.Serve(remoteConn, "HTTPS")
} }

View File

@ -2,10 +2,10 @@ package proxy
import ( import (
"log" "log"
"net"
"os" "os"
"github.com/xvzc/SpoofDPI/doh" "github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/net"
"github.com/xvzc/SpoofDPI/packet" "github.com/xvzc/SpoofDPI/packet"
) )
@ -40,7 +40,7 @@ func (p *Proxy) Start() {
go func() { go func() {
defer clientConn.Close() defer clientConn.Close()
b, err := ReadBytes(clientConn) b, err := clientConn.ReadBytes()
if err != nil { if err != nil {
return return
} }

View File

@ -17,14 +17,6 @@ func ParseArgs() (string, string, bool) {
return *port, *dns, *debug 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) { func PrintWelcome(port string, dns string, debug bool) {
cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan)) cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan))
purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta)) purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta))