Skip to content

Commit

Permalink
Merge pull request #23 from xvzc/url
Browse files Browse the repository at this point in the history
Url
  • Loading branch information
xvzc authored May 9, 2022
2 parents f35c261 + 157a0df commit d2cb0e9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 49 deletions.
4 changes: 2 additions & 2 deletions net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
// Create connection to server
var port = ":80"
if p.Port() != "" {
port = p.Port()
port = ":" + p.Port()
}

rConn, err := Dial("tcp", ip + port)
Expand Down Expand Up @@ -154,7 +154,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
// Create a connection to the requested server
var port = ":443"
if p.Port() != "" {
port = p.Port()
port = ":" + p.Port()
}

rConn, err := Dial("tcp", ip + port)
Expand Down
68 changes: 23 additions & 45 deletions packet/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package packet

import (
"errors"
"bufio"
"net"
"net/http"
"strings"
)

Expand Down Expand Up @@ -122,58 +124,34 @@ func (p *HttpPacket) Tidy() {
}

func (p *HttpPacket )parse() error {
var firstLine string
for i := 0; i < len(p.raw); i++ {
if (p.raw)[i] == '\r' {
firstLine = string(p.raw[:i])
break
}
}

tokens := strings.Split(firstLine, " ")
reader := bufio.NewReader(strings.NewReader(string(p.raw)))
request, err := http.ReadRequest(reader)
if err != nil {
return err
}

if (len(tokens) < 3) {
return errors.New("Error parsing http request")
p.domain, p.port, err = net.SplitHostPort(request.Host)
if err != nil {
p.domain = request.Host
p.port = ""
}

p.method = tokens[0]
url := tokens[1]
p.version = tokens[2]
p.method = request.Method
p.version = request.Proto
p.path = request.URL.Path

if strings.HasPrefix(url, "http://") {
url = strings.Replace(url, "http://", "", 1)
if request.URL.RawQuery != "" {
p.path += "?" + request.URL.RawQuery
}

if strings.HasPrefix(url, "https://") {
url = strings.Replace(url, "https://", "", 1)
if request.URL.RawFragment != "" {
p.path += "#" + request.URL.RawFragment
}
if p.path == "" {
p.path = "/"
}

domain := ""
port := ""
for i := 0; i < len(url); i++ {
if url[i] == ':' {
domain = url[:i]
port = url[i:]
break
}

if url[i] == '/' {
domain = url[:i]
break
}
}
p.domain = domain
p.port = port

path := "/"
for i := 0; i < len(url); i++ {
if url[i] == '/' {
path = url[i:]
break
}
}

p.path = path
request.Body.Close()

return nil
}
3 changes: 1 addition & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func (p *Proxy) Start() {
log.Fatal("Error accepting connection: ", err)
continue
}
// conn.SetDeadLine(time.Now().Add(3 * time.Second))
// conn.SetKeepAlive(false)
conn.SetKeepAlive(false)

go func() {
b, err := conn.ReadBytes()
Expand Down

0 comments on commit d2cb0e9

Please sign in to comment.