mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 14:26:31 +00:00
77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package proxy
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/xvzc/SpoofDPI/doh"
|
|
"github.com/xvzc/SpoofDPI/net"
|
|
"github.com/xvzc/SpoofDPI/packet"
|
|
)
|
|
|
|
type Proxy struct {
|
|
Port string
|
|
}
|
|
|
|
func New(port string) *Proxy {
|
|
return &Proxy{
|
|
Port: port,
|
|
}
|
|
}
|
|
|
|
func (p *Proxy) Start() {
|
|
l, err := net.Listen("tcp", ":"+p.Port)
|
|
if err != nil {
|
|
log.Fatal("Error creating listener: ", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// util.Debug("Created a listener")
|
|
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
log.Fatal("Error accepting connection: ", err)
|
|
continue
|
|
}
|
|
|
|
// util.Debug("Accepted a new connection.", clientConn.RemoteAddr())
|
|
|
|
go func() {
|
|
defer conn.Close()
|
|
|
|
b, err := conn.ReadBytes()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// util.Debug("Client sent data: ", len(b))
|
|
|
|
r := packet.NewHttpPacket(b)
|
|
// util.Debug("Request: \n" + string(*r.Raw))
|
|
|
|
if !r.IsValidMethod() {
|
|
log.Println("Unsupported method: ", r.Method)
|
|
return
|
|
}
|
|
|
|
// Dns lookup over https
|
|
ip, err := doh.Lookup(r.Domain)
|
|
if err != nil {
|
|
log.Println("Error looking up dns: "+r.Domain, err)
|
|
return
|
|
}
|
|
|
|
// util.Debug("ip: " + ip)
|
|
|
|
if r.IsConnectMethod() {
|
|
// util.Debug("HTTPS Requested")
|
|
HandleHttps(conn, ip, &r)
|
|
} else {
|
|
// util.Debug("HTTP Requested.")
|
|
HandleHttp(conn, ip, &r)
|
|
}
|
|
}()
|
|
}
|
|
}
|