Only open port once initial sync is done

This commit is contained in:
emerson 2022-10-19 07:49:19 -04:00
parent 159846664c
commit 513b945b77
5 changed files with 30 additions and 31 deletions

View file

@ -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"]}`);
})
new IRCServer();

View file

@ -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<string>
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();

View file

@ -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<string, Batch>
constructor(public server: Server) {
constructor(public server: IRCServer) {
this.capVersion = '301';
this.enabledCaps = new Map();
this.allCaps = new Map([

View file

@ -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)});

View file

@ -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"]}`);
})
}
}