UNPKG

ros-client

Version:

Node.js client for MikroTik RouterOS API with support for plain text and encrypted connections

248 lines (175 loc) โ€ข 7.26 kB
# RouterOS API Client ๐ŸŒ [![npm version](https://img.shields.io/npm/v/ros-client.svg?style=flat-square)](https://www.npmjs.org/package/ros-client) [![Build Status](https://img.shields.io/travis/SDWORLLD/ros-client/master.svg?style=flat-square)](https://travis-ci.org/SDWORLLD/ros-client) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT) A powerful Node.js module for seamless interaction with MikroTik RouterOS API, supporting both plain text (port 8728) and encrypted (port 8729) connections. ๐Ÿš€ ## ๐Ÿ†• What's New in v1.1.0 - ๐Ÿ“š **Comprehensive Command Reference** - Complete RouterOS API command reference in `Refrence.js` - ๐Ÿงช **Production Example** - Enhanced `app.js` with advanced error handling and connectivity testing - ๐Ÿ”ง **Improved Buffer Management** - Enhanced buffer handling for better performance - ๐Ÿ“– **Enhanced Documentation** - Better examples and troubleshooting guides - ๐Ÿ’ฌ **Community Support** - Telegram support group for community assistance ## โœจ Key Features - ๐Ÿ”’ Secure connections via plain text and TLS encryption - ๐Ÿ”„ Full support for RouterOS API protocol encoding/decoding - โšก Modern Promise-based API implementation - ๐Ÿ› ๏ธ Comprehensive MikroTik API command helpers - ๐Ÿ› Robust error handling mechanism - ๐Ÿ” Built-in debug mode for troubleshooting - ๐Ÿ“ฆ Lightweight with minimal dependencies ## ๐Ÿ“ฆ Installation ```bash npm install ros-client ``` ## ๐Ÿš€ Quick Start ```javascript const RouterOSClient = require("ros-client"); // Create API client instance const api = new RouterOSClient({ host: "192.168.88.1", username: "admin", password: "your-password", port: 8728, // Use 8729 for TLS tls: false, // Set to true for encrypted connection }); async function example() { try { await api.connect(); console.log("โœ… Connected successfully!"); // Get system identity const identity = await api.send(["/system/identity/print"]); console.log("๐Ÿ–ฅ๏ธ Router identity:", identity); // Get all interfaces const interfaces = await api.send(["/interface/print"]); console.log("๐ŸŒ Interfaces:", interfaces); await api.close(); } catch (err) { console.error("โŒ Error:", err.message); } } example(); ``` ## ๐Ÿ“š Documentation & Examples ### ๐Ÿ“– Command Reference For a comprehensive list of all available RouterOS API commands with detailed documentation, see the [**Refrence.js**](./Refrence.js) file. This file contains: - ๐Ÿ”ง **System Commands** - Identity, resources, clock, logging, etc. - ๐ŸŒ **Interface Management** - Ethernet, wireless, bridge, VLAN, etc. - ๐ŸŒ **IP Configuration** - Addresses, routes, DNS, DHCP, firewall, etc. - ๐Ÿ“ถ **Wireless Operations** - Registration, scanning, security, etc. - ๐Ÿ”’ **Security Features** - Firewall rules, NAT, user management, etc. - ๐Ÿ“Š **Monitoring Tools** - Traffic, queues, logs, statistics, etc. ### ๐Ÿงช Complete Example For a production-ready example with advanced error handling and connectivity testing, see the [**app.js**](./app.js) file which demonstrates: - โœ… **Connection Testing** - TCP connectivity validation - ๐Ÿ”ฅ **Advanced Error Handling** - Detailed error categorization and troubleshooting - ๐Ÿ“Š **Event Monitoring** - Comprehensive event listeners - ๐Ÿšจ **Timeout Management** - Connection and operation timeouts - ๐Ÿ” **Debug Logging** - Detailed connection and operation logging ## โš™๏ธ Configuration Options ```javascript const api = new RouterOSClient({ host: "192.168.88.1", // Router IP address username: "admin", // Username password: "password", // Password port: 8728, // API port (8729 for TLS) tls: false, // TLS encryption timeout: 10000, // Connection timeout (ms) debug: false, // Debug output }); ``` ## ๐Ÿ“š API Documentation ### ๐Ÿ”Œ Connection Methods #### `connect()` Establishes and authenticates the RouterOS connection. ```javascript await api.connect(); ``` #### `close()` Gracefully terminates the RouterOS connection. ```javascript await api.close(); ``` ### ๐Ÿ› ๏ธ Command Methods #### `send(words)` Executes commands on the RouterOS device. ```javascript const result = await api.send(["/system/resource/print"]); ``` ## ๐Ÿ“ Command Examples ### ๐Ÿ–ฅ๏ธ System Management ```javascript // System identity operations const identity = await api.send(["/system/identity/print"]); await api.send(["/system/identity/set", "=name=my-router"]); // Resource monitoring const resources = await api.send(["/system/resource/print"]); // System maintenance await api.send(["/system/reboot"]); ``` ### ๐ŸŒ Network Interface Management ```javascript // Interface operations const interfaces = await api.send(["/interface/print"]); await api.send(["/interface/enable", "=.id=ether1"]); // Wireless interface management const wireless = await api.send(["/interface/wireless/print"]); ``` ### ๐Ÿ”ง IP Configuration ```javascript // IP address management const addresses = await api.send(["/ip/address/print"]); await api.send([ "/ip/address/add", "=address=192.168.1.1/24", "=interface=ether1", ]); // DHCP server management const leases = await api.send(["/ip/dhcp-server/lease/print"]); ``` ## ๐ŸŽฏ Events The client emits these events: - `connected` โœ… - Connection established - `error` โŒ - Error occurred - `close` ๐Ÿ”’ - Connection terminated ```javascript api.on("connected", () => console.log("โœ… Connected")); api.on("error", (err) => console.error("โŒ Error:", err.message)); api.on("close", () => console.log("๐Ÿ”’ Connection closed")); ``` ## ๐Ÿ” Debug Mode Enable detailed logging: ```javascript const api = new RouterOSClient({ debug: true, // other options... }); ``` ## โŒ Error Handling ```javascript try { await api.connect(); const result = await api.send(["/command"]); } catch (err) { console.error("โŒ Error:", err.message); } ``` ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿค Contributing We welcome contributions! Here's how: 1. ๐Ÿ”€ Fork the repository 2. ๐ŸŒฟ Create your feature branch (`git checkout -b feature/amazing-feature`) 3. โœ๏ธ Commit changes (`git commit -m 'Add amazing feature'`) 4. ๐Ÿ“ค Push to branch (`git push origin feature/amazing-feature`) 5. ๐Ÿ“ซ Open a Pull Request ## ๐Ÿ‘ฅ Contributors We thank the following contributors for their valuable contributions to this project: - **[mbingsdk](https://github.com/mbingsdk)** - Update limit buffer functionality ๐Ÿ”ง - **[AviStudio](https://github.com/AviStudio)** - Command Reference (Refrence.js) documentation ๐Ÿ“š ## ๐Ÿ’ฌ Support Need help or have questions? Join our community: ๐Ÿ“ฒ **Telegram Support Group**: [https://t.me/ros_client](https://t.me/ros_client) ## ๐Ÿ“‹ Changelog For detailed information about changes in each version, see the [CHANGELOG.md](./CHANGELOG.md) file. --- โญ Star this repository if you find it helpful!