-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
190 lines (162 loc) · 7.06 KB
/
app.js
File metadata and controls
190 lines (162 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* RouterOS API Client - Production Example & Reference Implementation
*
* This file serves as a comprehensive reference for implementing RouterOS API
* connections with advanced error handling, connectivity testing, and debugging.
*
* Features demonstrated:
* - ✅ TCP connectivity pre-testing
* - 🔥 Advanced error categorization and handling
* - 📊 Comprehensive event monitoring
* - 🚨 Connection and operation timeouts
* - 🔍 Detailed debug logging and troubleshooting
*
* @version 1.1.0
* @author RouterOS API Client Library
* @example
* // Run this example:
* // node app.js
*/
const RouterOSClient = require("./index");
// RouterOS API connection configuration
// Create API client instance
const api = new RouterOSClient({
host: "192.168.1.2", // Your RouterOS IP
username: "sdworlld", // Your username
password: "Shivam!024@", // Your password
port: 8728, // Default API port
debug: true, // Enable debug mode for detailed logs
});
// Add event listeners for better error tracking
api.on('error', (err) => {
console.error("🔥 RouterOS Client Error Event:", err);
});
api.on('trap', (trap, id) => {
console.error("🪤 RouterOS Trap Event:", { trap, id });
});
api.on('close', () => {
console.log("📡 Connection closed by RouterOS");
});
api.on('timeout', () => {
console.error("⏰ Connection timeout occurred");
});
// Function to test basic connectivity
async function testConnectivity() {
const net = require('net');
return new Promise((resolve, reject) => {
console.log("🔍 Testing basic connectivity...");
const socket = new net.Socket();
socket.setTimeout(5000); // 5 second timeout
socket.connect(api.port, api.host, () => {
console.log("✅ Basic TCP connection successful");
socket.destroy();
resolve(true);
});
socket.on('error', (err) => {
console.error("❌ TCP connection failed:", err.message);
reject(err);
});
socket.on('timeout', () => {
console.error("⏰ TCP connection timeout");
socket.destroy();
reject(new Error('TCP connection timeout'));
});
});
}
// Enhanced error handling function
function handleConnectionError(err) {
console.error("❌ Connection Failed!");
console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
// Log the full error object for debugging
console.error("Full Error Object:", err);
// Check for specific error types and provide helpful messages
if (err.code) {
switch (err.code) {
case 'ECONNREFUSED':
console.error("🚫 Connection Refused:");
console.error(" - Router is not reachable at the specified IP address");
console.error(" - RouterOS API service might be disabled");
console.error(" - Wrong port number (default is 8728)");
console.error(" - Firewall blocking the connection");
break;
case 'ENOTFOUND':
console.error("🔍 Host Not Found:");
console.error(" - Invalid IP address or hostname");
console.error(" - DNS resolution failed");
console.error(" - Network connectivity issues");
break;
case 'ETIMEDOUT':
console.error("⏰ Connection Timeout:");
console.error(" - Router is not responding");
console.error(" - Network latency issues");
console.error(" - Router might be overloaded");
break;
case 'ECONNRESET':
console.error("🔄 Connection Reset:");
console.error(" - Router forcibly closed the connection");
console.error(" - API service might have crashed");
break;
default:
console.error(`🔧 Network Error (${err.code}):`);
console.error(" - Check network connectivity");
console.error(" - Verify router configuration");
}
} else if (err.message) {
// Check for authentication-related errors
if (err.message.includes('login') || err.message.includes('auth') || err.message.includes('password')) {
console.error("🔐 Authentication Failed:");
console.error(" - Wrong username or password");
console.error(" - Account might be disabled");
console.error(" - User might not have API access permissions");
} else if (err.message.includes('trap')) {
console.error("⚠️ RouterOS Trap Error:");
console.error(" - Invalid command or parameters");
console.error(" - Insufficient permissions");
} else if (err.message.includes('timeout')) {
console.error("⏰ Operation Timeout:");
console.error(" - Command took too long to execute");
console.error(" - Router might be busy");
} else {
console.error("❓ Unknown Error:");
console.error(` - ${err.message}`);
}
}
console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
console.error("🔧 Troubleshooting Steps:");
console.error("1. Verify the router IP address and port");
console.error("2. Check if RouterOS API is enabled");
console.error("3. Confirm username and password are correct");
console.error("4. Ensure the user has API access permissions");
console.error("5. Check firewall rules on both router and client");
console.error("6. Test connectivity with ping or telnet");
console.error("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
}
async function example() {
console.log("🔄 Attempting to connect to RouterOS...");
console.log(`📡 Host: ${api.host || 'undefined'}`);
console.log(`👤 Username: ${api.username || 'undefined'}`);
console.log(`🔌 Port: ${api.port || 'undefined'}`);
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
try {
// First test basic TCP connectivity
await testConnectivity();
// Attempt RouterOS API connection with timeout
console.log("🔄 Attempting RouterOS API connection...");
const connectionPromise = api.connect();
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('RouterOS API connection timeout after 10 seconds')), 10000);
});
await Promise.race([connectionPromise, timeoutPromise]);
console.log("✅ RouterOS API connected successfully!");
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
// Get system identity
console.log("🔍 Fetching system identity...");
const identity = await api.send(["/system/identity/print"]);
console.log("🖥️ Router identity:", identity);
await api.close();
console.log("✅ Connection closed successfully!");
} catch (err) {
handleConnectionError(err);
}
}
example();