SpoofDPI/net/conn.go

190 lines
3.9 KiB
Go
Raw Normal View History

package net
2022-01-04 16:47:18 +00:00
import (
"net"
2022-03-04 23:46:47 +00:00
"time"
2022-03-05 00:56:53 +00:00
"errors"
2022-01-11 15:05:16 +00:00
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/packet"
2022-01-04 16:47:18 +00:00
)
const BUF_SIZE = 1024
type Conn struct {
2022-01-11 17:15:45 +00:00
conn net.Conn
}
2022-01-13 16:06:14 +00:00
func (c *Conn) CloseWrite() {
c.conn.(*net.TCPConn).CloseWrite()
}
2022-01-11 17:15:45 +00:00
func (c *Conn) Close() {
c.conn.Close()
}
2022-01-11 17:15:45 +00:00
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
2022-01-11 15:05:16 +00:00
}
2022-01-11 17:15:45 +00:00
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
2022-01-11 15:05:16 +00:00
}
2022-01-11 17:15:45 +00:00
func (c *Conn) Read(b []byte) (n int, err error) {
return c.conn.Read(b)
}
2022-01-11 17:15:45 +00:00
func (c *Conn) Write(b []byte) (n int, err error) {
return c.conn.Write(b)
}
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
}
b += total
}
return total, nil
}
func (conn *Conn) ReadBytes() ([]byte, error) {
2022-01-04 16:47:18 +00:00
ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE)
2022-03-04 23:46:47 +00:00
conn.conn.SetReadDeadline(time.Now().Add(3 * time.Second))
2022-01-04 16:47:18 +00:00
for {
n, err := conn.Read(buf)
2022-03-05 00:56:53 +00:00
if err != nil {
switch err.(type) {
case *net.OpError:
return nil, errors.New("timed out")
default:
return nil, err
}
}
2022-01-04 16:47:18 +00:00
ret = append(ret, buf[:n]...)
if n < BUF_SIZE {
break
}
}
return ret, nil
}
func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
2022-01-15 16:44:00 +00:00
p.Tidy()
log.Debug("[HTTP] request: \n\n" + string(p.Raw()))
2022-01-11 17:15:45 +00:00
ip, err := doh.Lookup(p.Domain())
if err != nil {
2022-03-05 00:56:53 +00:00
log.Debug("[DOH] Error looking up for domain with ", p.Domain() , err)
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create connection to server
rConn, err := Dial("tcp", ip+":80")
if err != nil {
log.Debug("[HTTPS] ", err)
2022-01-13 16:06:14 +00:00
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[HTTP] Connected to ", p.Domain())
_, 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())
go rConn.Serve(lConn, "[HTTP]", p.Domain(), "localhost")
lConn.Serve(rConn, "[HTTP]", "localhost", p.Domain())
2022-03-04 15:46:33 +00:00
}
func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
2022-01-15 16:44:00 +00:00
log.Debug("[HTTPS] request: \n\n" + string(p.Raw()))
2022-01-11 17:15:45 +00:00
ip, err := doh.Lookup(p.Domain())
if err != nil {
2022-03-05 00:56:53 +00:00
log.Debug("[DOH] Error looking up for domain: ", p.Domain(), " ", err)
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create a connection to the requested server
rConn, err := Dial("tcp", ip+":443")
if err != nil {
2022-01-11 18:06:14 +00:00
log.Debug("[HTTPS] ", err)
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[HTTPS] Connected to ", p.Domain())
_, err = lConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
if err != nil {
2022-03-05 00:56:53 +00:00
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[HTTPS] Sent 200 Connection Estabalished to the client")
// 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)
log.Debug("[HTTPS] Closing local connection..")
return
}
2022-03-05 00:56:53 +00:00
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
pkt := packet.NewHttpsPacket(clientHello)
chunks := pkt.SplitInChunks()
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)
return
}
2022-03-04 15:46:33 +00:00
// Generate a go routine that reads from the server
2022-03-05 00:56:53 +00:00
go rConn.Serve(lConn, "[HTTPS]", p.Domain(), "localhost")
lConn.Serve(rConn, "[HTTPS]", "localhost", p.Domain())
}
2022-03-05 00:56:53 +00:00
func (from *Conn) Serve(to *Conn, proto string, fd string, td string) {
defer from.Close()
defer to.CloseWrite()
proto += " "
2022-01-04 16:47:18 +00:00
for {
buf, err := from.ReadBytes()
2022-01-04 16:47:18 +00:00
if err != nil {
2022-03-05 00:56:53 +00:00
log.Debug(proto, "Error reading from ", fd, " ", err)
2022-01-04 16:47:18 +00:00
break
}
2022-03-05 00:56:53 +00:00
// log.Debug(proto, fd, " sent data: ", len(buf), "bytes")
2022-01-04 16:47:18 +00:00
2022-01-11 15:05:16 +00:00
if _, err := to.Write(buf); err != nil {
2022-03-05 00:56:53 +00:00
log.Debug(proto, "Error Writing to ", td)
2022-01-04 16:47:18 +00:00
break
}
}
}