1
0
mirror of https://e.coding.net/circlecloud/CTZServer.git synced 2024-11-24 11:58:46 +00:00

add CTZAuth...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-08 15:10:47 +08:00
parent 51f5b8431d
commit 652a9d6eb2
3 changed files with 302 additions and 272 deletions

View File

@ -0,0 +1,24 @@
package cn.citycraft.CTZServer;
public class CTZAuth {
public static void changePassword(String username, String password) {
// TODO 处理玩家密码修改事件
}
public static boolean checkPassword(String username, String password) {
// TODO 检查玩家密码
return false;
}
public static boolean isRegistered(String username) {
// TODO 检查玩家是否注册
return false;
}
public static boolean registerPlayer(String username, String password) {
// TODO 注册玩家
return false;
}
}

View File

@ -1,96 +1,104 @@
package cn.citycraft.CTZServer; package cn.citycraft.CTZServer;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.commands.HandlerCommand; import cn.citycraft.CTZServer.commands.HandlerCommand;
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket; import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
import cn.citycraft.sql.MySQLHelper; import cn.citycraft.sql.MySQLHelper;
import cn.citycraft.sql.SQLHelper; import cn.citycraft.sql.SQLHelper;
import cn.citycraft.utils.FileUtil; import cn.citycraft.utils.FileUtil;
public class ServerThread implements Runnable { public class ServerThread implements Runnable {
protected CTZLoginServerSocket server; protected CTZLoginServerSocket server;
protected SQLHelper sql; protected SQLHelper sql;
HandlerCommand handlercmd; HandlerCommand handlercmd;
public CTZLoginServerSocket getServer() { public CTZLoginServerSocket getServer() {
return server; return server;
} }
@Override @Override
public void run() { public void run() {
CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动..."); CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动...");
initCommand(); initCommand();
initDatabase(); initDatabase();
initServerInfo(); initServerInfo();
server = new CTZLoginServerSocket(); server = new CTZLoginServerSocket();
server.start(); server.start();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) { while (true) {
String cmdline; String cmdline;
try { try {
cmdline = br.readLine(); cmdline = br.readLine();
if (cmdline == null || cmdline.isEmpty()) if (cmdline == null || cmdline.isEmpty())
continue; continue;
String[] cmdargs = cmdline.split(" "); String[] cmdargs = cmdline.split(" ");
String cmd = cmdargs[0]; String cmd = cmdargs[0];
handlercmd.execute(cmd, cmdargs); handlercmd.execute(cmd, cmdargs);
} catch (IOException e) { } catch (IOException e) {
} }
} }
} }
/** /**
* 初始化命令 * 初始化命令
*/ */
void initCommand() { void initCommand() {
CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令..."); CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令...");
handlercmd = new HandlerCommand(this); handlercmd = new HandlerCommand(this);
} }
/** /**
* 初始化数据库 * 初始化数据库
*/ */
void initDatabase() { void initDatabase() {
String dbtable = "ctzserver"; String dbtable = "ctzserver";
CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接..."); CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接...");
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", ""); sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "");
if (!sql.dbConnection()) { if (!sql.dbConnection()) {
CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败..."); CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败...");
return; return;
} }
CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在..."); CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在...");
if (!sql.isTableExists(dbtable)) { if (!sql.isTableExists(dbtable)) {
CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "..."); CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "...");
HashMap<String, String> fields = new HashMap<String, String>(); HashMap<String, String> fields = new HashMap<String, String>();
fields.put("player", "VARCHAR(16) NOT NULL"); fields.put("player", "VARCHAR(16) NOT NULL");
fields.put("prefix", "VARCHAR(600) NOT NULL"); fields.put("password", "VARCHAR(50) NOT NULL");
String Conditions = "UNIQUE (`player`)"; fields.put("ip", "varchar(40) NOT NULL");
if (!sql.createTables(dbtable, fields, Conditions)) fields.put("lastloginout", "BIGINT(20) NOT NULL");
CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器..."); fields.put("x", "DOUBLE NOT NULL");
} fields.put("y", "DOUBLE NOT NULL");
} fields.put("z", "DOUBLE NOT NULL");
fields.put("email", "VARCHAR(50)");
/** fields.put("world", "SMALLINT(6)");
* 初始化服务器信息 fields.put("islogged", "SMALLINT(6) NOT NULL");
*/ String Conditions = "UNIQUE (`player`)";
void initServerInfo() { if (!sql.createTables(dbtable, fields, Conditions))
CTZServer.getLogger().info(ChatColor.GREEN + "加载服务器信息..."); CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
FileUtil.saveResource(this, "/servers.json", false); }
String json = FileUtil.readFile(new File("servers.json")); }
if (CTZServer.Init(json))
CTZServer.getLogger().info(ChatColor.GREEN + "服务器信息加载成功..."); /**
else * 初始化服务器信息
CTZServer.getLogger().warning(ChatColor.RED + "服务器信息加载失败..."); */
} void initServerInfo() {
} CTZServer.getLogger().info(ChatColor.GREEN + "加载服务器信息...");
FileUtil.saveResource(this, "/servers.json", false);
String json = FileUtil.readFile(new File("servers.json"));
if (CTZServer.Init(json))
CTZServer.getLogger().info(ChatColor.GREEN + "服务器信息加载成功...");
else
CTZServer.getLogger().warning(ChatColor.RED + "服务器信息加载失败...");
}
}

