@minimaltech/node-infra 0.4.2 → 0.4.3
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.
@@ -1,31 +1,29 @@
|
|
1
1
|
import { BaseHelper } from '../../base/base.helper';
|
2
|
-
import {
|
2
|
+
import { ValueOrPromise } from '../../common/types';
|
3
|
+
import { dayjs } from '../../utilities/date.utility';
|
3
4
|
import { ListenOptions, Socket as SocketClient, createServer } from 'net';
|
4
5
|
interface ITcpSocketClient {
|
5
6
|
id: string;
|
6
7
|
socket: SocketClient;
|
7
8
|
state: 'unauthorized' | 'authenticating' | 'authenticated';
|
8
9
|
subscriptions: Set<string>;
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
};
|
14
|
-
localAddress?: {
|
15
|
-
host?: string;
|
16
|
-
port?: number;
|
17
|
-
family?: string;
|
10
|
+
storage: {
|
11
|
+
connectedAt: dayjs.Dayjs;
|
12
|
+
authenticatedAt: dayjs.Dayjs | null;
|
13
|
+
[additionField: symbol | string]: any;
|
18
14
|
};
|
19
15
|
}
|
20
16
|
export interface ITcpSocketServerOptions {
|
21
17
|
identifier: string;
|
22
18
|
serverOptions: Partial<ListenOptions>;
|
23
|
-
|
24
|
-
|
19
|
+
authenticateOptions: {
|
20
|
+
required: boolean;
|
21
|
+
duration?: number;
|
22
|
+
};
|
25
23
|
extraEvents?: Record<string, (opts: {
|
26
24
|
id: string;
|
27
25
|
socket: SocketClient;
|
28
|
-
|
26
|
+
args: any;
|
29
27
|
}) => ValueOrPromise<void>>;
|
30
28
|
onServerReady?: (opts: {
|
31
29
|
server: ReturnType<typeof createServer>;
|
@@ -51,9 +49,9 @@ export interface ITcpSocketServerOptions {
|
|
51
49
|
}
|
52
50
|
export declare class NetworkTcpServer extends BaseHelper {
|
53
51
|
private serverOptions;
|
52
|
+
private authenticateOptions;
|
54
53
|
private clients;
|
55
54
|
private server;
|
56
|
-
private parser?;
|
57
55
|
private extraEvents;
|
58
56
|
private onServerReady?;
|
59
57
|
private onClientConnected?;
|
@@ -71,6 +69,10 @@ export declare class NetworkTcpServer extends BaseHelper {
|
|
71
69
|
id: string;
|
72
70
|
}): ITcpSocketClient;
|
73
71
|
getServer(): import("net").Server;
|
72
|
+
doAuthenticate(opts: {
|
73
|
+
id: string;
|
74
|
+
state: 'unauthorized' | 'authenticating' | 'authenticated';
|
75
|
+
}): void;
|
74
76
|
emit(opts: {
|
75
77
|
clientId: string;
|
76
78
|
payload: Buffer | string;
|
@@ -1,8 +1,14 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
6
|
exports.NetworkTcpServer = void 0;
|
4
7
|
const base_helper_1 = require("../../base/base.helper");
|
8
|
+
const utilities_1 = require("../../utilities");
|
9
|
+
const date_utility_1 = require("../../utilities/date.utility");
|
5
10
|
const parse_utility_1 = require("../../utilities/parse.utility");
|
11
|
+
const omit_1 = __importDefault(require("lodash/omit"));
|
6
12
|
const net_1 = require("net");
|
7
13
|
class NetworkTcpServer extends base_helper_1.BaseHelper {
|
8
14
|
constructor(opts) {
|
@@ -11,7 +17,13 @@ class NetworkTcpServer extends base_helper_1.BaseHelper {
|
|
11
17
|
this.serverOptions = {};
|
12
18
|
this.clients = Object.assign({});
|
13
19
|
this.serverOptions = opts.serverOptions;
|
14
|
-
this.
|
20
|
+
this.authenticateOptions = opts.authenticateOptions;
|
21
|
+
if (this.authenticateOptions.required &&
|
22
|
+
(!this.authenticateOptions.duration || this.authenticateOptions.duration < 0)) {
|
23
|
+
throw (0, utilities_1.getError)({
|
24
|
+
message: 'TCP Server | Invalid authenticate duration | Required duration for authenticateOptions',
|
25
|
+
});
|
26
|
+
}
|
15
27
|
this.extraEvents = (_a = opts.extraEvents) !== null && _a !== void 0 ? _a : {};
|
16
28
|
this.onServerReady = opts.onServerReady;
|
17
29
|
this.onClientConnected = opts.onClientConnected;
|
@@ -38,37 +50,53 @@ class NetworkTcpServer extends base_helper_1.BaseHelper {
|
|
38
50
|
const id = (0, parse_utility_1.getUID)();
|
39
51
|
socket.on('data', (data) => {
|
40
52
|
var _a;
|
41
|
-
this.
|
42
|
-
(_a = this.onClientData) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket, data: this.parser ? this.parser(data) : data });
|
53
|
+
(_a = this.onClientData) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket, data });
|
43
54
|
});
|
44
55
|
socket.on('error', (error) => {
|
45
56
|
var _a;
|
46
57
|
this.logger.error('[onClientConnect][error] ID: %s | Error: %s', id, error);
|
47
58
|
(_a = this.onClientError) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket, error });
|
59
|
+
socket.end();
|
48
60
|
});
|
49
61
|
socket.on('close', (hasError) => {
|
50
62
|
var _a;
|
51
63
|
this.logger.info('[onClientConnect][close] ID: %s | hasError: %s', id, hasError);
|
52
64
|
(_a = this.onClientClose) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket });
|
65
|
+
this.clients = (0, omit_1.default)(this.clients, [id]);
|
53
66
|
});
|
54
67
|
for (const extraEvent in this.extraEvents) {
|
55
|
-
socket.on(extraEvent,
|
56
|
-
this.extraEvents[extraEvent]({ id, socket,
|
68
|
+
socket.on(extraEvent, args => {
|
69
|
+
this.extraEvents[extraEvent]({ id, socket, args });
|
57
70
|
});
|
58
71
|
}
|
59
72
|
this.clients[id] = {
|
60
73
|
id,
|
61
74
|
socket,
|
62
|
-
state: 'unauthorized',
|
75
|
+
state: this.authenticateOptions.required ? 'unauthorized' : 'authenticated',
|
63
76
|
subscriptions: new Set([]),
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
family: socket.remoteFamily,
|
77
|
+
storage: {
|
78
|
+
connectedAt: (0, date_utility_1.dayjs)(),
|
79
|
+
authenticatedAt: this.authenticateOptions.required ? null : (0, date_utility_1.dayjs)(),
|
68
80
|
},
|
69
81
|
};
|
70
|
-
this.logger.info('[onClientConnect] New TCP SocketClient | Client: %s', `${id} - ${socket.remoteAddress} - ${socket.remotePort} - ${socket.remoteFamily}
|
82
|
+
this.logger.info('[onClientConnect] New TCP SocketClient | Client: %s | authenticateOptions: %s %s', `${id} - ${socket.remoteAddress} - ${socket.remotePort} - ${socket.remoteFamily}`, this.authenticateOptions.required, this.authenticateOptions.duration);
|
71
83
|
(_a = this.onClientConnected) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket });
|
84
|
+
// Check client authentication
|
85
|
+
if (this.authenticateOptions.required &&
|
86
|
+
this.authenticateOptions.duration &&
|
87
|
+
this.authenticateOptions.duration > 0) {
|
88
|
+
setTimeout(() => {
|
89
|
+
const client = this.getClient({ id });
|
90
|
+
if (!client) {
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
if (client.state === 'authenticated') {
|
94
|
+
return;
|
95
|
+
}
|
96
|
+
this.emit({ clientId: id, payload: 'Unauthorized Client' });
|
97
|
+
client.socket.end();
|
98
|
+
}, this.authenticateOptions.duration);
|
99
|
+
}
|
72
100
|
}
|
73
101
|
getClients() {
|
74
102
|
return this.clients;
|
@@ -80,6 +108,29 @@ class NetworkTcpServer extends base_helper_1.BaseHelper {
|
|
80
108
|
getServer() {
|
81
109
|
return this.server;
|
82
110
|
}
|
111
|
+
doAuthenticate(opts) {
|
112
|
+
const { id, state } = opts;
|
113
|
+
const client = this.getClient({ id });
|
114
|
+
if (!client) {
|
115
|
+
this.logger.error('[authenticateClient][%s] Client %s NOT FOUND', id);
|
116
|
+
return;
|
117
|
+
}
|
118
|
+
client.state = state;
|
119
|
+
switch (state) {
|
120
|
+
case 'unauthorized':
|
121
|
+
case 'authenticating': {
|
122
|
+
client.storage.authenticatedAt = null;
|
123
|
+
break;
|
124
|
+
}
|
125
|
+
case 'authenticated': {
|
126
|
+
client.storage.authenticatedAt = (0, date_utility_1.dayjs)();
|
127
|
+
break;
|
128
|
+
}
|
129
|
+
default: {
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
83
134
|
emit(opts) {
|
84
135
|
const { clientId, payload } = opts;
|
85
136
|
const client = this.getClient({ id: clientId });
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"network-tcp-server.helper.js","sourceRoot":"","sources":["../../../src/helpers/network/network-tcp-server.helper.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"network-tcp-server.helper.js","sourceRoot":"","sources":["../../../src/helpers/network/network-tcp-server.helper.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAgD;AAEhD,2CAAuC;AACvC,2DAAiD;AACjD,6DAAmD;AACnD,uDAA+B;AAC/B,6BAA0E;AAgC1E,MAAa,gBAAiB,SAAQ,wBAAU;IAuB9C,YAAY,IAA6B;;QACvC,KAAK,CAAC,EAAE,KAAK,EAAE,gBAAgB,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;QAvB/D,kBAAa,GAA2B,EAAE,CAAC;QAyBjD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAEjC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,mBAAmB,CAAC;QAEpD,IACE,IAAI,CAAC,mBAAmB,CAAC,QAAQ;YACjC,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,QAAQ,IAAI,IAAI,CAAC,mBAAmB,CAAC,QAAQ,GAAG,CAAC,CAAC,EAC7E,CAAC;YACD,MAAM,IAAA,oBAAQ,EAAC;gBACb,OAAO,EACL,wFAAwF;aAC3F,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,WAAW,GAAG,MAAA,IAAI,CAAC,WAAW,mCAAI,EAAE,CAAC;QAE1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QAC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