Merge pull request #3158 from dibdot/banIP
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / rpc.js
1 'use strict';
2
3 var rpcRequestID = 1,
4 rpcSessionID = L.env.sessionid || '00000000000000000000000000000000',
5 rpcBaseURL = L.url('admin/ubus'),
6 rpcInterceptorFns = [];
7
8 /**
9 * @class rpc
10 * @memberof LuCI
11 * @hideconstructor
12 * @classdesc
13 *
14 * The `LuCI.rpc` class provides high level ubus JSON-RPC abstractions
15 * and means for listing and invoking remove RPC methods.
16 */
17 return L.Class.extend(/** @lends LuCI.rpc.prototype */ {
18 /* privates */
19 call: function(req, cb, nobatch) {
20 var q = '';
21
22 if (Array.isArray(req)) {
23 if (req.length == 0)
24 return Promise.resolve([]);
25
26 for (var i = 0; i < req.length; i++)
27 if (req[i].params)
28 q += '%s%s.%s'.format(
29 q ? ';' : '/',
30 req[i].params[1],
31 req[i].params[2]
32 );
33 }
34 else if (req.params) {
35 q += '/%s.%s'.format(req.params[1], req.params[2]);
36 }
37
38 return L.Request.post(rpcBaseURL + q, req, {
39 timeout: (L.env.rpctimeout || 20) * 1000,
40 nobatch: nobatch,
41 credentials: true
42 }).then(cb, cb);
43 },
44
45 parseCallReply: function(req, res) {
46 var msg = null;
47
48 if (res instanceof Error)
49 return req.reject(res);
50
51 try {
52 if (!res.ok)
53 L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
54 req.object, req.method, res.status, res.statusText || '?');
55
56 msg = res.json();
57 }
58 catch (e) {
59 return req.reject(e);
60 }
61
62 /*
63 * The interceptor args are intentionally swapped.
64 * Response is passed as first arg to align with Request class interceptors
65 */
66 Promise.all(rpcInterceptorFns.map(function(fn) { return fn(msg, req) }))
67 .then(this.handleCallReply.bind(this, req, msg))
68 .catch(req.reject);
69 },
70
71 handleCallReply: function(req, msg) {
72 var type = Object.prototype.toString,
73 ret = null;
74
75 try {
76 /* verify message frame */
77 if (!L.isObject(msg) || msg.jsonrpc != '2.0')
78 L.raise('RPCError', 'RPC call to %s/%s returned invalid message frame',
79 req.object, req.method);
80
81 /* check error condition */
82 if (L.isObject(msg.error) && msg.error.code && msg.error.message)
83 L.raise('RPCError', 'RPC call to %s/%s failed with error %d: %s',
84 req.object, req.method, msg.error.code, msg.error.message || '?');
85 }
86 catch (e) {
87 return req.reject(e);
88 }
89
90 if (!req.object && !req.method) {
91 ret = msg.result;
92 }
93 else if (Array.isArray(msg.result)) {
94 ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
95 }
96
97 if (req.expect) {
98 for (var key in req.expect) {
99 if (ret != null && key != '')
100 ret = ret[key];
101
102 if (ret == null || type.call(ret) != type.call(req.expect[key]))
103 ret = req.expect[key];
104
105 break;
106 }
107 }
108
109 /* apply filter */
110 if (typeof(req.filter) == 'function') {
111 req.priv[0] = ret;
112 req.priv[1] = req.params;
113 ret = req.filter.apply(this, req.priv);
114 }
115
116 req.resolve(ret);
117 },
118
119 /**
120 * Lists available remote ubus objects or the method signatures of
121 * specific objects.
122 *
123 * This function has two signatures and is sensitive to the number of
124 * arguments passed to it:
125 * - `list()` -
126 * Returns an array containing the names of all remote `ubus` objects
127 * - `list("objname", ...)`
128 * Returns method signatures for each given `ubus` object name.
129 *
130 * @param {...string} [objectNames]
131 * If any object names are given, this function will return the method
132 * signatures of each given object.
133 *
134 * @returns {Promise<Array<string>|Object<string, Object<string, Object<string, string>>>>}
135 * When invoked without arguments, this function will return a promise
136 * resolving to an array of `ubus` object names. When invoked with one or
137 * more arguments, a promise resolving to an object describing the method
138 * signatures of each requested `ubus` object name will be returned.
139 */
140 list: function() {
141 var msg = {
142 jsonrpc: '2.0',
143 id: rpcRequestID++,
144 method: 'list',
145 params: arguments.length ? this.varargs(arguments) : undefined
146 };
147
148 return new Promise(L.bind(function(resolveFn, rejectFn) {
149 /* store request info */
150 var req = {
151 resolve: resolveFn,
152 reject: rejectFn
153 };
154
155 /* call rpc */
156 this.call(msg, this.parseCallReply.bind(this, req));
157 }, this));
158 },
159
160 /**
161 * @typedef {Object} DeclareOptions
162 * @memberof LuCI.rpc
163 *
164 * @property {string} object
165 * The name of the remote `ubus` object to invoke.
166 *
167 * @property {string} method
168 * The name of the remote `ubus` method to invoke.
169 *
170 * @property {string[]} [params]
171 * Lists the named parameters expected by the remote `ubus` RPC method.
172 * The arguments passed to the resulting generated method call function
173 * will be mapped to named parameters in the order they appear in this
174 * array.
175 *
176 * Extraneous parameters passed to the generated function will not be
177 * sent to the remote procedure but are passed to the
178 * {@link LuCI.rpc~filterFn filter function} if one is specified.
179 *
180 * Examples:
181 * - `params: [ "foo", "bar" ]` -
182 * When the resulting call function is invoked with `fn(true, false)`,
183 * the corresponding args object sent to the remote procedure will be
184 * `{ foo: true, bar: false }`.
185 * - `params: [ "test" ], filter: function(reply, args, extra) { ... }` -
186 * When the resultung generated function is invoked with
187 * `fn("foo", "bar", "baz")` then `{ "test": "foo" }` will be sent as
188 * argument to the remote procedure and the filter function will be
189 * invoked with `filterFn(reply, [ "foo" ], "bar", "baz")`
190 *
191 * @property {Object<string,*>} [expect]
192 * Describes the expected return data structure. The given object is
193 * supposed to contain a single key selecting the value to use from
194 * the returned `ubus` reply object. The value of the sole key within
195 * the `expect` object is used to infer the expected type of the received
196 * `ubus` reply data.
197 *
198 * If the received data does not contain `expect`'s key, or if the
199 * type of the data differs from the type of the value in the expect
200 * object, the expect object's value is returned as default instead.
201 *
202 * The key in the `expect` object may be an empty string (`''`) in which
203 * case the entire reply object is selected instead of one of its subkeys.
204 *
205 * If the `expect` option is omitted, the received reply will be returned
206 * as-is, regardless of its format or type.
207 *
208 * Examples:
209 * - `expect: { '': { error: 'Invalid response' } }` -
210 * This requires the entire `ubus` reply to be a plain JavaScript
211 * object. If the reply isn't an object but e.g. an array or a numeric
212 * error code instead, it will get replaced with
213 * `{ error: 'Invalid response' }` instead.
214 * - `expect: { results: [] }` -
215 * This requires the received `ubus` reply to be an object containing
216 * a key `results` with an array as value. If the received reply does
217 * not contain such a key, or if `reply.results` points to a non-array
218 * value, the empty array (`[]`) will be used instead.
219 * - `expect: { success: false }` -
220 * This requires the received `ubus` reply to be an object containing
221 * a key `success` with a boolean value. If the reply does not contain
222 * `success` or if `reply.success` is not a boolean value, `false` will
223 * be returned as default instead.
224 *
225 * @property {LuCI.rpc~filterFn} [filter]
226 * Specfies an optional filter function which is invoked to transform the
227 * received reply data before it is returned to the caller.
228 *
229 */
230
231 /**
232 * The filter function is invoked to transform a received `ubus` RPC call
233 * reply before returning it to the caller.
234 *
235 * @callback LuCI.rpc~filterFn
236 *
237 * @param {*} data
238 * The received `ubus` reply data or a subset of it as described in the
239 * `expect` option of the RPC call declaration. In case of remote call
240 * errors, `data` is numeric `ubus` error code instead.
241 *
242 * @param {Array<*>} args
243 * The arguments the RPC method has been invoked with.
244 *
245 * @param {...*} extraArgs
246 * All extraneous arguments passed to the RPC method exceeding the number
247 * of arguments describes in the RPC call declaration.
248 *
249 * @return {*}
250 * The return value of the filter function will be returned to the caller
251 * of the RPC method as-is.
252 */
253
254 /**
255 * The generated invocation function is returned by
256 * {@link LuCI.rpc#declare rpc.declare()} and encapsulates a single
257 * RPC method call.
258 *
259 * Calling this function will execute a remote `ubus` HTTP call request
260 * using the arguments passed to it as arguments and return a promise
261 * resolving to the received reply values.
262 *
263 * @callback LuCI.rpc~invokeFn
264 *
265 * @param {...*} params
266 * The parameters to pass to the remote procedure call. The given
267 * positional arguments will be named to named RPC parameters according
268 * to the names specified in the `params` array of the method declaration.
269 *
270 * Any additional parameters exceeding the amount of arguments in the
271 * `params` declaration are passed as private extra arguments to the
272 * declared filter function.
273 *
274 * @return {Promise<*>}
275 * Returns a promise resolving to the result data of the remote `ubus`
276 * RPC method invocation, optionally substituted and filtered according
277 * to the `expect` and `filter` declarations.
278 */
279
280 /**
281 * Describes a remote RPC call procedure and returns a function
282 * implementing it.
283 *
284 * @param {LuCI.rpc.DeclareOptions} options
285 * If any object names are given, this function will return the method
286 * signatures of each given object.
287 *
288 * @returns {LuCI.rpc~invokeFn}
289 * Returns a new function implementing the method call described in
290 * `options`.
291 */
292 declare: function(options) {
293 return Function.prototype.bind.call(function(rpc, options) {
294 var args = this.varargs(arguments, 2);
295 return new Promise(function(resolveFn, rejectFn) {
296 /* build parameter object */
297 var p_off = 0;
298 var params = { };
299 if (Array.isArray(options.params))
300 for (p_off = 0; p_off < options.params.length; p_off++)
301 params[options.params[p_off]] = args[p_off];
302
303 /* all remaining arguments are private args */
304 var priv = [ undefined, undefined ];
305 for (; p_off < args.length; p_off++)
306 priv.push(args[p_off]);
307
308 /* store request info */
309 var req = {
310 expect: options.expect,
311 filter: options.filter,
312 resolve: resolveFn,
313 reject: rejectFn,
314 params: params,
315 priv: priv,
316 object: options.object,
317 method: options.method
318 };
319
320 /* build message object */
321 var msg = {
322 jsonrpc: '2.0',
323 id: rpcRequestID++,
324 method: 'call',
325 params: [
326 rpcSessionID,
327 options.object,
328 options.method,
329 params
330 ]
331 };
332
333 /* call rpc */
334 rpc.call(msg, rpc.parseCallReply.bind(rpc, req), options.nobatch);
335 });
336 }, this, this, options);
337 },
338
339 /**
340 * Returns the current RPC session id.
341 *
342 * @returns {string}
343 * Returns the 32 byte session ID string used for authenticating remote
344 * requests.
345 */
346 getSessionID: function() {
347 return rpcSessionID;
348 },
349
350 /**
351 * Set the RPC session id to use.
352 *
353 * @param {string} sid
354 * Sets the 32 byte session ID string used for authenticating remote
355 * requests.
356 */
357 setSessionID: function(sid) {
358 rpcSessionID = sid;
359 },
360
361 /**
362 * Returns the current RPC base URL.
363 *
364 * @returns {string}
365 * Returns the RPC URL endpoint to issue requests against.
366 */
367 getBaseURL: function() {
368 return rpcBaseURL;
369 },
370
371 /**
372 * Set the RPC base URL to use.
373 *
374 * @param {string} sid
375 * Sets the RPC URL endpoint to issue requests against.
376 */
377 setBaseURL: function(url) {
378 rpcBaseURL = url;
379 },
380
381 /**
382 * Translates a numeric `ubus` error code into a human readable
383 * description.
384 *
385 * @param {number} statusCode
386 * The numeric status code.
387 *
388 * @returns {string}
389 * Returns the textual description of the code.
390 */
391 getStatusText: function(statusCode) {
392 switch (statusCode) {
393 case 0: return _('Command OK');
394 case 1: return _('Invalid command');
395 case 2: return _('Invalid argument');
396 case 3: return _('Method not found');
397 case 4: return _('Resource not found');
398 case 5: return _('No data received');
399 case 6: return _('Permission denied');
400 case 7: return _('Request timeout');
401 case 8: return _('Not supported');
402 case 9: return _('Unspecified error');
403 case 10: return _('Connection lost');
404 default: return _('Unknown error code');
405 }
406 },
407
408 /**
409 * Registered interceptor functions are invoked before the standard reply
410 * parsing and handling logic.
411 *
412 * By returning rejected promises, interceptor functions can cause the
413 * invocation function to fail, regardless of the received reply.
414 *
415 * Interceptors may also modify their message argument in-place to
416 * rewrite received replies before they're processed by the standard
417 * response handling code.
418 *
419 * A common use case for such functions is to detect failing RPC replies
420 * due to expired authentication in order to trigger a new login.
421 *
422 * @callback LuCI.rpc~interceptorFn
423 *
424 * @param {*} msg
425 * The unprocessed, JSON decoded remote RPC method call reply.
426 *
427 * Since interceptors run before the standard parsing logic, the reply
428 * data is not verified for correctness or filtered according to
429 * `expect` and `filter` specifications in the declarations.
430 *
431 * @param {Object} req
432 * The related request object which is an extended variant of the
433 * declaration object, allowing access to internals of the invocation
434 * function such as `filter`, `expect` or `params` values.
435 *
436 * @return {Promise<*>|*}
437 * Interceptor functions may return a promise to defer response
438 * processing until some delayed work completed. Any values the returned
439 * promise resolves to are ignored.
440 *
441 * When the returned promise rejects with an error, the invocation
442 * function will fail too, forwarding the error to the caller.
443 */
444
445 /**
446 * Registers a new interceptor function.
447 *
448 * @param {LuCI.rpc~interceptorFn} interceptorFn
449 * The inteceptor function to register.
450 *
451 * @returns {LuCI.rpc~interceptorFn}
452 * Returns the given function value.
453 */
454 addInterceptor: function(interceptorFn) {
455 if (typeof(interceptorFn) == 'function')
456 rpcInterceptorFns.push(interceptorFn);
457 return interceptorFn;
458 },
459
460 /**
461 * Removes a registered interceptor function.
462 *
463 * @param {LuCI.rpc~interceptorFn} interceptorFn
464 * The inteceptor function to remove.
465 *
466 * @returns {boolean}
467 * Returns `true` if the given function has been removed or `false`
468 * if it has not been found.
469 */
470 removeInterceptor: function(interceptorFn) {
471 var oldlen = rpcInterceptorFns.length, i = oldlen;
472 while (i--)
473 if (rpcInterceptorFns[i] === interceptorFn)
474 rpcInterceptorFns.splice(i, 1);
475 return (rpcInterceptorFns.length < oldlen);
476 }
477 });