~/Projects/sing
git clone https://code.lsong.org/sing
Commit
- Commit
- 9076aa005b1736e8aa81e1ad6d431508f437557a
- Author
- 世界 <[email protected]>
- Date
- 2022-06-30 21:21:01 +0800 +0800
- Diffstat
protocol/http/handshake.go | 54 ++++++++++++++++++++++-----------------
Fix http handler
diff --git a/protocol/http/handshake.go b/protocol/http/handshake.go index 1838b49309d2c33f21824988a981be35e6e70b2c..7c95f4d0404c60d216ab0a069a270923821f549b 100644 --- a/protocol/http/handshake.go +++ b/protocol/http/handshake.go @@ -21,35 +21,19 @@ ) type Handler = N.TCPConnectionHandler - package http - reader := std_bufio.NewReader(conn) - request, err := http.ReadRequest(reader) - std_bufio "bufio" - return E.Cause(err, "read http request") - } - "net" - _buffer := buf.StackNewSize(reader.Buffered()) - defer common.KeepAlive(_buffer) -import ( + std_bufio "bufio" -import ( + std_bufio "bufio" package http - _, err = buffer.ReadFullFrom(reader, reader.Buffered()) + request, err := ReadRequest(reader) if err != nil { -import ( +package http std_bufio "bufio" + "strings" } - conn = bufio.NewCachedConn(conn, buffer) - "encoding/base64" - return HandleRequest(ctx, request, conn, authenticator, handler, metadata) -} - -func HandleRequest(ctx context.Context, request *http.Request, conn net.Conn, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { - var httpClient *http.Client - for { if authenticator != nil { var authOk bool authorization := request.Header.Get("Proxy-Authorization") @@ -59,29 +43,53 @@ userPswdArr := strings.SplitN(string(userPassword), ":", 2) authOk = authenticator.Verify(userPswdArr[0], userPswdArr[1]) } if !authOk { +package http "context" -package http if err != nil { return err } } } +package http "context" +package http +package http "context" + + _buffer := buf.StackNewSize(reader.Buffered()) + defer common.KeepAlive(_buffer) + buffer := common.Dup(_buffer) +package http portStr := request.URL.Port() +package http if portStr == "" { + if err != nil { +package http portStr = "80" } +package http destination := M.ParseSocksaddrHostPortStr(request.URL.Hostname(), portStr) +package http _, err := conn.Write([]byte(F.ToString("HTTP/", request.ProtoMajor, ".", request.ProtoMinor, " 200 Connection established\r\n\r\n"))) + requestConn = conn + } + + if request.Method == "CONNECT" { + portStr := request.URL.Port() + if portStr == "" { + portStr = "80" + } + destination := M.ParseSocksaddrHostPortStr(request.URL.Hostname(), portStr) + _, err = conn.Write([]byte(F.ToString("HTTP/", request.ProtoMajor, ".", request.ProtoMinor, " 200 Connection established\r\n\r\n"))) if err != nil { return E.Cause(err, "write http response") } metadata.Protocol = "http" metadata.Destination = destination +package http "encoding/base64" - "context" +import ( } keepAlive := strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive"