1
0
mirror of https://e.coding.net/circlecloud/CTZLoginServer.git synced 2024-12-11 05:18:48 +00:00

Updata Compelate SendServerList...

This commit is contained in:
j502647092 2015-08-10 00:10:35 +08:00
parent a1068c62b2
commit 5106fa61f7
10 changed files with 282 additions and 228 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.settings
# netbeans
/nbproject

View File

@ -1,28 +1,39 @@
/**
*
*
*/
package cn.citycraft.CTZLoginServer;
import java.util.HashMap;
/**
* @author 蒋天蓓
* 2015年7月28日下午9:08:23
* TODO
* @author 蒋天蓓 2015年7月28日下午9:08:23 TODO
*/
public class CTZLoginQueue {
protected static boolean checkIP = true;;
protected static HashMap<String, String> list = new HashMap<String, String>();
public static void add(String player) {
list.put(player.toLowerCase(), null);
}
public static void add(String player, String IP) {
list.put(player.toLowerCase(), IP);
}
public static boolean canLogin(String player) {
return list.containsKey(player.toLowerCase());
}
public static boolean canLogin(String player, String IP) {
return list.containsKey(player.toLowerCase())
&& list.get(player.toLowerCase()).equalsIgnoreCase(IP);
}
public static boolean isCheckIP() {
return checkIP;
}
public static void setCheckIP(boolean checkIP) {
CTZLoginQueue.checkIP = checkIP;
}
protected static HashMap<String, String> list = new HashMap<String, String>();
public static boolean Login(String player) {
return list.remove(player.toLowerCase()) != null;
}
@ -36,11 +47,7 @@ public class CTZLoginQueue {
return false;
}
public static void add(String player) {
list.put(player.toLowerCase(), null);
}
public static void add(String player, String IP) {
list.put(player.toLowerCase(), IP);
public static void setCheckIP(boolean checkIP) {
CTZLoginQueue.checkIP = checkIP;
}
}

View File

@ -5,7 +5,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.CTZLoginServer.Socket.CTZLoginServerSocket;
import cn.citycraft.CTZLoginServer.config.Config;
@ -13,11 +12,16 @@ import cn.citycraft.CTZLoginServer.listen.PlayerListen;
public class CTZLoginServer extends JavaPlugin {
public String version;
public BukkitTask loginserver;
public CTZLoginServerSocket serversocket;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
@Override
public void onDisable() {
loginserver.cancel();
serversocket.ShutDown();
getLogger().info("CTZLoginServer已卸载!");
}
@ -38,30 +42,20 @@ public class CTZLoginServer extends JavaPlugin {
getLogger().info("CTZLoginServer已加载!");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
public void onLoadLoginServer() {
if (loginserver != null) {
loginserver.cancel();
getLogger().info("CTZL登录服务器已关闭!");
}
loginserver = this
.getServer()
.getScheduler()
.runTaskAsynchronously(
this,
new CTZLoginServerSocket(this, Config.getInstance().getInt("config.port",
25580)));
getLogger().info("CTZL登录服务器已开启!");
}
@Override
public void onLoad() {
Config.load(this, "1.0");
CTZServers.Init(Config.getInstance().getConfigurationSection("areas"));
}
public void onLoadLoginServer() {
if (serversocket != null) {
serversocket.ShutDown();
getLogger().info("CTZL登录服务器已关闭!");
}
serversocket = new CTZLoginServerSocket(this, Config.getInstance().getInt("config.port",
25580));
this.getServer().getScheduler().runTaskAsynchronously(this, serversocket);
getLogger().info("CTZL登录服务器已开启!");
}
}

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package cn.citycraft.CTZLoginServer;
@ -11,17 +11,57 @@ import org.bukkit.configuration.ConfigurationSection;
import com.google.gson.Gson;
class Area {
String name;
List<Server> servers = new ArrayList<Server>();
public String getName() {
return name;
}
public List<Server> getServers() {
return servers;
}
public void setName(String name) {
this.name = name;
}
public void setServers(List<Server> servers) {
this.servers = servers;
}
@Override
public String toString() {
return String.format("\"name\":\"{0}\",\"servers\":\"{1}\"", name, servers.toString());
}
}
class CTZServer {
List<Area> areas = new ArrayList<Area>();
public List<Area> getAreas() {
return areas;
}
public void setAreas(List<Area> aareas) {
areas = aareas;
}
}
/**
* @author 蒋天蓓
* 2015年8月8日下午2:50:02
* TODO
* @author 蒋天蓓 2015年8月8日下午2:50:02 TODO
*/
public class CTZServers {
CTZServers stz = new CTZServers();
static CTZServer sl = new CTZServer();
public static String getJson() {
Gson gson = new Gson();
return chinaToUnicode(gson.toJson(sl));
}
public static void Init(ConfigurationSection cs) {
Set<String> arealist = cs.getKeys(false);
for (String a : arealist) {
@ -42,47 +82,43 @@ public class CTZServers {
}
}
public static String getJson() {
Gson gson = new Gson();
return gson.toJson(sl);
}
}
CTZServers stz = new CTZServers();
class CTZServer {
List<Area> areas = new ArrayList<Area>();
public List<Area> getAreas() {
return areas;
/**
* 把中文转成Unicode码
*
* @param str
* @return
*/
public static String chinaToUnicode(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
result += "\\u" + Integer.toHexString(chr1);
} else {
result += str.charAt(i);
}
}
return result;
}
public void setAreas(List<Area> aareas) {
areas = aareas;
}
}
class Area {
String name;
List<Server> servers = new ArrayList<Server>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Server> getServers() {
return servers;
}
public void setServers(List<Server> servers) {
this.servers = servers;
}
@Override
public String toString() {
return String.format("\"name\":\"{0}\",\"servers\":\"{1}\"", name, servers.toString());
/**
* 判断是否为中文字符
*
* @param c
* @return
*/
public boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
return true;
return false;
}
}
@ -101,56 +137,56 @@ class Server {
public String url;
@Override
public String toString() {
return String.format("{0}:{1}", address, port);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
public int getPort() {
return port;
}
public String getUrl() {
return url;
}
public String getVersion() {
return version;
}
public void setAddress(String address) {
this.address = address;
}
public void setInfo(String info) {
this.info = info;
}
public void setName(String name) {
this.name = name;
}
public void setPort(int port) {
this.port = port;
}
public void setUrl(String url) {
this.url = url;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return String.format("{0}:{1}", address, port);
}
}

View File

@ -2,6 +2,7 @@ package cn.citycraft.CTZLoginServer.Socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
@ -17,43 +18,6 @@ import cn.citycraft.CTZLoginServer.Socket.Response.HttpStates;
import fr.xephi.authme.api.API;
public class CTZLoginServerSocket implements Runnable {
public int port = 25580;
public CTZLoginServer plugin;
/**
* 初始化服务端口
*/
public CTZLoginServerSocket(CTZLoginServer plugin, int port) {
this.plugin = plugin;
this.port = port;
}
@Override
public void run() {
ServerSocket s = null;
Socket socket = null;
try {
// 设定服务端的端口号
s = new ServerSocket(port);
System.out.println("CTZL服务器开始监听 端口:" + s.getLocalPort());
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
while (true) {
socket = s.accept();
new ClientThread(socket).start();
}
} catch (Exception e) {
System.out.println("CTZL服务器崩溃...");
} finally {
System.out.println("CTZL服务器已关闭...");
try {
socket.close();
s.close();
} catch (Exception e2) {
}
}
}
class ClientThread extends Thread {
Socket client = null;
BufferedReader br = null;
@ -90,8 +54,9 @@ public class CTZLoginServerSocket implements Runnable {
}
if (API.isRegistered(username)) {
res.setHtml("true");
} else
} else {
res.setHtml("false");
}
break;
case "register":
username = req.Query("username");
@ -104,8 +69,9 @@ public class CTZLoginServerSocket implements Runnable {
res.setHtml("true");
Bukkit.getConsoleSender().sendMessage(
"§6玩家: §a" + username + " §d注册成功 IP: " + ip);
} else
} else {
res.setHtml("false");
}
break;
case "login":
username = req.Query("username");
@ -116,14 +82,16 @@ public class CTZLoginServerSocket implements Runnable {
}
if (API.checkPassword(username, password)) {
res.setHtml("true");
if (CTZLoginQueue.isCheckIP())
if (CTZLoginQueue.isCheckIP()) {
CTZLoginQueue.add(username, ip);
else
} else {
CTZLoginQueue.add(username);
}
Bukkit.getConsoleSender().sendMessage(
"§6玩家: §a" + username + " §3登录成功 IP: " + ip);
} else
} else {
res.setHtml("false");
}
break;
case "islogin":
username = req.Query("username");
@ -131,10 +99,11 @@ public class CTZLoginServerSocket implements Runnable {
res.setState(HttpStates.Bad_Request);
break;
}
if (Bukkit.getPlayer(username) != null)
if (Bukkit.getPlayer(username) != null) {
res.setHtml("true");
else
} else {
res.setHtml("false");
}
break;
case "changepassword":
username = req.Query("username");
@ -143,16 +112,15 @@ public class CTZLoginServerSocket implements Runnable {
res.setState(HttpStates.Bad_Request);
break;
}
if (API.checkPassword(username, password))
if (API.checkPassword(username, password)) {
res.setHtml("true");
// TODO 处理玩家密码修改事件
else
// TODO 处理玩家密码修改事件
} else {
res.setHtml("false");
}
break;
case "serverlist":
res.setState(HttpStates.OK);
// {"areas":[{"name":"纯净大区","servers":[{"name":"光板小镇","address":"CityCraft.cn","port":25565,"version":"1.8","url":"http://CityCraft.cn:88/gbxz.zip","info":"纯净 有爱"}]}]}
res.setHeader("Content-Type", "text/html;charset=utf-8");
res.setHtml(CTZServers.getJson());
break;
default:
@ -171,4 +139,51 @@ public class CTZLoginServerSocket implements Runnable {
}
}
}
ServerSocket s = null;
Socket socket = null;
public int port = 25580;
public CTZLoginServer plugin;
/**
* 初始化服务端口
*/
public CTZLoginServerSocket(CTZLoginServer plugin, int port) {
this.plugin = plugin;
this.port = port;
}
@Override
public void run() {
try {
// 设定服务端的端口号
s = new ServerSocket(port);
System.out.println("CTZL服务器开始监听 端口:" + s.getLocalPort());
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
while (true) {
socket = s.accept();
new ClientThread(socket).start();
}
} catch (Exception e) {
System.out.println("CTZL服务器崩溃...");
} finally {
System.out.println("CTZL服务器已关闭...");
try {
socket.close();
s.close();
} catch (Exception e2) {
}
}
}
public void ShutDown() {
try {
socket.close();
s.close();
} catch (IOException e) {
System.out.println("CTZL服务器已关闭...");
}
}
}

