Commit e4cdd091b4d367650d4b6f341a0f18628f2953a2
1 parent
c4a8ece634
Exists in
master
and in
1 other branch
ESLINTED
Showing 1 changed file with 14 additions and 4 deletions Inline Diff
lib/webadmin/router/config.js
1 | const fs = require('fs'); | 1 | const fs = require('fs'); |
2 | const express = require('express'); | 2 | const express = require('express'); |
3 | const { orderBy } = require('natural-orderby'); | 3 | const { orderBy } = require('natural-orderby'); |
4 | const config = require('komodo-sdk/config'); | 4 | const config = require('komodo-sdk/config'); |
5 | 5 | ||
6 | const router = express.Router(); | 6 | const router = express.Router(); |
7 | module.exports = router; | 7 | module.exports = router; |
8 | 8 | ||
9 | async function writeConfigFile() { | 9 | async function writeConfigFile() { |
10 | try { | 10 | try { |
11 | await fs.promises.writeFile('config.json', JSON.stringify(config, null, 4)); | 11 | await fs.promises.writeFile('config.json', JSON.stringify(config, null, 4)); |
12 | } catch (e) { | 12 | } catch (e) { |
13 | // | 13 | // |
14 | } | 14 | } |
15 | } | 15 | } |
16 | 16 | ||
17 | function pageMain(req, res) { | 17 | function pageMain(req, res) { |
18 | res.render('config.index.html', { | 18 | res.render('config.index.html', { |
19 | config: JSON.stringify(config, null, 4), | 19 | config: JSON.stringify(config, null, 4), |
20 | modems: orderBy(config.modems, [(v) => v.name], ['asc']), | 20 | modems: orderBy(config.modems, [(v) => v.name], ['asc']), |
21 | baseUrl: req.baseUrl, | 21 | baseUrl: req.baseUrl, |
22 | }); | 22 | }); |
23 | } | 23 | } |
24 | 24 | ||
25 | async function pageSetOutgoing(req, res) { | 25 | async function pageSetOutgoing(req, res) { |
26 | const modemName = (req.params.modemName || '').trim(); | 26 | const modemName = (req.params.modemName || '').trim(); |
27 | if (!modemName) { | 27 | if (!modemName) { |
28 | res.end('Invalid modem name'); | 28 | res.end('Invalid modem name'); |
29 | return; | 29 | return; |
30 | } | 30 | } |
31 | 31 | ||
32 | const idx = (config.modems || []).findIndex((modem) => modem.name === modemName); | 32 | const idx = (config.modems || []).findIndex((modem) => modem.name === modemName); |
33 | if (idx < 0) { | 33 | if (idx < 0) { |
34 | res.end('No modem matched'); | 34 | res.end('No modem matched'); |
35 | return; | 35 | return; |
36 | } | 36 | } |
37 | 37 | ||
38 | config.modems[idx].outgoing = req.params.newValue === '1' || req.params.newValue === 'true'; | 38 | config.modems[idx].outgoing = req.params.newValue === '1' || req.params.newValue === 'true'; |
39 | 39 | ||
40 | await writeConfigFile(); | 40 | await writeConfigFile(); |
41 | 41 | ||
42 | res.redirect(`${req.baseUrl}`); | 42 | res.redirect(`${req.baseUrl}`); |
43 | } | 43 | } |
44 | 44 | ||
45 | async function pageDelPrefix(req, res) { | 45 | async function pageDelPrefix(req, res) { |
46 | const modemName = (req.params.modemName || '').trim(); | 46 | const modemName = (req.params.modemName || '').trim(); |
47 | if (!modemName) { | 47 | if (!modemName) { |
48 | res.end('Invalid modem name'); | 48 | res.end('Invalid modem name'); |
49 | return; | 49 | return; |
50 | } | 50 | } |
51 | 51 | ||
52 | const prefix = (req.params.prefix || '').trim(); | 52 | const prefix = (req.params.prefix || '').trim(); |
53 | if (!prefix) { | 53 | if (!prefix) { |
54 | res.end('Invalid prefix'); | 54 | res.end('Invalid prefix'); |
55 | return; | 55 | return; |
56 | } | 56 | } |
57 | 57 | ||
58 | const modemIdx = (config.modems || []).findIndex((modem) => modem.name === modemName); | 58 | const modemIdx = (config.modems || []).findIndex((modem) => modem.name === modemName); |
59 | if (modemIdx < 0) { | 59 | if (modemIdx < 0) { |
60 | res.end('No modem matched'); | 60 | res.end('No modem matched'); |
61 | return; | 61 | return; |
62 | } | 62 | } |
63 | 63 | ||
64 | const prefixIdx = (config.modems[modemIdx].prefix || []).indexOf(prefix); | 64 | const prefixIdx = (config.modems[modemIdx].prefix || []).indexOf(prefix); |
65 | if (prefixIdx < 0) { | 65 | if (prefixIdx < 0) { |
66 | res.end('No prefix matched'); | 66 | res.end('No prefix matched'); |
67 | return; | 67 | return; |
68 | } | 68 | } |
69 | 69 | ||
70 | config.modems[modemIdx].prefix.splice(prefixIdx, 1); | 70 | config.modems[modemIdx].prefix.splice(prefixIdx, 1); |
71 | 71 | ||
72 | await writeConfigFile(); | 72 | await writeConfigFile(); |
73 | 73 | ||
74 | res.redirect(`${req.baseUrl}`); | 74 | res.redirect(`${req.baseUrl}`); |
75 | } | 75 | } |
76 | 76 | ||
77 | async function modemAdd(req, res) { | 77 | async function modemAdd(req, res) { |
78 | const modem = { | 78 | const modem = { |
79 | name: (req.body.name || '').trim(), | 79 | name: (req.body.name || '').trim(), |
80 | imsi: (req.body.imsi || '').trim(), | 80 | imsi: (req.body.imsi || '').trim(), |
81 | outgoing: !!req.body.outgoing, | 81 | outgoing: !!req.body.outgoing, |
82 | prefix: (req.body.prefix || '').split(/[, ]+/).map((val) => val.trim().replace(/^0/, '62')).filter((row) => row), | 82 | prefix: (req.body.prefix || '').split(/[, ]+/) |
83 | .map((val) => val.trim().replace(/^0/, '62')) | ||
84 | .filter((row) => row), | ||
83 | }; | 85 | }; |
84 | const index = config.modems.find((item) => item.name.toUpperCase() === modem.name.toUpperCase()); | 86 | |
87 | const index = config.modems.find( | ||
88 | (item) => item.name.toUpperCase() === modem.name.toUpperCase(), | ||
89 | ); | ||
90 | |||
85 | if (index >= 0) { | 91 | if (index >= 0) { |
86 | res.end('Modem duplikat'); | 92 | res.end('Modem duplikat'); |
87 | return; | 93 | return; |
88 | } | 94 | } |
89 | 95 | ||
90 | config.modems.push(modem); | 96 | config.modems.push(modem); |
91 | await writeConfigFile(); | 97 | await writeConfigFile(); |
92 | 98 | ||
93 | res.redirect(`${req.baseUrl}`); | 99 | res.redirect(`${req.baseUrl}`); |
94 | } | 100 | } |
95 | 101 | ||
96 | async function modemAddPrefix(req, res) { | 102 | async function modemAddPrefix(req, res) { |
97 | const modemName = (req.body.name || '').trim(); | 103 | const modemName = (req.body.name || '').trim(); |
98 | const prefix = (req.body.prefix || '').split(/[, ]+/).map((val) => val.trim().replace(/^0/, '62')).filter((row) => row); | 104 | const prefix = (req.body.prefix || '').split(/[, ]+/) |
105 | .map((val) => val.trim().replace(/^0/, '62')) | ||
106 | .filter((row) => row); | ||
107 | |||
99 | const index = config.modems.findIndex( | 108 | const index = config.modems.findIndex( |
100 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), | 109 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), |
101 | ); | 110 | ); |
102 | if (Array.isArray(config.modems[index].prefix)) { | 111 | if (Array.isArray(config.modems[index].prefix)) { |
103 | config.modems[index].prefix = config.modems[index].prefix.concat(prefix); | 112 | config.modems[index].prefix = config.modems[index].prefix.concat(prefix); |
104 | } else { | 113 | } else { |
105 | config.modems[index].prefix = prefix; | 114 | config.modems[index].prefix = prefix; |
106 | } | 115 | } |
107 | 116 | ||
108 | if (index < 0) { | 117 | if (index < 0) { |
109 | res.end('Modem tidak ditemukan'); | 118 | res.end('Modem tidak ditemukan'); |
110 | return; | 119 | return; |
111 | } | 120 | } |
112 | 121 | ||
113 | await writeConfigFile(); | 122 | await writeConfigFile(); |
114 | 123 | ||
115 | res.redirect(`${req.baseUrl}`); | 124 | res.redirect(`${req.baseUrl}`); |
116 | } | 125 | } |
117 | 126 | ||
118 | async function modemUpdateImsi(req, res) { | 127 | async function modemUpdateImsi(req, res) { |
119 | const modemName = (req.body.name || '').trim(); | 128 | const modemName = (req.body.name || '').trim(); |
120 | const imsi = (req.body.imsi || '').trim(); | 129 | const imsi = (req.body.imsi || '').trim(); |
121 | 130 | ||
122 | const index = config.modems.findIndex( | 131 | const index = config.modems.findIndex( |
123 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), | 132 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), |
124 | ); | 133 | ); |
125 | config.modems[index].imsi = imsi; | 134 | config.modems[index].imsi = imsi; |
126 | 135 | ||
127 | if (index < 0) { | 136 | if (index < 0) { |
128 | res.end('Modem tidak ditemukan'); | 137 | res.end('Modem tidak ditemukan'); |
129 | return; | 138 | return; |
130 | } | 139 | } |
131 | 140 | ||
132 | await writeConfigFile(); | 141 | await writeConfigFile(); |
133 | 142 | ||
134 | res.redirect(`${req.baseUrl}`); | 143 | res.redirect(`${req.baseUrl}`); |
135 | } | 144 | } |
136 | 145 | ||
137 | async function modemUpdateCustomIp(req, res) { | 146 | async function modemUpdateCustomIp(req, res) { |
138 | const modemName = (req.body.name || '').trim(); | 147 | const modemName = (req.body.name || '').trim(); |
139 | const url = (req.body.url || '').trim() || null; | 148 | const url = (req.body.url || '').trim() || null; |
140 | const username = (req.body.username || '').trim() || null; | 149 | const username = (req.body.username || '').trim() || null; |
141 | const password = (req.body.password || '').trim() || null; | 150 | const password = (req.body.password || '').trim() || null; |
142 | 151 | ||
143 | const index = config.modems.findIndex( | 152 | const index = config.modems.findIndex( |
144 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), | 153 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), |
145 | ); | 154 | ); |
155 | |||
146 | config.modems[index].url = url; | 156 | config.modems[index].url = url; |
147 | config.modems[index].username = username; | 157 | config.modems[index].username = username; |
148 | config.modems[index].password = password; | 158 | config.modems[index].password = password; |
149 | 159 | ||
150 | if (index < 0) { | 160 | if (index < 0) { |
151 | res.end('Modem tidak ditemukan'); | 161 | res.end('Modem tidak ditemukan'); |
152 | return; | 162 | return; |
153 | } | 163 | } |
154 | 164 | ||
155 | await writeConfigFile(); | 165 | await writeConfigFile(); |
156 | 166 | ||
157 | res.redirect(`${req.baseUrl}`); | 167 | res.redirect(`${req.baseUrl}`); |
158 | } | 168 | } |
159 | 169 | ||
160 | async function modemDelete(req, res) { | 170 | async function modemDelete(req, res) { |
161 | const modemName = (req.body.name || '').trim(); | 171 | const modemName = (req.body.name || '').trim(); |
162 | 172 | ||
163 | const index = config.modems.findIndex( | 173 | const index = config.modems.findIndex( |
164 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), | 174 | (item) => item.name.toUpperCase() === modemName.toUpperCase(), |
165 | ); | 175 | ); |
166 | 176 | ||
167 | if (index < 0) { | 177 | if (index < 0) { |
168 | res.end('Modem tidak ditemukan'); | 178 | res.end('Modem tidak ditemukan'); |
169 | return; | 179 | return; |
170 | } | 180 | } |
171 | 181 | ||
172 | config.modems.splice(index, 1); | 182 | config.modems.splice(index, 1); |
173 | 183 | ||
174 | await writeConfigFile(); | 184 | await writeConfigFile(); |
175 | 185 | ||
176 | res.redirect(`${req.baseUrl}`); | 186 | res.redirect(`${req.baseUrl}`); |
177 | } | 187 | } |
178 | 188 | ||
179 | router.get('/', (req, res) => { | 189 | router.get('/', (req, res) => { |
180 | res.redirect(`${req.baseUrl}/modem`); // sementara redirect ke modem | 190 | res.redirect(`${req.baseUrl}/modem`); // sementara redirect ke modem |
181 | }); | 191 | }); |
182 | 192 | ||
183 | router.get('/modem', pageMain); | 193 | router.get('/modem', pageMain); |
184 | router.post('/modem/add', express.urlencoded({ extended: true }), modemAdd); | 194 | router.post('/modem/add', express.urlencoded({ extended: true }), modemAdd); |
185 | router.post('/modem/add-prefix', express.urlencoded({ extended: true }), modemAddPrefix); | 195 | router.post('/modem/add-prefix', express.urlencoded({ extended: true }), modemAddPrefix); |
186 | router.post('/modem/update-imsi', express.urlencoded({ extended: true }), modemUpdateImsi); | 196 | router.post('/modem/update-imsi', express.urlencoded({ extended: true }), modemUpdateImsi); |
187 | router.post('/modem/update-custom-ip', express.urlencoded({ extended: true }), modemUpdateCustomIp); | 197 | router.post('/modem/update-custom-ip', express.urlencoded({ extended: true }), modemUpdateCustomIp); |
188 | router.post('/modem/delete', express.urlencoded({ extended: true }), modemDelete); | 198 | router.post('/modem/delete', express.urlencoded({ extended: true }), modemDelete); |
189 | router.get('/modem/set-outgoing/:modemName/:newValue', pageSetOutgoing); | 199 | router.get('/modem/set-outgoing/:modemName/:newValue', pageSetOutgoing); |
190 | router.get('/modem/del-prefix/:modemName/:prefix', pageDelPrefix); | 200 | router.get('/modem/del-prefix/:modemName/:prefix', pageDelPrefix); |
191 | 201 |