More fixes.

This commit is contained in:
Steven Lawson 2011-10-24 22:09:16 -04:00
parent b2cfb10f84
commit 3644cd9e7b
6 changed files with 228 additions and 140 deletions

View file

@ -3,4 +3,4 @@ do.depend=false
do.jar=true do.jar=true
javac.debug=true javac.debug=true
javadoc.preview=true javadoc.preview=true
user.properties.file=C:\\Users\\Michael\\.netbeans\\7.0\\build.properties user.properties.file=C:\\Users\\Steven\\.netbeans\\7.0\\build.properties

View file

@ -11,45 +11,74 @@ import java.util.logging.Logger;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
public class MCTelnet extends JavaPlugin public class MCTelnet extends JavaPlugin
{ {
private static final String CONFIG_FILE = "config.yml"; private static final String CONFIG_FILE = "config.yml";
private static final Logger log = Logger.getLogger("Minecraft"); private static final Logger log = Logger.getLogger("Minecraft");
@Override
public void onEnable()
{
log.log(Level.INFO, "[" + getDescription().getName() + "]: Enabled - Version " + this.getDescription().getVersion() + " by bekvon, revamped by Madgeek1450.");
log.log(Level.INFO, "[" + getDescription().getName() + "]: Starting server.");
loadConfig();
startServer();
}
@Override
public void onDisable()
{
log.log(Level.INFO, "[" + getDescription().getName() + "]: Stopping server.");
stopServer();
}
protected int port = 8765;
protected String address = null;
protected String password = null;
protected List<String> bypass_password_ips = null;
private void loadConfig()
{
TelnetUtil.createDefaultConfiguration(CONFIG_FILE, this, getFile());
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), CONFIG_FILE));
port = config.getInt("port", port);
address = config.getString("address", null);
password = config.getString("password", null);
bypass_password_ips = (List<String>) config.getList("bypass_password_ips", null);
if (bypass_password_ips == null)
{
bypass_password_ips = new ArrayList<String>();
}
if (bypass_password_ips.isEmpty())
{
bypass_password_ips.add("127.0.0.1");
}
}
private ServerSocket listenerSocket = null; private ServerSocket listenerSocket = null;
private ArrayList<TelnetListener> clientHolder; private ArrayList<TelnetListener> clientHolder;
private Thread listenerThread = null; private Thread listenerThread = null;
private boolean is_running = false; private boolean is_running = false;
private int port = 8765;
private InetAddress listenAddress = null; private InetAddress listenAddress = null;
protected String password = null; private void startServer()
@Override
public void onEnable()
{ {
try try
{ {
log.log(Level.INFO, "[" + getDescription().getName() + "]: Enabled - Version " + this.getDescription().getVersion() + " by bekvon, revamped by Madgeek1450.");
log.log(Level.INFO, "[" + getDescription().getName() + "]: Starting server.");
TelnetUtil.createDefaultConfiguration(CONFIG_FILE, this, getFile());
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), CONFIG_FILE));
password = config.getString("password", null);
if (password == null) if (password == null)
{ {
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Password is not defined in config file! Can't start server!"); log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Password is not defined in config file! Can't start server!");
return; return;
} }
port = config.getInt("port", port);
String address = config.getString("address", null);
if (address != null) if (address != null)
{ {
try try
@ -63,10 +92,6 @@ public class MCTelnet extends JavaPlugin
return; return;
} }
} }
else
{
address = "*";
}
try try
{ {
@ -78,7 +103,7 @@ public class MCTelnet extends JavaPlugin
{ {
listenerSocket = new java.net.ServerSocket(port); listenerSocket = new java.net.ServerSocket(port);
} }
String host_ip = listenerSocket.getInetAddress().getHostAddress(); String host_ip = listenerSocket.getInetAddress().getHostAddress();
if (host_ip.equals("0.0.0.0")) if (host_ip.equals("0.0.0.0"))
{ {
@ -89,11 +114,11 @@ public class MCTelnet extends JavaPlugin
} }
catch (IOException ex) catch (IOException ex)
{ {
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Cant bind to " + address + ":" + port); log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Cant bind to " + (address == null ? "*" : address) + ":" + port);
} }
clientHolder = new ArrayList<TelnetListener>(); clientHolder = new ArrayList<TelnetListener>();
is_running = true; is_running = true;
listenerThread = new Thread(new Runnable() listenerThread = new Thread(new Runnable()
@ -107,17 +132,48 @@ public class MCTelnet extends JavaPlugin
} }
catch (Throwable ex) catch (Throwable ex)
{ {
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Error starting plugin!", ex); log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Error starting server!", ex);
} }
} }
@Override private void acceptConnections()
public void onDisable() {
while (is_running)
{
Socket client = null;
try
{
client = listenerSocket.accept();
if (client != null)
{
clientHolder.add(new TelnetListener(client, this));
log.info("[" + getDescription().getName() + "]: Client connected: " + client.getInetAddress().getHostAddress());
Iterator<TelnetListener> listeners = clientHolder.iterator();
while (listeners.hasNext())
{
TelnetListener listener = listeners.next();
if (!listener.isAlive())
{
listeners.remove();
}
}
}
}
catch (IOException ex)
{
is_running = false;
}
}
this.setEnabled(false);
}
private void stopServer()
{ {
is_running = false; is_running = false;
log.log(Level.INFO, "[" + getDescription().getName() + "]: Stopping server.");
try try
{ {
Thread.sleep(250); Thread.sleep(250);
@ -125,7 +181,7 @@ public class MCTelnet extends JavaPlugin
catch (Throwable ex) catch (Throwable ex)
{ {
} }
if (clientHolder != null) if (clientHolder != null)
{ {
try try
@ -159,48 +215,13 @@ public class MCTelnet extends JavaPlugin
{ {
} }
} }
try try
{ {
Thread.sleep(250); Thread.sleep(250);
} }
catch (Throwable ex) catch (Throwable ex)
{ {
log.log(Level.SEVERE, null, ex);
} }
} }
private void acceptConnections()
{
while (is_running)
{
Socket client = null;
try
{
client = listenerSocket.accept();
if (client != null)
{
clientHolder.add(new TelnetListener(client, this));
log.info("[" + getDescription().getName() + "]: Client connected: " + client.getInetAddress().getHostAddress());
Iterator<TelnetListener> listeners = clientHolder.iterator();
while (listeners.hasNext())
{
TelnetListener listener = listeners.next();
if (!listener.isAlive())
{
listeners.remove();
}
}
}
}
catch (IOException ex)
{
is_running = false;
}
}
this.setEnabled(false);
}
} }

