2024-07-22 04:49:18 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2024-08-22 08:49:05 +00:00
|
|
|
"context"
|
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
2024-07-22 10:59:11 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util/log"
|
2024-07-22 10:59:11 +00:00
|
|
|
|
2024-07-22 04:49:18 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/packet"
|
|
|
|
)
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
const protoHTTP = "HTTP"
|
|
|
|
|
|
|
|
func (pxy *Proxy) handleHttp(ctx context.Context, lConn *net.TCPConn, pkt *packet.HttpRequest, ip string) {
|
|
|
|
ctx = util.GetCtxWithScope(ctx, protoHTTP)
|
|
|
|
logger := log.GetCtxLogger(ctx)
|
|
|
|
|
2024-07-22 04:49:18 +00:00
|
|
|
pkt.Tidy()
|
|
|
|
|
2024-07-22 10:59:11 +00:00
|
|
|
// Create a connection to the requested server
|
|
|
|
var port int = 80
|
|
|
|
var err error
|
2024-07-22 04:49:18 +00:00
|
|
|
if pkt.Port() != "" {
|
2024-07-22 10:59:11 +00:00
|
|
|
port, err = strconv.Atoi(pkt.Port())
|
|
|
|
if err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error while parsing port for %s aborting..", pkt.Domain())
|
2024-07-22 10:59:11 +00:00
|
|
|
}
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 10:59:11 +00:00
|
|
|
rConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: net.ParseIP(ip), Port: port})
|
2024-07-22 04:49:18 +00:00
|
|
|
if err != nil {
|
2024-07-22 10:59:11 +00:00
|
|
|
lConn.Close()
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("%s", err)
|
2024-07-22 04:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("new connection to the server %s -> %s", rConn.LocalAddr(), pkt.Domain())
|
2024-07-22 04:49:18 +00:00
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
go Serve(ctx, rConn, lConn, protoHTTP, pkt.Domain(), lConn.RemoteAddr().String(), pxy.timeout)
|
2024-07-22 04:49:18 +00:00
|
|
|
|
|
|
|
_, err = rConn.Write(pkt.Raw())
|
|
|
|
if err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error sending request to %s: %s", pkt.Domain(), err)
|
2024-07-22 04:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("sent a request to %s", pkt.Domain())
|
2024-07-22 04:49:18 +00:00
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
go Serve(ctx, lConn, rConn, protoHTTP, lConn.RemoteAddr().String(), pkt.Domain(), pxy.timeout)
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|