This commit is contained in:
gospider
2024-04-30 23:17:25 +08:00
parent b342e31849
commit b6e507d5e7
3 changed files with 18 additions and 30 deletions

41
dial.go
View File

@@ -7,8 +7,8 @@ import (
"errors"
"io"
"net"
"net/textproto"
"net/url"
"strings"
"sync"
"time"
@@ -398,36 +398,21 @@ func (obj *DialClient) clientVerifyHttps(ctx context.Context, scheme string, pro
return errors.New("clientVerifyHttps not found port")
}
}
var resp *http.Response
didReadResponse := make(chan struct{}) // closed after CONNECT write+read is done or fails
go func() {
connectReq := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Opaque: addr},
Host: cHost,
Header: hdr,
}
if err = connectReq.Write(conn); err != nil {
return
}
resp, err = http.ReadResponse(bufio.NewReader(conn), connectReq)
close(didReadResponse)
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-didReadResponse:
}
connectReq, err := newRequestWithContext(ctx, http.MethodConnect, &url.URL{Opaque: addr}, nil)
if err != nil {
return
return err
}
connectReq.Header = hdr
connectReq.Host = cHost
if err = connectReq.Write(conn); err != nil {
return err
}
resp, err := readResponse(textproto.NewReader(bufio.NewReader(conn)), connectReq)
if err != nil {
return err
}
if resp.StatusCode != 200 {
_, text, ok := strings.Cut(resp.Status, " ")
if !ok {
return errors.New("unknown status code")
}
return errors.New(text)
return errors.New(resp.Status)
}
return
}

View File

@@ -36,7 +36,11 @@ func (obj *reqTask) inPool() bool {
}
func getKey(ctxData *reqCtxData, req *http.Request) (key string) {
return fmt.Sprintf("%s@%s", getAddr(ctxData.proxy), getAddr(req.URL))
var proxyUser string
if ctxData.proxy != nil {
proxyUser = ctxData.proxy.User.String()
}
return fmt.Sprintf("%s@%s@%s", proxyUser, getAddr(ctxData.proxy), getAddr(req.URL))
}
type roundTripper struct {

View File

@@ -166,7 +166,6 @@ func httpWrite(r *http.Request, w *bufio.Writer, orderHeaders []string) (err err
}
return w.Flush()
}
func newRequestWithContext(ctx context.Context, method string, u *url.URL, body io.Reader) (*http.Request, error) {
req := (&http.Request{}).WithContext(ctx)
if method == "" {