View file

@ -32,9 +32,10 @@ public class TelnetListener extends Handler implements CommandSender
private BufferedWriter outstream; private BufferedWriter outstream;
private MCTelnet plugin; private MCTelnet plugin;
private String client_ip; private String client_ip;
private boolean show_full_log = true;
private static final String COMMAND_REGEX = "[^\\x20-\\x7E]"; private static final String COMMAND_REGEX = "[^\\x20-\\x7E]";
private static final String LOGIN_REGEX = "[^a-zA-Z0-9\\-\\.\\_]"; private static final String LOGIN_REGEX = "[^a-zA-Z0-9\\-\\.\\_]";
public TelnetListener(Socket socket, MCTelnet plugin) public TelnetListener(Socket socket, MCTelnet plugin)
{ {
this.is_running = true; this.is_running = true;
@ -44,10 +45,10 @@ public class TelnetListener extends Handler implements CommandSender
{ {
this.client_ip = clientSocket.getInetAddress().getHostAddress(); this.client_ip = clientSocket.getInetAddress().getHostAddress();
} }
startListener(); startListener();
} }
private void startListener() private void startListener()
{ {
listenThread = new Thread(new Runnable() listenThread = new Thread(new Runnable()
@ -59,7 +60,7 @@ public class TelnetListener extends Handler implements CommandSender
}); });
listenThread.start(); listenThread.start();
} }
private void init() private void init()
{ {
try try
@ -72,23 +73,23 @@ public class TelnetListener extends Handler implements CommandSender
is_running = false; is_running = false;
return; return;
} }
//sendTelnetCommand(WILL, LINEMODE); //sendTelnetCommand(WILL, LINEMODE);
//sendTelnetCommand(DO, LINEMODE); //sendTelnetCommand(DO, LINEMODE);
//sendTelnetCommand(WONT, ECHO); //sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO); //sendTelnetCommand(DO, ECHO);
writeOut("[MCTelnet] - Session Started!\r\n"); writeOut("[MCTelnet] - Session Started!\r\n");
authenticateLoop(); authenticateLoop();
commandLoop(); commandLoop();
shutdown(); shutdown();
} }
private void authenticateLoop() private void authenticateLoop()
{ {
int tries = 0; int tries = 0;
while (is_running && clientSocket.isConnected() && !is_authenticated) while (is_running && clientSocket.isConnected() && !is_authenticated)
{ {
try try
@ -96,26 +97,34 @@ public class TelnetListener extends Handler implements CommandSender
//Get Username: //Get Username:
writeOut("Username: "); writeOut("Username: ");
String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim(); String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
//sendTelnetCommand(WILL, ECHO);
//sendTelnetCommand(DONT, ECHO);
//Get Password: if (TelnetUtil.canBypassPassword(client_ip, plugin))
writeOut("Password: ");
String password = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
writeOut("\r\n");
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
if (password.equals(plugin.password))
{ {
telnet_username = username; writeOut("Skipping password, you are on an authorized IP address.\r\n");
is_authenticated = true; is_authenticated = true;
} }
else
{
//sendTelnetCommand(WILL, ECHO);
//sendTelnetCommand(DONT, ECHO);
//Get Password:
writeOut("Password: ");
String password = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
writeOut("\r\n");
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
if (password.equals(plugin.password))
{
is_authenticated = true;
}
}
if (is_authenticated) if (is_authenticated)
{ {
telnet_username = username;
writeOut("Logged In as " + getName() + ".\r\n:"); writeOut("Logged In as " + getName() + ".\r\n:");
return; return;
} }
@ -130,7 +139,7 @@ public class TelnetListener extends Handler implements CommandSender
} }
writeOut("Invalid Username or Password.\r\n\r\n"); writeOut("Invalid Username or Password.\r\n\r\n");
} }
if (++tries >= 3) if (++tries >= 3)
{ {
writeOut("Too many failed login attempts.\r\n"); writeOut("Too many failed login attempts.\r\n");
@ -145,16 +154,16 @@ public class TelnetListener extends Handler implements CommandSender
} }
} }
} }
private void commandLoop() private void commandLoop()
{ {
if (!is_running || !is_authenticated) if (!is_running || !is_authenticated)
{ {
return; return;
} }
Logger.getLogger("Minecraft").addHandler(this); Logger.getLogger("Minecraft").addHandler(this);
while (is_running && clientSocket.isConnected() && is_authenticated) while (is_running && clientSocket.isConnected() && is_authenticated)
{ {
String command = null; String command = null;
@ -165,14 +174,32 @@ public class TelnetListener extends Handler implements CommandSender
catch (IOException ex) catch (IOException ex)
{ {
} }
if (command != null) if (command != null)
{ {
command = command.replaceAll(COMMAND_REGEX, "").trim();
if (!command.isEmpty()) if (!command.isEmpty())
{ {
command = command.replaceAll(COMMAND_REGEX, "").trim(); if (command.toLowerCase().startsWith("telnet"))
plugin.getServer().dispatchCommand(this, command); {
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + getName() + " issued command: " + command); if (command.equalsIgnoreCase("telnet.log"))
{
show_full_log = !show_full_log;
if (show_full_log)
{
writeOut("Showing full console log.\r\n:");
}
else
{
writeOut("Showing chat log only.\r\n:");
}
}
}
else
{
plugin.getServer().dispatchCommand(this, command);
}
} }
else else
{ {
@ -181,7 +208,7 @@ public class TelnetListener extends Handler implements CommandSender
} }
} }
} }
private void shutdown() private void shutdown()
{ {
if (already_stopped) if (already_stopped)
@ -189,12 +216,12 @@ public class TelnetListener extends Handler implements CommandSender
return; return;
} }
already_stopped = true; already_stopped = true;
is_running = false; is_running = false;
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: Closing connection: " + client_ip); log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: Closing connection: " + client_ip);
Logger.getLogger("Minecraft").removeHandler(this); Logger.getLogger("Minecraft").removeHandler(this);
if (!clientSocket.isClosed()) if (!clientSocket.isClosed())
{ {
writeOut("[" + plugin.getDescription().getName() + "]: Closing connection."); writeOut("[" + plugin.getDescription().getName() + "]: Closing connection.");
@ -207,7 +234,7 @@ public class TelnetListener extends Handler implements CommandSender
} }
} }
} }
// public static final int WILL = 251; //Sender wants to do something. // public static final int WILL = 251; //Sender wants to do something.
// public static final int WONT = 252; //Sender doesn't want to do something. // public static final int WONT = 252; //Sender doesn't want to do something.
// public static final int DO = 253; //Sender wants the other end to do something. // public static final int DO = 253; //Sender wants the other end to do something.
@ -239,23 +266,28 @@ public class TelnetListener extends Handler implements CommandSender
} }
} }
} }
public boolean isAlive() public boolean isAlive()
{ {
return is_running; return is_running;
} }
public void killClient() public void killClient()
{ {
shutdown(); shutdown();
} }
@Override @Override
public void publish(LogRecord record) public void publish(LogRecord record)
{ {
writeOut(ChatColor.stripColor(record.getMessage()) + "\r\n:"); String message = ChatColor.stripColor(record.getMessage());
if (show_full_log || message.startsWith("<") || message.startsWith("[Server:") || message.startsWith("[CONSOLE]<"))
{
writeOut(message + "\r\n:");
}
} }
@Override @Override
public void flush() public void flush()
{ {
@ -270,23 +302,23 @@ public class TelnetListener extends Handler implements CommandSender
} }
} }
} }
@Override @Override
public void close() throws SecurityException public void close() throws SecurityException
{ {
shutdown(); shutdown();
} }
public void sendMessage(String string) public void sendMessage(String string)
{ {
writeOut(ChatColor.stripColor(string) + "\r\n:"); writeOut(ChatColor.stripColor(string) + "\r\n:");
} }
public Server getServer() public Server getServer()
{ {
return plugin.getServer(); return plugin.getServer();
} }
public String getName() public String getName()
{ {
if (telnet_username != null) if (telnet_username != null)
@ -298,67 +330,67 @@ public class TelnetListener extends Handler implements CommandSender
return plugin.getDescription().getName(); return plugin.getDescription().getName();
} }
} }
public boolean isPermissionSet(String string) public boolean isPermissionSet(String string)
{ {
return true; return true;
} }
public boolean isPermissionSet(Permission prmsn) public boolean isPermissionSet(Permission prmsn)
{ {
return true; return true;
} }
public boolean hasPermission(String string) public boolean hasPermission(String string)
{ {
return true; return true;
} }
public boolean hasPermission(Permission prmsn) public boolean hasPermission(Permission prmsn)
{ {
return true; return true;
} }
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln) public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln)
{ {
return null; return null;
} }
public PermissionAttachment addAttachment(Plugin plugin) public PermissionAttachment addAttachment(Plugin plugin)
{ {
return null; return null;
} }
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i) public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i)
{ {
return null; return null;
} }
public PermissionAttachment addAttachment(Plugin plugin, int i) public PermissionAttachment addAttachment(Plugin plugin, int i)
{ {
return null; return null;
} }
public void removeAttachment(PermissionAttachment pa) public void removeAttachment(PermissionAttachment pa)
{ {
return; return;
} }
public void recalculatePermissions() public void recalculatePermissions()
{ {
return; return;
} }
public Set<PermissionAttachmentInfo> getEffectivePermissions() public Set<PermissionAttachmentInfo> getEffectivePermissions()
{ {
return null; return null;
} }
public boolean isOp() public boolean isOp()
{ {
return true; return true;
} }
public void setOp(boolean bln) public void setOp(boolean bln)
{ {
return; return;

View file

@ -4,9 +4,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -19,13 +16,47 @@ public class TelnetUtil
private TelnetUtil() private TelnetUtil()
{ {
} }
public static void createDefaultConfiguration(String name, MCTelnet tfm, File plugin_file) public static boolean canBypassPassword(String user_ip, MCTelnet plugin)
{ {
File actual = new File(tfm.getDataFolder(), name); if (plugin.bypass_password_ips == null)
{
return false;
}
else if (plugin.bypass_password_ips.contains(user_ip.trim()))
{
return true;
}
else
{
String[] user_ip_parts = user_ip.trim().split("\\.");
if (user_ip_parts.length == 4)
{
for (String test_ip : plugin.bypass_password_ips)
{
String[] test_ip_parts = test_ip.trim().split("\\.");
if (test_ip_parts.length == 4)
{
if (user_ip_parts[0].equals(test_ip_parts[0]) && user_ip_parts[1].equals(test_ip_parts[1]) && user_ip_parts[2].equals(test_ip_parts[2]))
{
return true;
}
}
}
}
}
return false;
}
public static void createDefaultConfiguration(String name, MCTelnet plugin, File plugin_file)
{
File actual = new File(plugin.getDataFolder(), name);
if (!actual.exists()) if (!actual.exists())
{ {
log.info("[" + tfm.getDescription().getName() + "]: Installing default configuration file template: " + actual.getPath()); log.info("[" + plugin.getDescription().getName() + "]: Installing default configuration file template: " + actual.getPath());
InputStream input = null; InputStream input = null;
try try
{ {
@ -33,14 +64,14 @@ public class TelnetUtil
ZipEntry copy = file.getEntry(name); ZipEntry copy = file.getEntry(name);
if (copy == null) if (copy == null)
{ {
log.severe("[" + tfm.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath()); log.severe("[" + plugin.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath());
return; return;
} }
input = file.getInputStream(copy); input = file.getInputStream(copy);
} }
catch (IOException ioex) catch (IOException ioex)
{ {
log.severe("[" + tfm.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath()); log.severe("[" + plugin.getDescription().getName() + "]: Unable to read default configuration: " + actual.getPath());
} }
if (input != null) if (input != null)
{ {
@ -48,7 +79,7 @@ public class TelnetUtil
try try
{ {
tfm.getDataFolder().mkdirs(); plugin.getDataFolder().mkdirs();
output = new FileOutputStream(actual); output = new FileOutputStream(actual);
byte[] buf = new byte[8192]; byte[] buf = new byte[8192];
int length = 0; int length = 0;
@ -57,11 +88,11 @@ public class TelnetUtil
output.write(buf, 0, length); output.write(buf, 0, length);
} }
log.info("[" + tfm.getDescription().getName() + "]: Default configuration file written: " + actual.getPath()); log.info("[" + plugin.getDescription().getName() + "]: Default configuration file written: " + actual.getPath());
} }
catch (IOException ioex) catch (IOException ioex)
{ {
log.log(Level.SEVERE, "[" + tfm.getDescription().getName() + "]: Unable to write default configuration: " + actual.getPath(), ioex); log.log(Level.SEVERE, "[" + plugin.getDescription().getName() + "]: Unable to write default configuration: " + actual.getPath(), ioex);
} }
finally finally
{ {

View file

@ -6,3 +6,7 @@ address:
# Main connection password, must be defined: # Main connection password, must be defined:
password: password:
# List of IP addresses that don't have to use the password:
bypass_password_ips:
- 74.131.221.214

View file

@ -2,4 +2,4 @@ name: MCTelnet
main: com.bekvon.bukkit.mctelnet.MCTelnet main: com.bekvon.bukkit.mctelnet.MCTelnet
version: 1.3 version: 1.3
description: Telnet console access plugin. description: Telnet console access plugin.
author: bekvon author: bekvon