mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 06:46:47 +00:00
chore: avoid the request context never being canceled in trusttunnel client
This commit is contained in:
1 parent
314864362a
commit
dd4eb63c00
2 files changed
+44
-54
No files matched your search
@@ -131,37 +131,45 @@ func (c *Client) resetHealthCheckTimer() {
|
||||
c.healthCheckTimer.Reset(DefaultHealthCheckTimeout)
|
||||
}
|
||||
|
||||
func (c *Client) dial(request *http.Request, conn *httpConn, pipeReader *io.PipeReader, pipeWriter *io.PipeWriter) {
|
||||
func (c *Client) roundTrip(request *http.Request, conn *httpConn) {
|
||||
c.startOnce.Do(c.start)
|
||||
trace := &httptrace.ClientTrace{
|
||||
GotConn: func(connInfo httptrace.GotConnInfo) {
|
||||
conn.SetLocalAddr(connInfo.Conn.LocalAddr())
|
||||
conn.SetRemoteAddr(connInfo.Conn.RemoteAddr())
|
||||
},
|
||||
}
|
||||
ctx, cancel := context.WithCancel(c.ctx) // requestCtx must alive during conn not closed
|
||||
timeout := time.AfterFunc(C.DefaultTCPTimeout, cancel) // only cancel when RoundTrip timeout
|
||||
defer timeout.Stop() // RoundTrip already returned, stop the timer
|
||||
request = request.WithContext(httptrace.WithClientTrace(ctx, trace))
|
||||
response, err := c.roundTripper.RoundTrip(request)
|
||||
if err != nil {
|
||||
_ = pipeWriter.CloseWithError(err)
|
||||
_ = pipeReader.CloseWithError(err)
|
||||
conn.setUp(nil, err)
|
||||
} else if response.StatusCode != http.StatusOK {
|
||||
_ = response.Body.Close()
|
||||
err = fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
||||
_ = pipeWriter.CloseWithError(err)
|
||||
_ = pipeReader.CloseWithError(err)
|
||||
conn.setUp(nil, err)
|
||||
} else {
|
||||
c.resetHealthCheckTimer()
|
||||
conn.setUp(response.Body, nil)
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
request.Body = pipeReader
|
||||
*conn = httpConn{
|
||||
writer: pipeWriter,
|
||||
created: make(chan struct{}),
|
||||
}
|
||||
ctx, cancel := context.WithCancel(c.ctx) // requestCtx must alive during conn not closed
|
||||
conn.cancelFn = cancel // cancel ctx when conn closed
|
||||
go func() {
|
||||
timeout := time.AfterFunc(C.DefaultTCPTimeout, cancel) // only cancel when RoundTrip timeout
|
||||
defer timeout.Stop() // RoundTrip already returned, stop the timer
|
||||
trace := &httptrace.ClientTrace{
|
||||
GotConn: func(connInfo httptrace.GotConnInfo) {
|
||||
conn.SetLocalAddr(connInfo.Conn.LocalAddr())
|
||||
conn.SetRemoteAddr(connInfo.Conn.RemoteAddr())
|
||||
},
|
||||
}
|
||||
request = request.WithContext(httptrace.WithClientTrace(ctx, trace))
|
||||
response, err := c.roundTripper.RoundTrip(request)
|
||||
if err != nil {
|
||||
_ = pipeWriter.CloseWithError(err)
|
||||
_ = pipeReader.CloseWithError(err)
|
||||
conn.setUp(nil, err)
|
||||
} else if response.StatusCode != http.StatusOK {
|
||||
_ = response.Body.Close()
|
||||
err = fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
||||
_ = pipeWriter.CloseWithError(err)
|
||||
_ = pipeReader.CloseWithError(err)
|
||||
conn.setUp(nil, err)
|
||||
} else {
|
||||
c.resetHealthCheckTimer()
|
||||
conn.setUp(response.Body, nil)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Client) Dial(ctx context.Context, host string) (net.Conn, error) {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
request := &http.Request{
|
||||
Method: http.MethodConnect,
|
||||
URL: &url.URL{
|
||||
@@ -169,23 +177,16 @@ func (c *Client) Dial(ctx context.Context, host string) (net.Conn, error) {
|
||||
Host: host,
|
||||
},
|
||||
Header: make(http.Header),
|
||||
Body: pipeReader,
|
||||
Host: host,
|
||||
}
|
||||
request.Header.Add("User-Agent", TCPUserAgent)
|
||||
request.Header.Add("Proxy-Authorization", c.auth)
|
||||
conn := &tcpConn{
|
||||
httpConn: httpConn{
|
||||
writer: pipeWriter,
|
||||
created: make(chan struct{}),
|
||||
},
|
||||
}
|
||||
go c.dial(request, &conn.httpConn, pipeReader, pipeWriter)
|
||||
conn := &tcpConn{}
|
||||
c.roundTrip(request, &conn.httpConn)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
request := &http.Request{
|
||||
Method: http.MethodConnect,
|
||||
URL: &url.URL{
|
||||
@@ -193,25 +194,16 @@ func (c *Client) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
Host: UDPMagicAddress,
|
||||
},
|
||||
Header: make(http.Header),
|
||||
Body: pipeReader,
|
||||
Host: UDPMagicAddress,
|
||||
}
|
||||
request.Header.Add("User-Agent", UDPUserAgent)
|
||||
request.Header.Add("Proxy-Authorization", c.auth)
|
||||
conn := &clientPacketConn{
|
||||
packetConn: packetConn{
|
||||
httpConn: httpConn{
|
||||
writer: pipeWriter,
|
||||
created: make(chan struct{}),
|
||||
},
|
||||
},
|
||||
}
|
||||
go c.dial(request, &conn.httpConn, pipeReader, pipeWriter)
|
||||
conn := &clientPacketConn{}
|
||||
c.roundTrip(request, &conn.httpConn)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListenICMP(ctx context.Context) (*IcmpConn, error) {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
request := &http.Request{
|
||||
Method: http.MethodConnect,
|
||||
URL: &url.URL{
|
||||
@@ -219,18 +211,12 @@ func (c *Client) ListenICMP(ctx context.Context) (*IcmpConn, error) {
|
||||
Host: ICMPMagicAddress,
|
||||
},
|
||||
Header: make(http.Header),
|
||||
Body: pipeReader,
|
||||
Host: ICMPMagicAddress,
|
||||
}
|
||||
request.Header.Add("User-Agent", ICMPUserAgent)
|
||||
request.Header.Add("Proxy-Authorization", c.auth)
|
||||
conn := &IcmpConn{
|
||||
httpConn{
|
||||
writer: pipeWriter,
|
||||
created: make(chan struct{}),
|
||||
},
|
||||
}
|
||||
go c.dial(request, &conn.httpConn, pipeReader, pipeWriter)
|
||||
conn := &IcmpConn{}
|
||||
c.roundTrip(request, &conn.httpConn)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@ type httpConn struct {
|
||||
body io.ReadCloser
|
||||
created chan struct{}
|
||||
createErr error
|
||||
cancelFn func()
|
||||
gun.NetAddr
|
||||
|
||||
// deadlines
|
||||
@@ -125,6 +126,9 @@ func (h *httpConn) Close() error {
|
||||
if h.body != nil {
|
||||
errorArr = append(errorArr, h.body.Close())
|
||||
}
|
||||
if h.cancelFn != nil {
|
||||
h.cancelFn()
|
||||
}
|
||||
return errors.Join(errorArr...)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user