-
-
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
Open
yel0h
wants to merge
7
commits into
PixelGuys:master
Choose a base branch
from
yel0h:whitelist-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08b260a
Enforce whitelist during connection handshake
yel0h 97f1729
Add /whitelist command
yel0h 12b960b
Decouple ban from whitelist
yel0h 7d6edbc
Make JoinResult not pub
yel0h 49bd0fd
Assert context on add and block
yel0h a3d57b8
Log warning and add toggle command
yel0h 4ded5ab
Persist whitelistEnabled through Settings on every save
yel0h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 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> | ||
|
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. Would it be possible to also have whitelist enable/disable commands? |
||
| \\/whitelist <enable/disable> | ||
| ; | ||
|
|
||
| const Action = enum { add, block }; | ||
| const Toggle = enum { enable, disable }; | ||
|
|
||
| pub const Args = union(enum) { | ||
| @"/whitelist <action> <key>": struct { action: Action, key: command.KeyString }, | ||
| @"/whitelist <action> <playerIndex>": struct { action: Action, playerIndex: command.PlayerIndex }, | ||
| @"/whitelist <enable/disable>": struct { toggle: Toggle }, | ||
| }; | ||
|
|
||
| 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); | ||
| }, | ||
| .@"/whitelist <enable/disable>" => |params| { | ||
| main.server.world.?.settings.whitelistEnabled.store(params.toggle == .enable, .monotonic); | ||
| source.sendMessage("#00ff00Whitelist {s}", .{if (params.toggle == .enable) "enabled" else "disabled"}); | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| 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}), | ||
| } | ||
| const userList = main.server.getUserList(main.stackAllocator); | ||
| defer main.stackAllocator.free(userList); | ||
| for (userList) |user| { | ||
| if (user.newKeyString) |userKey| { | ||
| if (std.mem.eql(u8, userKey, key)) { | ||
| user.conn.disconnect(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of adding your own validation logic, please use
authentication.PublicKey.initFromBase64