auth
This commit is contained in:
parent
fd105e001e
commit
481eab7a03
5 changed files with 140 additions and 3 deletions
|
@ -77,6 +77,7 @@ public class Bot {
|
||||||
public VoiceChatPlugin voiceChat;
|
public VoiceChatPlugin voiceChat;
|
||||||
public TagPlugin tag;
|
public TagPlugin tag;
|
||||||
public WorldPlugin world;
|
public WorldPlugin world;
|
||||||
|
public AuthPlugin auth;
|
||||||
|
|
||||||
public Bot (Configuration.BotOption botOption, List<Bot> bots, Configuration config) {
|
public Bot (Configuration.BotOption botOption, List<Bot> bots, Configuration config) {
|
||||||
this.host = botOption.host;
|
this.host = botOption.host;
|
||||||
|
@ -125,6 +126,7 @@ public class Bot {
|
||||||
this.voiceChat = new VoiceChatPlugin(this);
|
this.voiceChat = new VoiceChatPlugin(this);
|
||||||
this.tag = new TagPlugin(this);
|
this.tag = new TagPlugin(this);
|
||||||
this.world = new WorldPlugin(this);
|
this.world = new WorldPlugin(this);
|
||||||
|
this.auth = new AuthPlugin(this);
|
||||||
|
|
||||||
for (Listener listener : listeners) listener.loadedPlugins();
|
for (Listener listener : listeners) listener.loadedPlugins();
|
||||||
|
|
||||||
|
|
|
@ -26,12 +26,20 @@ public class Configuration {
|
||||||
|
|
||||||
public String ownerName = "chayapak"; // mabe mabe
|
public String ownerName = "chayapak"; // mabe mabe
|
||||||
|
|
||||||
|
public OwnerAuthentication ownerAuthentication = new OwnerAuthentication();
|
||||||
|
|
||||||
public List<String> trusted = new ArrayList<>();
|
public List<String> trusted = new ArrayList<>();
|
||||||
public SelfCare selfCare = new SelfCare();
|
public SelfCare selfCare = new SelfCare();
|
||||||
public Eval eval = new Eval();
|
public Eval eval = new Eval();
|
||||||
|
|
||||||
public BotOption[] bots = new BotOption[]{};
|
public BotOption[] bots = new BotOption[]{};
|
||||||
|
|
||||||
|
public static class OwnerAuthentication {
|
||||||
|
public boolean enabled = false;
|
||||||
|
public String key = "";
|
||||||
|
public int timeout = 6000;
|
||||||
|
}
|
||||||
|
|
||||||
public static class InternetCheck {
|
public static class InternetCheck {
|
||||||
public boolean enabled = true;
|
public boolean enabled = true;
|
||||||
public String address = "https://sus.red";
|
public String address = "https://sus.red";
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
package land.chipmunk.chayapak.chomens_bot.plugins;
|
||||||
|
|
||||||
|
import com.google.common.hash.Hashing;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.util.UUIDUtilities;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.TextComponent;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class AuthPlugin extends PlayersPlugin.Listener {
|
||||||
|
public final String id = "chomens_bot_verify"; // should this be static
|
||||||
|
|
||||||
|
private final Bot bot;
|
||||||
|
|
||||||
|
private String key = null;
|
||||||
|
|
||||||
|
private long timeJoined;
|
||||||
|
|
||||||
|
private boolean hasCorrectHash;
|
||||||
|
|
||||||
|
private boolean started = false;
|
||||||
|
|
||||||
|
public AuthPlugin (Bot bot) {
|
||||||
|
this.bot = bot;
|
||||||
|
|
||||||
|
if (!bot.config.ownerAuthentication.enabled) return;
|
||||||
|
|
||||||
|
this.key = bot.config.ownerAuthentication.key;
|
||||||
|
|
||||||
|
bot.players.addListener(this);
|
||||||
|
bot.chat.addListener(new ChatPlugin.Listener() {
|
||||||
|
@Override
|
||||||
|
public void systemMessageReceived(Component component, String string, String ansi) {
|
||||||
|
AuthPlugin.this.systemMessageReceived(component);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
bot.executor.scheduleAtFixedRate(this::check, 0, 1, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerJoined(PlayerEntry target) {
|
||||||
|
if (!target.profile.getName().equals(bot.config.ownerName) || !bot.options.useCore) return;
|
||||||
|
|
||||||
|
started = true;
|
||||||
|
|
||||||
|
final long currentTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
timeJoined = currentTime;
|
||||||
|
|
||||||
|
final long time = currentTime / 10_000;
|
||||||
|
|
||||||
|
final String hash = Hashing.sha256()
|
||||||
|
.hashString(key + time, StandardCharsets.UTF_8)
|
||||||
|
.toString()
|
||||||
|
.substring(0, 8);
|
||||||
|
|
||||||
|
bot.chat.tellraw(
|
||||||
|
Component
|
||||||
|
.text(id)
|
||||||
|
.append(Component.text(hash))
|
||||||
|
.append(Component.text(UUIDUtilities.selector(bot.profile.getId()))), // convenient reason
|
||||||
|
target.profile.getId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void playerLeft(PlayerEntry target) {
|
||||||
|
if (!target.profile.getName().equals(bot.config.ownerName)) return;
|
||||||
|
|
||||||
|
hasCorrectHash = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void systemMessageReceived (Component component) {
|
||||||
|
try {
|
||||||
|
if (!(component instanceof TextComponent)) return;
|
||||||
|
|
||||||
|
final String content = ((TextComponent) component).content();
|
||||||
|
|
||||||
|
if (!content.equals(id)) return;
|
||||||
|
|
||||||
|
final List<Component> children = component.children();
|
||||||
|
|
||||||
|
if (children.size() != 1) return;
|
||||||
|
|
||||||
|
if (!(children.get(0) instanceof TextComponent)) return;
|
||||||
|
|
||||||
|
final String inputHash = ((TextComponent) children.get(0)).content();
|
||||||
|
|
||||||
|
final long time = System.currentTimeMillis() / 10_000;
|
||||||
|
|
||||||
|
final String hash = Hashing.sha256()
|
||||||
|
// very pro hash input
|
||||||
|
.hashString(key + key + time + time, StandardCharsets.UTF_8)
|
||||||
|
.toString()
|
||||||
|
.substring(0, 8);
|
||||||
|
|
||||||
|
bot.logger.info("Input: " + inputHash + " Real Hash: " + hash);
|
||||||
|
|
||||||
|
hasCorrectHash = inputHash.equals(hash);
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void check() {
|
||||||
|
if (!started) return;
|
||||||
|
|
||||||
|
final PlayerEntry entry = bot.players.getEntry(bot.config.ownerName);
|
||||||
|
|
||||||
|
if (entry == null) return;
|
||||||
|
|
||||||
|
final long timeSinceJoined = System.currentTimeMillis() - timeJoined;
|
||||||
|
|
||||||
|
if (timeSinceJoined > bot.config.ownerAuthentication.timeout && !hasCorrectHash) {
|
||||||
|
bot.filter.mute(entry, "Not verified");
|
||||||
|
bot.filter.deOp(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -125,11 +125,12 @@ public class FilterPlugin extends PlayersPlugin.Listener {
|
||||||
mute(message.sender);
|
mute(message.sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mute (PlayerEntry target) {
|
public void mute (PlayerEntry target) { mute(target, ""); }
|
||||||
bot.core.run("essentials:mute " + target.profile.getIdAsString() + " 10y");
|
public void mute (PlayerEntry target, String reason) {
|
||||||
|
bot.core.run("essentials:mute " + target.profile.getIdAsString() + " 10y " + reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deOp (PlayerEntry target) {
|
public void deOp (PlayerEntry target) {
|
||||||
bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId()));
|
bot.core.run("minecraft:execute run deop " + UUIDUtilities.selector(target.profile.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,11 @@ colorPalette:
|
||||||
|
|
||||||
ownerName: 'chayapak' # currently this is only used in the console
|
ownerName: 'chayapak' # currently this is only used in the console
|
||||||
|
|
||||||
|
ownerAuthentication:
|
||||||
|
enabled: false
|
||||||
|
key: ''
|
||||||
|
timeout: 6000
|
||||||
|
|
||||||
trusted:
|
trusted:
|
||||||
- 'player name'
|
- 'player name'
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue