Sessions are now unique - no more than 1 can exist per url
This commit is contained in:
39
main.js
39
main.js
@@ -2,12 +2,15 @@ const smpp = require("smpp");
|
|||||||
const keyboard = require("keyboardjs");
|
const keyboard = require("keyboardjs");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require("events");
|
||||||
|
|
||||||
const express = require('express');
|
const express = require("express");
|
||||||
const app = express();
|
const app = express();
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require("body-parser");
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require("ws");
|
||||||
|
|
||||||
|
const crypto = require("crypto");
|
||||||
|
|
||||||
|
|
||||||
const SERVER_PORT = process.env.SERVER_PORT || 8190;
|
const SERVER_PORT = process.env.SERVER_PORT || 8190;
|
||||||
const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191;
|
const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191;
|
||||||
@@ -112,7 +115,6 @@ class SessionStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Session {
|
class Session {
|
||||||
// TODO: Create hash based on url, username and password
|
|
||||||
auto_enquire_link_period = 500;
|
auto_enquire_link_period = 500;
|
||||||
eventEmitter = new EventEmitter();
|
eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
@@ -139,11 +141,10 @@ class Session {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
this.logger = new Logger(`Session-${this.id}`);
|
this.logger = new Logger(`Session-${this.id}`);
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
if (!this.url.includes("smpp://")) {
|
|
||||||
this.url = "smpp://" + this.url;
|
|
||||||
}
|
|
||||||
this.logger.log1(`Session created with url ${this.url}, username ${this.username}, password ${this.password} and ID ${this.id}`);
|
this.logger.log1(`Session created with url ${this.url}, username ${this.username}, password ${this.password} and ID ${this.id}`);
|
||||||
this.status = SessionStatus.NOT_CONNECTED;
|
this.status = SessionStatus.NOT_CONNECTED;
|
||||||
}
|
}
|
||||||
@@ -297,10 +298,15 @@ class SessionManager {
|
|||||||
logger = new Logger("SessionManager");
|
logger = new Logger("SessionManager");
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.sessions = [];
|
this.sessions = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
createSession(url, username, password) {
|
createSession(url, username, password) {
|
||||||
|
let urlB64 = btoa(url);
|
||||||
|
if (this.sessions[urlB64]) {
|
||||||
|
this.logger.log1(`Session to ${url} already exists`);
|
||||||
|
return this.sessions[urlB64];
|
||||||
|
}
|
||||||
this.logger.log1(`Creating session to ${url} with username ${username} and password ${password}`);
|
this.logger.log1(`Creating session to ${url} with username ${username} and password ${password}`);
|
||||||
let session = new Session(this.sessionIdCounter++, url, username, password);
|
let session = new Session(this.sessionIdCounter++, url, username, password);
|
||||||
this.addSession(session);
|
this.addSession(session);
|
||||||
@@ -309,7 +315,7 @@ class SessionManager {
|
|||||||
|
|
||||||
addSession(session) {
|
addSession(session) {
|
||||||
this.logger.log1(`Adding session with ID ${session.id}`);
|
this.logger.log1(`Adding session with ID ${session.id}`);
|
||||||
this.sessions.push(session);
|
this.sessions[btoa(session.url)] = session;
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteSession(session) {
|
deleteSession(session) {
|
||||||
@@ -317,18 +323,17 @@ class SessionManager {
|
|||||||
if (session.status === SessionStatus.BOUND || session.status === SessionStatus.CONNECTED) {
|
if (session.status === SessionStatus.BOUND || session.status === SessionStatus.CONNECTED) {
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
delete this.sessions[this.sessions.indexOf(session)];
|
delete this.sessions[btoa(session.url)];
|
||||||
this.sessions = this.sessions.filter(Boolean);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getSession(id) {
|
getSession(id) {
|
||||||
return this.sessions.find((session) => {
|
return Object.values(this.sessions).find((session) => {
|
||||||
return session.id == id;
|
return session.id == id;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize() {
|
serialize() {
|
||||||
return this.sessions.map((session) => {
|
return Object.values(this.sessions).map((session) => {
|
||||||
return session.serialize();
|
return session.serialize();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -360,7 +365,6 @@ class HTTPServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createSession(req, res) {
|
createSession(req, res) {
|
||||||
// TODO: Check for existing session
|
|
||||||
this.logger.log1("Creating session");
|
this.logger.log1("Creating session");
|
||||||
let session = sessionManager.createSession(req.body.url, req.body.username, req.body.password);
|
let session = sessionManager.createSession(req.body.url, req.body.username, req.body.password);
|
||||||
res.send(JSON.stringify(session.serialize()));
|
res.send(JSON.stringify(session.serialize()));
|
||||||
@@ -383,8 +387,7 @@ class HTTPServer {
|
|||||||
let source = req.body.source;
|
let source = req.body.source;
|
||||||
let destination = req.body.destination;
|
let destination = req.body.destination;
|
||||||
let message = req.body.message;
|
let message = req.body.message;
|
||||||
this.logger.log1(
|
this.logger.log1(`Sending message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`)
|
||||||
`Sending message from ${source} to ${destination} with message ${message} on session with ID ${req.params.id}`)
|
|
||||||
if (session) {
|
if (session) {
|
||||||
session.send(source, destination, message)
|
session.send(source, destination, message)
|
||||||
.then(pdu => res.send(JSON.stringify(pdu)))
|
.then(pdu => res.send(JSON.stringify(pdu)))
|
||||||
@@ -538,7 +541,7 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let sessionManager = new SessionManager();
|
let sessionManager = new SessionManager();
|
||||||
let session = sessionManager.createSession('localhost:7001', 'test', 'test');
|
let session = sessionManager.createSession('smpp://localhost:7001', 'test', 'test');
|
||||||
session.connect().then(() => session.bind());
|
session.connect().then(() => session.bind());
|
||||||
new WSServer();
|
new WSServer();
|
||||||
new HTTPServer();
|
new HTTPServer();
|
Reference in New Issue
Block a user