-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: move packages for better structure * feat: check if player's address is a proxy on join via proxycheck.io * feat: address whitelist interfaces * chore: provide tests for proxies and cache * fix: code smells * fix: interrupt when catching exception * feat: api url for proxycheck is configurable * use VARBINARY for address --------- Co-authored-by: Netherwhal <netherwhal@simplyvanilla.net>
- Loading branch information
1 parent
1c5356b
commit 009531d
Showing
52 changed files
with
1,150 additions
and
257 deletions.
There are no files selected for viewing
This file contains 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 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
29 changes: 29 additions & 0 deletions
29
src/main/java/net/simplyvanilla/simplyrank/addresswhitelist/AddressWhitelistService.java
This file contains 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,29 @@ | ||
package net.simplyvanilla.simplyrank.addresswhitelist; | ||
|
||
import net.simplyvanilla.simplyrank.database.addresswhitelist.AddressWhitelist; | ||
import net.simplyvanilla.simplyrank.database.addresswhitelist.AddressWhitelistRepository; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
public class AddressWhitelistService { | ||
private final AddressWhitelistRepository repository; | ||
|
||
public AddressWhitelistService(AddressWhitelistRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public void addAddress(String address, UUID invokerId) { | ||
this.repository.save(new AddressWhitelist(address, invokerId, LocalDateTime.now())); | ||
} | ||
|
||
public void removeAddress(String address) { | ||
this.repository.deleteByAddress(address); | ||
} | ||
|
||
public boolean isWhitelisted(Player player) { | ||
String address = player.getAddress().getAddress().getHostAddress(); | ||
return this.repository.existsByAddress(address); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
src/main/java/net/simplyvanilla/simplyrank/command/AbstractCommand.java
This file contains 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
4 changes: 2 additions & 2 deletions
4
src/main/java/net/simplyvanilla/simplyrank/command/SimplyRankCommandExecutor.java
This file contains 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
6 changes: 3 additions & 3 deletions
6
src/main/java/net/simplyvanilla/simplyrank/command/SubCommand.java
This file contains 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
66 changes: 66 additions & 0 deletions
66
src/main/java/net/simplyvanilla/simplyrank/command/address/AddressWhitelistCommand.java
This file contains 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,66 @@ | ||
package net.simplyvanilla.simplyrank.command.address; | ||
|
||
import net.simplyvanilla.simplyrank.addresswhitelist.AddressWhitelistService; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.UUID; | ||
|
||
import static net.kyori.adventure.text.Component.text; | ||
|
||
public class AddressWhitelistCommand implements CommandExecutor { | ||
private static final UUID CONSOLE_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000"); | ||
private final AddressWhitelistService service; | ||
|
||
public AddressWhitelistCommand(AddressWhitelistService service) { | ||
this.service = service; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { | ||
UUID senderId = sender instanceof Player player ? player.getUniqueId() : CONSOLE_UUID; | ||
|
||
if (args.length != 2) { | ||
this.printHelp(sender); | ||
} else if ("add".equals(args[0])) { | ||
this.handleAddCommand(sender, args[1], senderId); | ||
} else if ("remove".equals(args[0])) { | ||
this.handleRemoveCommand(sender, args[1]); | ||
} else { | ||
this.printHelp(sender); | ||
} | ||
return true; | ||
} | ||
|
||
private void printHelp(CommandSender sender) { | ||
sender.sendMessage(text("Usage: /vpn-whitelist <add|remove> <address>")); | ||
} | ||
|
||
private boolean validateAddress(String address) { | ||
// check if address is valid ipv4 or ipv6 address | ||
if (address.contains(":")) { | ||
// ipv6 | ||
return address.matches("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); | ||
} else { | ||
// ipv4 | ||
return address.matches("^(?:\\d{1,3}\\.){3}\\d{1,3}$"); | ||
} | ||
} | ||
|
||
private void handleRemoveCommand(CommandSender sender, String address) { | ||
this.service.removeAddress(address); | ||
sender.sendMessage(text("Removed address " + address)); | ||
} | ||
|
||
private void handleAddCommand(CommandSender sender, String address, UUID invokerId) { | ||
if (!this.validateAddress(address)) { | ||
sender.sendMessage(text("Invalid address " + address)); | ||
return; | ||
} | ||
this.service.addAddress(address, invokerId); | ||
sender.sendMessage(text("Added address " + address)); | ||
} | ||
} |
This file contains 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 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 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 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 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.