From 513b945b77b64dd11718498df566c92f5602a78f Mon Sep 17 00:00:00 2001 From: emerson Date: Wed, 19 Oct 2022 07:49:19 -0400 Subject: [PATCH] Only open port once initial sync is done --- reflection.js | 21 ++------------------- src/Channel.ts | 4 ++-- src/Client.ts | 5 ++--- src/IRCClient.ts | 4 ++-- src/Server.ts | 27 ++++++++++++++++++++++----- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/reflection.js b/reflection.js index 765d22c..c8fb8f5 100644 --- a/reflection.js +++ b/reflection.js @@ -1,20 +1,3 @@ -import { readFileSync } from 'fs'; -import { createServer } from 'tls'; -import { IRCClient } from './lib/IRCClient.js'; -import { Server } from './lib/Server.js'; +import { IRCServer } from './lib/Server.js'; -const config = JSON.parse(readFileSync(process.argv[2])); -const ircd = new Server(config); - -const listener = createServer({ - cert: readFileSync(config["certFile"]), - key: readFileSync(config["keyFile"]) -}); - -listener.on('secureConnection', (c) => { - new IRCClient(c, ircd); -}) - -listener.listen(config["port"], () => { - console.log(`Listening on port ${config["port"]}`); -}) \ No newline at end of file +new IRCServer(); \ No newline at end of file diff --git a/src/Channel.ts b/src/Channel.ts index 156742f..9f7f084 100644 --- a/src/Channel.ts +++ b/src/Channel.ts @@ -1,5 +1,5 @@ import { MatrixUser } from "./MatrixUser.js"; -import { Server } from "./Server.js"; +import { IRCServer } from "./Server.js"; export class Channel { public name: string @@ -16,7 +16,7 @@ export class Channel { public roomVersion: number public roomType: string private syncLocks: Set - constructor(public roomId: string, private server: Server) { + constructor(public roomId: string, private server: IRCServer) { this.name = roomId; this.matrixUsers = new Map(); this.powerLevels = new Map(); diff --git a/src/Client.ts b/src/Client.ts index 884cd16..bdd77aa 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,11 +1,10 @@ import { Axios } from 'axios'; import { randomUUID } from 'crypto'; -import { Socket } from 'net'; import { Batch } from './Batch.js'; import { Channel } from './Channel.js'; import { MatrixUser } from './MatrixUser.js'; import { IRCMessage, parseIRCMessage } from './Message.js'; -import { Server } from './Server.js'; +import { IRCServer } from './Server.js'; export class Client { capVersion: string @@ -15,7 +14,7 @@ export class Client { isRegistered: boolean apiCall: Axios batchesInProgress: Map - constructor(public server: Server) { + constructor(public server: IRCServer) { this.capVersion = '301'; this.enabledCaps = new Map(); this.allCaps = new Map([ diff --git a/src/IRCClient.ts b/src/IRCClient.ts index 001a4eb..738e347 100644 --- a/src/IRCClient.ts +++ b/src/IRCClient.ts @@ -1,9 +1,9 @@ import { Socket } from "net"; import { Client } from "./Client.js"; -import { Server } from './Server.js'; +import { IRCServer } from './Server.js'; export class IRCClient extends Client { - constructor(private socket: Socket , public server: Server) { + constructor(private socket: Socket , public server: IRCServer) { super(server); this.socket.on('data', (data) => this.receiveData(data)); //this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)}); diff --git a/src/Server.ts b/src/Server.ts index 2c68132..4639d4c 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -1,9 +1,13 @@ import axios, { Axios } from "axios"; +import { readFileSync } from "fs"; +import { createServer, Server } from "tls"; +import { IRCClient } from "./IRCClient.js"; import { Channel } from "./Channel.js"; import { Client } from "./Client.js"; import { MatrixUser } from "./MatrixUser.js"; -export class Server { +export class IRCServer { + public config: any public homeserver: string public mxid: string public name: string @@ -24,10 +28,12 @@ export class Server { private isSyncing: boolean private initialSync: boolean private currentSyncTime: number - constructor(public config: any) { - this.homeserver = config.homeserver; - this.mxid = config.mxid; - this.name = config.serverName; + private listener: Server; + constructor() { + this.config = JSON.parse(readFileSync(process.argv[2], {"encoding": "utf-8"})); + this.homeserver = this.config.homeserver; + this.mxid = this.config.mxid; + this.name = this.config.serverName; this.apiCall = axios.create({ baseURL: `${this.homeserver}/_matrix/client/v3`, timeout: 180000, @@ -49,6 +55,10 @@ export class Server { this.initialSync = false; this.currentSyncTime = 0; setInterval(this.doSync.bind(this), 2000); + this.listener = createServer({ + cert: readFileSync(this.config["certFile"]), + key: readFileSync(this.config["keyFile"]) + }); this.ourMatrixUser = this.getOrCreateMatrixUser(this.mxid); this.apiCall.get("/account/whoami").then(r => { this.doLog("Authentication successful, starting initial sync"); @@ -143,6 +153,13 @@ export class Server { if (this.initialSync === false && this.syncLocks.size === 0) { this.initialSync = true; this.doLog('Synced to network!'); + this.listener.on('secureConnection', (c) => { + new IRCClient(c, this); + }) + + this.listener.listen(this.config["port"], () => { + console.log(`Listening on port ${this.config["port"]}`); + }) } }