update port parsing, and request order

This commit is contained in:
xvzc 2022-03-05 11:51:17 +09:00
parent d7f37377d9
commit 9d9e17f3d2
2 changed files with 36 additions and 14 deletions

View File

@ -94,7 +94,12 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
log.Debug("[DOH] Found ", ip, " with ", p.Domain()) log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create connection to server // Create connection to server
rConn, err := Dial("tcp", ip+":80") var port = ":80"
if p.Port() != "" {
port = p.Port()
}
rConn, err := Dial("tcp", ip + port)
if err != nil { if err != nil {
log.Debug("[HTTPS] ", err) log.Debug("[HTTPS] ", err)
return return
@ -102,6 +107,9 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
log.Debug("[HTTP] Connected to ", p.Domain()) log.Debug("[HTTP] Connected to ", p.Domain())
go lConn.Serve(rConn, "[HTTP]", "localhost", p.Domain())
go rConn.Serve(lConn, "[HTTP]", p.Domain(), "localhost")
_, err = rConn.Write(p.Raw()) _, err = rConn.Write(p.Raw())
if err != nil { if err != nil {
log.Debug("[HTTP] Error sending request to ", p.Domain(), err) log.Debug("[HTTP] Error sending request to ", p.Domain(), err)
@ -109,10 +117,6 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
} }
log.Debug("[HTTP] Sent a request to ", p.Domain()) log.Debug("[HTTP] Sent a request to ", p.Domain())
go rConn.Serve(lConn, "[HTTP]", p.Domain(), "localhost")
lConn.Serve(rConn, "[HTTP]", "localhost", p.Domain())
} }
func (lConn *Conn) HandleHttps(p packet.HttpPacket) { func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
@ -127,7 +131,12 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
log.Debug("[DOH] Found ", ip, " with ", p.Domain()) log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create a connection to the requested server // Create a connection to the requested server
rConn, err := Dial("tcp", ip+":443") var port = ":443"
if p.Port() != "" {
port = p.Port()
}
rConn, err := Dial("tcp", ip + port)
if err != nil { if err != nil {
log.Debug("[HTTPS] ", err) log.Debug("[HTTPS] ", err)
return return
@ -135,7 +144,7 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
log.Debug("[HTTPS] Connected to ", p.Domain()) log.Debug("[HTTPS] Connected to ", p.Domain())
_, err = lConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) _, err = lConn.Write([]byte(p.Version() + " 200 Connection Established\r\n\r\n"))
if err != nil { if err != nil {
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err) log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
return return
@ -152,6 +161,10 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes") log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
// Generate a go routine that reads from the server
go lConn.Serve(rConn, "[HTTPS]", "localhost", p.Domain())
go rConn.Serve(lConn, "[HTTPS]", p.Domain(), "localhost")
pkt := packet.NewHttpsPacket(clientHello) pkt := packet.NewHttpsPacket(clientHello)
chunks := pkt.SplitInChunks() chunks := pkt.SplitInChunks()
@ -161,9 +174,6 @@ func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
return return
} }
// Generate a go routine that reads from the server
go rConn.Serve(lConn, "[HTTPS]", p.Domain(), "localhost")
lConn.Serve(rConn, "[HTTPS]", "localhost", p.Domain())
} }
func (from *Conn) Serve(to *Conn, proto string, fd string, td string) { func (from *Conn) Serve(to *Conn, proto string, fd string, td string) {

View File

@ -44,17 +44,23 @@ type HttpPacket struct {
raw []byte raw []byte
method string method string
domain string domain string
port string
path string path string
version string version string
} }
func ParseUrl(raw []byte) {
}
func NewHttpPacket(raw []byte) HttpPacket { func NewHttpPacket(raw []byte) HttpPacket {
method, domain, path, version := parse(raw) method, domain, port, path, version := parse(raw)
return HttpPacket{ return HttpPacket{
raw: raw, raw: raw,
method: method, method: method,
domain: domain, domain: domain,
port: port,
path: path, path: path,
version: version, version: version,
} }
@ -71,6 +77,10 @@ func (p *HttpPacket) Domain() string {
return p.domain return p.domain
} }
func (p *HttpPacket) Port() string {
return p.port
}
func (p *HttpPacket) Version() string { func (p *HttpPacket) Version() string {
return p.version return p.version
} }
@ -115,7 +125,7 @@ func (p *HttpPacket) Tidy() {
p.raw = []byte(result) p.raw = []byte(result)
} }
func parse(raw []byte) (string, string, string, string) { func parse(raw []byte) (string, string, string, string, string) {
var firstLine string var firstLine string
for i := 0; i < len(raw); i++ { for i := 0; i < len(raw); i++ {
if (raw)[i] == '\n' { if (raw)[i] == '\n' {
@ -133,10 +143,12 @@ func parse(raw []byte) (string, string, string, string) {
url = strings.Replace(url, "http://", "", 1) url = strings.Replace(url, "http://", "", 1)
url = strings.Replace(url, "https://", "", 1) url = strings.Replace(url, "https://", "", 1)
domain := url var domain string
var port string
for i := 0; i < len(url); i++ { for i := 0; i < len(url); i++ {
if url[i] == ':' { if url[i] == ':' {
domain = url[:i] domain = url[:i]
port = url[i:]
break break
} }
@ -154,5 +166,5 @@ func parse(raw []byte) (string, string, string, string) {
} }
} }
return method, domain, path, version return method, domain, port, path, version
} }