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
{
@ -89,7 +114,7 @@ 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>();
@ -107,16 +132,47 @@ 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;
}
}
log.log(Level.INFO, "[" + getDescription().getName() + "]: Stopping server.");
this.setEnabled(false);
}
private void stopServer()
{
is_running = false;
try
{
@ -166,41 +222,6 @@ public class MCTelnet extends JavaPlugin
}
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 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\\-\\.\\_]";
@ -97,6 +98,13 @@ public class TelnetListener extends Handler implements CommandSender
writeOut("Username: ");
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(DONT, ECHO);
@ -110,12 +118,13 @@ public class TelnetListener extends Handler implements CommandSender
if (password.equals(plugin.password))
{
telnet_username = username;
is_authenticated = true;
}
}
if (is_authenticated)
{
telnet_username = username;
writeOut("Logged In as " + getName() + ".\r\n:");
return;
}
@ -168,11 +177,29 @@ public class TelnetListener extends Handler implements CommandSender
if (command != null)
{
command = command.replaceAll(COMMAND_REGEX, "").trim();
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);
log.log(Level.INFO, "[" + plugin.getDescription().getName() + "]: " + getName() + " issued command: " + command);
}
}
else
{
@ -253,7 +280,12 @@ public class TelnetListener extends Handler implements CommandSender
@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

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;
@ -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())
{
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