2022-01-10 17:11:30 +00:00
|
|
|
package net
|
2022-01-04 16:47:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
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 {
|
2022-01-11 17:15:45 +00:00
|
|
|
conn net.Conn
|
2022-01-10 17:11:30 +00:00
|
|
|
}
|
|
|
|
|
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-10 17:11:30 +00:00
|
|
|
}
|
|
|
|
|
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-10 17:11:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 17:15:45 +00:00
|
|
|
func (c *Conn) Write(b []byte) (n int, err error) {
|
|
|
|
return c.conn.Write(b)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, buf[:n]...)
|
|
|
|
|
|
|
|
if n < BUF_SIZE {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-01-11 16:59:55 +00:00
|
|
|
func (lConn *Conn) HandleHttp(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-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTP] Error looking up for domain: ", err)
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTP] Found ip over HTTPS: ", ip)
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-01-13 16:06:14 +00:00
|
|
|
rConn, err := Dial("tcp", ip+":80") // create connection to server
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug(err)
|
2022-01-11 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rConn.Close()
|
|
|
|
|
2022-01-13 16:06:14 +00:00
|
|
|
if _, err := rConn.Write(p.Raw()); err != nil {
|
|
|
|
log.Debug("failed:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rConn.CloseWrite()
|
2022-01-11 16:59:55 +00:00
|
|
|
|
2022-01-13 16:06:14 +00:00
|
|
|
buf, err := rConn.ReadBytes()
|
2022-01-11 16:59:55 +00:00
|
|
|
if err != nil {
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug("failed:", err)
|
|
|
|
return
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTP] Response from the server : \n\n", string(buf))
|
|
|
|
|
|
|
|
// Write to client
|
|
|
|
if _, err = lConn.Write(buf); err != nil {
|
|
|
|
log.Debug("failed:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer lConn.CloseWrite()
|
2022-01-11 16:59:55 +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-01-11 18:06:14 +00:00
|
|
|
log.Debug("[HTTPS] Error looking up for domain: ", p.Domain(), " ", err)
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
log.Debug("[HTTPS] Found ip over HTTPS: ", ip)
|
|
|
|
|
|
|
|
// 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)
|
2022-01-11 16:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rConn.Close()
|
|
|
|
|
|
|
|
log.Debug("[HTTPS] Connected to the server.")
|
|
|
|
|
|
|
|
_, err = lConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("[HTTPS] Error sending client hello: ", err)
|
|
|
|
}
|
|
|
|
log.Debug("[HTTPS] Sent 200 Connection Estabalished")
|
|
|
|
|
|
|
|
// Read client hello
|
|
|
|
clientHello, err := lConn.ReadBytes()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("[HTTPS] Error reading client hello: ", err)
|
2022-01-11 18:06:14 +00:00
|
|
|
log.Debug("[HTTPS] Closing connection: ", lConn.RemoteAddr())
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 18:06:14 +00:00
|
|
|
log.Debug("[HTTPS] Client "+lConn.RemoteAddr().String()+" sent hello: ", len(clientHello), "bytes")
|
2022-01-11 16:59:55 +00:00
|
|
|
|
|
|
|
// Generate a go routine that reads from the server
|
2022-01-13 16:06:14 +00:00
|
|
|
go rConn.ServeHttps(lConn)
|
2022-01-11 16:59:55 +00:00
|
|
|
|
|
|
|
pkt := packet.NewHttpsPacket(clientHello)
|
|
|
|
|
|
|
|
chunks := pkt.SplitInChunks()
|
|
|
|
|
|
|
|
if _, err := rConn.WriteChunks(chunks); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the client
|
2022-01-13 16:06:14 +00:00
|
|
|
lConn.ServeHttps(rConn)
|
2022-01-11 16:59:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 16:06:14 +00:00
|
|
|
func (from *Conn) ServeHttps(to *Conn) {
|
2022-01-04 16:47:18 +00:00
|
|
|
for {
|
2022-01-10 17:11:30 +00:00
|
|
|
buf, err := from.ReadBytes()
|
2022-01-04 16:47:18 +00:00
|
|
|
if err != nil {
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTPS] "+"Error reading from ", from.RemoteAddr())
|
|
|
|
log.Debug("[HTTPS] ", err)
|
|
|
|
log.Debug("[HTTPS] " + "Exiting Serve() method. ")
|
2022-01-04 16:47:18 +00:00
|
|
|
break
|
|
|
|
}
|
2022-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTPS] ", from.RemoteAddr(), " 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-01-13 16:06:14 +00:00
|
|
|
log.Debug("[HTTPS] "+"Error Writing to ", to.RemoteAddr())
|
|
|
|
log.Debug("[HTTPS] ", err)
|
|
|
|
log.Debug("[HTTPS] " + "Exiting Serve() method. ")
|
2022-01-04 16:47:18 +00:00
|
|
|
break
|
|
|
|
}
|
2022-01-13 16:06:14 +00:00
|
|
|
defer to.CloseWrite()
|
2022-01-04 16:47:18 +00:00
|
|
|
}
|
|
|
|
}
|