udphole

Basic UDP wormhole proxy
git clone git://git.finwo.net/app/udphole
Log | Files | Refs | README | LICENSE

basic-forwarding-tcp.js (3410B)


      1 const path = require('path');
      2 const {
      3   spawnDaemon,
      4   killDaemon,
      5   killAllDaemons,
      6   connectApi,
      7   apiCommand,
      8   createUdpEchoServer,
      9   sendUdp,
     10   TIMEOUT
     11 } = require('./helpers');
     12 
     13 const CONFIG_PATH = path.join(__dirname, 'config-tcp.ini');
     14 const API_PORT = 9123;
     15 
     16 async function runTest() {
     17   let daemon = null;
     18   let apiSock = null;
     19   let echoServer = null;
     20   let returnCode = 0;
     21 
     22   console.log('=== Basic Forwarding Test ===');
     23   console.log('Testing: UDP packets are forwarded from listen socket to connect socket\n');
     24 
     25   try {
     26     console.log('1. Spawning daemon...');
     27     daemon = await spawnDaemon(CONFIG_PATH);
     28     console.log(`   Daemon started (PID: ${daemon.pid})`);
     29 
     30     console.log('2. Connecting to API...');
     31     apiSock = await connectApi(API_PORT);
     32     console.log('   Connected to API');
     33 
     34     console.log('3. Authenticating...');
     35     let resp = await apiCommand(apiSock, 'auth', 'finwo', 'testsecret');
     36     console.log(`   Auth response: ${resp}`);
     37     if (resp !== 'OK') throw new Error('Authentication failed');
     38 
     39     console.log('4. Creating session...');
     40     resp = await apiCommand(apiSock, 'session.create', 'test-session', '60');
     41     console.log(`   Session create: ${resp}`);
     42 
     43     console.log('5. Creating listen socket...');
     44     resp = await apiCommand(apiSock, 'session.socket.create.listen', 'test-session', 'client-a');
     45     const listenPort = resp[0];
     46     console.log(`   Listen socket port: ${listenPort}`);
     47 
     48     console.log('6. Starting echo server...');
     49     echoServer = await createUdpEchoServer();
     50     console.log(`   Echo server on port: ${echoServer.port}`);
     51 
     52     console.log('7. Creating connect socket to echo server...');
     53     resp = await apiCommand(apiSock, 'session.socket.create.connect', 'test-session', 'relay', '127.0.0.1', echoServer.port);
     54     console.log(`   Connect socket: ${resp}`);
     55 
     56     console.log('8. Creating forward: client-a -> relay...');
     57     resp = await apiCommand(apiSock, 'session.forward.create', 'test-session', 'client-a', 'relay');
     58     console.log(`   Forward create: ${resp}`);
     59 
     60     console.log('   Waiting for session to initialize...');
     61     await new Promise(r => setTimeout(r, 100));
     62 
     63     console.log('9. Sending UDP packet to listen socket...');
     64     await sendUdp(listenPort, '127.0.0.1', 'hello');
     65     console.log('   Sent "hello"');
     66 
     67     console.log('10. Waiting for echo response...');
     68     const messages = echoServer.getMessages();
     69     const start = Date.now();
     70     while (messages.length === 0 && Date.now() - start < TIMEOUT) {
     71       await new Promise(r => setTimeout(r, 50));
     72     }
     73 
     74     if (messages.length === 0) {
     75       throw new Error('Timeout: no message received by echo server');
     76     }
     77 
     78     const msg = messages[0];
     79     console.log(`    Received: "${msg.data}" from ${msg.rinfo.address}:${msg.rinfo.port}`);
     80 
     81     if (msg.data === 'hello') {
     82       console.log('\n✓ PASS: UDP forwarding works correctly');
     83       console.log('   Packet was forwarded from listen socket to connect socket');
     84       console.log('   and echoed back successfully.');
     85     } else {
     86       throw new Error(`Expected "hello", got "${msg.data}"`);
     87     }
     88 
     89   } catch (err) {
     90     console.error(`\n✗ FAIL: ${err.message}`);
     91     returnCode = 1;
     92   } finally {
     93     if (echoServer) echoServer.socket.close();
     94     if (apiSock) apiSock.end();
     95     if (daemon) await killDaemon(daemon);
     96     await killAllDaemons();
     97     process.exit(returnCode);
     98   }
     99 }
    100 
    101 runTest();