This file is indexed.

/usr/share/maas/web/static/js/angular/factories/region.js is in maas-region-api 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright 2015-2016 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * MAAS Region Connection
 *
 * Provides the websocket connection between the client and the MAAS regiond
 * service.
 */

angular.module('MAAS').factory(
    'RegionConnection',
    ['$q', '$rootScope', '$timeout', '$window', '$cookies', function(
        $q, $rootScope, $timeout, $window, $cookies) {

        // Message types
        var MSG_TYPE = {
            REQUEST: 0,
            RESPONSE: 1,
            NOTIFY: 2
        };

        // Response types
        var RESPONSE_TYPE = {
            SUCCESS: 0,
            ERROR: 1
        };

        // Constructor
        function RegionConnection() {
            this.callbacks = {};
            this.requests = {};
            this.requestId = 0;
            this.url = null;
            this.websocket = null;
            this.connected = false;
            this.autoReconnect = true;
            this.retryTimeout = 5000;
            this.error = null;

            // Defer used for defaultConnect. If defaultConnect is called
            // quickly only the first one will start the connection. The
            // remaining will recieve this defer.
            this.defaultConnectDefer = null;

            // List of functions to call when a WebSocket event occurs. Each
            // function will get the WebSocket event passed to it.
            this.handlers = {
                open: [],
                error: [],
                close: []
            };

            // Object containing a fields with list of functions. When
            // a NOTIFY message is received it will match the name to a field
            // in this object. If the field exists in the object the list
            // of functions will be called with the action and obj_id.
            this.notifiers = {};
        }

        // Return a new request id.
        RegionConnection.prototype.newRequestId = function() {
            this.requestId += 1;
            return this.requestId;
        };

        // Register event handler.
        RegionConnection.prototype.registerHandler = function (name, func) {
            if(!angular.isDefined(this.handlers[name])) {
                throw new Error("Invalid handler: " + name);
            }
            if(!angular.isFunction(func)) {
                throw new Error("Requires a function to register a handler.");
            }
            this.handlers[name].push(func);
        };

        // Unregister event handler.
        RegionConnection.prototype.unregisterHandler = function (name, func) {
            if(!angular.isDefined(this.handlers[name])) {
                throw new Error("Invalid handler: " + name);
            }
            var idx = this.handlers[name].indexOf(func);
            if(idx >= 0) {
                this.handlers[name].splice(idx, 1);
            }
        };

        // Register notification handler.
        RegionConnection.prototype.registerNotifier = function(name, func) {
            if(!angular.isFunction(func)) {
                throw new Error("Requires a function to register a notifier.");
            }
            if(angular.isUndefined(this.notifiers[name])) {
                this.notifiers[name] = [];
            }
            this.notifiers[name].push(func);
        };

        // Unregister notification handler.
        RegionConnection.prototype.unregisterNotifier = function(name, func) {
            if(angular.isUndefined(this.notifiers[name])) {
                return;
            }
            var idx = this.notifiers[name].indexOf(func);
            if(idx >= 0) {
                this.notifiers[name].splice(idx, 1);
            }
        };

        // Return True if currently connected to region.
        RegionConnection.prototype.isConnected = function() {
            return this.connected;
        };

        // Builds the websocket connection.
        RegionConnection.prototype.buildSocket = function(url) {
            return new WebSocket(url);
        };

        // Opens the websocket connection.
        RegionConnection.prototype.connect = function() {
            this.url = this._buildUrl();
            this.autoReconnect = true;
            this.websocket = this.buildSocket(this.url);

            var self = this;
            this.websocket.onopen = function(evt) {
                self.connected = true;
                angular.forEach(self.handlers.open, function(func) {
                    func(evt);
                });
            };
            this.websocket.onerror = function(evt) {
                angular.forEach(self.handlers.error, function(func) {
                    func(evt);
                });
            };
            this.websocket.onclose = function(evt) {
                self.connected = false;
                self.error = "Unable to connect to: " + self.url.split("?")[0];
                angular.forEach(self.handlers.close, function(func) {
                    func(evt);
                });
                if(self.autoReconnect) {
                    $timeout(function() {
                        self.connect();
                    }, self.retryTimeout);
                }
            };
            this.websocket.onmessage = function(evt) {
                self.onMessage(angular.fromJson(evt.data));
            };
        };

        // Closes the websocket connection.
        RegionConnection.prototype.close = function() {
            this.autoReconnect = false;
            this.websocket.close();
            this.websocket = null;
        };

        // Return the protocol used for the websocket connection.
        RegionConnection.prototype._getProtocol = function() {
            return $window.location.protocol;
        };

        // Return connection url to websocket from current location and
        // html options.
        RegionConnection.prototype._buildUrl = function() {
            var host = $window.location.hostname;
            var port = $window.location.port;
            var path = $window.location.pathname;
            var proto = 'ws';
            if (this._getProtocol() === 'https:') {
                proto = 'wss';
            }

            // Path and port can be overridden by href and data-websocket-port
            // in the base element respectively.
            var base = angular.element("base");
            if(angular.isDefined(base)) {
                var newPath = base.attr("href");
                if(angular.isDefined(newPath)) {
                    path = newPath;
                }
                var newPort = base.data("websocket-port");
                if(angular.isDefined(newPort)) {
                    port = newPort;
                }
            }

            // Append final '/' if missing from end of path.
            if(path[path.length - 1] !== '/') {
                path += '/';
            }

            // Build the URL. Include the :port only if it has a value.
            url = proto + "://" + host;
            if(angular.isString(port) && port.length > 0){
                url += ":" + port;
            }
            url += path + "ws";

            // Include the csrftoken in the URL if it's defined.
            var csrftoken;
            if(angular.isFunction($cookies.get)) {
                csrftoken = $cookies.get('csrftoken');
            } else {
                csrftoken = $cookies.csrftoken;
            }
            if(angular.isDefined(csrftoken)) {
                url += '?csrftoken=' + encodeURIComponent(csrftoken);
            }

            return url;
        };

        // Opens the default websocket connection.
        RegionConnection.prototype.defaultConnect = function() {
            // Already been called but the connection has not been completed.
            if(angular.isObject(this.defaultConnectDefer)) {
                return this.defaultConnectDefer.promise;
            }

            // Already connected.
            var defer;
            if(this.isConnected()) {
                // Create a new defer as the defaultConnectDefer would
                // have already been resolved.
                defer = $q.defer();

                // Cannot resolve the defer inline as it hasn't been given
                // back to the caller. It will be called in the next loop.
                $timeout(defer.resolve);
                return defer.promise;
            }

            // Start the connection.
            var self = this, opened, errored;
            defer = this.defaultConnectDefer = $q.defer();
            opened = function(evt) {
                this.defaultConnectDefer = null;
                self.unregisterHandler("open", opened);
                self.unregisterHandler("error", errored);
                $rootScope.$apply(defer.resolve(evt));
            };
            errored = function(evt) {
                this.defaultConnectDefer = null;
                self.unregisterHandler("open", opened);
                self.unregisterHandler("error", errored);
                $rootScope.$apply(defer.reject(evt));
            };
            this.registerHandler("open", opened);
            this.registerHandler("error", errored);
            this.connect();
            return defer.promise;
        };

        // Called when a message is received.
        RegionConnection.prototype.onMessage = function(msg) {
            // Response
            if(msg.type === MSG_TYPE.RESPONSE) {
                this.onResponse(msg);
            // Notify
            } else if(msg.type === MSG_TYPE.NOTIFY) {
                this.onNotify(msg);
            }
        };

        // Called when a response message is recieved.
        RegionConnection.prototype.onResponse = function(msg) {
            // Grab the registered defer from the callbacks list.
            var defer = this.callbacks[msg.request_id];
            var remembered_request = this.requests[msg.request_id];
            if(angular.isDefined(defer)) {
                if(msg.rtype === RESPONSE_TYPE.SUCCESS) {
                    // Resolve the defer inside of the digest cycle, so any
                    // update to an object or collection will trigger a
                    // watcher.
                    $rootScope.$apply(defer.resolve(msg.result));
                } else if(msg.rtype === RESPONSE_TYPE.ERROR) {
                    // Reject the defer since an error occurred.
                    if(angular.isObject(remembered_request)) {
                        $rootScope.$apply(defer.reject({
                            "error": msg.error,
                            "request": remembered_request
                        }));
                    } else {
                        $rootScope.$apply(defer.reject(msg.error));
                    }
                }
                // Remove the defer from the callback list.
                delete this.callbacks[msg.request_id];
                delete this.requests[msg.request_id];
            }
        };

        // Called when a notify response is recieved.
        RegionConnection.prototype.onNotify = function(msg) {
            var handlers = this.notifiers[msg.name];
            if(angular.isArray(handlers)) {
                angular.forEach(handlers, function(handler) {
                    handler(msg.action, msg.data);
                });
            }
        };

        // Call method on the region.
        RegionConnection.prototype.callMethod = function(
                method, params, remember) {
            var defer = $q.defer();
            var request_id = this.newRequestId();
            var request = {
                type: MSG_TYPE.REQUEST,
                request_id: request_id,
                method: method,
                params: params
            };
            this.callbacks[request_id] = defer;
            // If requested, remember what the details of the request were,
            // so that the controller can refresh its memory.
            if (remember) {
                this.requests[request_id] = request;
            }
            this.websocket.send(angular.toJson(request));
            return defer.promise;
        };

        return new RegionConnection();
    }]);