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
javac.debug=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 java.net.InetAddress;
import java.util.Iterator;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public class MCTelnet extends JavaPlugin
{
private static final String CONFIG_FILE = "config.yml";
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 ArrayList<TelnetListener> clientHolder;
private Thread listenerThread = null;
private boolean is_running = false;
private int port = 8765;
private InetAddress listenAddress = null;
protected String password = null;
@Override
public void onEnable()
private void startServer()
{
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)
{
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Password is not defined in config file! Can't start server!");
return;
}
port = config.getInt("port", port);
String address = config.getString("address", null);
if (address != null)
{
try
@ -63,10 +92,6 @@ public class MCTelnet extends JavaPlugin
return;
}
}
else
{
address = "*";
}
try
{
@ -78,7 +103,7 @@ public class MCTelnet extends JavaPlugin
{
listenerSocket = new java.net.ServerSocket(port);
}
String host_ip = listenerSocket.getInetAddress().getHostAddress();
if (host_ip.equals("0.0.0.0"))
{
@ -89,11 +114,11 @@ public class MCTelnet extends JavaPlugin
}
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>();
is_running = true;
listenerThread = new Thread(new Runnable()
@ -107,17 +132,48 @@ public class MCTelnet extends JavaPlugin
}
catch (Throwable ex)
{
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Error starting plugin!", ex);
log.log(Level.SEVERE, "[" + getDescription().getName() + "]: Error starting server!", ex);
}
}
@Override
public void onDisable()
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);
}
private void stopServer()
{
is_running = false;
log.log(Level.INFO, "[" + getDescription().getName() + "]: Stopping server.");
try
{
Thread.sleep(250);
@ -125,7 +181,7 @@ public class MCTelnet extends JavaPlugin
catch (Throwable ex)
{
}
if (clientHolder != null)
{
try
@ -159,48 +215,13 @@ public class MCTelnet extends JavaPlugin
{
}
}
try
{
Thread.sleep(250);
}
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 MCTelnet plugin;
private String client_ip;
private boolean show_full_log = true;
private static final String COMMAND_REGEX = "[^\\x20-\\x7E]";
private static final String LOGIN_REGEX = "[^a-zA-Z0-9\\-\\.\\_]";
public TelnetListener(Socket socket, MCTelnet plugin)
{
this.is_running = true;
@ -44,10 +45,10 @@ public class TelnetListener extends Handler implements CommandSender
{
this.client_ip = clientSocket.getInetAddress().getHostAddress();
}
startListener();
}
private void startListener()
{
listenThread = new Thread(new Runnable()
@ -59,7 +60,7 @@ public class TelnetListener extends Handler implements CommandSender
});
listenThread.start();
}
private void init()
{
try
@ -72,23 +73,23 @@ public class TelnetListener extends Handler implements CommandSender
is_running = false;
return;
}
//sendTelnetCommand(WILL, LINEMODE);
//sendTelnetCommand(DO, LINEMODE);
//sendTelnetCommand(WONT, ECHO);
//sendTelnetCommand(DO, ECHO);
writeOut("[MCTelnet] - Session Started!\r\n");
authenticateLoop();
commandLoop();
shutdown();
}
private void authenticateLoop()
{
int tries = 0;
while (is_running && clientSocket.isConnected() && !is_authenticated)
{
try
@ -96,26 +97,34 @@ public class TelnetListener extends Handler implements CommandSender
//Get Username:
writeOut("Username: ");
String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
//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))
if (TelnetUtil.canBypassPassword(client_ip, plugin))
{
telnet_username = username;
writeOut("Skipping password, you are on an authorized IP address.\r\n");
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)
{
telnet_username = username;
writeOut("Logged In as " + getName() + ".\r\n:");
return;
}
@ -130,7 +139,7 @@ public class TelnetListener extends Handler implements CommandSender
}
writeOut("Invalid Username or Password.\r\n\r\n");
}
if (++tries >= 3)
{
writeOut("Too many failed login attempts.\r\n");
@ -145,16 +154,16 @@ public class TelnetListener extends Handler implements CommandSender
}
}
}
private void commandLoop()
{
if (!is_running || !is_authenticated)
{
return;
}
Logger.getLogger("Minecraft").addHandler(this);
while (is_running && clientSocket.isConnected() && is_authenticated)
{
String command = null;
@ -165,14 +174,32 @@ public class TelnetListener extends Handler implements CommandSender
catch (IOException ex)
{
}
if (command != null)
{
command = command.replaceAll(COMMAND_REGEX, "").trim();
if (!command.isEmpty())
{
command = command.replaceAll(COMMAND_REGEX, "").trim();
plugin.getServer().dispatchCommand(this, command);
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + getName() + " issued command: " + command);
if (command.toLowerCase().startsWith("telnet"))
{
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
{
@ -181,7 +208,7 @@ public class TelnetListener extends Handler implements CommandSender
}
}
}
private void shutdown()
{
if (already_stopped)
@ -189,12 +216,12 @@ public class TelnetListener extends Handler implements CommandSender
return;
}
already_stopped = true;
is_running = false;
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: Closing connection: " + client_ip);
Logger.getLogger("Minecraft").removeHandler(this);
if (!clientSocket.isClosed())
{
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 WONT = 252; //Sender doesn't want 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()
{
return is_running;
}
public void killClient()
{
shutdown();
}
@Override
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
public void flush()
{
@ -270,23 +302,23 @@ public class TelnetListener extends Handler implements CommandSender
}
}
}
@Override
public void close() throws SecurityException
{
shutdown();
}
public void sendMessage(String string)
{
writeOut(ChatColor.stripColor(string) + "\r\n:");
}
public Server getServer()
{
return plugin.getServer();
}
public String getName()
{
if (telnet_username != null)
@ -298,67 +330,67 @@ public class TelnetListener extends Handler implements CommandSender
return plugin.getDescription().getName();
}
}
public boolean isPermissionSet(String string)
{
return true;
}
public boolean isPermissionSet(Permission prmsn)
{
return true;
}
public boolean hasPermission(String string)
{
return true;
}
public boolean hasPermission(Permission prmsn)
{
return true;
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin, String string, boolean bln, int i)
{
return null;
}
public PermissionAttachment addAttachment(Plugin plugin, int i)
{
return null;
}
public void removeAttachment(PermissionAttachment pa)
{
return;
}
public void recalculatePermissions()
{
return;
}
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return null;
}
public boolean isOp()
{
return true;
}
public void setOp(boolean bln)
{
return;

View file

@ -4,9 +4,6 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -19,13 +16,47 @@ public class 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())
{
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;
try
{
@ -33,14 +64,14 @@ public class TelnetUtil
ZipEntry copy = file.getEntry(name);
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;
}
input = file.getInputStream(copy);
}
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)
{
@ -48,7 +79,7 @@ public class TelnetUtil
try
{
tfm.getDataFolder().mkdirs();
plugin.getDataFolder().mkdirs();
output = new FileOutputStream(actual);
byte[] buf = new byte[8192];
int length = 0;
@ -57,11 +88,11 @@ public class TelnetUtil
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)
{
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
{

View file

@ -6,3 +6,7 @@ address:
# Main connection password, must be defined:
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
version: 1.3
description: Telnet console access plugin.
author: bekvon
author: bekvon