1
0
mirror of https://e.coding.net/circlecloud/CTZLoginServer.git synced 2024-12-26 07:48:47 +00:00

update...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-07 20:49:43 +08:00
parent a42ef6a78b
commit 15f8c08279
2 changed files with 116 additions and 122 deletions

View File

@ -38,7 +38,7 @@ class Area {
} }
} }
class CTZServer { class ServerInfo {
/** /**
* 服务器分区信息 * 服务器分区信息
*/ */
@ -60,7 +60,7 @@ class CTZServer {
*/ */
public class CTZServers { public class CTZServers {
static CTZServer sl = new CTZServer(); static ServerInfo sl = new ServerInfo();
/** /**
* 获得json字符串 * 获得json字符串

View File

@ -17,128 +17,9 @@ import cn.citycraft.CTZLoginServer.Socket.Response.HttpStates;
import fr.xephi.authme.api.API; import fr.xephi.authme.api.API;
public class CTZLoginServerSocket implements Runnable { public class CTZLoginServerSocket implements Runnable {
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(CTZServers.getJson());
break;
default:
res.setState(HttpStates.Bad_Request);
}
pw.write(res.toString());
pw.flush();
}
break;
}
}
br.close();
pw.close();
client.close();
} catch (Exception e) {
}
}
}
ServerSocket s = null; ServerSocket s = null;
Socket socket = null;
Socket socket = null;
public int port = 25580; public int port = 25580;
public CTZLoginServer plugin; public CTZLoginServer plugin;
@ -182,4 +63,117 @@ public class CTZLoginServerSocket implements Runnable {
System.out.println("CTZL服务器已关闭..."); System.out.println("CTZL服务器已关闭...");
} }
} }
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(CTZServers.getJson());
break;
default:
res.setState(HttpStates.Bad_Request);
}
pw.write(res.toString());
pw.flush();
}
break;
}
}
br.close();
pw.close();
client.close();
} catch (Exception e) {
}
}
}
} }