-
-
Notifications
You must be signed in to change notification settings - Fork 232
Enforce whitelist and add command #3467
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
08b260a
97f1729
12b960b
7d6edbc
49bd0fd
a3d57b8
4ded5ab
cb54974
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,6 +171,18 @@ pub const handShake = struct { // MARK: handShake | |
| const keys = zon.getChild("keys"); | ||
| try conn.user.?.identifyFromKeysAndName(name, keys); | ||
|
|
||
| switch (main.server.players.isAllowedToJoin(conn.user.?.newKeyString.?)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to have this switch and the check for |
||
| .allowed => {}, | ||
| .blocked => { | ||
| std.log.info("Rejected connection from '{s}': blocked", .{name}); | ||
|
IntegratedQuantum marked this conversation as resolved.
Outdated
|
||
| return error.NotWhitelisted; | ||
| }, | ||
| .neutral => if (main.server.world.?.settings.whitelistEnabled) { | ||
| std.log.info("Rejected connection from '{s}': not on whitelist", .{name}); | ||
| return error.NotWhitelisted; | ||
| }, | ||
| } | ||
|
|
||
| var writer: utils.BinaryWriter = .init(main.stackAllocator); | ||
| defer writer.deinit(); | ||
| writer.writeEnum(Connection.HandShakeState, .signatureRequest); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const main = @import("main"); | ||
| const command = main.server.command; | ||
| const Source = command.Source; | ||
| const players = main.server.players; | ||
|
|
||
| pub const description = "Manages the connection whitelist"; | ||
| pub const usage = | ||
| \\/whitelist <add/block> <keyType>:<base64Key> | ||
| \\/whitelist <add/block> @<playerIndex> | ||
|
IntegratedQuantum marked this conversation as resolved.
|
||
| ; | ||
|
|
||
| const Action = enum { add, block }; | ||
|
|
||
| pub const Args = union(enum) { | ||
| @"/whitelist <action> <key>": struct { action: Action, key: command.KeyString }, | ||
| @"/whitelist <action> <playerIndex>": struct { action: Action, playerIndex: command.PlayerIndex }, | ||
| }; | ||
|
|
||
| pub fn execute(args: Args, source: Source) void { | ||
| switch (args) { | ||
| .@"/whitelist <action> <key>" => |params| applyAction(source, params.action, params.key.key), | ||
| .@"/whitelist <action> <playerIndex>" => |params| { | ||
| const target = command.Target.fromPlayerIndex(params.playerIndex, source) catch return; | ||
| const key = target.user.newKeyString orelse { | ||
| source.sendMessage("#ff0000Player {s}§#ff0000 has no public key to whitelist", .{target.user.name}); | ||
| return; | ||
| }; | ||
| applyAction(source, params.action, key); | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn applyAction(source: Source, action: Action, key: []const u8) void { | ||
| switch (action) { | ||
| .add => switch (players.add(key)) { | ||
| .added => source.sendMessage("#00ff00Added {s}§#00ff00 to the whitelist", .{key}), | ||
| .alreadyAllowed => source.sendMessage("#ff0000{s}§#ff0000 is already on the whitelist", .{key}), | ||
| }, | ||
| .block => { | ||
| switch (players.block(key)) { | ||
| .blocked => source.sendMessage("#00ff00Blocked {s}§#00ff00 from connecting", .{key}), | ||
| .alreadyBlocked => source.sendMessage("#ff0000{s}§#ff0000 is already blocked", .{key}), | ||
| } | ||
| if (main.server.getUserByKey(key)) |user| { | ||
| user.conn.disconnect(); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -891,3 +891,14 @@ pub fn getUserByIndex(index: PlayerIndex) ?*User { | |
| } | ||
| return null; | ||
| } | ||
|
|
||
| pub fn getUserByKey(key: []const u8) ?*User { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a one-time use case, there is no need to pollute the namespace with it, everything used inside here is already public, so please inline it to the implementation site. |
||
| const userList = getUserList(main.stackAllocator); | ||
| defer main.stackAllocator.free(userList); | ||
| for (userList) |user| { | ||
| if (user.newKeyString) |userKey| { | ||
| if (std.mem.eql(u8, userKey, key)) return user; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this code here should worry about the error sets of arbitrary network protocols. I assume you want to get rid of the error popup? I think the right solution here would be to change the error to a warning, there are many other cases where this code is run (e.g. version differences) that probably shouldn't have the error popup either.