Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul metadata parsing #68

Merged
merged 4 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"async-mutex": "^0.4.0",
"better-sqlite3": "^8.5.0",
"kysely": "^0.26.1",
"source-map-support": "^0.5.21"
"source-map-support": "^0.5.21",
"zod": "^3.21.4",
"zod-validation-error": "^1.3.1"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4",
Expand Down
24 changes: 12 additions & 12 deletions server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,36 @@ async function handle_input(input: string): Promise<void> {
console.log(
'whitelist_remove_ign <ign> - Removes the UUID cached with\n the given IGN from the whitelist, and saves the whitelist to disk',
)
} else if (command === 'whitelist_load') await metadata.whitelist_load()
else if (command === 'whitelist_save') await metadata.whitelist_save()
} else if (command === 'whitelist_load') await metadata.loadWhitelist()
else if (command === 'whitelist_save') await metadata.saveWhitelist()
else if (command === 'whitelist_add') {
if (extras.length === 0)
throw new Error('Did not provide UUID to whitelist')
const uuid = extras
await metadata.whitelist_add(uuid)
await metadata.whitelist_save()
metadata.whitelist.add(uuid)
await metadata.saveWhitelist()
} else if (command === 'whitelist_add_ign') {
if (extras.length === 0)
throw new Error('Did not provide UUID to whitelist')
const ign = extras
const uuid = metadata.uuid_cache_lookup(ign)
const uuid = metadata.getCachedPlayerUuid(ign)
if (uuid == null) throw new Error('No cached UUID for IGN ' + ign)
await metadata.whitelist_add(uuid)
await metadata.whitelist_save()
metadata.whitelist.add(uuid)
await metadata.saveWhitelist()
} else if (command === 'whitelist_remove') {
if (extras.length === 0)
throw new Error('Did not provide UUID to whitelist')
const uuid = extras
await metadata.whitelist_remove(uuid)
await metadata.whitelist_save()
metadata.whitelist.delete(uuid)
await metadata.saveWhitelist()
} else if (command === 'whitelist_remove_ign') {
if (extras.length === 0)
throw new Error('Did not provide UUID to whitelist')
const ign = extras
const uuid = metadata.uuid_cache_lookup(ign)
const uuid = metadata.getCachedPlayerUuid(ign)
if (uuid == null) throw new Error('No cached UUID for IGN ' + ign)
await metadata.whitelist_remove(uuid)
await metadata.whitelist_save()
metadata.whitelist.delete(uuid)
await metadata.saveWhitelist()
} else {
throw new Error(`Unknown command "${command}"`)
}
Expand Down
49 changes: 49 additions & 0 deletions server/src/deps/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import node_os from 'node:os'
import node_utils from 'node:util'

export enum ErrorType {
FileExists,
FileNotFound,
UNKNOWN,
}

/**
* Attempts to transform Node's less-than-helpful exceptions into something
* more readable and logic-able.
*/
export function getErrorType(error: any): ErrorType {
switch (Math.abs(error.errno ?? Infinity)) {
case node_os.constants.errno.ENOENT:
return ErrorType.FileNotFound
case node_os.constants.errno.EEXIST:
return ErrorType.FileExists
default:
return ErrorType.UNKNOWN
}
}

/**
* Utility that guarantees that the error is an instance of Error.
*/
export function ensureError(error: any): Error {
if (error instanceof Error) {
return error
}
switch (typeof error) {
case 'string':
return new Error(error)
case 'number':
case 'bigint':
return new Error(String(error))
}
return new Error(node_utils.inspect(error))
}

/**
* This is useful in cases where you need to throw but can't because of
* Javascript. Read more for context:
* https://www.proposals.es/proposals/throw%20expressions
*/
export function inlineThrow<T>(error: any): T {
throw error
}
16 changes: 16 additions & 0 deletions server/src/deps/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type JSONObject = { [key: string]: JSONValue | undefined }
export type JSONArray = JSONValue[]
export type JSONValue =
| JSONObject
| JSONArray
| string
| number
| boolean
| null

/**
* Wrapper function for JSON.parse() that provides a proper return type.
*/
export function parse(raw: string): JSONValue {
return JSON.parse(raw)
}
23 changes: 18 additions & 5 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import './cli'
import * as database from './database'
import { uuid_cache_store, getConfig, whitelist_check } from './metadata'
import * as metadata from './metadata'
import { ClientPacket } from './protocol'
import { CatchupRequestPacket } from './protocol/CatchupRequestPacket'
import { ChunkTilePacket } from './protocol/ChunkTilePacket'
import { TcpClient, TcpServer } from './server'
import { RegionCatchupPacket } from './protocol/RegionCatchupPacket'

database.setup().then(() => new Main())
let config: metadata.Config = null!
Promise.resolve().then(async () => {
await database.setup()

config = metadata.getConfig()

// These two are only used if whitelist is enabled... but best to load them
// anyway lest there be a modification to them that is then saved.
await metadata.loadWhitelist()
await metadata.loadUuidCache()

new Main()
})

type ProtocolClient = TcpClient // TODO cleanup

Expand All @@ -20,10 +32,11 @@ export class Main {
async handleClientAuthenticated(client: ProtocolClient) {
if (!client.uuid) throw new Error('Client not authenticated')

uuid_cache_store(client.mcName!, client.uuid)
metadata.cachePlayerUuid(client.mcName!, client.uuid!)
await metadata.saveUuidCache()

if ((await getConfig()).whitelist) {
if (!whitelist_check(client.uuid)) {
if (config.whitelist) {
if (!metadata.whitelist.has(client.uuid)) {
client.log(
`Rejected unwhitelisted user ${client.mcName} (${client.uuid})`,
)
Expand Down
Loading
Loading