|
1 | 1 | package httpenrollmentconfirmation |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/base32" |
5 | 6 | "net" |
6 | 7 | "net/http" |
7 | 8 | "sync" |
8 | 9 | "time" |
9 | 10 |
|
10 | 11 | "github.com/v2fly/v2ray-core/v5/common" |
11 | | - "github.com/v2fly/v2ray-core/v5/common/signal/done" |
12 | 12 | "github.com/v2fly/v2ray-core/v5/transport/internet/tlsmirror" |
13 | 13 | "github.com/v2fly/v2ray-core/v5/transport/internet/tlsmirror/httponconnection" |
14 | 14 | ) |
@@ -83,31 +83,16 @@ func (c *clientRoundtripper) roundTrip(request *http.Request) (*http.Response, e |
83 | 83 | } |
84 | 84 | defer c.currentConnLock.RUnlock() |
85 | 85 |
|
86 | | - timeoutTimer := time.NewTimer(30 * time.Second) |
87 | | - defer timeoutTimer.Stop() |
88 | | - |
89 | | - waitForConnection := done.New() |
90 | | - |
91 | | - var resp *http.Response |
92 | | - var error_resp_ error |
93 | | - var err error |
94 | | - |
95 | | - go func() { |
96 | | - resp_, err_ := c.currentConn.RoundTrip(request) |
97 | | - resp = resp_ |
98 | | - error_resp_ = err_ |
99 | | - waitForConnection.Close() |
100 | | - }() |
101 | | - |
102 | | - select { |
103 | | - case <-timeoutTimer.C: |
104 | | - err = newError("timeout during enrollment verification round trip") |
105 | | - case <-waitForConnection.Wait(): |
106 | | - err = error_resp_ |
107 | | - } |
| 86 | + timeoutContext, _ := context.WithTimeout(context.Background(), time.Second*30) |
| 87 | + request = request.WithContext(timeoutContext) |
108 | 88 |
|
| 89 | + resp, err := c.currentConn.RoundTrip(request) |
109 | 90 | // Use the current connection to perform the round trip |
110 | 91 | if err != nil { |
| 92 | + if resp != nil && resp.Body != nil { |
| 93 | + resp.Body.Close() |
| 94 | + } |
| 95 | + |
111 | 96 | defer func() { |
112 | 97 | c.currentConnLock.RUnlock() |
113 | 98 | c.currentConnLock.Lock() |
@@ -140,7 +125,7 @@ func (c *clientRoundtripper) createNewConnection() error { |
140 | 125 | c.currentConnInnerConn = conn |
141 | 126 | c.currentConn, err = httponconnection.NewSingleConnectionHTTPTransport(conn, "h2") |
142 | 127 | if err != nil { |
143 | | - conn.Close() // Close the connection if transport creation fails |
| 128 | + _ = conn.Close() // Close the connection if transport creation fails |
144 | 129 | return newError("failed to create HTTP transport: ", err) |
145 | 130 | } |
146 | 131 | return nil |
|
0 commit comments