|
| 1 | +# ROS REST - RouterOS REST API |
| 2 | + |
| 3 | +## Pengantar |
| 4 | + |
| 5 | +Buat permintaan HTTP (REST API) dari Node.js ke RouterOS. REST API adalah fitur baru, ini mulai tersedia di **RouterOS v7.1beta4**. |
| 6 | + |
| 7 | +## Getting Started |
| 8 | + |
| 9 | +### Prasyarat |
| 10 | + |
| 11 | +- Gunakan [RouterOS v7.1beta4](https://mikrotik.com/download) atau yang paling baru. |
| 12 | +- Install [Node.js](https://nodejs.org/en/). |
| 13 | +- Aktifkan layanan **www-ssl** (Winbox: IP > Services). |
| 14 | +- Pasang sertifikat CA, kalau hanya digunakan di jaringan lokal (untuk pengujian) buat sertifikat yang ditandatangani sendiri (_self-signed_), jalankan perintah berikut di Terminal Winbox ([selengkapnya](https://forum.mikrotik.com/viewtopic.php?f=1&t=172789)): |
| 15 | + |
| 16 | + ``` |
| 17 | + /certificate |
| 18 | + add name=ca-template days-valid=3650 common-name=your.server.url key-usage=key-cert-sign,crl-sign |
| 19 | + add name=server-template days-valid=3650 common-name=your.server.url |
| 20 | +
|
| 21 | + /certificate |
| 22 | + sign ca-template name=root-ca |
| 23 | + :delay 3s |
| 24 | + sign ca=root-ca server-template name=server |
| 25 | + :delay 3s |
| 26 | +
|
| 27 | + /certificate |
| 28 | + set root-ca trusted=yes |
| 29 | + set server trusted=yes |
| 30 | +
|
| 31 | + /ip service |
| 32 | + set www-ssl certificate=server disabled=no |
| 33 | + ``` |
| 34 | + |
| 35 | +### Instalasi |
| 36 | + |
| 37 | +Untuk menginstall ROS REST, jalankan perintah berikut: |
| 38 | + |
| 39 | +**npm:** |
| 40 | + |
| 41 | +```shell |
| 42 | +npm i ros-rest |
| 43 | +``` |
| 44 | + |
| 45 | +atau, **yarn:** |
| 46 | + |
| 47 | +```shell |
| 48 | +yarn add ros-rest |
| 49 | +``` |
| 50 | + |
| 51 | +### Konfigurasi |
| 52 | + |
| 53 | +```js |
| 54 | +const rosRest = require('ros-rest'); |
| 55 | + |
| 56 | +const clientRosRest = rosRest({ |
| 57 | + host: '192.168.100.32', |
| 58 | + user: 'user', |
| 59 | + password: 'password', |
| 60 | + port: 443, // default 443 |
| 61 | + secure: false, // default false |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +Fungsi `rosRest` mengharapkan objek kredential RouterOS dan mengembalikan method yang akan kita bahas di bawah. |
| 66 | + |
| 67 | +Berikut daftar properti objek yang diharapkan `rosRest`: |
| 68 | + |
| 69 | +| Properti | Tipe | Default | Deskripsi | |
| 70 | +| ---------- | ------- | ------- | --------------------------------------------------------------------------------- | |
| 71 | +| `host` | string | - | host RouterOS | |
| 72 | +| `port` | number | `443` | port layanan _www-sssl_ RouterOS | |
| 73 | +| `user` | string | - | user untuk masuk ke RouterOS | |
| 74 | +| `password` | string | - | password untuk masuk ke RouterOS | |
| 75 | +| `secure` | boolean | `false` | apakah sertifikat divalidasi oleh otoritas sertifikat (CA) yang dipercaya publik? | |
| 76 | + |
| 77 | +> **Catatan:** Jika sertifikat ditandatangani sendiri atur `secure` menjadi `false` (default). Jika sertifikat divalidasi oleh [otoritas seritifkat (CA)](https://en.wikipedia.org/wiki/Certificate_authority) dan RouterOS dapat diakses melalui jaringan publik, atur `secure` menjadi `true` untuk keamanan. |
| 78 | +
|
| 79 | +## Method |
| 80 | + |
| 81 | +Berikut daftar method yang bisa kita gunakan yang dikembalikan fungsi `rosRest`: |
| 82 | + |
| 83 | +| HTTP | RouterOS | ROS REST | Description | |
| 84 | +| ------ | -------- | -------- | ---------------------------------------- | |
| 85 | +| GET | print | get | untuk mengambil data | |
| 86 | +| PUT | add | add | untuk membuat data baru | |
| 87 | +| PATCH | set | set | untuk mempebarui data | |
| 88 | +| DELETE | remove | remove | untuk menghapus data | |
| 89 | +| POST | | command | untuk mengakses berbagai perintah konsol | |
| 90 | + |
| 91 | +Pelajari selengkapnya: [Dokumentasi RouteOS REST API](https://help.mikrotik.com/docs/display/ROS/REST+API) |
| 92 | + |
| 93 | +Semua method mengembalikan Promise, kita bisa menanganinya menggunakan `then/catch` atau `try/catch`. |
| 94 | + |
| 95 | +`then/catch`: |
| 96 | + |
| 97 | +```js |
| 98 | +clientRosRest |
| 99 | + .get('ip/address') |
| 100 | + .then((res) => { |
| 101 | + console.log('result:', res); |
| 102 | + }) |
| 103 | + .catch((err) => { |
| 104 | + console.log('error:', err); |
| 105 | + }); |
| 106 | +``` |
| 107 | + |
| 108 | +`try/catch`: |
| 109 | + |
| 110 | +```js |
| 111 | +const fetchRouterOS = async () => { |
| 112 | + try { |
| 113 | + const res = await clientRosRest.get('ip/address'); |
| 114 | + console.log('result:', res); |
| 115 | + } catch (err) { |
| 116 | + console.log('error:', err); |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +fetchRouterOS(); |
| 121 | +``` |
| 122 | + |
| 123 | +### `get` |
| 124 | + |
| 125 | +Contoh mengambil semua data IP Address (Winbox: IP > Address): |
| 126 | + |
| 127 | +```js |
| 128 | +clientRosRest.get('ip/address'); |
| 129 | +``` |
| 130 | + |
| 131 | +Ambil berdasarkan id, ehter, atau berdasarkan properti yang berisi nilai tertentu: |
| 132 | + |
| 133 | +```js |
| 134 | +clientRosRest.get('ip/address/*2'); |
| 135 | +clientRosRest.get('ip/address/ether1'); |
| 136 | +clientRosRest.get('ip/address?network=10.155.101.0&dynamic=true'); |
| 137 | +``` |
| 138 | + |
| 139 | +Jika kita hanya memerlukan properti tertentu, gunakan `.proplist`: |
| 140 | + |
| 141 | +```js |
| 142 | +clientRosRest.get('ip/address?.proplist=address,disabled'); |
| 143 | +``` |
| 144 | + |
| 145 | +### `add` |
| 146 | + |
| 147 | +Contoh menambahkan IP Address baru: |
| 148 | + |
| 149 | +```js |
| 150 | +clientRosRest.add('ip/address', { |
| 151 | + address: '192.168.10.1/24', |
| 152 | + network: '192.168.10.1', |
| 153 | + interface: 'ether2', |
| 154 | + comment: 'test ROS REST', |
| 155 | +}); |
| 156 | +``` |
| 157 | + |
| 158 | +### `set` |
| 159 | + |
| 160 | +Contoh memperbarui komentar IP Address yang memiliki id `*13`: |
| 161 | + |
| 162 | +```js |
| 163 | +clientRosRest.set('ip/address/*13', { |
| 164 | + comment: 'update comment test ROS REST', |
| 165 | +}); |
| 166 | +``` |
| 167 | + |
| 168 | +### `remove` |
| 169 | + |
| 170 | +Contoh menghapus IP Address yang memiliki id `*13`: |
| 171 | + |
| 172 | +```js |
| 173 | +clientRosRest.remove('ip/address/*13'); |
| 174 | +``` |
| 175 | + |
| 176 | +### `command` |
| 177 | + |
| 178 | +Semua fitur API tersedia melalui `command` metode. |
| 179 | + |
| 180 | +```js |
| 181 | +clientRosRest.command('interface/print', { |
| 182 | + '.proplist': ['name', 'type'], |
| 183 | +}); |
| 184 | + |
| 185 | +// or |
| 186 | + |
| 187 | +clientRosRest.command('interface/print', { |
| 188 | + '.proplist': 'name,type', |
| 189 | +}); |
| 190 | +``` |
| 191 | + |
| 192 | +## Berkontribusi |
| 193 | + |
| 194 | +Kontribusi, masalah, dan permintaan fitur dipersilakan. Jangan ragu untuk memeriksa [halaman masalah](https://github.com/renomureza/ros-rest/issues) jika Anda ingin berkontribusi. |
0 commit comments