UNPKG

@vermaysha/routeros

Version:

NodeJS / Bun RouterOS API

219 lines (218 loc) 8.49 kB
import type { TLSSocketOptions } from 'node:tls'; import type { IRosOptions } from './IRosOptions'; import { RStream, RStreamCallback } from './RStream'; import { EventEmitter } from 'node:events'; import type { IRosGenericResponse } from './IRosGenericResponse'; /** * Creates a connection object with the credentials provided */ export declare class RouterOSAPI extends EventEmitter { /** * Host to connect */ host: string; /** * Username to use */ user: string; /** * Password of the username */ password: string; /** * Port of the API */ port: number; /** * Timeout of the connection */ timeout: number; /** * TLS Options to use, if any */ tls: TLSSocketOptions | null; /** * Connected flag */ connected: boolean; /** * Connecting flag */ connecting: boolean; /** * Closing flag */ closing: boolean; /** * Keep connection alive */ keepalive: boolean; /** * The connector which will be used */ private connector; /** * The function timeout that will keep the connection alive */ private keptaliveby; /** * Counter for channels open */ private channelsOpen; /** * Flag if the connection was held by the keepalive parameter * or keepaliveBy function */ private holdingConnectionWithKeepalive; /** * Store the timeout when holding the connection * when waiting for a channel response */ private connectionHoldInterval; /** * List of streams registered * for handling continuous data * from the routeros * * @type {RStream[]} */ private registeredStreams; /** * Creates a new RouterOSAPI connection object with the given options * * @param {IRosOptions} options - connection options * @param {string} options.host - The host to connect to * @param {string} [options.user='admin'] - The username to use * @param {string} [options.password=''] - The password of the username * @param {number} [options.port=8728] - The port of the API * @param {number} [options.timeout=10] - The timeout of the connection * @param {TLSSocketOptions} [options.tls=null] - TLS Options to use, if any * @param {boolean} [options.keepalive=false] - Keep connection alive */ constructor(options: IRosOptions); /** * Connect to the routerboard * * @returns {Promise<RouterOSAPI>} - The current RouterOSAPI instance */ connect(): Promise<RouterOSAPI>; /** * Sends the provided command(s) to the routerboard via a new channel. * Opens a new channel for communication, writes the command(s) over * the socket, and manages the connection hold state. * * @param {string | string[]} params - The primary command or an array of commands to send. * @param {...Array<string | string[]>} moreParams - Additional commands or parameters. * @returns {Promise<IRosGenericResponse[]>} - A promise that resolves with the response from the routerboard. */ write(params: string | string[], ...moreParams: Array<string | string[]>): Promise<IRosGenericResponse[]>; /** * Sends the provided command(s) to the routerboard via a new stream channel. * Opens a new channel for communication, writes the command(s) over * the socket, and manages the connection hold state. * * @param {string | string[]} params - The primary command or an array of commands to send. * @param {...Array<string | string[]>} moreParams - Additional commands or parameters. * @returns {RStream} - A stream object for handling continuous data flow. */ writeStream(params: string | string[], ...moreParams: Array<string | string[]>): RStream; /** * Starts a new stream with the provided command(s) and optional callback. * If no callback is provided, the function will return the stream object. * If a callback is provided, the function will call the callback with the packet data. * * @param {string | string[]} params - The primary command or an array of commands to send. * @returns {RStream} - The stream object. */ stream<T>(params?: string | string[], callback?: RStreamCallback<T>): RStream; /** * Initiates a keepalive mechanism by executing the provided command(s) at regular intervals. * This function ensures that the connection remains active by continuously sending commands * to the server. If a callback is provided, it will be called with the packet data on success * or an error object on failure. * * @param {string | string[]} params - The primary command or an array of commands to send. * @param {function} [callback] - The callback function to handle the response data. */ keepaliveBy(params?: string | string[], callback?: (err: Error | null, data: any) => void): void; /** * Close the connection to the routerboard. If the connection is already * being closed, rejects the promise with the "ALRDYCLOSNG" error. * * @returns {Promise<RouterOSAPI>} - The current RouterOSAPI instance on success */ close(): Promise<RouterOSAPI>; /** * Creates a new Channel instance and increases the channelsOpen counter. * Should be used by methods that need to create a new channel to * communicate with the routerboard. * @returns {Channel} - The newly created Channel instance */ private openChannel; /** * Increments the count of open channels. * This function should be called whenever a new channel is opened * to keep track of the current number of active channels. */ private increaseChannelsOpen; /** * Decreases the count of open channels. * This function should be called whenever a channel is closed * to keep track of the current number of active channels. */ private decreaseChannelsOpen; /** * Registers a RStream instance to keep track of it. * This method is used internally to keep track of all * RStream instances created by the RouterOSAPI instance. * @param {RStream} stream - RStream instance to be registered */ private registerStream; /** * Unregisters a RStream instance from the list of registered streams. * This function is called internally when a RStream instance is stopped. * @param {RStream} stream - RStream instance to be unregistered */ private unregisterStream; /** * Stops all registered RStream instances. * This method is called internally when the connection is closed. */ private stopAllStreams; /** * If there is only one channel open, holds the connection by sending * a keepalive message to the routerboard every half of the timeout * period. This is necessary because the routerboard will close the * connection if no data is received for the timeout period. */ private holdConnection; /** * Releases the connection hold by clearing the keepalive interval. * If there are no channels open, it clears the keepalive interval, * effectively stopping the periodic keepalive messages to the * routerboard. If there are still channels open, the function * returns early without taking any action. */ private releaseConnectionHold; /** * Connects to the routerboard using the credentials provided * in the constructor. It will either login using the 6.43+ * method or the old method. * * @returns {Promise<RouterOSAPI>} - The current instance of the RouterOSAPI * class, after it has connected. If the connection fails, it will * reject the promise with an error. */ private login; /** * Concatenates the given parameters into a single array. * If any of the parameters are strings, they are converted to arrays with a single element. * If any of the parameters are arrays, they are concatenated into the first parameter. * This is useful for generating the * parameters for a mikrotik api call * @param {string | string[]} firstParameter - The first parameter to be concatenated * @param {any[]} parameters - The parameters to be concatenated into the first parameter * @returns {string[]} - The concatenated array */ private concatParams; }