@iobroker/socket-classes 1.6.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,19 +0,0 @@
1
- const SocketAdmin = require('./lib/socketAdmin');
2
- const SocketCommon = require('./lib/socketCommon');
3
- const SocketCommands = require('./lib/socketCommands');
4
- const SocketCommandsAdmin = require('./lib/socketCommandsAdmin');
5
- const passportSocket = require('./lib/passportSocket');
6
-
7
- module.exports = {
8
- SocketAdmin,
9
- SocketCommon,
10
- SocketCommands,
11
- SocketCommandsAdmin,
12
- passportSocket,
13
- get passport() {
14
- return require('passport');
15
- },
16
- get cookieParser() {
17
- return require('cookie-parser');
18
- }
19
- };
@@ -1,201 +0,0 @@
1
- /**
2
- * Class Socket
3
- *
4
- * Copyright 2014-2022 bluefox <dogafox@gmail.com>,
5
- * MIT License
6
- *
7
- */
8
- const SocketCommon = require('./socketCommon');
9
- const SocketCommandsAdmin = require('./socketCommandsAdmin');
10
- let passport; // require('passport') - only if auth is activated
11
- let cookieParser; // require('cookie-parser') - only if auth is activated
12
- let passportSocketIo; // require('./passportSocket') - only if auth is activated
13
-
14
- class SocketAdmin extends SocketCommon {
15
- constructor(settings, adapter, objects) {
16
- super(settings, adapter);
17
-
18
- // user another set of commands for admin
19
- this.commands = new SocketCommandsAdmin(adapter, socket => this.__updateSession(socket), objects);
20
- }
21
-
22
- __getIsNoDisconnect() {
23
- // ws does not require disconnect
24
- return true;
25
- }
26
-
27
- _onAuthorizeSuccess = (data, accept) => {
28
- this.adapter.log.debug(`successful connection to socket.io from ${data.connection.remoteAddress}`);
29
- accept();
30
- };
31
-
32
- _onAuthorizeFail = (data, message, error, accept) => {
33
- setTimeout(() => data.socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE), 100);
34
-
35
- error &&
36
- this.adapter.log.info(`failed connection to socket.io from ${data.connection.remoteAddress}: ${message}`);
37
-
38
- if (error) {
39
- accept(new Error(message));
40
- } else {
41
- accept(new Error(`failed connection to socket.io: ${message}`)); //null, false);
42
- }
43
- // this error will be sent to the user as a special error-package
44
- // see: http://socket.io/docs/client-api/#socket > error-object
45
- };
46
-
47
- __initAuthentication(authOptions) {
48
- passportSocketIo = passportSocketIo || require('./passportSocket');
49
- passport = passport || require('passport');
50
- cookieParser = cookieParser || require('cookie-parser');
51
-
52
- this.store = authOptions.store;
53
-
54
- this.server.use(
55
- passportSocketIo.authorize({
56
- passport,
57
- cookieParser,
58
- key: authOptions.userKey, // the name of the cookie where express/connect stores its session_id
59
- secret: authOptions.secret, // the session_secret to parse the cookie
60
- store: authOptions.store, // we NEED to use a sessionstore. no memorystore please
61
- success: this._onAuthorizeSuccess, // *optional* callback on success - read more below
62
- fail: this._onAuthorizeFail // *optional* callback on fail/error - read more below
63
- })
64
- );
65
- }
66
-
67
- // Extract username from socket
68
- __getUserFromSocket(socket, callback) {
69
- if (socket.conn.request.sessionID) {
70
- socket._secure = true;
71
- socket._sessionID = socket.conn.request.sessionID;
72
- // Get user for session
73
- return this.adapter.getSession(socket.conn.request.sessionID, obj => {
74
- if (!obj || !obj.passport) {
75
- socket._acl.user = '';
76
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
77
- callback('Cannot detect user');
78
- } else {
79
- callback(null, obj.passport.user ? `system.user.${obj.passport.user}` : '');
80
- }
81
- });
82
- }
83
- callback('Cannot detect user');
84
- }
85
-
86
- __getClientAddress(socket) {
87
- let address;
88
- if (socket.connection) {
89
- address = socket.connection && socket.connection.remoteAddress;
90
- } else {
91
- address = socket.ws._socket.remoteAddress;
92
- }
93
-
94
- if (!address && socket.handshake) {
95
- address = socket.handshake.address;
96
- }
97
- if (!address && socket.conn.request && socket.conn.request.connection) {
98
- address = socket.conn.request.connection.remoteAddress;
99
- }
100
- return address;
101
- }
102
-
103
- // update session ID, but not ofter than 60 seconds
104
- __updateSession(socket) {
105
- if (socket._sessionID) {
106
- const time = Date.now();
107
- if (socket._lastActivity && time - socket._lastActivity > this.settings.ttl * 1000) {
108
- this.adapter.log.warn('REAUTHENTICATE!');
109
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
110
- return false;
111
- }
112
- socket._lastActivity = time;
113
- socket._sessionTimer =
114
- socket._sessionTimer ||
115
- setTimeout(() => {
116
- socket._sessionTimer = null;
117
- this.adapter.getSession(socket._sessionID, obj => {
118
- if (obj) {
119
- this.adapter.setSession(socket._sessionID, this.settings.ttl, obj);
120
- } else {
121
- this.adapter.log.warn('REAUTHENTICATE!');
122
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
123
- }
124
- });
125
- }, 60_000);
126
- }
127
-
128
- return true;
129
- }
130
-
131
- __getSessionID(socket) {
132
- return this.settings.auth && socket.conn.request.sessionID;
133
- }
134
-
135
- start(server, socketClass, authOptions, socketOptions) {
136
- super.start(server, socketClass, authOptions, socketOptions);
137
-
138
- this.commands.start(thresholdEnabled => this.onThresholdChanged(thresholdEnabled));
139
- }
140
-
141
- onThresholdChanged(enabled) {
142
- this.server && this.server.sockets && this.server.sockets.emit('eventsThreshold', enabled);
143
- }
144
-
145
- stateChange(id, state) {
146
- this.commands.stateChange(id, state);
147
-
148
- const sockets = this.getSocketsList();
149
- if (sockets) {
150
- sockets.forEach(socket => this.commands.publish(socket, 'stateChange', id, state));
151
- }
152
- }
153
-
154
- fileChange(id, fileName, size) {
155
- const sockets = this.getSocketsList();
156
- if (sockets) {
157
- sockets.forEach(socket => this.commands.publishFile(socket, id, fileName, size));
158
- }
159
- }
160
-
161
- repoUpdated() {
162
- const sockets = this.getSocketsList();
163
- if (sockets) {
164
- sockets.forEach(socket => this.__updateSession(socket));
165
-
166
- this.server.sockets.emit('repoUpdated');
167
- }
168
- }
169
-
170
- objectChange(id, obj) {
171
- const sockets = this.getSocketsList();
172
- if (sockets) {
173
- sockets.forEach(socket => this.commands.publish(socket, 'objectChange', id, obj));
174
- }
175
- }
176
-
177
- subscribe(type, pattern) {
178
- this.commands.subscribe(null, type, pattern);
179
- }
180
-
181
- subscribeFile(id, pattern) {
182
- this.commands.subscribe(null, 'fileChange', id, pattern);
183
- }
184
-
185
- sendCommand(obj) {
186
- if (this.commands.sendCommand(obj)) {
187
- const sockets = this.getSocketsList();
188
- if (sockets) {
189
- sockets.forEach(socket => this.__updateSession(socket));
190
-
191
- this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
192
- }
193
- }
194
- }
195
-
196
- updateRatings(uuid, isAutoUpdate) {
197
- return this.commands.updateRatings(uuid, isAutoUpdate);
198
- }
199
- }
200
-
201
- module.exports = SocketAdmin;