View File

@ -1,177 +1,175 @@
package cn.citycraft.CTZServer.socket; package cn.citycraft.CTZServer.socket;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import cn.citycraft.CTZServer.CTZServer; import cn.citycraft.CTZServer.CTZAuth;
import cn.citycraft.CTZServer.socket.Response.HttpStates; import cn.citycraft.CTZServer.CTZServer;
import cn.citycraft.CTZServer.socket.Response.HttpStates;
public class CTZLoginServerSocket extends Thread {
ServerSocket s = null; public class CTZLoginServerSocket extends Thread {
Socket socket = null; ServerSocket s = null;
int port = 25580; Socket socket = null;
int port = 25580;
/**
* 初始化服务端口 /**
*/ * 初始化服务端口
public CTZLoginServerSocket() { */
} public CTZLoginServerSocket() {
}
/**
* 初始化服务端口 /**
*/ * 初始化服务端口
public CTZLoginServerSocket(int port) { */
this.port = port; public CTZLoginServerSocket(int port) {
} this.port = port;
}
@Override
public void run() { @Override
try { public void run() {
// 设定服务端的端口号 try {
s = new ServerSocket(port); // 设定服务端的端口号
CTZServer.getLogger().info(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort()); s = new ServerSocket(port);
// 等待请求,此方法会一直阻塞,直到获得请求才往下走 CTZServer.getLogger().info(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
while (true) { // 等待请求,此方法会一直阻塞,直到获得请求才往下走
socket = s.accept(); while (true) {
new ClientThread(socket).start(); socket = s.accept();
} new ClientThread(socket).start();
} catch (Exception e) { }
CTZServer.getLogger().warning(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage()); } catch (Exception e) {
} finally { CTZServer.getLogger().warning(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
ShutDown(); } finally {
} ShutDown();
} }
}
/**
* 关闭服务器 /**
*/ * 关闭服务器
public void ShutDown() { */
try { public void ShutDown() {
socket.close(); try {
s.close(); socket.close();
} catch (Exception e) { s.close();
} } catch (Exception e) {
CTZServer.getLogger().info(ChatColor.RED + "CTZ服务器已关闭..."); }
System.exit(0); CTZServer.getLogger().info(ChatColor.RED + "CTZ服务器已关闭...");
} System.exit(0);
}
class ClientThread extends Thread {
Socket client = null; class ClientThread extends Thread {
BufferedReader br = null; Socket client = null;
PrintWriter pw = null; BufferedReader br = null;
String ip = null; PrintWriter pw = null;
String ip = null;
public ClientThread(Socket client) {
this.client = client; public ClientThread(Socket client) {
} this.client = client;
}
@Override
public void run() { @Override
try { public void run() {
// 用于接收客户端发来的请求 try {
br = new BufferedReader(new InputStreamReader(client.getInputStream())); // 用于接收客户端发来的请求
// 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法 br = new BufferedReader(new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true); // 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
ip = client.getInetAddress().getHostAddress(); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
while (true) { ip = client.getInetAddress().getHostAddress();
String str = br.readLine(); while (true) {
if (str != null) { String str = br.readLine();
Request req = new Request(str); if (str != null) {
Response res = new Response(); Request req = new Request(str);
String username = ""; Response res = new Response();
String password = ""; String username = "";
if (req.isSuccess) { String password = "";
switch (req.getPrefix().substring(1)) { if (req.isSuccess) {
case "isregistered": switch (req.getPrefix().substring(1)) {
username = req.Query("username"); case "isregistered":
if (username == null) { username = req.Query("username");
res.setState(HttpStates.Bad_Request); if (username == null) {
break; res.setState(HttpStates.Bad_Request);
} break;
// if (API.isRegistered(username)) }
// res.setHtml("true"); if (CTZAuth.isRegistered(username))
// else res.setHtml("true");
// res.setHtml("false"); else
break; res.setHtml("false");
case "register": break;
username = req.Query("username"); case "register":
password = req.Query("password"); username = req.Query("username");
if (username == null || password == null) { password = req.Query("password");
res.setState(HttpStates.Bad_Request); if (username == null || password == null) {
break; res.setState(HttpStates.Bad_Request);
} break;
// if (API.registerPlayer(username, password)) { }
// res.setHtml("true"); if (CTZAuth.registerPlayer(username, password)) {
// Bukkit.getConsoleSender().sendMessage("§6玩家: §a" + username + " §d注册成功 IP: " + ip); res.setHtml("true");
// } else CTZServer.getLogger().info("§6玩家: §a" + username + " §d注册成功 IP: " + ip);
// res.setHtml("false"); } else
break; res.setHtml("false");
case "login": break;
username = req.Query("username"); case "login":
password = req.Query("password"); username = req.Query("username");
if (username == null || password == null) { password = req.Query("password");
res.setState(HttpStates.Bad_Request); if (username == null || password == null) {
break; res.setState(HttpStates.Bad_Request);
} break;
// if (API.checkPassword(username, password)) { }
// res.setHtml("true"); if (CTZAuth.checkPassword(username, password)) {
// if (CTZLoginQueue.isCheckIP()) res.setHtml("true");
// CTZLoginQueue.add(username, ip); // TODO 是否检查登录IP
// else CTZServer.getLogger().info("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
// CTZLoginQueue.add(username); } else
// Bukkit.getConsoleSender().sendMessage("§6玩家: §a" + username + " §3登录成功 IP: " + ip); res.setHtml("false");
// } else break;
// res.setHtml("false"); case "islogin":
break; username = req.Query("username");
case "islogin": if (username == null) {
username = req.Query("username"); res.setState(HttpStates.Bad_Request);
if (username == null) { break;
res.setState(HttpStates.Bad_Request); }
break; // if (Bukkit.getPlayer(username) != null)
} // res.setHtml("true");
// if (Bukkit.getPlayer(username) != null) // else
// res.setHtml("true"); // res.setHtml("false");
// else // break;
// res.setHtml("false"); case "changepassword":
// break; username = req.Query("username");
case "changepassword": password = req.Query("password");
username = req.Query("username"); if (username == null || password == null) {
password = req.Query("password"); res.setState(HttpStates.Bad_Request);
if (username == null || password == null) { break;
res.setState(HttpStates.Bad_Request); }
break; if (CTZAuth.checkPassword(username, password)) {
} res.setHtml("true");
// if (API.checkPassword(username, password)) CTZAuth.changePassword(username, password);
// res.setHtml("true"); } else
// // TODO 处理玩家密码修改事件 res.setHtml("false");
// else break;
// res.setHtml("false"); case "serverlist":
break; res.setState(HttpStates.OK);
case "serverlist": res.setHtml(CTZServer.getJson());
res.setState(HttpStates.OK); break;
res.setHtml(CTZServer.getJson()); default:
break; res.setState(HttpStates.Bad_Request);
default: }
res.setState(HttpStates.Bad_Request); pw.write(res.toString());
} pw.flush();
pw.write(res.toString()); }
pw.flush(); break;
} }
break; }
} br.close();
} pw.close();
br.close(); client.close();
pw.close(); } catch (Exception e) {
client.close(); }
} catch (Exception e) { }
} }
}
}
} }