Skip to content

Commit 1496488

Browse files
committed
debug info
Signed-off-by: zhuangbowei.zbw <zhuangbowei.zbw@alibaba-inc.com>
1 parent 800d449 commit 1496488

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

net/http/client.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,44 +131,41 @@ class PooledDialer {
131131
// Factory: full CONNECT tunnel handshake sequence.
132132
// DNS resolve proxy -> TCP -> [TLS to proxy] -> CONNECT handshake -> TLS to target.
133133
ISocketStream* make_tunnel_stream(const StoredURL& proxy_url, std::string_view target_host,
134-
uint16_t target_port, const Headers* headers, uint64_t timeout) {
134+
uint16_t target_port, const Headers* headers) {
135+
LOG_DEBUG("CONNECT tunnel: begin, proxy=`:` target=`:`",
136+
proxy_url.host_no_port(), proxy_url.port(), target_host, target_port);
135137
auto proxy_ip = resolver->resolve(proxy_url.host_no_port());
136138
if (proxy_ip.undefined())
137139
LOG_ERROR_RETURN(ENOENT, nullptr, "CONNECT tunnel: DNS resolve failed for proxy `", proxy_url.host_no_port());
138140
EndPoint proxy_ep(proxy_ip, proxy_url.port());
139-
tcpsock->timeout(timeout);
140141
auto stream = tcpsock->connect(proxy_ep);
141142
if (!stream) {
142143
resolver->discard_cache(proxy_url.host_no_port(), proxy_ip);
143144
LOG_ERRNO_RETURN(0, nullptr, "CONNECT tunnel: failed to connect to proxy");
144145
}
145-
stream->timeout(timeout);
146+
LOG_DEBUG("CONNECT tunnel: connected to proxy `", proxy_ep);
146147
if (proxy_url.secure()) {
147-
auto tls = new_tls_stream(tls_ctx, stream, SecurityRole::Client, true);
148-
if (!tls) {
149-
delete stream;
148+
stream = new_tls_stream(tls_ctx, stream, SecurityRole::Client, true);
149+
if (!stream)
150150
LOG_ERRNO_RETURN(0, nullptr, "CONNECT tunnel: TLS to proxy failed");
151-
}
152-
stream = tls;
153151
tls_stream_set_hostname(stream, std::string(proxy_url.host_no_port()).c_str());
154152
}
155-
if (do_connect_handshake(stream, target_host, target_port, headers, timeout) != 0) {
153+
if (do_connect_handshake(stream, target_host, target_port, headers) != 0) {
156154
delete stream;
157155
return nullptr;
158156
}
159-
auto tls = new_tls_stream(tls_ctx, stream, SecurityRole::Client, true);
160-
if (!tls) {
161-
delete stream;
157+
LOG_DEBUG("CONNECT tunnel: handshake done, wrapping TLS to target `", target_host);
158+
stream = new_tls_stream(tls_ctx, stream, SecurityRole::Client, true);
159+
if (!stream)
162160
LOG_ERRNO_RETURN(0, nullptr, "CONNECT tunnel: TLS to target failed");
163-
}
164-
stream = tls;
165161
tls_stream_set_hostname(stream, std::string(target_host).c_str());
162+
LOG_DEBUG("CONNECT tunnel: established to target `", target_host);
166163
return stream;
167164
}
168165

169166
// Send CONNECT request and verify 2xx response.
170167
int do_connect_handshake(ISocketStream* stream, std::string_view target_host,
171-
uint16_t target_port, const Headers* headers, uint64_t timeout) {
168+
uint16_t target_port, const Headers* headers) {
172169
char buf[4096];
173170
estring authority;
174171
authority.appends(target_host, ":", target_port);
@@ -179,14 +176,16 @@ class PooledDialer {
179176
}
180177
if (req.send_header(stream) < 0)
181178
LOG_ERRNO_RETURN(0, -1, "CONNECT tunnel: failed to send CONNECT request");
179+
LOG_DEBUG("CONNECT tunnel: sent CONNECT `", authority);
182180

183181
// Response needs room for receive_bytes' MAX_TRANSFER_BYTES + RESERVED_INDEX_SIZE.
184182
char rbuf[8192];
185183
Response resp(rbuf, (uint16_t)sizeof(rbuf));
186184
resp.reset(stream, false);
187-
if (resp.receive_header(timeout) != 0)
185+
if (resp.receive_header() != 0)
188186
LOG_ERRNO_RETURN(0, -1, "CONNECT tunnel: failed to read proxy response");
189187
auto sc = resp.status_code();
188+
LOG_DEBUG("CONNECT tunnel: proxy response status `", sc);
190189
if (sc < 200 || sc >= 300)
191190
LOG_ERROR_RETURN(EINVAL, -1, "CONNECT tunnel: proxy returned status `", sc);
192191
return 0;
@@ -200,7 +199,7 @@ ISocketStream* PooledDialer::dial(Client::Operation* op, Timeout tmo) {
200199
if (proxy && op->req.secure()) {
201200
auto key = make_tunnel_key(*proxy, op->req.host_no_port(), op->req.port(), &op->proxy_header);
202201
return tcp_pool->connect(key, [&]() -> ISocketStream* {
203-
return make_tunnel_stream(*proxy, op->req.host_no_port(), op->req.port(), &op->proxy_header, tmo.timeout());
202+
return make_tunnel_stream(*proxy, op->req.host_no_port(), op->req.port(), &op->proxy_header);
204203
});
205204
}
206205

net/http/server.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,25 +302,29 @@ class ForwardProxyHandler: public ProxyHandler {
302302
{this, &default_forward_proxy_modifier},
303303
client, client_ownership) {}
304304

305-
int tunnel_copy(ISocketStream *src, ISocketStream *dst) {
305+
int tunnel_copy(ISocketStream *src, ISocketStream *dst, const char* tag) {
306306
const size_t buf_size = 65536;
307307
char seg_buf[buf_size + 4096];
308308
char *aligned_buf = (char*) (((uint64_t)(&seg_buf[0]) + 4095) / 4096 * 4096);
309309

310+
LOG_DEBUG("tunnel_copy ` start", tag);
310311
while (true) {
312+
LOG_DEBUG("tunnel_copy ` waiting recv", tag);
311313
ssize_t rc = src->recv(aligned_buf, buf_size);
314+
LOG_DEBUG("tunnel_copy ` recv=`", tag, rc);
312315
if (rc == 0) {
313-
LOG_DEBUG("end of stream");
316+
LOG_DEBUG("tunnel_copy ` end of stream", tag);
314317
break;
315318
}
316319
if (rc < 0) {
317-
LOG_ERRNO_RETURN(0, -1, "read src stream failed");
320+
LOG_ERRNO_RETURN(0, -1, "tunnel_copy ` read src stream failed", tag);
318321
}
319322
ssize_t wc = dst->write(aligned_buf, rc);
323+
LOG_DEBUG("tunnel_copy ` write=` recv=`", tag, wc, rc);
320324
if (wc != rc)
321-
LOG_ERRNO_RETURN(0, -1, "write dst stream failed", VALUE(wc), VALUE(rc));
325+
LOG_ERRNO_RETURN(0, -1, "tunnel_copy ` write dst stream failed", tag, VALUE(wc), VALUE(rc));
322326
}
323-
327+
LOG_DEBUG("tunnel_copy ` exit", tag);
324328
return 0;
325329
}
326330

@@ -344,17 +348,19 @@ class ForwardProxyHandler: public ProxyHandler {
344348
DEFER(delete server_stream);
345349

346350
auto client_stream = req.get_socket_stream();
351+
LOG_DEBUG("CONNECT: target ` connected, sending 200", req.target());
347352
resp.set_result(200, "Connection Established");
348353
resp.send();
354+
LOG_DEBUG("CONNECT: 200 Connection Established sent, starting tunnel");
349355
bool stopped = false;
350356

351357
auto th = photon::thread_enable_join(photon::thread_create11([&, other=photon::CURRENT]{
352-
tunnel_copy(server_stream, client_stream);
358+
tunnel_copy(server_stream, client_stream, "downlink(target->client)");
353359
if (!stopped)
354360
photon::thread_interrupt(other, ECANCELED);
355361
}));
356362

357-
tunnel_copy(client_stream, server_stream);
363+
tunnel_copy(client_stream, server_stream, "uplink(client->target)");
358364
stopped = true;
359365

360366
photon::thread_interrupt((thread*)th, ECANCELED);

net/security-context/tls-stream.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,18 @@ class TLSSocketStream : public ForwardSocketStream {
477477
#endif
478478

479479
static int ssbio_bwrite(BIO* b, const char* buf, int cnt) {
480+
LOG_DEBUG("TLS BIO write enter: req=`", cnt);
480481
auto ret = get_bio_sockstream(b)->write(buf, cnt);
481482
PRESERVE_FP_ACROSS_YIELD(ret);
483+
LOG_DEBUG("TLS BIO write exit: req=` ret=`", cnt, ret);
482484
return ret;
483485
}
484486

485487
static int ssbio_bread(BIO* b, char* buf, int cnt) {
488+
LOG_DEBUG("TLS BIO read enter: req=`", cnt);
486489
auto ret = get_bio_sockstream(b)->recv(buf, cnt);
487490
PRESERVE_FP_ACROSS_YIELD(ret);
491+
LOG_DEBUG("TLS BIO read exit: req=` ret=`", cnt, ret);
488492
return ret;
489493
}
490494
#undef PRESERVE_FP_ACROSS_YIELD

0 commit comments

Comments
 (0)