2022-01-10 17:11:30 +00:00
|
|
|
package net
|
2022-01-04 16:47:18 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-12 12:21:54 +00:00
|
|
|
"errors"
|
2022-06-05 05:50:55 +00:00
|
|
|
"io"
|
2022-01-04 16:47:18 +00:00
|
|
|
"net"
|
2022-03-04 23:46:47 +00:00
|
|
|
"time"
|
2022-01-11 15:05:16 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-11 16:59:55 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/doh"
|
|
|
|
"github.com/xvzc/SpoofDPI/packet"
|
2022-01-04 16:47:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const BUF_SIZE = 1024
|
|
|
|
|
2022-01-10 17:11:30 +00:00
|
|
|
type Conn struct {
|
2023-09-01 02:29:55 +00:00
|
|
|
net.TCPConn
|
2022-05-01 15:31:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 17:11:30 +00:00
|
|
|
func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) {
|
|
|
|
total := 0
|
|
|
|
for i := 0; i < len(c); i++ {
|
|
|
|
b, err := conn.Write(c[i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2023-09-03 02:24:16 +00:00
|
|
|
total += b
|
2022-01-10 17:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) ReadBytes() ([]byte, error) {
|
2022-11-29 07:54:28 +00:00
|
|
|
ret := make([]byte, 0)
|
|
|
|
buf := make([]byte, BUF_SIZE)
|
2022-05-12 12:21:54 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
for {
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *net.OpError:
|
|
|
|
return nil, errors.New("timed out")
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = append(ret, buf[:n]...)
|
2022-01-04 16:47:18 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
if n < BUF_SIZE {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 16:47:18 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
if len(ret) == 0 {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2022-06-05 05:50:55 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
return ret, nil
|
2022-01-04 16:47:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 11:38:00 +00:00
|
|
|
func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
|
2022-01-15 16:44:00 +00:00
|
|
|
p.Tidy()
|
|
|
|
|
2022-01-11 17:15:45 +00:00
|
|
|
ip, err := doh.Lookup(p.Domain())
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-11-29 07:54:28 +00:00
|
|
|
log.Error("[HTTP DOH] Error looking up for domain with ", p.Domain(), " ", err)
|
|
|
|
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
|
|
|
|
return
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
2022-03-05 00:56:53 +00:00
|
|
|
|
|
|
|
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-01-14 08:23:41 +00:00
|
|
|
// Create connection to server
|
2022-11-29 07:54:28 +00:00
|
|
|
var port = "80"
|
|
|
|
if p.Port() != "" {
|
|
|
|
port = p.Port()
|
|
|
|
}
|
2022-03-05 02:51:17 +00:00
|
|
|
|
2022-05-13 09:49:05 +00:00
|
|
|
rConn, err := DialTCP("tcp", ip, port)
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-05-08 10:39:22 +00:00
|
|
|
log.Debug("[HTTP] ", err)
|
2022-01-13 16:06:14 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-12 08:56:12 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
defer func() {
|
|
|
|
lConn.Close()
|
|
|
|
log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr())
|
2022-07-24 06:12:07 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
rConn.Close()
|
|
|
|
log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
|
|
|
|
}()
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
log.Debug("[HTTP] New connection to the server ", p.Domain(), " ", rConn.LocalAddr())
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
go rConn.Serve(lConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
|
2022-07-24 06:12:07 +00:00
|
|
|
|
2022-01-14 08:23:41 +00:00
|
|
|
_, err = rConn.Write(p.Raw())
|
|
|
|
if err != nil {
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTP] Error sending request to ", p.Domain(), err)
|
2022-01-13 16:06:14 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-04 15:46:33 +00:00
|
|
|
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTP] Sent a request to ", p.Domain())
|
2022-05-08 07:55:13 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
lConn.Serve(rConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
|
2022-05-08 07:55:13 +00:00
|
|
|
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 11:38:00 +00:00
|
|
|
func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
|
2022-01-11 17:15:45 +00:00
|
|
|
ip, err := doh.Lookup(p.Domain())
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-05-01 15:31:22 +00:00
|
|
|
log.Error("[HTTPS DOH] Error looking up for domain: ", p.Domain(), " ", err)
|
2022-11-29 07:54:28 +00:00
|
|
|
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
|
|
|
|
return
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
2022-03-05 00:56:53 +00:00
|
|
|
|
|
|
|
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
|
2022-01-11 16:59:55 +00:00
|
|
|
|
|
|
|
// Create a connection to the requested server
|
2022-11-29 07:54:28 +00:00
|
|
|
var port = "443"
|
|
|
|
if p.Port() != "" {
|
|
|
|
port = p.Port()
|
|
|
|
}
|
2022-03-05 02:51:17 +00:00
|
|
|
|
2022-05-13 09:49:05 +00:00
|
|
|
rConn, err := DialTCP("tcp4", ip, port)
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-01-11 18:06:14 +00:00
|
|
|
log.Debug("[HTTPS] ", err)
|
2022-01-11 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-12 08:56:12 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
defer func() {
|
|
|
|
lConn.Close()
|
|
|
|
log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr())
|
2022-07-24 06:12:07 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
rConn.Close()
|
|
|
|
log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
|
|
|
|
}()
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
log.Debug("[HTTPS] New connection to the server ", p.Domain(), " ", rConn.LocalAddr())
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-03-05 02:51:17 +00:00
|
|
|
_, err = lConn.Write([]byte(p.Version() + " 200 Connection Established\r\n\r\n"))
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
|
2022-11-29 07:54:28 +00:00
|
|
|
return
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
2022-06-05 05:50:55 +00:00
|
|
|
|
|
|
|
log.Debug("[HTTPS] Sent 200 Connection Estabalished to ", lConn.RemoteAddr())
|
2022-01-11 16:59:55 +00:00
|
|
|
|
|
|
|
// Read client hello
|
|
|
|
clientHello, err := lConn.ReadBytes()
|
|
|
|
if err != nil {
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTPS] Error reading client hello from the client", err)
|
2022-11-29 07:54:28 +00:00
|
|
|
return
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-03-05 02:51:17 +00:00
|
|
|
// Generate a go routine that reads from the server
|
|
|
|
|
2022-01-11 16:59:55 +00:00
|
|
|
pkt := packet.NewHttpsPacket(clientHello)
|
|
|
|
|
|
|
|
chunks := pkt.SplitInChunks()
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
go rConn.Serve(lConn, "[HTTPS]", rConn.RemoteAddr().String(), p.Domain())
|
2022-07-24 06:12:07 +00:00
|
|
|
|
2022-01-11 16:59:55 +00:00
|
|
|
if _, err := rConn.WriteChunks(chunks); err != nil {
|
2022-03-05 00:56:53 +00:00
|
|
|
log.Debug("[HTTPS] Error writing client hello to ", p.Domain(), err)
|
2022-01-11 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-04 15:46:33 +00:00
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
lConn.Serve(rConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain())
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 00:56:53 +00:00
|
|
|
func (from *Conn) Serve(to *Conn, proto string, fd string, td string) {
|
2022-01-14 08:23:41 +00:00
|
|
|
proto += " "
|
|
|
|
|
2022-11-29 07:54:28 +00:00
|
|
|
for {
|
2023-09-01 02:29:55 +00:00
|
|
|
from.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
|
2022-11-29 07:54:28 +00:00
|
|
|
buf, err := from.ReadBytes()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
log.Debug(proto, "Finished ", fd)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debug(proto, "Error reading from ", fd, " ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := to.Write(buf); err != nil {
|
|
|
|
log.Debug(proto, "Error Writing to ", td)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 16:47:18 +00:00
|
|
|
}
|