mirror of
https://e.coding.net/circlecloud/CTZServer.git
synced 2024-11-21 11:28:47 +00:00
add CTZAuth...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
parent
51f5b8431d
commit
652a9d6eb2
24
src/main/java/cn/citycraft/CTZServer/CTZAuth.java
Normal file
24
src/main/java/cn/citycraft/CTZServer/CTZAuth.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -1,96 +1,104 @@
|
||||
package cn.citycraft.CTZServer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import cn.citycraft.CTZServer.commands.HandlerCommand;
|
||||
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
|
||||
import cn.citycraft.sql.MySQLHelper;
|
||||
import cn.citycraft.sql.SQLHelper;
|
||||
import cn.citycraft.utils.FileUtil;
|
||||
|
||||
public class ServerThread implements Runnable {
|
||||
protected CTZLoginServerSocket server;
|
||||
protected SQLHelper sql;
|
||||
|
||||
HandlerCommand handlercmd;
|
||||
|
||||
public CTZLoginServerSocket getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动...");
|
||||
|
||||
initCommand();
|
||||
initDatabase();
|
||||
initServerInfo();
|
||||
|
||||
server = new CTZLoginServerSocket();
|
||||
server.start();
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String cmdline;
|
||||
try {
|
||||
cmdline = br.readLine();
|
||||
if (cmdline == null || cmdline.isEmpty())
|
||||
continue;
|
||||
String[] cmdargs = cmdline.split(" ");
|
||||
String cmd = cmdargs[0];
|
||||
handlercmd.execute(cmd, cmdargs);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化命令
|
||||
*/
|
||||
void initCommand() {
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令...");
|
||||
handlercmd = new HandlerCommand(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化数据库
|
||||
*/
|
||||
void initDatabase() {
|
||||
String dbtable = "ctzserver";
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接...");
|
||||
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "");
|
||||
if (!sql.dbConnection()) {
|
||||
CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败...");
|
||||
return;
|
||||
}
|
||||
CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在...");
|
||||
if (!sql.isTableExists(dbtable)) {
|
||||
CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "...");
|
||||
HashMap<String, String> fields = new HashMap<String, String>();
|
||||
fields.put("player", "VARCHAR(16) NOT NULL");
|
||||
fields.put("prefix", "VARCHAR(600) NOT NULL");
|
||||
String Conditions = "UNIQUE (`player`)";
|
||||
if (!sql.createTables(dbtable, fields, Conditions))
|
||||
CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务器信息
|
||||
*/
|
||||
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 + "服务器信息加载失败...");
|
||||
}
|
||||
}
|
||||
package cn.citycraft.CTZServer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import cn.citycraft.CTZServer.commands.HandlerCommand;
|
||||
import cn.citycraft.CTZServer.socket.CTZLoginServerSocket;
|
||||
import cn.citycraft.sql.MySQLHelper;
|
||||
import cn.citycraft.sql.SQLHelper;
|
||||
import cn.citycraft.utils.FileUtil;
|
||||
|
||||
public class ServerThread implements Runnable {
|
||||
protected CTZLoginServerSocket server;
|
||||
protected SQLHelper sql;
|
||||
|
||||
HandlerCommand handlercmd;
|
||||
|
||||
public CTZLoginServerSocket getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "服务器开始启动...");
|
||||
|
||||
initCommand();
|
||||
initDatabase();
|
||||
initServerInfo();
|
||||
|
||||
server = new CTZLoginServerSocket();
|
||||
server.start();
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String cmdline;
|
||||
try {
|
||||
cmdline = br.readLine();
|
||||
if (cmdline == null || cmdline.isEmpty())
|
||||
continue;
|
||||
String[] cmdargs = cmdline.split(" ");
|
||||
String cmd = cmdargs[0];
|
||||
handlercmd.execute(cmd, cmdargs);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化命令
|
||||
*/
|
||||
void initCommand() {
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "初始化基础命令...");
|
||||
handlercmd = new HandlerCommand(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化数据库
|
||||
*/
|
||||
void initDatabase() {
|
||||
String dbtable = "ctzserver";
|
||||
CTZServer.getLogger().info(ChatColor.GREEN + "初始化数据库连接...");
|
||||
sql = new MySQLHelper("127.0.0.1", 3306, "minecraft", "root", "");
|
||||
if (!sql.dbConnection()) {
|
||||
CTZServer.getLogger().warning(ChatColor.RED + "数据库连接失败...");
|
||||
return;
|
||||
}
|
||||
CTZServer.getLogger().info("数据库连接成功,检查数据表是否存在...");
|
||||
if (!sql.isTableExists(dbtable)) {
|
||||
CTZServer.getLogger().info("数据表不存在,新建表" + dbtable + "...");
|
||||
HashMap<String, String> fields = new HashMap<String, String>();
|
||||
fields.put("player", "VARCHAR(16) NOT NULL");
|
||||
fields.put("password", "VARCHAR(50) NOT NULL");
|
||||
fields.put("ip", "varchar(40) NOT NULL");
|
||||
fields.put("lastloginout", "BIGINT(20) NOT NULL");
|
||||
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`)";
|
||||
if (!sql.createTables(dbtable, fields, Conditions))
|
||||
CTZServer.getLogger().warning("数据表" + dbtable + "创建失败,请尝试手动创建并重启服务器...");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务器信息
|
||||
*/
|
||||
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 + "服务器信息加载失败...");
|
||||
}
|
||||
}
|
||||
|
@ -1,177 +1,175 @@
|
||||
package cn.citycraft.CTZServer.socket;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import cn.citycraft.CTZServer.CTZServer;
|
||||
import cn.citycraft.CTZServer.socket.Response.HttpStates;
|
||||
|
||||
public class CTZLoginServerSocket extends Thread {
|
||||
ServerSocket s = null;
|
||||
Socket socket = null;
|
||||
int port = 25580;
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 设定服务端的端口号
|
||||
s = new ServerSocket(port);
|
||||
CTZServer.getLogger().info(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
|
||||
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
||||
while (true) {
|
||||
socket = s.accept();
|
||||
new ClientThread(socket).start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CTZServer.getLogger().warning(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
|
||||
} finally {
|
||||
ShutDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭服务器
|
||||
*/
|
||||
public void ShutDown() {
|
||||
try {
|
||||
socket.close();
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
CTZServer.getLogger().info(ChatColor.RED + "CTZ服务器已关闭...");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
class ClientThread extends Thread {
|
||||
Socket client = null;
|
||||
BufferedReader br = null;
|
||||
PrintWriter pw = null;
|
||||
String ip = null;
|
||||
|
||||
public ClientThread(Socket client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 用于接收客户端发来的请求
|
||||
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
// 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
|
||||
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
|
||||
ip = client.getInetAddress().getHostAddress();
|
||||
while (true) {
|
||||
String str = br.readLine();
|
||||
if (str != null) {
|
||||
Request req = new Request(str);
|
||||
Response res = new Response();
|
||||
String username = "";
|
||||
String password = "";
|
||||
if (req.isSuccess) {
|
||||
switch (req.getPrefix().substring(1)) {
|
||||
case "isregistered":
|
||||
username = req.Query("username");
|
||||
if (username == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (API.isRegistered(username))
|
||||
// res.setHtml("true");
|
||||
// else
|
||||
// res.setHtml("false");
|
||||
break;
|
||||
case "register":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (API.registerPlayer(username, password)) {
|
||||
// res.setHtml("true");
|
||||
// Bukkit.getConsoleSender().sendMessage("§6玩家: §a" + username + " §d注册成功 IP: " + ip);
|
||||
// } else
|
||||
// res.setHtml("false");
|
||||
break;
|
||||
case "login":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (API.checkPassword(username, password)) {
|
||||
// res.setHtml("true");
|
||||
// if (CTZLoginQueue.isCheckIP())
|
||||
// CTZLoginQueue.add(username, ip);
|
||||
// else
|
||||
// CTZLoginQueue.add(username);
|
||||
// Bukkit.getConsoleSender().sendMessage("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
|
||||
// } else
|
||||
// res.setHtml("false");
|
||||
break;
|
||||
case "islogin":
|
||||
username = req.Query("username");
|
||||
if (username == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (Bukkit.getPlayer(username) != null)
|
||||
// res.setHtml("true");
|
||||
// else
|
||||
// res.setHtml("false");
|
||||
// break;
|
||||
case "changepassword":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (API.checkPassword(username, password))
|
||||
// res.setHtml("true");
|
||||
// // TODO 处理玩家密码修改事件
|
||||
// else
|
||||
// res.setHtml("false");
|
||||
break;
|
||||
case "serverlist":
|
||||
res.setState(HttpStates.OK);
|
||||
res.setHtml(CTZServer.getJson());
|
||||
break;
|
||||
default:
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
}
|
||||
pw.write(res.toString());
|
||||
pw.flush();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
pw.close();
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
package cn.citycraft.CTZServer.socket;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import cn.citycraft.CTZServer.CTZAuth;
|
||||
import cn.citycraft.CTZServer.CTZServer;
|
||||
import cn.citycraft.CTZServer.socket.Response.HttpStates;
|
||||
|
||||
public class CTZLoginServerSocket extends Thread {
|
||||
ServerSocket s = null;
|
||||
Socket socket = null;
|
||||
int port = 25580;
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 设定服务端的端口号
|
||||
s = new ServerSocket(port);
|
||||
CTZServer.getLogger().info(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
|
||||
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
||||
while (true) {
|
||||
socket = s.accept();
|
||||
new ClientThread(socket).start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CTZServer.getLogger().warning(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
|
||||
} finally {
|
||||
ShutDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭服务器
|
||||
*/
|
||||
public void ShutDown() {
|
||||
try {
|
||||
socket.close();
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
CTZServer.getLogger().info(ChatColor.RED + "CTZ服务器已关闭...");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
class ClientThread extends Thread {
|
||||
Socket client = null;
|
||||
BufferedReader br = null;
|
||||
PrintWriter pw = null;
|
||||
String ip = null;
|
||||
|
||||
public ClientThread(Socket client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 用于接收客户端发来的请求
|
||||
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||
// 用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
|
||||
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
|
||||
ip = client.getInetAddress().getHostAddress();
|
||||
while (true) {
|
||||
String str = br.readLine();
|
||||
if (str != null) {
|
||||
Request req = new Request(str);
|
||||
Response res = new Response();
|
||||
String username = "";
|
||||
String password = "";
|
||||
if (req.isSuccess) {
|
||||
switch (req.getPrefix().substring(1)) {
|
||||
case "isregistered":
|
||||
username = req.Query("username");
|
||||
if (username == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
if (CTZAuth.isRegistered(username))
|
||||
res.setHtml("true");
|
||||
else
|
||||
res.setHtml("false");
|
||||
break;
|
||||
case "register":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
if (CTZAuth.registerPlayer(username, password)) {
|
||||
res.setHtml("true");
|
||||
CTZServer.getLogger().info("§6玩家: §a" + username + " §d注册成功 IP: " + ip);
|
||||
} else
|
||||
res.setHtml("false");
|
||||
break;
|
||||
case "login":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
if (CTZAuth.checkPassword(username, password)) {
|
||||
res.setHtml("true");
|
||||
// TODO 是否检查登录IP
|
||||
CTZServer.getLogger().info("§6玩家: §a" + username + " §3登录成功 IP: " + ip);
|
||||
} else
|
||||
res.setHtml("false");
|
||||
break;
|
||||
case "islogin":
|
||||
username = req.Query("username");
|
||||
if (username == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (Bukkit.getPlayer(username) != null)
|
||||
// res.setHtml("true");
|
||||
// else
|
||||
// res.setHtml("false");
|
||||
// break;
|
||||
case "changepassword":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
if (username == null || password == null) {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
if (CTZAuth.checkPassword(username, password)) {
|
||||
res.setHtml("true");
|
||||
CTZAuth.changePassword(username, password);
|
||||
} else
|
||||
res.setHtml("false");
|
||||
break;
|
||||
case "serverlist":
|
||||
res.setState(HttpStates.OK);
|
||||
res.setHtml(CTZServer.getJson());
|
||||
break;
|
||||
default:
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
}
|
||||
pw.write(res.toString());
|
||||
pw.flush();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
pw.close();
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user