diff --git a/lib/webadmin/router/config.js b/lib/webadmin/router/config.js index c70c867..2498550 100644 --- a/lib/webadmin/router/config.js +++ b/lib/webadmin/router/config.js @@ -82,6 +82,118 @@ async function pageDelPrefix(req, res) { res.redirect(`${req.baseUrl}`); } -router.get('/', pageMain); +async function modemAdd(req, res) { + const modem = { + name: (req.body.name || '').trim(), + imsi: (req.body.imsi || '').trim(), + outgoing: !!req.body.outgoing, + prefix: (req.body.prefix || '').split(/[, ]+/).map((val) => val.trim()), + }; + const index = config.modems.find((item) => item.name.toUpperCase() === modem.name.toUpperCase()); + if (index >= 0) { + res.end('Modem duplikat'); + return; + } + + config.modems.push(modem); + await writeConfigFile(); + + res.redirect(`${req.baseUrl}`); +} + +async function modemAddPrefix(req, res) { + const modemName = (req.body.name || '').trim(); + const prefix = (req.body.prefix || '').split(/[, ]+/).map((val) => val.trim()); + console.log(prefix); + const index = config.modems.findIndex( + (item) => item.name.toUpperCase() === modemName.toUpperCase(), + ); + if (Array.isArray(config.modems[index].prefix)) { + config.modems[index].prefix = config.modems[index].prefix.concat(prefix); + } else { + config.modems[index].prefix = prefix; + } + + if (index < 0) { + res.end('Modem tidak ditemukan'); + return; + } + + await writeConfigFile(); + + res.redirect(`${req.baseUrl}`); +} + +async function modemUpdateImsi(req, res) { + const modemName = (req.body.name || '').trim(); + const imsi = (req.body.imsi || '').trim(); + + const index = config.modems.findIndex( + (item) => item.name.toUpperCase() === modemName.toUpperCase(), + ); + config.modems[index].imsi = imsi; + + if (index < 0) { + res.end('Modem tidak ditemukan'); + return; + } + + await writeConfigFile(); + + res.redirect(`${req.baseUrl}`); +} + +async function modemUpdateCustomIp(req, res) { + const modemName = (req.body.name || '').trim(); + const url = (req.body.url || '').trim() || null; + const username = (req.body.username || '').trim() || null; + const password = (req.body.password || '').trim() || null; + + const index = config.modems.findIndex( + (item) => item.name.toUpperCase() === modemName.toUpperCase(), + ); + config.modems[index].url = url; + config.modems[index].username = username; + config.modems[index].password = password; + + if (index < 0) { + res.end('Modem tidak ditemukan'); + return; + } + + await writeConfigFile(); + + res.redirect(`${req.baseUrl}`); +} + +async function modemDelete(req, res) { + const modemName = (req.body.name || '').trim(); + + const index = config.modems.findIndex( + (item) => item.name.toUpperCase() === modemName.toUpperCase(), + ); + + if (index < 0) { + res.end('Modem tidak ditemukan'); + return; + } + + config.modems.splice(index, 1); + + await writeConfigFile(); + + res.redirect(`${req.baseUrl}`); +} + +router.get('/', (req, res) => { + res.redirect(`${req.baseUrl}/modem`); // sementara redirect ke modem +}); + +router.get('/modem', pageMain); +router.post('/modem/add', express.urlencoded({ extended: true }), modemAdd); +router.post('/modem/add-prefix', express.urlencoded({ extended: true }), modemAddPrefix); +router.post('/modem/update-imsi', express.urlencoded({ extended: true }), modemUpdateImsi); +router.post('/modem/update-custom-ip', express.urlencoded({ extended: true }), modemUpdateCustomIp); +router.post('/modem/delete', express.urlencoded({ extended: true }), modemDelete); router.get('/modem/set-outgoing/:modemName/:newValue', pageSetOutgoing); router.get('/modem/del-prefix/:modemName/:prefix', pageDelPrefix); diff --git a/webadmin-views/config.include.addmodem.html b/webadmin-views/config.include.addmodem.html index a3de4b6..9421365 100644 --- a/webadmin-views/config.include.addmodem.html +++ b/webadmin-views/config.include.addmodem.html @@ -3,6 +3,26 @@ <h3>Tambah Modem</3> </div> <div class="card-body"> - + <form method="post" action="{{ baseUrl }}/modem/add"> + <div class="form-group"> + <label for="modem-name">Nama</label> + <input type="text" id="modem-name" name="name" class="form-control" placeholder="Masukkan nama" required> + </div> + <div class="form-group"> + <label for="modem-outgoing">Outgoing</label> + <input type="checkbox" id="modem-outgoing" name="outgoing"> + </div> + <div class="form-group"> + <label for="modem-imsi">IMSI</label> + <input type="text" id="modem-imsi" name="imsi" class="form-control" placeholder="Masukkan IMSI"> + </div> + <div class="form-group"> + <label for="modem-prefix">Prefix</label> + <input type="text" id="modem-prefix" name="prefix" class="form-control" + placeholder="Masukkan Prefix pisahkan dengan koma" + > + </div> + <button type="submit" class="btn btn-primary">Save</button> + </form> </div> </div> \ No newline at end of file diff --git a/webadmin-views/config.index.html b/webadmin-views/config.index.html index b1e9931..dfba339 100644 --- a/webadmin-views/config.index.html +++ b/webadmin-views/config.index.html @@ -34,9 +34,13 @@ <dd class="col-sm-9"> {{ modem.imsi or '-' }} <br> - [<a href="{{ baseUrl }}/modem/set-imsi/{{ modem.name | urlencode }}"> - edit - </a>] + <form method="post" action="{{ baseUrl }}/modem/update-imsi" class="form-inline"> + <input type="hidden" name="name" value="{{ modem.name | urlencode }}"> + <div class="form-group"> + <input type="text" name="imsi" class="form-control" placeholder="Update IMSI"> + </div> + <button type="submit" class="btn btn-primary">Save</button> + </form> </dd> </dl> @@ -50,26 +54,47 @@ </a> <br> {% endfor %} - - [ - <a href="{{ baseUrl }}/modem/add-prefix/{{ modem.name | urlencode }}"> - tambah - </a> - ] + <form method="post" action="{{ baseUrl }}/modem/add-prefix" class="form-inline"> + <input type="hidden" name="name" value="{{ modem.name | urlencode }}"> + <div class="form-group"> + <input type="text" name="prefix" class="form-control" placeholder="Masukkan prefix baru"> + </div> + <button type="submit" class="btn btn-primary">Save</button> + </form> </dd> </dl> - <dl class="row"> <dt class="col-sm-3">Custom IP</dt> <dd class="col-sm-9"> - {{ modem.ip or 'none' }} <br> - [<a href="{{ baseUrl }}/modem/set-ip/{{ modem.name | urlencode }}"> - edit - </a>] + <form method="post" action="{{ baseUrl }}/modem/update-custom-ip"> + <input type="hidden" name="name" value="{{ modem.name | urlencode }}"> + <div class="form-group"> + <input type="text" name="url" class="form-control" placeholder="Masukkan URL" value="{{ modem.url }}"> + </div> + <div class="form-group"> + <input type="text" name="username" class="form-control" placeholder="Masukkan Username" value="{{ modem.username }}"> + </div> + <div class="form-group"> + <input type="password" name="password" class="form-control" placeholder="Masukkan Password" value="{{ modem.password }}"> + </div> + <button type="submit" class="btn btn-primary">Save</button> + </form> + </dd> + </dl> + <dl class="row"> + <dt class="col-sm-3"> </dt> + <dd class="col-sm-9"> + <form method="post" action="{{ baseUrl }}/modem/delete"> + <input type="hidden" name="name" value="{{ modem.name | urlencode }}"> + <button type="submit" class="btn btn-block btn-danger" + onclick="return window.confirm('Apakah anda yakin ingin menghapus modem {{ modem.name }}?');" + > + Hapus Modem + </button> + </form> </dd> </dl> - </div> </div> <br>