Skip to content

Commit

Permalink
configurable list of networks to allow with lockNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
f0x52 committed Oct 8, 2024
1 parent 31cc4e7 commit 3c8daf7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
20 changes: 16 additions & 4 deletions defaults/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,8 @@ module.exports = {
// }
// ```
defaults: {
name: "Libera.Chat",
host: "irc.libera.chat",
port: 6697,
network: "Libera.Chat",
password: "",
tls: true,
rejectUnauthorized: true,
nick: "thelounge%%",
username: "thelounge",
Expand All @@ -285,6 +282,21 @@ module.exports = {
// This value is set to `false` by default.
lockNetwork: false,

networks: {
"Libera.Chat": {
host: "irc.libera.chat",
port: 6697,
tls: true,
rejectUnauthorized: true
},
"OFTC": {
host: "irc.oftc.net",
port: 6697,
tls: true,
rejectUnauthorized: true
}
}

// ## User management

// ### `messageStorage`
Expand Down
16 changes: 12 additions & 4 deletions server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ type FileUpload = {
export type Defaults = Pick<
Network,
| "name"
| "host"
| "port"
| "password"
| "tls"
| "rejectUnauthorized"
| "nick"
| "username"
| "realname"
Expand All @@ -46,6 +42,7 @@ export type Defaults = Pick<
| "saslPassword"
> & {
join: string;
network: string;
};

type Identd = {
Expand Down Expand Up @@ -83,6 +80,13 @@ type StoragePolicy = {
deletionPolicy: "statusOnly" | "everything";
};

type NetworkTemplate = {
host: string,
port: number,
tls: boolean,
rejectUnauthorized: boolean // if TLS certificates are validated
};

export type ConfigType = {
public: boolean;
host: string | undefined;
Expand All @@ -103,6 +107,7 @@ export type ConfigType = {
leaveMessage: string;
defaults: Defaults;
lockNetwork: boolean;
networks: {[name: string]: NetworkTemplate};
messageStorage: string[];
storagePolicy: StoragePolicy;
useHexIp: boolean;
Expand All @@ -119,6 +124,9 @@ class Config {
path.join(__dirname, "..", "defaults", "config.js")
)) as ConfigType;
#homePath = "";
networks = Object.fromEntries(Object.entries(this.values.networks).map(([name, network]) => {
return [name, {...network, name}];
}));

getHomePath() {
return this.#homePath;
Expand Down
27 changes: 11 additions & 16 deletions server/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,21 @@ class Network {

if (Config.values.lockNetwork) {
// This check is needed to prevent invalid user configurations
if (
!Config.values.public &&
this.host &&
this.host.length > 0 &&
this.host !== Config.values.defaults.host
) {

const allowedNetwork = Object.values(Config.networks).find((network) => {
return (this.name === network.name || this.host === network.host);
});

if (allowedNetwork === undefined) {
error(this, `The hostname you specified (${this.host}) is not allowed.`);
return false;
}

if (Config.values.public) {
this.name = Config.values.defaults.name;
// Sync lobby channel name
this.getLobby().name = Config.values.defaults.name;
}

this.host = Config.values.defaults.host;
this.port = Config.values.defaults.port;
this.tls = Config.values.defaults.tls;
this.rejectUnauthorized = Config.values.defaults.rejectUnauthorized;
this.name = allowedNetwork.name;
this.host = allowedNetwork.host;
this.port = allowedNetwork.port;
this.tls = allowedNetwork.tls;
this.rejectUnauthorized = allowedNetwork.rejectUnauthorized;
}

if (this.host.length === 0) {
Expand Down

0 comments on commit 3c8daf7

Please sign in to comment.