View File

@ -1,40 +1,39 @@
/**
*
*
*/
package cn.citycraft.CTZLoginServer.Socket;
import java.util.HashMap;
/**
* @author 蒋天蓓
* 2015年7月28日下午4:58:26
* HTTP 头文件解析类
* 目前只能解析GET
* @author 蒋天蓓 2015年7月28日下午4:58:26 HTTP 头文件解析类 目前只能解析GET
*/
public class Request {
protected boolean isSuccess = false;
protected String prefix;
protected Method method;
protected String version;
protected HashMap<String, String> querystring = new HashMap<String, String>();
enum Method {
GET, POST
}
protected boolean isSuccess = false;
protected String prefix;
protected Method method;
protected String version;
protected HashMap<String, String> querystring = new HashMap<String, String>();
/**
* 解析字符串为Response
*
*
* @param response
*/
public Request(String response) {
try {
String[] args = response.split(" ");
// 解析协议
if (args[0].equalsIgnoreCase(Method.GET.toString()))
if (args[0].equalsIgnoreCase(Method.GET.toString())) {
method = Method.GET;
else
} else {
method = Method.POST;
}
// 判断是否有查询字符串
if (args[1].contains("?")) {
// 获得分割符?的位置
@ -46,7 +45,7 @@ public class Request {
for (String param : params) {
if (param.contains("=")) {
String[] paraminfo = param.split("=");
querystring.put(paraminfo[0], paraminfo[1]);
querystring.put(paraminfo[0].toLowerCase(), paraminfo[1]);
}
}
} else {
@ -60,6 +59,13 @@ public class Request {
}
}
/**
* @return 获得请求方式
*/
public Method getMethod() {
return method;
}
/**
* @return 获得前缀区域
*/
@ -76,20 +82,6 @@ public class Request {
return querystring;
}
/**
* @return 查询数据
*/
public String Query(String key) {
return querystring.get(key);
}
/**
* @return 获得请求方式
*/
public Method getMethod() {
return method;
}
/**
* @return 获得HTTP版本
*/
@ -103,4 +95,11 @@ public class Request {
public boolean isSuccess() {
return isSuccess;
}
/**
* @return 查询数据
*/
public String Query(String key) {
return querystring.get(key);
}
}

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package cn.citycraft.CTZLoginServer.Socket;
@ -8,16 +8,9 @@ import java.util.HashMap;
import java.util.Map.Entry;
/**
* @author 蒋天蓓
* 2015年7月28日下午7:19:28
* HTTP客户端响应类
* @author 蒋天蓓 2015年7月28日下午7:19:28 HTTP客户端响应类
*/
public class Response {
protected HttpStates state = HttpStates.OK;
protected String version = "HTTP/1.1";
protected HashMap<String, String> header = new HashMap<String, String>();
protected String html = "";
public enum HttpStates {
OK(200), // 客户端请求成功
@ -30,6 +23,10 @@ public class Response {
private int num = 0;
HttpStates(int num) {
this.num = num;
}
/**
* @return 返回状态所表示的代码
*/
@ -37,20 +34,25 @@ public class Response {
return num;
}
HttpStates(int num) {
this.num = num;
}
@Override
public String toString() {
return super.toString().replace("_", " ");
}
}
protected HttpStates state = HttpStates.OK;
protected String version = "HTTP/1.1";
protected HashMap<String, String> header = new HashMap<String, String>();
protected String html = "";
public Response() {
header.put("Date", new Date().toString() + " GMT");
header.put("Server", "Minecraft-CTZLoginServer");
header.put("Content-Type", "text/html; charset=UTF-8");
}
public void setHeader(String key, String value) {
header.put(key, value);
}
public void setHtml(String html) {
@ -61,10 +63,6 @@ public class Response {
this.state = state;
}
public void setHeader(String key, String value) {
header.put(key, value);
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package cn.citycraft.CTZLoginServer.listen;
@ -16,34 +16,40 @@ import cn.citycraft.CTZLoginServer.config.Config;
import fr.xephi.authme.api.API;
/**
* @author 蒋天蓓
* 2015年7月28日下午9:05:20
* TODO
* @author 蒋天蓓 2015年7月28日下午9:05:20 TODO
*/
public class PlayerListen implements Listener {
protected HashMap<String, Long> lastquittime = new HashMap<String, Long>();
private long getNowTime() {
return System.currentTimeMillis() / 1000;
}
@EventHandler
public void PlayerLogin(PlayerLoginEvent e) {
String name = e.getPlayer().getName().toLowerCase();
long lasttime = lastquittime.get(name) == null ? 0 : lastquittime.get(name);
e.setKickMessage("§6[§bCTZLS§6] §c禁止入服 请使用服务器专用启动器进入游戏!");
if (getNowTime() - lasttime < Config.getInstance().getInt("server.timeout")) {
API.forceLogin(e.getPlayer());
return;
}
if (CTZLoginQueue.isCheckIP()) {
if (CTZLoginQueue.Login(name)) {
if (CTZLoginQueue.Login(name, e.getAddress().getHostAddress())) {
if (CTZLoginQueue.canLogin(name)) {
if (CTZLoginQueue.isCheckIP()) {
if (CTZLoginQueue.canLogin(name, e.getAddress().getHostAddress())) {
API.forceLogin(e.getPlayer());
return;
}
e.setKickMessage("§6[§bCTZLS§6] §c禁止入服 申请登录的IP与客户端IP不同!");
} else {
if (CTZLoginQueue.Login(name)) {
API.forceLogin(e.getPlayer());
return;
}
}
} else {
if (CTZLoginQueue.Login(name)) {
API.forceLogin(e.getPlayer());
if (!Config.getInstance().getBoolean("forceclient"))
return;
}
}
e.setResult(Result.KICK_WHITELIST);
}
@ -52,8 +58,4 @@ public class PlayerListen implements Listener {
public void PlayerQuit(PlayerQuitEvent e) {
lastquittime.put(e.getPlayer().getName().toLowerCase(), getNowTime());
}
private long getNowTime() {
return System.currentTimeMillis() / 1000;
}
}

View File

@ -12,6 +12,8 @@ config:
port: 25580
#断线重连超时
timeout: 10
#强制使用专用客户端(关闭则可以同时使用自带登录和外部登录)
forceclient: true
areas:
1:
name: '纯净大区'