Skip to content

Commit c1e5f81

Browse files
committed
ipn/pxclient: m debug logs
1 parent 5fa6a7a commit c1e5f81

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

intra/ipn/pxclient.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
x "github.com/celzero/firestack/intra/backend"
2424
"github.com/celzero/firestack/intra/core"
2525
"github.com/celzero/firestack/intra/dialers"
26+
"github.com/celzero/firestack/intra/log"
2627
)
2728

2829
const (
@@ -50,6 +51,8 @@ type proxyClient struct {
5051
p Proxy
5152
}
5253

54+
var _ x.Client = (*proxyClient)(nil)
55+
5356
func newProxyClient(p Proxy) x.Client {
5457
return &proxyClient{p: p}
5558
}
@@ -81,12 +84,12 @@ func fetchIPMetadata(p Proxy, network string) (*x.IPMetadata, error) {
8184
applyMullvad(meta, mull)
8285
meta.ProviderURL = mullvadURL
8386
} else {
84-
perr := fmt.Errorf("proxy: %s ip lookup failed", idstr(p))
87+
perr := fmt.Errorf("proxy: client: %s ip lookup failed", idstr(p))
8588
return nil, core.JoinErr(perr, err1, err2, err3)
8689
}
8790

8891
if len(meta.IP) <= 0 {
89-
return nil, fmt.Errorf("proxy: %s ip lookup failed", idstr(p))
92+
return nil, fmt.Errorf("proxy: client: %s ip lookup failed", idstr(p))
9093
}
9194

9295
return meta, nil
@@ -132,7 +135,7 @@ func fetchTrace(p Proxy, network string) (map[string]string, error) {
132135
}
133136

134137
if len(kv) == 0 {
135-
return nil, errors.New("empty trace response")
138+
return nil, errors.New("proxy: client: empty trace response")
136139
}
137140

138141
return kv, nil
@@ -179,7 +182,7 @@ func fetchWarp(p Proxy, network string) (*warpResp, error) {
179182
}
180183

181184
if resp.IP == "" {
182-
return nil, errors.New("empty warp response")
185+
return nil, errors.New("proxy: client: empty warp response")
183186
}
184187

185188
return &resp, nil
@@ -220,7 +223,7 @@ func fetchMullvad(p Proxy, network, url string) (*mullvadResp, error) {
220223
}
221224

222225
if resp.IP == "" {
223-
return nil, errors.New("empty mullvad response")
226+
return nil, errors.New("proxy: client: empty mullvad response")
224227
}
225228

226229
return &resp, nil
@@ -286,25 +289,30 @@ func fetch(p Proxy, network, rawurl string) ([]byte, error) {
286289
ctx, cancel := context.WithTimeout(context.Background(), httpTimeout)
287290
defer cancel()
288291

289-
client := httpClient(p, network, parsed)
290292
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawurl, nil)
291293
if err != nil {
292294
return nil, err
293295
}
294296

297+
log.VV("proxy: client: %s fetching %s via %s...", idstr(p), rawurl, network)
298+
299+
// TODO: pool clients
300+
client := httpClient(p, network, parsed)
295301
resp, err := client.Do(req)
296302
if err != nil {
297303
return nil, err
298304
}
299305
if resp == nil {
300-
return nil, errors.New("ip lookup: nil response")
306+
return nil, errors.New("proxy: client: ip lookup nil response")
301307
}
302-
defer resp.Body.Close()
308+
defer core.Close(resp.Body)
303309

304310
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
305-
return nil, fmt.Errorf("ip lookup %s: status %s", rawurl, resp.Status)
311+
return nil, fmt.Errorf("proxy: client: ip lookup %s: status %s", rawurl, resp.Status)
306312
}
307313

314+
log.VV("proxy: client: %s fetched %s via %s with status %s; reading body...", idstr(p), rawurl, network, resp.Status)
315+
308316
data, err := io.ReadAll(io.LimitReader(resp.Body, maxIPBodySize))
309317
if err != nil {
310318
return nil, err
@@ -358,12 +366,16 @@ func httpClient(p Proxy, network string, u *url.URL) *http.Client {
358366
return nil, errNoSuitableAddress
359367
}
360368

369+
log.VV("proxy: client: %s resolved %s to %v on port %d for %s", idstr(p), host, filtered, on, network)
370+
361371
var lastErr error
362372
for _, ip := range filtered {
363373
dest := netip.AddrPortFrom(ip, uint16(on)).String()
364374
if conn, err := p.Dial(network, dest); err == nil {
375+
log.VV("proxy: client: %s dialed %s @ %s on %s", idstr(p), host, dest, network)
365376
return conn, nil
366377
} else {
378+
log.E("proxy: client: %s failed to dial %s @ %s on %s: %v", idstr(p), host, dest, network, err)
367379
lastErr = err
368380
}
369381
}
@@ -373,8 +385,8 @@ func httpClient(p Proxy, network string, u *url.URL) *http.Client {
373385
}
374386
return nil, lastErr
375387
},
376-
TLSHandshakeTimeout: httpTimeout,
377-
ResponseHeaderTimeout: httpTimeout,
388+
TLSHandshakeTimeout: httpTimeout / 2,
389+
ResponseHeaderTimeout: httpTimeout - 2,
378390
DisableKeepAlives: true,
379391
ForceAttemptHTTP2: true,
380392
},
@@ -389,5 +401,5 @@ func (h *socks5) Client() x.Client { return newProxyClient(h) }
389401
func (h *http1) Client() x.Client { return newProxyClient(h) }
390402
func (h *wgproxy) Client() x.Client { return newProxyClient(h) }
391403
func (h *seproxy) Client() x.Client { return newProxyClient(h) }
392-
func (t *pipws) Client() x.Client { return newProxyClient(t) }
393-
func (t *piph2) Client() x.Client { return newProxyClient(t) }
404+
func (h *pipws) Client() x.Client { return newProxyClient(h) }
405+
func (h *piph2) Client() x.Client { return newProxyClient(h) }

0 commit comments

Comments
 (0)