SpoofDPI/proxy/proxy.go

84 lines
1.5 KiB
Go
Raw Normal View History

2021-12-29 17:08:30 +00:00
package proxy
import (
2023-09-08 08:35:41 +00:00
"fmt"
2021-12-29 17:08:30 +00:00
"os"
2022-01-03 07:24:39 +00:00
2022-01-11 15:05:16 +00:00
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/net"
2022-01-08 06:35:32 +00:00
"github.com/xvzc/SpoofDPI/packet"
2023-09-08 08:35:41 +00:00
"github.com/xvzc/SpoofDPI/util"
2021-12-29 17:08:30 +00:00
)
2022-01-08 15:48:19 +00:00
type Proxy struct {
2023-09-08 08:35:41 +00:00
addr string
port int
timeout int
2022-01-08 15:48:19 +00:00
}
2023-09-08 08:35:41 +00:00
func New(config *util.Config) *Proxy {
2022-01-08 15:48:19 +00:00
return &Proxy{
2023-09-08 08:35:41 +00:00
addr: *config.Addr,
port: *config.Port,
timeout: *config.Timeout,
2022-01-08 15:48:19 +00:00
}
}
2022-05-13 09:49:05 +00:00
func (p *Proxy) TcpAddr() *net.TCPAddr {
2022-11-29 07:54:28 +00:00
return net.TcpAddr(p.addr, p.port)
2022-05-08 05:39:35 +00:00
}
2022-05-13 09:49:05 +00:00
func (p *Proxy) Port() int {
2022-01-11 17:15:45 +00:00
return p.port
}
2022-01-08 15:48:19 +00:00
func (p *Proxy) Start() {
2022-05-13 09:49:05 +00:00
l, err := net.ListenTCP("tcp4", p.TcpAddr())
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
}
2023-09-08 08:35:41 +00:00
if p.timeout > 0 {
log.Println(fmt.Sprintf("Connection timeout is set to %dms", p.timeout))
}
log.Println("Created a listener on port", p.Port())
2021-12-29 17:08:30 +00:00
2022-01-04 16:47:18 +00:00
for {
2022-01-10 19:27:12 +00:00
conn, err := l.Accept()
2021-12-29 17:08:30 +00:00
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-04 16:47:18 +00:00
go func() {
2022-01-10 19:27:12 +00:00
b, err := conn.ReadBytes()
2022-01-04 16:47:18 +00:00
if err != nil {
return
}
2022-05-08 10:39:22 +00:00
2022-11-29 07:54:28 +00:00
log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(b))
2021-12-29 17:08:30 +00:00
2022-03-06 08:44:11 +00:00
pkt, err := packet.NewHttpPacket(b)
2022-11-29 07:54:28 +00:00
if err != nil {
log.Debug("Error while parsing request: ", string(b))
return
}
2022-01-07 15:39:58 +00:00
2022-01-11 17:15:45 +00:00
if !pkt.IsValidMethod() {
2022-03-06 08:44:11 +00:00
log.Debug("Unsupported method: ", pkt.Method())
2022-01-07 14:04:09 +00:00
return
}
2022-01-11 17:15:45 +00:00
if pkt.IsConnectMethod() {
2022-01-11 18:06:14 +00:00
log.Debug("[HTTPS] Start")
2023-09-08 08:35:41 +00:00
conn.HandleHttps(pkt, p.timeout)
2022-01-04 16:47:18 +00:00
} else {
2022-01-11 18:06:14 +00:00
log.Debug("[HTTP] Start")
2023-09-08 08:35:41 +00:00
conn.HandleHttp(pkt, p.timeout)
2022-01-04 16:47:18 +00:00
}
}()
}
2021-12-29 17:08:30 +00:00
}