add commands
This commit is contained in:
parent
eaf3bbb0e9
commit
8bd1b4dedb
13 changed files with 435 additions and 77 deletions
|
@ -23,10 +23,14 @@ import io.netty.util.concurrent.Future;
|
|||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.IDN;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -58,9 +62,10 @@ import proxy.network.PacketPrepender;
|
|||
import proxy.network.Handler.ThreadQuickExitException;
|
||||
import proxy.network.HandshakeHandler;
|
||||
import proxy.network.ProxyHandler;
|
||||
import proxy.command.*;
|
||||
import proxy.network.Connection;
|
||||
import proxy.packet.S40PacketDisconnect;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
import proxy.util.ServerInfo;
|
||||
import proxy.util.User;
|
||||
import proxy.util.LazyLoader;
|
||||
|
@ -84,21 +89,22 @@ public class Proxy {
|
|||
private final List<ChannelFuture> endpoints = Collections.<ChannelFuture>synchronizedList(Lists.newArrayList());
|
||||
private final List<Connection> networkManagers = Collections.<Connection>synchronizedList(Lists.newArrayList());
|
||||
private final Queue<FutureTask<?>> futureTaskQueue = Queues.<FutureTask<?>>newArrayDeque();
|
||||
private final Map<String, User> users = Maps.newHashMap();
|
||||
private final Map<String, ProxyHandler> players = Maps.newHashMap();
|
||||
private final Map<String, User> users = Maps.newTreeMap();
|
||||
private final Map<String, ProxyHandler> players = Maps.newTreeMap();
|
||||
private final Map<String, Command> commands = Maps.newTreeMap();
|
||||
private final Thread serverThread;
|
||||
|
||||
private int compression = -1;
|
||||
private boolean epoll = false;
|
||||
private boolean register = true;
|
||||
private boolean checkCase = true;
|
||||
private String forwardHost = "lian.dd";
|
||||
private String forwardHost = "127.0.0.1";
|
||||
private int forwardPort = 25565;
|
||||
private String proxyHost = "";
|
||||
private int proxyPort = 25566;
|
||||
private int minPassLength = 8;
|
||||
private int maxAttempts = 5;
|
||||
private ServerInfo status = getStatus(new File("icon.png"), ChatColor.AQUA + "Server\n" + ChatColor.GREEN + "Test!!", 90, 10, "Test1",
|
||||
private ServerInfo status = getStatus(new File("icon.png"), Formatter.AQUA + "Server\n" + Formatter.GREEN + "Test!!", 90, 10, "Test1",
|
||||
"Test2 ...", "Test #3 !!");
|
||||
|
||||
private static String getIcon(File file) {
|
||||
|
@ -144,6 +150,7 @@ public class Proxy {
|
|||
public Proxy() {
|
||||
this.serverThread = Thread.currentThread();
|
||||
this.isAlive = true;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
@ -160,6 +167,31 @@ public class Proxy {
|
|||
Proxy.this.terminateEndpoints();
|
||||
}
|
||||
}, "Proxy shutdown thread"));
|
||||
Thread con = new Thread(new Runnable() {
|
||||
private final BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(System.in)));
|
||||
|
||||
public void run() {
|
||||
while(true) {
|
||||
String line;
|
||||
try {
|
||||
line = this.reader.readLine();
|
||||
}
|
||||
catch(IOException e) {
|
||||
line = null;
|
||||
}
|
||||
if(line == null)
|
||||
break;
|
||||
final String cmd = line;
|
||||
Proxy.this.schedule(new Runnable() {
|
||||
public void run() {
|
||||
Proxy.this.runCommand(null, Formatter.filterSpaces(cmd).split(" ", -1), cmd);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}, "Proxy console handler");
|
||||
con.setDaemon(true);
|
||||
con.start();
|
||||
while(this.running) {
|
||||
synchronized (this.futureTaskQueue)
|
||||
{
|
||||
|
@ -357,4 +389,61 @@ public class Proxy {
|
|||
public boolean isLoggedIn(String user) {
|
||||
return this.players.containsKey(user.toLowerCase(Locale.US));
|
||||
}
|
||||
|
||||
public ProxyHandler getPlayer(String user) {
|
||||
return this.players.get(user.toLowerCase(Locale.US));
|
||||
}
|
||||
|
||||
private void register(Command cmd) {
|
||||
if(this.commands.containsKey(cmd.getName()))
|
||||
throw new IllegalArgumentException("Command '" + cmd.getName() + "' ist already registered");
|
||||
this.commands.put(cmd.getName(), cmd);
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
this.register(new CommandHelp());
|
||||
this.register(new CommandExit());
|
||||
}
|
||||
|
||||
public Map<String, Command> getCommands() {
|
||||
return this.commands;
|
||||
}
|
||||
|
||||
public Collection<ProxyHandler> getPlayers() {
|
||||
return this.players.values();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
this.running = false;
|
||||
}
|
||||
|
||||
public void runCommand(ProxyHandler player, String[] args, String line) {
|
||||
if(args.length == 0)
|
||||
args = new String[] {"help"};
|
||||
Command cmd = this.commands.get(args[0].toLowerCase());
|
||||
if(cmd == null) {
|
||||
if(player != null)
|
||||
player.sendMessage(Formatter.DARK_RED + "Proxy command '%s' not found", args[0]);
|
||||
else
|
||||
Log.error("Command '%s' not found", args[0]);
|
||||
return;
|
||||
}
|
||||
String[] argv = new String[args.length - 1];
|
||||
System.arraycopy(args, 1, argv, 0, argv.length);
|
||||
try {
|
||||
cmd.run(this, player, argv);
|
||||
}
|
||||
catch(RunException e) {
|
||||
if(player != null)
|
||||
player.sendMessage(Formatter.DARK_RED + e.getMessage());
|
||||
else
|
||||
Log.error(e.getMessage());
|
||||
}
|
||||
catch(Throwable t) {
|
||||
if(player != null)
|
||||
player.sendMessage(Formatter.DARK_RED + "Internal error trying to execute command");
|
||||
Log.error(t, "Could not execute '%s'" + (player != null ? " as %s" : ""), line, player != null ? player.getUsername() : null);
|
||||
}
|
||||
Log.info((player != null ? "%s executed" : "Executed") + ": %s", player != null ? player.getUsername() : line, line);
|
||||
}
|
||||
}
|
||||
|
|
29
proxy/src/main/java/proxy/command/Command.java
Normal file
29
proxy/src/main/java/proxy/command/Command.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
package proxy.command;
|
||||
|
||||
import proxy.Proxy;
|
||||
import proxy.network.ProxyHandler;
|
||||
import proxy.util.Formatter;
|
||||
import proxy.util.Log;
|
||||
|
||||
public abstract class Command {
|
||||
public abstract String getName();
|
||||
public abstract String getHelp();
|
||||
public String getArgs() {
|
||||
return null;
|
||||
}
|
||||
public abstract void run(Proxy proxy, ProxyHandler player, String[] args);
|
||||
public Iterable<String> complete(Proxy proxy, ProxyHandler player, String[] args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static void sendMessage(ProxyHandler player, String msg) {
|
||||
if(player != null)
|
||||
player.sendMessage(Formatter.DARK_PURPLE + msg);
|
||||
else
|
||||
Log.info(msg);
|
||||
}
|
||||
|
||||
protected static void sendMessage(ProxyHandler player, String fmt, Object ... args) {
|
||||
sendMessage(player, String.format(fmt, args));
|
||||
}
|
||||
}
|
29
proxy/src/main/java/proxy/command/CommandExit.java
Normal file
29
proxy/src/main/java/proxy/command/CommandExit.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
package proxy.command;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import proxy.Proxy;
|
||||
import proxy.network.ProxyHandler;
|
||||
import proxy.util.Formatter;
|
||||
|
||||
public class CommandExit extends Command {
|
||||
public String getName() {
|
||||
return "exit";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Stops the proxy with an optional exit message";
|
||||
}
|
||||
|
||||
public String getArgs() {
|
||||
return "[message ...]";
|
||||
}
|
||||
|
||||
public void run(Proxy proxy, ProxyHandler player, String[] args) {
|
||||
String msg = args.length == 0 ? "Proxy stopped" : Formatter.joinSpace(args);
|
||||
for(ProxyHandler plr : Lists.newArrayList(proxy.getPlayers())) {
|
||||
plr.disconnect(msg);
|
||||
}
|
||||
proxy.shutdown();
|
||||
}
|
||||
}
|
42
proxy/src/main/java/proxy/command/CommandHelp.java
Normal file
42
proxy/src/main/java/proxy/command/CommandHelp.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
package proxy.command;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import proxy.Proxy;
|
||||
import proxy.network.ProxyHandler;
|
||||
|
||||
public class CommandHelp extends Command {
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Displays proxy command help";
|
||||
}
|
||||
|
||||
public String getArgs() {
|
||||
return "[command]";
|
||||
}
|
||||
|
||||
private static String formatCommand(Command cmd, boolean prefix) {
|
||||
String args = cmd.getArgs();
|
||||
return (prefix ? "/vproxy " : "") + cmd.getName() + (args == null || args.isEmpty() ? "" : " " + args);
|
||||
}
|
||||
|
||||
public void run(Proxy proxy, ProxyHandler player, String[] args) {
|
||||
if(args.length == 0) {
|
||||
for(Command cmd : proxy.getCommands().values()) {
|
||||
sendMessage(player, formatCommand(cmd, player != null));
|
||||
}
|
||||
return;
|
||||
}
|
||||
Command cmd = proxy.getCommands().get(args[0].toLowerCase(Locale.US));
|
||||
if(cmd == null)
|
||||
throw new RunException("Command '%s' not found", args[0]);
|
||||
sendMessage(player, formatCommand(cmd, player != null));
|
||||
}
|
||||
|
||||
public Iterable<String> complete(Proxy proxy, ProxyHandler player, String[] args) {
|
||||
return args.length == 1 ? proxy.getCommands().keySet() : null;
|
||||
}
|
||||
}
|
28
proxy/src/main/java/proxy/command/RunException.java
Normal file
28
proxy/src/main/java/proxy/command/RunException.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package proxy.command;
|
||||
|
||||
public class RunException extends RuntimeException {
|
||||
public RunException(String desc) {
|
||||
super(desc);
|
||||
this.setStackTrace(new StackTraceElement[0]);
|
||||
}
|
||||
|
||||
public RunException(String fmt, Object ... args) {
|
||||
super(String.format(fmt, args));
|
||||
this.setStackTrace(new StackTraceElement[0]);
|
||||
}
|
||||
|
||||
public RunException(Throwable cause, String desc) {
|
||||
super(desc, cause);
|
||||
this.setStackTrace(new StackTraceElement[0]);
|
||||
}
|
||||
|
||||
public RunException(Throwable cause, String fmt, Object ... args) {
|
||||
super(String.format(fmt, args), cause);
|
||||
this.setStackTrace(new StackTraceElement[0]);
|
||||
}
|
||||
|
||||
public synchronized Throwable fillInStackTrace() {
|
||||
this.setStackTrace(new StackTraceElement[0]);
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -6,10 +6,17 @@ import io.netty.util.concurrent.GenericFutureListener;
|
|||
import java.net.IDN;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import proxy.Proxy;
|
||||
import proxy.command.Command;
|
||||
import proxy.packet.*;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
import proxy.util.User;
|
||||
import proxy.util.Stack;
|
||||
import proxy.util.Log;
|
||||
|
@ -47,6 +54,8 @@ public class ProxyHandler implements Handler {
|
|||
}
|
||||
}
|
||||
|
||||
private static final AtomicInteger CONN_COUNTER = new AtomicInteger();
|
||||
|
||||
private final Proxy proxy;
|
||||
private final Connection client;
|
||||
private final String username;
|
||||
|
@ -62,6 +71,8 @@ public class ProxyHandler implements Handler {
|
|||
private int passwordAttempts;
|
||||
private boolean wasClosed;
|
||||
private volatile int signPreloaded;
|
||||
private String completion;
|
||||
private boolean admin;
|
||||
|
||||
private static boolean isAllowedChar(char ch) {
|
||||
return ch != 167 && ch >= 32 && ch != 127;
|
||||
|
@ -92,19 +103,23 @@ public class ProxyHandler implements Handler {
|
|||
this.username = username;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setupPlayer() {
|
||||
this.sendToClient(new S01PacketJoinGame(0, true));
|
||||
this.sendToClient(new S03PacketTimeUpdate(3000L, false));
|
||||
this.sendToClient(new S26PacketMapChunkBulk(0, 64, 0));
|
||||
this.sendToClient(new S08PacketPlayerPosLook(0.5, 65.0, 0.5, 0.0f, 0.0f));
|
||||
this.sendMessage(ChatColor.DARK_PURPLE + "Welcome %s, please log in using the sign gui ...", this.username);
|
||||
this.sendMessage(Formatter.DARK_PURPLE + "Welcome %s, please log in using the sign gui ...", this.username);
|
||||
}
|
||||
|
||||
|
||||
public void sendMessage(String message) {
|
||||
this.sendToClient(new S02PacketChat(message));
|
||||
}
|
||||
|
||||
public void sendMessage(String fmt, Object ... args) {
|
||||
|
||||
public void sendMessage(String fmt, Object... args) {
|
||||
this.sendToClient(new S02PacketChat(String.format(fmt, args)));
|
||||
}
|
||||
|
||||
|
@ -156,33 +171,41 @@ public class ProxyHandler implements Handler {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void connectToServer() {
|
||||
this.proxy.setLoggedIn(this.username, this);
|
||||
Log.info("Connecting %s to %s:%d", this.username, this.proxy.getForwardHost(), this.proxy.getForwardPort());
|
||||
final Connection conn;
|
||||
try {
|
||||
conn = Connection.createNetworkManagerAndConnect(InetAddress.getByName(IDN.toASCII(this.proxy.getForwardHost())), this.proxy.getForwardPort(), this.proxy.isUsingEPoll());
|
||||
}
|
||||
catch(UnknownHostException e) {
|
||||
this.disconnect("Could not connect to server: unknown host, check if the hostname is correct");
|
||||
Log.error(e, "Could not connect to server");
|
||||
return;
|
||||
}
|
||||
catch(Exception e) {
|
||||
this.disconnect("Could not connect to server: connection failed, check if the server is running and reachable on this port");
|
||||
Log.error(e, "Could not connect to server");
|
||||
return;
|
||||
}
|
||||
conn.setNetHandler(new ProxyLoginHandler());
|
||||
conn.sendPacket(new H00PacketHandshake(47, this.proxy.getForwardHost(), this.proxy.getForwardPort(), Protocol.LOGIN), new GenericFutureListener<Future<? super Void>>() {
|
||||
public void operationComplete(Future<? super Void> future) throws Exception {
|
||||
conn.sendPacket(new L00PacketLoginStart(ProxyHandler.this.username));
|
||||
final String host = this.proxy.getForwardHost();
|
||||
final int port = this.proxy.getForwardPort();
|
||||
Log.info("Connecting %s to %s:%d", this.username, host, port);
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
final Connection conn;
|
||||
try {
|
||||
conn = Connection.createNetworkManagerAndConnect(InetAddress.getByName(IDN.toASCII(host)),
|
||||
port, ProxyHandler.this.proxy.isUsingEPoll());
|
||||
}
|
||||
catch(UnknownHostException e) {
|
||||
ProxyHandler.this.disconnect("Could not connect to server: unknown host, check if the hostname is correct");
|
||||
Log.error(e, "Could not connect to server");
|
||||
return;
|
||||
}
|
||||
catch(Exception e) {
|
||||
ProxyHandler.this.disconnect("Could not connect to server: connection failed, check if the server is running and reachable on this port");
|
||||
Log.error(e, "Could not connect to server");
|
||||
return;
|
||||
}
|
||||
conn.setNetHandler(new ProxyLoginHandler());
|
||||
conn.sendPacket(new H00PacketHandshake(47, host, port, Protocol.LOGIN),
|
||||
new GenericFutureListener<Future<? super Void>>() {
|
||||
public void operationComplete(Future<? super Void> future) throws Exception {
|
||||
conn.sendPacket(new L00PacketLoginStart(ProxyHandler.this.username));
|
||||
}
|
||||
});
|
||||
ProxyHandler.this.server = conn;
|
||||
ProxyHandler.this.connecting = true;
|
||||
Log.info("Connected %s to login phase", ProxyHandler.this.username);
|
||||
}
|
||||
});
|
||||
this.server = conn;
|
||||
this.connecting = true;
|
||||
Log.info("Connected %s to login phase", this.username);
|
||||
}, "Forward connector #" + CONN_COUNTER.incrementAndGet()).start();
|
||||
}
|
||||
|
||||
public void processInput(C0CPacketInput packetIn) {
|
||||
|
@ -198,7 +221,7 @@ public class ProxyHandler implements Handler {
|
|||
this.sendToClient(new S08PacketPlayerPosLook(0.5, 65.0, 0.5, 0.0f, 0.0f));
|
||||
if(this.signPreloaded == 1) {
|
||||
this.signPreloaded = 2;
|
||||
this.setSign(ChatColor.DARK_BLUE + "" + ChatColor.UNDERLINE + "Enter password", ChatColor.DARK_GRAY + "Use 1 or 2 lines");
|
||||
this.setSign(Formatter.DARK_BLUE + "" + Formatter.UNDERLINE + "Enter password", Formatter.DARK_GRAY + "Use 1 or 2 lines");
|
||||
}
|
||||
else if(this.signPreloaded == 0) {
|
||||
this.signPreloaded = 1;
|
||||
|
@ -219,12 +242,12 @@ public class ProxyHandler implements Handler {
|
|||
this.proxy.setLoggedOut(this.username);
|
||||
Log.info("%s lost connection: " + reason, this.username);
|
||||
}
|
||||
|
||||
|
||||
private void setSign(String message, String desc) {
|
||||
this.sendToClient(new S33PacketUpdateSign(0, 66, 1, "", "", message, desc));
|
||||
this.sendToClient(new S36PacketSignEditorOpen(0, 66, 1));
|
||||
}
|
||||
|
||||
|
||||
private void handlePassword(String password) {
|
||||
if(password.isEmpty()) {
|
||||
if(this.wasClosed) {
|
||||
|
@ -232,7 +255,7 @@ public class ProxyHandler implements Handler {
|
|||
}
|
||||
else {
|
||||
this.wasClosed = true;
|
||||
this.setSign(ChatColor.GOLD + "Press esc again", ChatColor.GOLD + "to disconnect ...");
|
||||
this.setSign(Formatter.GOLD + "Press esc again", Formatter.GOLD + "to disconnect ...");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -243,14 +266,17 @@ public class ProxyHandler implements Handler {
|
|||
this.disconnect("You are not whitelisted on this server");
|
||||
}
|
||||
else if(password.isEmpty() || password.length() < this.proxy.getMinimumPasswordLength()) {
|
||||
this.setSign(ChatColor.DARK_RED + "" + ChatColor.UNDERLINE + "Too short" + ChatColor.DARK_RED + ", use", ChatColor.DARK_RED + "" + this.proxy.getMinimumPasswordLength() + "-32 characters");
|
||||
this.setSign(Formatter.DARK_RED + "" + Formatter.UNDERLINE + "Too short" + Formatter.DARK_RED + ", use",
|
||||
Formatter.DARK_RED + "" + this.proxy.getMinimumPasswordLength() + "-32 characters");
|
||||
}
|
||||
else if(password.length() > 32) {
|
||||
this.setSign(ChatColor.DARK_RED + "" + ChatColor.UNDERLINE + "Too long" + ChatColor.DARK_RED + ", use", ChatColor.DARK_RED + "" + this.proxy.getMinimumPasswordLength() + "-32 characters");
|
||||
this.setSign(Formatter.DARK_RED + "" + Formatter.UNDERLINE + "Too long" + Formatter.DARK_RED + ", use",
|
||||
Formatter.DARK_RED + "" + this.proxy.getMinimumPasswordLength() + "-32 characters");
|
||||
}
|
||||
else {
|
||||
this.proxy.setUser(this.username, User.createUser(this.username, password));
|
||||
Log.info("%s registered with password", this.username);
|
||||
this.admin = true; //false;
|
||||
this.connectToServer();
|
||||
}
|
||||
}
|
||||
|
@ -259,7 +285,7 @@ public class ProxyHandler implements Handler {
|
|||
if(++this.passwordAttempts >= this.proxy.getMaximumPasswordAttempts())
|
||||
this.disconnect("Too many password attempts");
|
||||
else
|
||||
this.setSign(ChatColor.DARK_RED + "" + ChatColor.UNDERLINE + "Wrong password", ChatColor.DARK_RED + "Please try again");
|
||||
this.setSign(Formatter.DARK_RED + "" + Formatter.UNDERLINE + "Wrong password", Formatter.DARK_RED + "Please try again");
|
||||
Log.info("%s failed password attempt %d/%d", this.username, this.passwordAttempts, this.proxy.getMaximumPasswordAttempts());
|
||||
}
|
||||
else if(this.proxy.isCheckingCase() && !stored.getName().equals(this.username)) {
|
||||
|
@ -268,6 +294,7 @@ public class ProxyHandler implements Handler {
|
|||
}
|
||||
else {
|
||||
Log.info("%s logged in with password", this.username);
|
||||
this.admin = true; //stored.isAdmin();
|
||||
this.connectToServer();
|
||||
}
|
||||
}
|
||||
|
@ -276,8 +303,8 @@ public class ProxyHandler implements Handler {
|
|||
public void processUpdateSign(C12PacketUpdateSign packetIn) {
|
||||
if(!this.connected) {
|
||||
Handler.syncToMain(packetIn, this, this.proxy);
|
||||
String line1 = ChatColor.fromJsonString(packetIn.getLines()[0]);
|
||||
String line2 = ChatColor.fromJsonString(packetIn.getLines()[1]);
|
||||
String line1 = Formatter.fromJsonString(packetIn.getLines()[0]);
|
||||
String line2 = Formatter.fromJsonString(packetIn.getLines()[1]);
|
||||
if(line1 == null || line2 == null || line1.length() > 50 || line2.length() > 50 || !isValidString(line1) || !isValidString(line2))
|
||||
return;
|
||||
String password = line1 + line2;
|
||||
|
@ -286,29 +313,111 @@ public class ProxyHandler implements Handler {
|
|||
}
|
||||
String[] lines = packetIn.getLines();
|
||||
for(String line : lines) {
|
||||
String value = ChatColor.fromJsonString(line);
|
||||
String value = Formatter.fromJsonString(line);
|
||||
if(value == null || value.length() > 50 || !isValidString(value))
|
||||
return;
|
||||
}
|
||||
this.sendToServer(packetIn);
|
||||
}
|
||||
|
||||
public List<String> getCompletions(String[] args) {
|
||||
if(args.length == 0)
|
||||
args = new String[] {""};
|
||||
String param = args[0];
|
||||
List<String> list = Lists.<String>newArrayList();
|
||||
if(args.length == 1) {
|
||||
for(String cmd : this.proxy.getCommands().keySet()) {
|
||||
if(cmd.regionMatches(true, 0, param, 0, param.length()))
|
||||
list.add(cmd);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
else if(args.length > 1) {
|
||||
Command cmd = this.proxy.getCommands().get(param.toLowerCase(Locale.US));
|
||||
if(cmd == null)
|
||||
return list;
|
||||
String[] argv = new String[args.length - 1];
|
||||
System.arraycopy(args, 1, argv, 0, argv.length);
|
||||
Iterable<String> comps = cmd.complete(this.proxy, this, argv);
|
||||
if(comps == null)
|
||||
return list;
|
||||
param = args[args.length - 1];
|
||||
for(String comp : comps) {
|
||||
if(comp.regionMatches(true, 0, param, 0, param.length()))
|
||||
list.add(comp);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public void processChatMessage(C01PacketChatMessage packetIn) {
|
||||
if(this.connected)
|
||||
if(this.connected) {
|
||||
Handler.syncToMain(packetIn, this, this.proxy);
|
||||
String msg = packetIn.getMessage();
|
||||
if(msg.startsWith("/")) {
|
||||
if(msg.indexOf(' ') == -1) {
|
||||
if(msg.equals("/vproxy")) {
|
||||
if(!this.admin) {
|
||||
this.sendMessage(Formatter.DARK_RED + "You are not allowed to execute this command");
|
||||
return;
|
||||
}
|
||||
this.proxy.runCommand(this, new String[0], msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(msg.startsWith("/vproxy ")) {
|
||||
if(!this.admin) {
|
||||
this.sendMessage(Formatter.DARK_RED + "You are not allowed to execute this command");
|
||||
return;
|
||||
}
|
||||
String[] args = Formatter.filterSpaces(msg).split(" ", -1);
|
||||
String[] argv = new String[args.length - 1];
|
||||
System.arraycopy(args, 1, argv, 0, argv.length);
|
||||
this.proxy.runCommand(this, argv, msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.sendToServer(packetIn);
|
||||
}
|
||||
}
|
||||
|
||||
public void processTabComplete(C14PacketTabComplete packetIn) {
|
||||
// PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.proxy);
|
||||
if(this.connected) // {
|
||||
// this.completion = packetIn.getMessage();
|
||||
if(this.connected) {
|
||||
Handler.syncToMain(packetIn, this, this.proxy);
|
||||
this.completion = packetIn.getMessage();
|
||||
this.sendToServer(packetIn);
|
||||
// }
|
||||
// else {
|
||||
// List<String> list = Lists.newArrayList();
|
||||
// // TODO: completions
|
||||
// this.sendToClient(new S3APacketTabComplete(list.toArray(new String[list.size()])));
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public void handleTabComplete(S3APacketTabComplete packetIn) {
|
||||
Handler.syncToMain(packetIn, this, this.proxy);
|
||||
String[] comps = packetIn.getMatches();
|
||||
if(this.completion != null) {
|
||||
if(this.admin && this.completion.startsWith("/")) {
|
||||
if(this.completion.indexOf(' ') == -1) {
|
||||
List<String> list = Lists.newArrayList(comps);
|
||||
if("/vproxy".regionMatches(true, 0, this.completion, 0, this.completion.length()))
|
||||
list.add("/vproxy");
|
||||
Collections.sort(list);
|
||||
this.sendToClient(new S3APacketTabComplete(list.toArray(new String[list.size()])));
|
||||
this.completion = null;
|
||||
return;
|
||||
}
|
||||
else if(this.completion.startsWith("/vproxy ")) {
|
||||
String[] args = this.completion.split(" ", -1);
|
||||
String[] argv = new String[args.length - 1];
|
||||
System.arraycopy(args, 1, argv, 0, argv.length);
|
||||
List<String> list = this.getCompletions(argv);
|
||||
this.sendToClient(new S3APacketTabComplete(list.toArray(new String[list.size()])));
|
||||
this.completion = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.completion = null;
|
||||
}
|
||||
this.sendToClient(packetIn);
|
||||
}
|
||||
|
||||
public void processPlayerDigging(C07PacketPlayerDigging packetIn) {
|
||||
|
@ -405,7 +514,7 @@ public class ProxyHandler implements Handler {
|
|||
PacketBuffer packetbuffer = packetIn.getBufferData();
|
||||
String brand = packetbuffer.readStringFromBuffer(32767);
|
||||
Log.info("Client brand of %s: %s", this.username,
|
||||
filterString(ChatColor.strip(brand.length() > 128 ? brand.substring(0, 128) + " ..." : brand)));
|
||||
filterString(Formatter.strip(brand.length() > 128 ? brand.substring(0, 128) + " ..." : brand)));
|
||||
packetbuffer.release();
|
||||
}
|
||||
return;
|
||||
|
@ -527,15 +636,6 @@ public class ProxyHandler implements Handler {
|
|||
this.server.setCompressionTreshold(packetIn.getThreshold());
|
||||
}
|
||||
|
||||
public void handleTabComplete(S3APacketTabComplete packetIn) {
|
||||
// PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.proxy);
|
||||
// String[] comps = packetIn.func_149630_c();
|
||||
// if(this.completion != null) {
|
||||
// // TODO: completion
|
||||
// }
|
||||
this.sendToClient(packetIn);
|
||||
}
|
||||
|
||||
public void handleKeepAlive(S00PacketKeepAlive packetIn) {
|
||||
this.sendToClient(packetIn);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.io.IOException;
|
|||
import proxy.network.ProxyHandler.ProxyLoginHandler;
|
||||
import proxy.network.Packet;
|
||||
import proxy.network.PacketBuffer;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
|
||||
public class R00PacketDisconnect implements Packet<ProxyLoginHandler>
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ public class R00PacketDisconnect implements Packet<ProxyLoginHandler>
|
|||
|
||||
public R00PacketDisconnect(String reasonIn)
|
||||
{
|
||||
this.reason = ChatColor.toJsonString(reasonIn);
|
||||
this.reason = Formatter.toJsonString(reasonIn);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,6 +46,6 @@ public class R00PacketDisconnect implements Packet<ProxyLoginHandler>
|
|||
|
||||
public String getReason()
|
||||
{
|
||||
return ChatColor.getStringOrJson(this.reason);
|
||||
return Formatter.getStringOrJson(this.reason);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.io.IOException;
|
|||
import proxy.network.ProxyHandler;
|
||||
import proxy.network.Packet;
|
||||
import proxy.network.PacketBuffer;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
|
||||
public class S02PacketChat implements Packet<ProxyHandler>
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ public class S02PacketChat implements Packet<ProxyHandler>
|
|||
|
||||
public S02PacketChat(String message)
|
||||
{
|
||||
this.chatComponent = ChatColor.toJsonString(message);
|
||||
this.chatComponent = Formatter.toJsonString(message);
|
||||
this.type = 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import proxy.network.ProxyHandler;
|
|||
import proxy.network.Packet;
|
||||
import proxy.network.PacketBuffer;
|
||||
import proxy.util.Vec3;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
|
||||
public class S33PacketUpdateSign implements Packet<ProxyHandler>
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ public class S33PacketUpdateSign implements Packet<ProxyHandler>
|
|||
|
||||
public S33PacketUpdateSign(int x, int y, int z, String line1, String line2, String line3, String line4) {
|
||||
this.blockPos = new Vec3(x, y, z);
|
||||
this.lines = new String[] {ChatColor.toJsonString(line1), ChatColor.toJsonString(line2), ChatColor.toJsonString(line3), ChatColor.toJsonString(line4)};
|
||||
this.lines = new String[] {Formatter.toJsonString(line1), Formatter.toJsonString(line2), Formatter.toJsonString(line3), Formatter.toJsonString(line4)};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -53,7 +53,7 @@ public class S3APacketTabComplete implements Packet<ProxyHandler>
|
|||
handler.handleTabComplete(this);
|
||||
}
|
||||
|
||||
public String[] func_149630_c()
|
||||
public String[] getMatches()
|
||||
{
|
||||
return this.matches;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.io.IOException;
|
|||
import proxy.network.ProxyHandler;
|
||||
import proxy.network.Packet;
|
||||
import proxy.network.PacketBuffer;
|
||||
import proxy.util.ChatColor;
|
||||
import proxy.util.Formatter;
|
||||
|
||||
public class S40PacketDisconnect implements Packet<ProxyHandler>
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ public class S40PacketDisconnect implements Packet<ProxyHandler>
|
|||
|
||||
public S40PacketDisconnect(String reasonIn)
|
||||
{
|
||||
this.reason = ChatColor.toJsonString(reasonIn);
|
||||
this.reason = Formatter.toJsonString(reasonIn);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,6 +46,6 @@ public class S40PacketDisconnect implements Packet<ProxyHandler>
|
|||
|
||||
public String getReason()
|
||||
{
|
||||
return ChatColor.getStringOrJson(this.reason);
|
||||
return Formatter.getStringOrJson(this.reason);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.google.gson.JsonElement;
|
|||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public enum ChatColor {
|
||||
public enum Formatter {
|
||||
BLACK('0'),
|
||||
DARK_BLUE('1'),
|
||||
DARK_GREEN('2'),
|
||||
|
@ -58,8 +58,39 @@ public enum ChatColor {
|
|||
String str = fromJsonString(json);
|
||||
return str == null ? "JSON:" + json : str;
|
||||
}
|
||||
|
||||
public static String filterSpaces(String str) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean space = true;
|
||||
for(int z = 0; z < str.length(); z++) {
|
||||
char ch = str.charAt(z);
|
||||
if(ch != ' ') {
|
||||
sb.append(ch);
|
||||
space = false;
|
||||
}
|
||||
else if(!space) {
|
||||
sb.append(' ');
|
||||
space = true;
|
||||
}
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
public static String joinSpace(String ... objs) {
|
||||
return joinSpace(objs);
|
||||
}
|
||||
|
||||
public static String joinSpace(Object ... objs) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(Object obj : objs) {
|
||||
if(sb.length() != 0)
|
||||
sb.append(' ');
|
||||
sb.append(obj);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private ChatColor(char code) {
|
||||
private Formatter(char code) {
|
||||
this.value = "\u00a7" + code;
|
||||
}
|
||||
|
|
@ -12,6 +12,8 @@ public class User {
|
|||
private final byte[] hash;
|
||||
private final byte[] salt;
|
||||
|
||||
private boolean admin;
|
||||
|
||||
public User(String username, byte[] hash, byte[] salt) {
|
||||
this.username = username;
|
||||
this.hash = hash;
|
||||
|
@ -26,6 +28,14 @@ public class User {
|
|||
return MessageDigest.isEqual(hashPassword(password, this.salt), this.hash);
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return this.admin;
|
||||
}
|
||||
|
||||
public void setAdmin(boolean admin) {
|
||||
this.admin = admin;
|
||||
}
|
||||
|
||||
private static byte[] hashPassword(String pass, byte[] salt) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-512");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue