Add system for setting handbook address and port

This commit is contained in:
KingRainbow44 2023-05-30 16:53:57 -04:00
parent 36a35c11aa
commit a35ce5fecb
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
7 changed files with 84 additions and 12 deletions

View File

@ -7,6 +7,11 @@
<script>
window["hide"] = ["quests", "achievements"];
window["details"] = {
address: "{{DETAILS_ADDRESS}}",
port: "{{DETAILS_PORT}}",
disable: "{{DETAILS_DISABLE}}"
};
</script>
</head>

View File

@ -1,12 +1,14 @@
import type { CommandResponse } from "@backend/types";
import emitter from "@backend/events";
import { getWindowDetails } from "@app/utils";
let playerToken: string | null = null; // The session token for the player.
export let targetPlayer = 0; // The UID of the target player.
// The server's address and port.
export let address: string = "127.0.0.1",
port: string = "443";
export let address: string = getWindowDetails().address,
port: string = getWindowDetails().port.toString();
export let encrypted: boolean = true;
export let lockedPlayer = false; // Whether the UID field is locked.
@ -16,6 +18,9 @@ export let connected = false; // Whether the server is connected.
* Loads the server details from local storage.
*/
export function setup(): void {
// Check if the server is disabled.
if (getWindowDetails().disable) return;
// Load the server details from local storage.
const storedAddress = localStorage.getItem("address");
const storedPort = localStorage.getItem("port");

View File

@ -135,6 +135,12 @@ export type CommandResponse = {
message: string;
};
export type WindowDetails = {
address: string,
port: number,
disable: boolean
};
/**
* Checks if a string is a page.
*

View File

@ -4,6 +4,7 @@ import emitter from "@backend/events";
import { targetPlayer, address, port, setServerDetails, url, setTargetPlayer } from "@backend/server";
import "@css/widgets/ServerSettings.scss";
import { getWindowDetails } from "@app/utils";
interface IState {
webview: boolean;
@ -97,6 +98,8 @@ class ServerSettings extends React.Component<{}, IState> {
}
render() {
const { disable } = getWindowDetails();
return (
<div className={"ServerSettings"}>
{this.state.webview ? (
@ -109,7 +112,14 @@ class ServerSettings extends React.Component<{}, IState> {
<div className={"ServerSettings_Content ServerSettings_Top"}>
<h1 className={"ServerSettings_Title"}>Server Settings</h1>
<div className={"ServerSettings_Details"}>
<div
className={"ServerSettings_Details"}
style={{
opacity: disable ? 0.5 : 1,
cursor: disable ? "not-allowed" : "default",
userSelect: disable ? "none" : "auto"
}}
>
<div>
<p>Address:</p>
<input
@ -121,6 +131,10 @@ class ServerSettings extends React.Component<{}, IState> {
this.setState({ address: value });
}}
disabled={disable}
style={{
cursor: disable ? "not-allowed" : "text"
}}
/>
</div>
@ -139,6 +153,10 @@ class ServerSettings extends React.Component<{}, IState> {
this.setState({ port: Number(value) });
}}
disabled={disable}
style={{
cursor: disable ? "not-allowed" : "text"
}}
/>
</div>
</div>

View File

@ -1,4 +1,4 @@
import type { Entity, Item, EntityInfo, ItemInfo } from "@backend/types";
import type { Entity, Item, EntityInfo, ItemInfo, WindowDetails } from "@backend/types";
import { ItemType, Quality } from "@backend/types";
/**
@ -165,3 +165,20 @@ export function notNaN(value: number | string): string {
const number = parseInt(value.toString());
return isNaN(number) ? "" : number.toString();
}
/**
* Extracts the server details out of the window.
*/
export function getWindowDetails(): WindowDetails {
const details = (window as any).details;
const { address, port, disable } = details;
return {
address: address == "{{DETAILS_ADDRESS}}" ?
"127.0.0.1" : address,
port: port == "{{DETAILS_PORT}}" ?
443 : parseInt(port),
disable: disable == "{{DETAILS_DISABLE}}" ?
false : disable == "true"
};
}

View File

@ -32,9 +32,11 @@ public class ConfigContainer {
* The field for 'legacyResources' has been removed.
* Version 7 - 'regionKey' is being added for authentication
* with the new dispatch server.
* Version 8 - 'server' is being added for enforcing handbook server
* addresses.
*/
private static int version() {
return 7;
return 8;
}
/**
@ -299,9 +301,19 @@ public class ConfigContainer {
public static class HandbookOptions {
public boolean enable = false;
public boolean allowCommands = true;
public int maxRequests = 10;
public int maxEntities = 100;
public Server server = new Server();
public static class Server {
public boolean enforced = false;
public String address = "127.0.0.1";
public int port = 443;
public boolean canChange = true;
}
}
}

View File

@ -15,7 +15,7 @@ import static emu.grasscutter.config.Configuration.HANDBOOK;
/** Handles requests for the new GM Handbook. */
public final class HandbookHandler implements Router {
private final byte[] handbook;
private String handbook;
private final boolean serve;
/**
@ -23,8 +23,16 @@ public final class HandbookHandler implements Router {
* found.
*/
public HandbookHandler() {
this.handbook = FileUtils.readResource("/html/handbook.html");
this.serve = HANDBOOK.enable && this.handbook.length > 0;
this.handbook = new String(FileUtils.readResource("/html/handbook.html"));
this.serve = HANDBOOK.enable && this.handbook.length() > 0;
var server = HANDBOOK.server;
if (this.serve && server.enforced) {
this.handbook = this.handbook
.replace("{{DETAILS_ADDRESS}}", server.address)
.replace("{{DETAILS_PORT}}", String.valueOf(server.port))
.replace("{{DETAILS_DISABLE}}", Boolean.toString(server.canChange));
}
}
@Override
@ -48,7 +56,7 @@ public final class HandbookHandler implements Router {
* @return True if the server can execute handbook commands.
*/
private boolean controlSupported() {
return HANDBOOK.enable;
return HANDBOOK.enable && HANDBOOK.allowCommands;
}
/**
@ -61,7 +69,9 @@ public final class HandbookHandler implements Router {
if (!this.serve) {
ctx.status(500).result("Handbook not found.");
} else {
ctx.contentType("text/html").result(this.handbook);
ctx
.contentType(ContentType.TEXT_HTML)
.result(this.handbook);
}
}
@ -102,8 +112,7 @@ public final class HandbookHandler implements Router {
} else {
ctx.status(result.getStatus())
.result(result.getBody())
.contentType(
result.isHtml() ? ContentType.TEXT_HTML : ContentType.TEXT_PLAIN);
.contentType(result.isHtml() ? ContentType.TEXT_HTML : ContentType.TEXT_PLAIN);
}
}
}