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
{ {
@ -89,7 +114,7 @@ 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>();
@ -107,16 +132,47 @@ 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; is_running = false;
}
}
log.log(Level.INFO, "[" + getDescription().getName() + "]: Stopping server."); this.setEnabled(false);
}
private void stopServer()
{
is_running = false;
try try
{ {
@ -166,41 +222,6 @@ public class MCTelnet extends JavaPlugin
} }
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,6 +32,7 @@ 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\\-\\.\\_]";
@ -97,6 +98,13 @@ public class TelnetListener extends Handler implements CommandSender
writeOut("Username: "); writeOut("Username: ");
String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim(); String username = instream.readLine().replaceAll(LOGIN_REGEX, "").trim();
if (TelnetUtil.canBypassPassword(client_ip, plugin))
{
writeOut("Skipping password, you are on an authorized IP address.\r\n");
is_authenticated = true;
}
else
{
//sendTelnetCommand(WILL, ECHO); //sendTelnetCommand(WILL, ECHO);
//sendTelnetCommand(DONT, ECHO); //sendTelnetCommand(DONT, ECHO);
@ -110,12 +118,13 @@ public class TelnetListener extends Handler implements CommandSender
if (password.equals(plugin.password)) if (password.equals(plugin.password))
{ {
telnet_username = username;
is_authenticated = true; 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;
} }
@ -168,11 +177,29 @@ public class TelnetListener extends Handler implements CommandSender
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"))
{
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); plugin.getServer().dispatchCommand(this, command);
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + getName() + " issued command: " + command); }
} }
else else
{ {
@ -253,7 +280,12 @@ public class TelnetListener extends Handler implements CommandSender
@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

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;
@ -20,12 +17,46 @@ public class 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