Skip to content

Commit

Permalink
fix: config https
Browse files Browse the repository at this point in the history
  • Loading branch information
JadlionHD committed Jul 6, 2024
1 parent 240e531 commit 2ae2646
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class Client extends EventEmitter {
plugins: options?.plugins ?? [],
https: {
ip: options?.https?.ip ?? "127.0.0.1",
port: options?.https?.port ?? 17091,
enetPort: options?.https?.enetPort ?? 17091,
httpPort: options?.https?.httpPort ?? 80,
httpsPort: options?.https?.httpsPort ?? 443,
enable: options?.https?.enable ?? true,
type2: options?.https?.type2 ?? false
},
Expand Down Expand Up @@ -110,8 +112,7 @@ class Client extends EventEmitter {

private _startWeb() {
if (!this.config.https) return;
if (this.config.https.enable)
WebServer(this.config?.https?.ip!, this.config?.https?.port!, this.config.https.type2);
if (this.config.https.enable) WebServer(this.config.https);
}

private _handleEvent() {
Expand Down
11 changes: 6 additions & 5 deletions src/structures/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import http from "http";
import https from "https";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import type { HTTPSServerOptions } from "../../types";

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand All @@ -12,7 +13,7 @@ const options = {
cert: readFileSync(join(__dirname, "..", "..", "misc", "ssl", "server.crt"))
};

export function WebServer(ip: string, port: number, type2 = false) {
export function WebServer(data: HTTPSServerOptions) {
const app = express();
let done = false;

Expand All @@ -21,14 +22,14 @@ export function WebServer(ip: string, port: number, type2 = false) {

app.use("/growtopia/server_data.php", (req, res) => {
res.send(
`server|${ip}\nport|${port}\ntype|1\n${
type2 ? "type2|1" : ""
`server|${data.ip}\nport|${data.enetPort}\ntype|1\n${
data.type2 ? "type2|1" : ""
}\n#maint|Server is Maintenance\nmeta|growtopiajs\nRTENDMARKERBS1001`
);
});

httpServer.listen(80);
httpsServer.listen(443);
httpServer.listen(data.httpPort);
httpsServer.listen(data.httpsPort);
httpsServer.on("listening", function () {
done = true;
});
Expand Down
2 changes: 1 addition & 1 deletion test/basic-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const client = new Client({

client.on("ready", () => {
console.log(
`ENet server: port ${client.config.enet.port} on ${client.config.enet.ip}\nHttps server: port ${client.config.https.port} on ${client.config.https.ip}`
`ENet server: port ${client.config.enet.port} on ${client.config.enet.ip}\nHttps server: port ${client.config.https.httpsPort} on ${client.config.https.ip}`
);
});

Expand Down
32 changes: 19 additions & 13 deletions types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ export interface ClientOptions {
plugins?: GrowApi[];

/** Built-in https web server */
https?: {
ip?: string;
port?: number;
enable?: boolean;
type2?: boolean;
};
enet?: {
ip?: string;
port?: number;
maxPeers?: number;
useNewPacket?: {
asClient?: boolean;
};
https?: HTTPSServerOptions;
enet?: ENetServerOptions;
}

export interface HTTPSServerOptions {
ip?: string;
enetPort?: number;
httpPort?: number;
httpsPort?: number;
enable?: boolean;
type2?: boolean;
}

export interface ENetServerOptions {
ip?: string;
port?: number;
maxPeers?: number;
useNewPacket?: {
asClient?: boolean;
};
}

Expand Down

0 comments on commit 2ae2646

Please sign in to comment.