SpoofDPI/proxy/proxy.go
2022-01-11 04:27:12 +09:00

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