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 { IRCServer } from './lib/Server.js';
import { createServer } from 'tls';
import { IRCClient } from './lib/IRCClient.js';
import { Server } from './lib/Server.js';
const config = JSON.parse(readFileSync(process.argv[2])); new IRCServer();
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"]}`);
})

View file

@ -1,5 +1,5 @@
import { MatrixUser } from "./MatrixUser.js"; import { MatrixUser } from "./MatrixUser.js";
import { Server } from "./Server.js"; import { IRCServer } from "./Server.js";
export class Channel { export class Channel {
public name: string public name: string
@ -16,7 +16,7 @@ export class Channel {
public roomVersion: number public roomVersion: number
public roomType: string public roomType: string
private syncLocks: Set<string> private syncLocks: Set<string>
constructor(public roomId: string, private server: Server) { constructor(public roomId: string, private server: IRCServer) {
this.name = roomId; this.name = roomId;
this.matrixUsers = new Map(); this.matrixUsers = new Map();
this.powerLevels = new Map(); this.powerLevels = new Map();

View file

@ -1,11 +1,10 @@
import { Axios } from 'axios'; import { Axios } from 'axios';
import { randomUUID } from 'crypto'; import { randomUUID } from 'crypto';
import { Socket } from 'net';
import { Batch } from './Batch.js'; import { Batch } from './Batch.js';
import { Channel } from './Channel.js'; import { Channel } from './Channel.js';
import { MatrixUser } from './MatrixUser.js'; import { MatrixUser } from './MatrixUser.js';
import { IRCMessage, parseIRCMessage } from './Message.js'; import { IRCMessage, parseIRCMessage } from './Message.js';
import { Server } from './Server.js'; import { IRCServer } from './Server.js';
export class Client { export class Client {
capVersion: string capVersion: string
@ -15,7 +14,7 @@ export class Client {
isRegistered: boolean isRegistered: boolean
apiCall: Axios apiCall: Axios
batchesInProgress: Map<string, Batch> batchesInProgress: Map<string, Batch>
constructor(public server: Server) { constructor(public server: IRCServer) {
this.capVersion = '301'; this.capVersion = '301';
this.enabledCaps = new Map(); this.enabledCaps = new Map();
this.allCaps = new Map([ this.allCaps = new Map([

View file

@ -1,9 +1,9 @@
import { Socket } from "net"; import { Socket } from "net";
import { Client } from "./Client.js"; import { Client } from "./Client.js";
import { Server } from './Server.js'; import { IRCServer } from './Server.js';
export class IRCClient extends Client { export class IRCClient extends Client {
constructor(private socket: Socket , public server: Server) { constructor(private socket: Socket , public server: IRCServer) {
super(server); super(server);
this.socket.on('data', (data) => this.receiveData(data)); this.socket.on('data', (data) => this.receiveData(data));
//this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)}); //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 axios, { Axios } from "axios";
import { readFileSync } from "fs";
import { createServer, Server } from "tls";
import { IRCClient } from "./IRCClient.js";
import { Channel } from "./Channel.js"; import { Channel } from "./Channel.js";
import { Client } from "./Client.js"; import { Client } from "./Client.js";
import { MatrixUser } from "./MatrixUser.js"; import { MatrixUser } from "./MatrixUser.js";
export class Server { export class IRCServer {
public config: any
public homeserver: string public homeserver: string
public mxid: string public mxid: string
public name: string public name: string
@ -24,10 +28,12 @@ export class Server {
private isSyncing: boolean private isSyncing: boolean
private initialSync: boolean private initialSync: boolean
private currentSyncTime: number private currentSyncTime: number
constructor(public config: any) { private listener: Server;
this.homeserver = config.homeserver; constructor() {
this.mxid = config.mxid; this.config = JSON.parse(readFileSync(process.argv[2], {"encoding": "utf-8"}));
this.name = config.serverName; this.homeserver = this.config.homeserver;
this.mxid = this.config.mxid;
this.name = this.config.serverName;
this.apiCall = axios.create({ this.apiCall = axios.create({
baseURL: `${this.homeserver}/_matrix/client/v3`, baseURL: `${this.homeserver}/_matrix/client/v3`,
timeout: 180000, timeout: 180000,
@ -49,6 +55,10 @@ export class Server {
this.initialSync = false; this.initialSync = false;
this.currentSyncTime = 0; this.currentSyncTime = 0;
setInterval(this.doSync.bind(this), 2000); 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.ourMatrixUser = this.getOrCreateMatrixUser(this.mxid);
this.apiCall.get("/account/whoami").then(r => { this.apiCall.get("/account/whoami").then(r => {
this.doLog("Authentication successful, starting initial sync"); this.doLog("Authentication successful, starting initial sync");
@ -143,6 +153,13 @@ export class Server {
if (this.initialSync === false && this.syncLocks.size === 0) { if (this.initialSync === false && this.syncLocks.size === 0) {
this.initialSync = true; this.initialSync = true;
this.doLog('Synced to network!'); 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"]}`);
})
} }
} }