2021-12-29 17:08:30 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-01-03 07:24:39 +00:00
|
|
|
|
|
|
|
"github.com/xvzc/SpoofDPI/config"
|
2022-01-04 16:47:18 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
2021-12-29 17:08:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Start() {
|
2022-01-04 16:47:18 +00:00
|
|
|
listener, err := net.Listen("tcp", ":"+config.GetConfig().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-04 16:47:18 +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-04 16:47:18 +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-04 16:47:18 +00:00
|
|
|
message, err := ReadBytes(clientConn)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-29 17:08:30 +00:00
|
|
|
|
2022-01-04 16:47:18 +00:00
|
|
|
util.Debug("Client sent data: ", len(message))
|
2021-12-29 17:08:30 +00:00
|
|
|
|
2022-01-07 14:04:09 +00:00
|
|
|
method := util.ExtractMethod(&message)
|
|
|
|
if !util.IsValidMethod(method) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-04 16:47:18 +00:00
|
|
|
domain := util.ExtractDomain(&message)
|
2021-12-29 17:08:30 +00:00
|
|
|
|
2022-01-04 16:47:18 +00:00
|
|
|
ip, err := util.DnsLookupOverHttps(config.GetConfig().DNS, domain) // Dns lookup over https
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-29 17:08:30 +00:00
|
|
|
|
2022-01-04 16:47:18 +00:00
|
|
|
util.Debug("ip: " + ip)
|
2021-12-29 17:08:30 +00:00
|
|
|
|
2022-01-04 16:47:18 +00:00
|
|
|
if util.ExtractMethod(&message) == "CONNECT" {
|
|
|
|
util.Debug("HTTPS Requested")
|
|
|
|
HandleHttps(clientConn, ip)
|
|
|
|
} else {
|
|
|
|
util.Debug("HTTP Requested.")
|
|
|
|
HandleHttp(clientConn, ip, message)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-12-29 17:08:30 +00:00
|
|
|
}
|