2021-12-30 02:08:30 +09:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-01-03 16:24:39 +09:00
|
|
|
|
2022-01-12 00:05:16 +09:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-11 02:11:30 +09:00
|
|
|
"github.com/xvzc/SpoofDPI/net"
|
2022-01-08 15:35:32 +09:00
|
|
|
"github.com/xvzc/SpoofDPI/packet"
|
2021-12-30 02:08:30 +09:00
|
|
|
)
|
|
|
|
|
2022-01-09 00:48:19 +09:00
|
|
|
type Proxy struct {
|
2022-01-12 02:15:45 +09:00
|
|
|
port string
|
2022-01-09 00:48:19 +09:00
|
|
|
}
|
|
|
|
|
2022-01-11 00:14:51 +09:00
|
|
|
func New(port string) *Proxy {
|
2022-01-09 00:48:19 +09:00
|
|
|
return &Proxy{
|
2022-01-12 02:15:45 +09:00
|
|
|
port: port,
|
2022-01-09 00:48:19 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 02:15:45 +09:00
|
|
|
func (p *Proxy) Port() string {
|
|
|
|
return p.port
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:48:19 +09:00
|
|
|
func (p *Proxy) Start() {
|
2022-01-12 02:15:45 +09:00
|
|
|
l, err := net.Listen("tcp", ":"+p.Port())
|
2021-12-30 02:08:30 +09:00
|
|
|
if err != nil {
|
2022-01-05 01:47:18 +09:00
|
|
|
log.Fatal("Error creating listener: ", err)
|
|
|
|
os.Exit(1)
|
2021-12-30 02:08:30 +09:00
|
|
|
}
|
|
|
|
|
2022-01-12 02:15:45 +09:00
|
|
|
log.Println("Created a listener on :", p.Port())
|
2021-12-30 02:08:30 +09:00
|
|
|
|
2022-01-05 01:47:18 +09:00
|
|
|
for {
|
2022-01-11 04:27:12 +09:00
|
|
|
conn, err := l.Accept()
|
2021-12-30 02:08:30 +09:00
|
|
|
if err != nil {
|
2022-01-05 01:47:18 +09:00
|
|
|
log.Fatal("Error accepting connection: ", err)
|
2021-12-30 02:08:30 +09:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-12 00:05:16 +09:00
|
|
|
log.Debug("Accepted a new connection.", conn.RemoteAddr())
|
2021-12-30 02:08:30 +09:00
|
|
|
|
2022-01-05 01:47:18 +09:00
|
|
|
go func() {
|
2022-01-11 04:27:12 +09:00
|
|
|
b, err := conn.ReadBytes()
|
2022-01-05 01:47:18 +09:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-01-12 00:05:16 +09:00
|
|
|
log.Debug("Client sent data: ", len(b))
|
2021-12-30 02:08:30 +09:00
|
|
|
|
2022-01-12 02:15:45 +09:00
|
|
|
pkt := packet.NewHttpPacket(b)
|
2022-01-08 00:39:58 +09:00
|
|
|
|
2022-01-12 02:15:45 +09:00
|
|
|
if !pkt.IsValidMethod() {
|
|
|
|
log.Println("Unsupported method: ", pkt.Method())
|
2022-01-07 23:04:09 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-12 02:15:45 +09:00
|
|
|
if pkt.IsConnectMethod() {
|
2022-01-12 03:06:14 +09:00
|
|
|
log.Debug("[HTTPS] Start")
|
2022-01-14 17:23:41 +09:00
|
|
|
go conn.HandleHttps(pkt)
|
2022-01-05 01:47:18 +09:00
|
|
|
} else {
|
2022-01-12 03:06:14 +09:00
|
|
|
log.Debug("[HTTP] Start")
|
2022-01-14 17:23:41 +09:00
|
|
|
go conn.HandleHttp(pkt)
|
2022-01-05 01:47:18 +09:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-12-30 02:08:30 +09:00
|
|
|
}
|