Merge pull request #8 from xvzc/conn

update handlers to belong to conn module
This commit is contained in:
Kwanghoo Park 2022-01-12 02:02:09 +09:00 committed by GitHub
commit c97539cb3f
4 changed files with 80 additions and 82 deletions

View File

@ -4,6 +4,8 @@ import (
"net"
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/packet"
)
const BUF_SIZE = 1024
@ -65,7 +67,82 @@ func (conn *Conn) ReadBytes() ([]byte, error) {
return ret, nil
}
func (from *Conn) Serve(to Conn, proto string) {
func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
ip, err := doh.Lookup(p.Domain)
if err != nil {
log.Debug("[HTTPS] Error looking up for domain: ", err)
}
log.Debug("[HTTPS] Found ip over HTTPS: ", ip)
// Create connection to server
rConn, err := Dial("tcp", ip+":80")
if err != nil {
log.Debug(err)
return
}
defer rConn.Close()
log.Debug("[HTTP] Connected to the server.")
go rConn.Serve(lConn, "HTTP")
_, err = rConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
if err != nil {
log.Debug("[HTTP] Error sending request to the server: ", err)
}
log.Debug("[HTTP] Sent a request to the server")
go lConn.Serve(&rConn, "HTTP")
}
func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
ip, err := doh.Lookup(p.Domain)
if err != nil {
log.Debug("[HTTPS] Error looking up for domain: ", err)
}
log.Debug("[HTTPS] Found ip over HTTPS: ", ip)
// Create a connection to the requested server
rConn, err := Dial("tcp", ip+":443")
if err != nil {
log.Debug(err)
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)
log.Debug("Closing connection: ", lConn.RemoteAddr())
}
log.Debug(lConn.RemoteAddr(), "[HTTPS] Client sent hello: ", len(clientHello), "bytes")
// Generate a go routine that reads from the server
go rConn.Serve(lConn, "HTTPS")
pkt := packet.NewHttpsPacket(clientHello)
chunks := pkt.SplitInChunks()
if _, err := rConn.WriteChunks(chunks); err != nil {
return
}
// Read from the client
lConn.Serve(&rConn, "HTTPS")
}
func (from *Conn) Serve(to *Conn, proto string) {
for {
buf, err := from.ReadBytes()
if err != nil {

View File

@ -1,3 +0,0 @@
package proxy
// "github.com/xvzc/SpoofDPI/util"

View File

@ -1,67 +0,0 @@
package proxy
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/net"
"github.com/xvzc/SpoofDPI/packet"
)
func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) {
// Create connection to server
remoteConn, err := net.Dial("tcp", ip+":80")
if err != nil {
log.Debug(err)
return
}
defer remoteConn.Close()
log.Debug("[HTTP] Connected to the server.")
go remoteConn.Serve(clientConn, "HTTP")
log.Debug("[HTTP] Sending request to the server")
fmt.Fprintf(remoteConn.Conn, string(p.Raw))
go clientConn.Serve(remoteConn, "HTTP")
}
func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) {
// Create a connection to the requested server
remoteConn, err := net.Dial("tcp", ip+":443")
if err != nil {
log.Debug(err)
return
}
defer remoteConn.Close()
log.Debug("[HTTPS] Connected to the server.")
// Send self generated response for connect request
fmt.Fprintf(clientConn.Conn, "HTTP/1.1 200 Connection Established\r\n\r\n")
log.Debug("[HTTPS] Sent 200 Connection Estabalished")
// Read client hello
clientHello, err := clientConn.ReadBytes()
if err != nil {
log.Debug("[HTTPS] Error reading client hello: ", err)
log.Debug("Closing connection: ", clientConn.RemoteAddr())
}
log.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello: ", len(clientHello), "bytes")
// Generate a go routine that reads from the server
go remoteConn.Serve(clientConn, "HTTPS")
pkt := packet.NewHttpsPacket(clientHello)
chunks := pkt.SplitInChunks()
if _, err := remoteConn.WriteChunks(chunks); err != nil {
return
}
// Read from the client
clientConn.Serve(remoteConn, "HTTPS")
}

View File

@ -4,7 +4,6 @@ import (
"os"
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/doh"
"github.com/xvzc/SpoofDPI/net"
"github.com/xvzc/SpoofDPI/packet"
)
@ -54,20 +53,12 @@ func (p *Proxy) Start() {
return
}
// Dns lookup over https
ip, err := doh.Lookup(r.Domain)
if err != nil {
log.Println("Error looking up dns: "+r.Domain, err)
return
}
log.Debug("ip: " + ip)
if r.IsConnectMethod() {
log.Debug("HTTPS Requested")
HandleHttps(conn, ip, &r)
conn.HandleHttps(r)
} else {
log.Debug("HTTP Requested.")
HandleHttp(conn, ip, &r)
conn.HandleHttp(r)
}
}()
}