SpoofDPI/proxy/proxy.go

97 lines
2.0 KiB
Go
Raw Normal View History

2021-12-29 17:08:30 +00:00
package proxy
import (
2022-01-08 15:48:19 +00:00
"fmt"
2021-12-29 17:08:30 +00:00
"log"
"net"
"os"
2022-01-03 07:24:39 +00:00
2022-01-08 16:02:30 +00:00
"github.com/babolivier/go-doh-client"
2022-01-08 15:48:19 +00:00
"github.com/pterm/pterm"
2022-01-08 06:35:32 +00:00
"github.com/xvzc/SpoofDPI/packet"
2021-12-29 17:08:30 +00:00
)
2022-01-08 15:48:19 +00:00
type Proxy struct {
Port string
2022-01-08 16:02:30 +00:00
DNS doh.Resolver
2022-01-08 15:48:19 +00:00
OS string
Debug bool
}
func New(port string, dns string, os string, debug bool) *Proxy {
return &Proxy{
Port: port,
2022-01-08 16:02:30 +00:00
DNS: doh.Resolver{Host: dns, Class: doh.IN},
2022-01-08 15:48:19 +00:00
OS: os,
Debug: debug,
}
}
func (p *Proxy) PrintWelcome() {
cyan := pterm.NewLettersFromStringWithStyle("Spoof", pterm.NewStyle(pterm.FgCyan))
purple := pterm.NewLettersFromStringWithStyle("DPI", pterm.NewStyle(pterm.FgLightMagenta))
pterm.DefaultBigText.WithLetters(cyan, purple).Render()
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
{Level: 0, Text: "PORT : " + p.Port},
2022-01-08 16:02:30 +00:00
{Level: 0, Text: "DNS : " + p.DNS.Host},
2022-01-08 15:48:19 +00:00
{Level: 0, Text: "DEBUG : " + fmt.Sprint(p.Debug)},
}).Render()
}
func (p *Proxy) Start() {
listener, err := net.Listen("tcp", ":"+p.Port)
2021-12-29 17:08:30 +00:00
if err != nil {
2022-01-04 16:47:18 +00:00
log.Fatal("Error creating listener: ", err)
os.Exit(1)
2021-12-29 17:08:30 +00:00
}
2022-01-08 15:48:19 +00:00
// util.Debug("Created a listener")
2021-12-29 17:08:30 +00:00
2022-01-04 16:47:18 +00:00
for {
2021-12-29 17:08:30 +00:00
clientConn, err := listener.Accept()
if err != nil {
2022-01-04 16:47:18 +00:00
log.Fatal("Error accepting connection: ", err)
2021-12-29 17:08:30 +00:00
continue
}
2022-01-08 15:48:19 +00:00
// util.Debug("Accepted a new connection.", clientConn.RemoteAddr())
2021-12-29 17:08:30 +00:00
2022-01-04 16:47:18 +00:00
go func() {
defer clientConn.Close()
2021-12-29 17:08:30 +00:00
2022-01-07 17:41:02 +00:00
b, err := ReadBytes(clientConn)
2022-01-04 16:47:18 +00:00
if err != nil {
return
}
2021-12-29 17:08:30 +00:00
2022-01-08 15:48:19 +00:00
// util.Debug("Client sent data: ", len(b))
2021-12-29 17:08:30 +00:00
2022-01-08 15:09:01 +00:00
r := packet.NewHttpPacket(&b)
2022-01-08 15:48:19 +00:00
// util.Debug("Request: \n" + string(*r.Raw))
2022-01-07 15:39:58 +00:00
2022-01-07 17:41:02 +00:00
if !r.IsValidMethod() {
log.Println("Unsupported method: ", r.Method)
2022-01-07 14:04:09 +00:00
return
}
2022-01-07 17:41:02 +00:00
// Dns lookup over https
2022-01-08 16:02:30 +00:00
ip, err := p.DnsLookupOverHttps(r.Domain)
2022-01-04 16:47:18 +00:00
if err != nil {
2022-01-07 17:41:02 +00:00
log.Println("Error looking up dns: "+r.Domain, err)
2022-01-04 16:47:18 +00:00
return
}
2021-12-29 17:08:30 +00:00
2022-01-08 15:48:19 +00:00
// util.Debug("ip: " + ip)
2021-12-29 17:08:30 +00:00
2022-01-08 06:35:32 +00:00
if r.IsConnectMethod() {
2022-01-08 15:48:19 +00:00
// util.Debug("HTTPS Requested")
2022-01-08 06:35:32 +00:00
HandleHttps(clientConn, ip, &r)
2022-01-04 16:47:18 +00:00
} else {
2022-01-08 15:48:19 +00:00
// util.Debug("HTTP Requested.")
2022-01-07 18:34:26 +00:00
HandleHttp(clientConn, ip, &r)
2022-01-04 16:47:18 +00:00
}
}()
}
2021-12-29 17:08:30 +00:00
}