2022-01-02 12:56:12 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-01-11 15:05:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-10 17:11:30 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/net"
|
2022-01-08 06:35:32 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/packet"
|
2022-01-02 12:56:12 +00:00
|
|
|
)
|
|
|
|
|
2022-01-10 17:11:30 +00:00
|
|
|
func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) {
|
2022-01-11 15:05:16 +00:00
|
|
|
// Create connection to server
|
|
|
|
remoteConn, err := net.Dial("tcp", ip+":80")
|
2022-01-10 17:11:30 +00:00
|
|
|
if err != nil {
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug(err)
|
2022-01-10 17:11:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer remoteConn.Close()
|
|
|
|
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug("[HTTP] Connected to the server.")
|
2022-01-10 17:11:30 +00:00
|
|
|
|
|
|
|
go remoteConn.Serve(clientConn, "HTTP")
|
|
|
|
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug("[HTTP] Sending request to the server")
|
2022-01-10 19:27:12 +00:00
|
|
|
fmt.Fprintf(remoteConn.Conn, string(p.Raw))
|
2022-01-10 17:11:30 +00:00
|
|
|
|
|
|
|
go clientConn.Serve(remoteConn, "HTTP")
|
|
|
|
}
|
|
|
|
|
2022-01-08 15:09:01 +00:00
|
|
|
func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) {
|
2022-01-07 14:04:09 +00:00
|
|
|
// Create a connection to the requested server
|
|
|
|
remoteConn, err := net.Dial("tcp", ip+":443")
|
|
|
|
if err != nil {
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug(err)
|
2022-01-07 14:04:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer remoteConn.Close()
|
|
|
|
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug("[HTTPS] Connected to the server.")
|
2022-01-07 14:04:09 +00:00
|
|
|
|
|
|
|
// Send self generated response for connect request
|
2022-01-10 17:11:30 +00:00
|
|
|
fmt.Fprintf(clientConn.Conn, "HTTP/1.1 200 Connection Established\r\n\r\n")
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug("[HTTPS] Sent 200 Connection Estabalished")
|
2022-01-07 14:04:09 +00:00
|
|
|
|
|
|
|
// Read client hello
|
2022-01-10 17:11:30 +00:00
|
|
|
clientHello, err := clientConn.ReadBytes()
|
2022-01-07 14:04:09 +00:00
|
|
|
if err != nil {
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug("[HTTPS] Error reading client hello: ", err)
|
|
|
|
log.Debug("Closing connection: ", clientConn.RemoteAddr())
|
2022-01-07 14:04:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 15:05:16 +00:00
|
|
|
log.Debug(clientConn.RemoteAddr(), "[HTTPS] Client sent hello: ", len(clientHello), "bytes")
|
2022-01-07 14:04:09 +00:00
|
|
|
|
|
|
|
// Generate a go routine that reads from the server
|
2022-01-10 17:11:30 +00:00
|
|
|
go remoteConn.Serve(clientConn, "HTTPS")
|
|
|
|
|
2022-01-10 19:27:12 +00:00
|
|
|
pkt := packet.NewHttpsPacket(clientHello)
|
2022-01-10 17:11:30 +00:00
|
|
|
|
|
|
|
chunks := pkt.SplitInChunks()
|
|
|
|
|
|
|
|
if _, err := remoteConn.WriteChunks(chunks); err != nil {
|
|
|
|
return
|
2022-01-07 14:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the client
|
2022-01-10 17:11:30 +00:00
|
|
|
clientConn.Serve(remoteConn, "HTTPS")
|
2022-01-02 12:56:12 +00:00
|
|
|
}
|