Skip to content

Commit 69330b3

Browse files
committed
[nexus] fix proxy nexus and add debug option
1 parent 7592d72 commit 69330b3

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

extended/src/main/java/io/vproxy/vproxyx/ProxyNexus.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import io.vproxy.msquic.MsQuicUpcall;
99
import io.vproxy.msquic.MsQuicUtils;
1010
import io.vproxy.msquic.QuicCertificateFile;
11+
import io.vproxy.msquic.callback.ListenerCallback;
12+
import io.vproxy.msquic.callback.ListenerCallbackList;
1113
import io.vproxy.msquic.wrap.Configuration;
1214
import io.vproxy.msquic.wrap.Listener;
1315
import io.vproxy.pni.Allocator;
@@ -34,6 +36,7 @@ public class ProxyNexus {
3436
certificate=<cert-pem-path> Certificate used by the QUIC
3537
privatekey=<key-pem-path> Private key used by the QUIC
3638
cacert=<ca-cert-pem-path> Ca-certificate used by the QUIC
39+
debug=<on|off> Enable or disable debug logging
3740
Api:
3841
curl -X POST /apis/v1.0/proxies --data '{"node":"{nodeName}", "target":"{host}:{port}", "listen":"{port}"}'
3942
curl -X GET /apis/v1.0/proxies
@@ -57,6 +60,7 @@ public static void main0(String[] args) throws Exception {
5760
var certificatePath = "";
5861
var privateKeyPath = "";
5962
var cacertPath = "";
63+
var debug = false;
6064
for (var arg : args) {
6165
if (arg.equals("-h") || arg.equals("--help") || arg.equals("-help") || arg.equals("help")) {
6266
System.out.println(HELP_STR);
@@ -105,6 +109,15 @@ public static void main0(String[] args) throws Exception {
105109
privateKeyPath = arg.substring("privatekey=".length()).trim();
106110
} else if (arg.startsWith("cacert=")) {
107111
cacertPath = arg.substring("cacert=".length()).trim();
112+
} else if (arg.startsWith("debug=")) {
113+
var value = arg.substring("debug=".length()).trim();
114+
if (value.equals("on")) {
115+
debug = true;
116+
} else if (value.equals("off")) {
117+
debug = false;
118+
} else {
119+
throw new IllegalArgumentException("debug=" + value + " is not valid");
120+
}
108121
} else {
109122
throw new IllegalArgumentException("unknown argument: " + arg);
110123
}
@@ -197,7 +210,7 @@ public static void main0(String[] args) throws Exception {
197210

198211
var nexus = new Nexus();
199212
var resources = new ResHolder();
200-
var nctx = new NexusContext(nodeName, nexus, resources, loop, reg, clientConf, serverConf);
213+
var nctx = new NexusContext(nodeName, nexus, resources, loop, reg, clientConf, serverConf, debug);
201214

202215
var self = new NexusNode(nodeName, null);
203216
nexus.setSelfNode(self);
@@ -208,7 +221,11 @@ public static void main0(String[] args) throws Exception {
208221

209222
if (serverPort > 0) {
210223
var listenerAllocator = Allocator.ofUnsafe();
211-
var lsn = new Listener(new Listener.Options(reg, listenerAllocator, new NexusQuicListenerCallback(nctx),
224+
ListenerCallback cb = new NexusQuicListenerCallback(nctx);
225+
if (debug) {
226+
cb = ListenerCallbackList.withLog(cb);
227+
}
228+
var lsn = new Listener(new Listener.Options(reg, listenerAllocator, cb,
212229
ref -> reg.opts.registrationQ.openListener(
213230
MsQuicUpcall.listenerCallback, ref.MEMORY, retCode, listenerAllocator
214231
)));

extended/src/main/java/io/vproxy/vproxyx/nexus/NexusContext.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ public class NexusContext {
1616
public final Registration registration;
1717
public final Configuration clientConfiguration;
1818
public final Configuration serverConfiguration;
19+
public final boolean debug;
1920

2021
public NexusContext(String selfNodeName, Nexus nexus, ResHolder resources, NetEventLoop loop, Registration registration,
21-
Configuration clientConfiguration, Configuration serverConfiguration) {
22+
Configuration clientConfiguration, Configuration serverConfiguration,
23+
boolean debug) {
2224
this.selfNodeName = selfNodeName;
2325
this.nexus = nexus;
2426
this.resources = resources;
2527
this.loop = loop;
2628
this.registration = registration;
2729
this.clientConfiguration = clientConfiguration;
2830
this.serverConfiguration = serverConfiguration;
31+
this.debug = debug;
2932
}
3033
}

extended/src/main/java/io/vproxy/vproxyx/nexus/NexusPeer.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.vproxy.base.util.coll.RingQueue;
88
import io.vproxy.msquic.*;
99
import io.vproxy.msquic.callback.ConnectionCallback;
10+
import io.vproxy.msquic.callback.ConnectionCallbackList;
1011
import io.vproxy.msquic.wrap.Connection;
1112
import io.vproxy.msquic.wrap.Listener;
1213
import io.vproxy.pni.Allocator;
@@ -41,8 +42,15 @@ public static int createAccepted(NexusContext nctx, IPPort remote,
4142
QuicConnection connQ, Listener listener, QuicListenerEventNewConnection data, Allocator allocator) {
4243
var peer = new NexusPeer(nctx, remote);
4344
peer.isServer = true;
44-
peer.quicConn = new Connection(new Connection.Options(listener, allocator, peer.new NexusNodeConnectionCallback(), connQ));
45+
ConnectionCallback cb = peer.new NexusNodeConnectionCallback();
46+
if (nctx.debug) {
47+
cb = ConnectionCallbackList.withLog(cb, true);
48+
}
49+
peer.quicConn = new Connection(new Connection.Options(listener, allocator, cb, connQ));
4550
peer.quicConn.setConnectionInfo(data);
51+
if (nctx.debug) {
52+
peer.quicConn.enableTlsSecretDebug();
53+
}
4654
connQ.setCallbackHandler(MsQuicUpcall.connectionCallback, peer.quicConn.ref.MEMORY);
4755
var err = connQ.setConfiguration(nctx.serverConfiguration.opts.configurationQ);
4856
if (err != 0) {
@@ -76,8 +84,11 @@ private void doConnect() {
7684
Connection conn;
7785
try (var tmpAllocator = Allocator.ofConfined()) {
7886
var returnStatus = new IntArray(tmpAllocator, 1);
79-
conn = new Connection(new Connection.Options(nctx.registration, allocator,
80-
new NexusNodeConnectionCallback(),
87+
ConnectionCallback cb = new NexusNodeConnectionCallback();
88+
if (nctx.debug) {
89+
cb = ConnectionCallbackList.withLog(cb, true);
90+
}
91+
conn = new Connection(new Connection.Options(nctx.registration, allocator, cb,
8192
ref -> nctx.registration.opts.registrationQ.openConnection(
8293
MsQuicUpcall.connectionCallback, ref.MEMORY, returnStatus, allocator
8394
)));
@@ -87,6 +98,9 @@ private void doConnect() {
8798
return;
8899
}
89100
}
101+
if (nctx.debug) {
102+
conn.enableTlsSecretDebug();
103+
}
90104
int errcode = conn.start(nctx.clientConfiguration, remoteAddress);
91105
if (errcode != 0) {
92106
Logger.error(LogType.CONN_ERROR, "starting quic connection to " + remoteAddress + " failed, errcode=" + errcode);
@@ -180,7 +194,7 @@ private void initializeServerActiveControlStream() {
180194
}
181195
QuicSocketFD fd;
182196
try {
183-
fd = QuicSocketFD.newStream(quicConn);
197+
fd = QuicSocketFD.newStream(nctx.debug, quicConn);
184198
} catch (IOException e) {
185199
Logger.error(LogType.CONN_ERROR, "failed to initiate quic stream to " + quicConn.getRemoteAddress(), e);
186200
terminate(quicConn, "failed to initiate quic stream");
@@ -200,7 +214,7 @@ public int connected(Connection conn, QuicConnectionEventConnected data) {
200214
nctx.loop.getSelectorEventLoop().nextTick(() -> {
201215
QuicSocketFD fd;
202216
try {
203-
fd = QuicSocketFD.newStream(conn);
217+
fd = QuicSocketFD.newStream(nctx.debug, conn);
204218
} catch (IOException e) {
205219
Logger.error(LogType.SOCKET_ERROR, "failed to create stream from " + conn, e);
206220
conn.close();
@@ -219,7 +233,7 @@ public int shutdownComplete(Connection conn, QuicConnectionEventConnectionShutdo
219233

220234
@Override
221235
public int peerStreamStarted(Connection conn, QuicConnectionEventPeerStreamStarted data) {
222-
var fd = QuicSocketFD.wrapAcceptedStream(conn, data.getStream());
236+
var fd = QuicSocketFD.wrapAcceptedStream(nctx.debug, conn, data.getStream());
223237
if (isInitialized) {
224238
StreamHandlers.INSTANCE.handleAccepted(nctx, NexusPeer.this, fd);
225239
} else if (isServer) {

extended/src/main/java/io/vproxy/vproxyx/nexus/StreamHandlers.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ object StreamHandlers {
148148

149149
val quicConn = nextNode.peer.quicConnection ?: return resultCallback(400, "node $nextNode is disconnected")
150150

151-
val nextFD = QuicSocketFD.newStream(quicConn)
151+
val nextFD = QuicSocketFD.newStream(nctx.debug, quicConn)
152152
nextConn = ConnectableConnection.wrap(
153153
nextFD, nextNode.peer.remoteAddress, ConnectionOpts().setTimeout(NexusContext.GENERAL_TIMEOUT),
154154
RingBuffer.allocateDirect(4096), RingBuffer.allocateDirect(4096)
@@ -406,7 +406,6 @@ object StreamHandlers {
406406
Logger.warn(LogType.INVALID_EXTERNAL_DATA, "unexpected request from ${httpconn.conn.remote()}: ${req.method} ${req.uri}")
407407
httpconn.response(404).send()
408408
}
409-
handleLinkStatusAdvertisement(nctx, httpconn, req)
410409
}
411410

412411
private suspend fun handleLinkStatusAdvertisement(nctx: NexusContext, httpconn: CoroutineHttp1ServerConnection, req: Request) {

0 commit comments

Comments
 (0)