appium-ios-tuntap 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { TunTap } from '../lib/index.js';
4
+ import { log } from '../lib/logger.js';
5
+
6
+ // Enable debug logging
7
+ process.argv.push('--debug');
8
+
9
+ async function main() {
10
+ console.log('TunTap Bridge Test Utility');
11
+ console.log('=========================');
12
+ console.log('Platform:', process.platform);
13
+ console.log('Node.js version:', process.version);
14
+ console.log('User:', process.env.USER);
15
+ console.log('Running as root:', process.getuid && process.getuid() === 0 ? 'Yes' : 'No');
16
+ console.log('');
17
+
18
+ try {
19
+ // Step 1: Create and open TUN device
20
+ console.log('Step 1: Creating TUN device...');
21
+ const tun = new TunTap();
22
+
23
+ const success = tun.open();
24
+ if (!success) {
25
+ throw new Error('Failed to open TUN device');
26
+ }
27
+
28
+ console.log(`✓ Successfully opened TUN device: ${tun.name}`);
29
+ console.log(` Device file descriptor: ${tun.fd}`);
30
+
31
+ // Step 2: Configure the interface
32
+ console.log('\nStep 2: Configuring interface...');
33
+ try {
34
+ await tun.configure('fd00::1', 1500);
35
+ console.log('✓ Successfully configured interface');
36
+ } catch (err) {
37
+ console.error('✗ Failed to configure interface:', err.message);
38
+
39
+ if (process.platform === 'linux') {
40
+ // Check for common Linux issues
41
+ try {
42
+ const { exec } = await import('child_process');
43
+ const util = await import('util');
44
+ const execPromise = util.promisify(exec);
45
+
46
+ // Check if TUN module is loaded
47
+ try {
48
+ const { stdout } = await execPromise('lsmod | grep tun');
49
+ console.log(' TUN kernel module is loaded:', stdout.trim());
50
+ } catch (e) {
51
+ console.log(' TUN kernel module is NOT loaded. Try: sudo modprobe tun');
52
+ }
53
+
54
+ // Check if iproute2 is installed
55
+ try {
56
+ const { stdout } = await execPromise('which ip');
57
+ console.log(' ip command is available at:', stdout.trim());
58
+ } catch (e) {
59
+ console.log(' ip command is NOT available. Try: sudo apt install iproute2');
60
+ }
61
+
62
+ // Check permissions on /dev/net/tun
63
+ try {
64
+ const { stdout } = await execPromise('ls -la /dev/net/tun');
65
+ console.log(' /dev/net/tun permissions:', stdout.trim());
66
+ } catch (e) {
67
+ console.log(' Could not check /dev/net/tun permissions:', e.message);
68
+ }
69
+ } catch (diagErr) {
70
+ console.error(' Error running diagnostics:', diagErr);
71
+ }
72
+ }
73
+
74
+ throw err;
75
+ }
76
+
77
+ // Step 3: Add a route
78
+ console.log('\nStep 3: Adding route...');
79
+ try {
80
+ await tun.addRoute('fd00::/64');
81
+ console.log('✓ Successfully added route');
82
+ } catch (err) {
83
+ console.error('✗ Failed to add route:', err.message);
84
+ throw err;
85
+ }
86
+
87
+ // Step 4: Test read/write
88
+ console.log('\nStep 4: Testing read/write (will timeout after 5 seconds)...');
89
+ console.log(' Waiting for data on the TUN interface...');
90
+
91
+ // Set up a timeout to end the test after 5 seconds
92
+ const timeout = setTimeout(() => {
93
+ console.log(' No data received within timeout period (this is normal if no traffic is being sent)');
94
+ console.log('\nTest completed successfully!');
95
+ console.log('If you encountered any errors, please check the README.md troubleshooting section.');
96
+
97
+ // Clean up
98
+ tun.close();
99
+ process.exit(0);
100
+ }, 5000);
101
+
102
+ // Try to read data from the TUN device
103
+ const readInterval = setInterval(() => {
104
+ try {
105
+ const data = tun.read(4096);
106
+ if (data && data.length > 0) {
107
+ console.log(` Received ${data.length} bytes from TUN interface`);
108
+
109
+ // Echo the data back
110
+ const bytesWritten = tun.write(data);
111
+ console.log(` Wrote ${bytesWritten} bytes back to TUN interface`);
112
+
113
+ // Clear the timeout and interval
114
+ clearTimeout(timeout);
115
+ clearInterval(readInterval);
116
+
117
+ console.log('\nTest completed successfully!');
118
+ console.log('If you want to generate traffic on this interface, try:');
119
+ if (process.platform === 'darwin') {
120
+ console.log(` ping6 -c 3 fd00::1%${tun.name}`);
121
+ } else {
122
+ console.log(` ping6 -c 3 fd00::1 -I ${tun.name}`);
123
+ }
124
+
125
+ // Keep the test running for a bit longer
126
+ setTimeout(() => {
127
+ tun.close();
128
+ process.exit(0);
129
+ }, 10000);
130
+ }
131
+ } catch (err) {
132
+ console.error(' Error reading from TUN interface:', err.message);
133
+ clearTimeout(timeout);
134
+ clearInterval(readInterval);
135
+ tun.close();
136
+ process.exit(1);
137
+ }
138
+ }, 100);
139
+
140
+ } catch (err) {
141
+ console.error('\nTest failed:', err.message);
142
+ console.log('Please check the README.md troubleshooting section for solutions.');
143
+ process.exit(1);
144
+ }
145
+ }
146
+
147
+ main().catch(err => {
148
+ console.error('Unhandled error:', err);
149
+ process.exit(1);
150
+ });