LoginPasswordHandler.java
handlePacket函数,封包前6位就是客户端发来的mac
int[] bytes = new int[6];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = slea.readByteAsInt();
}
StringBuilder sps = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sps.append(StringUtil.getLeftPaddedStr(Integer.toHexString(bytes[i]).toUpperCase(), '0', 2));
sps.append("-");
}
String macData = sps.toString();
macData = macData.substring(0, macData.length() - 1);
c.setMac(macData);
if (LoginServer.isCheckMacs() && CheckLandersMac.checkMac(macData) || isBanned) {
c.clearInformation();
c.getSession().write(MaplePacketCreator.serverNotice(1, "无法进入游戏."));
c.getSession().write(LoginPacket.getLoginFailed(16));
return;
}
StringUtil.getLeftPaddedStr
public static String getLeftPaddedStr(String in, char padchar, int length) { //左
StringBuilder builder = new StringBuilder(length);
for (int x = in.getBytes().length; x < length; x++) {
builder.append(padchar);
}
builder.append(in);
return builder.toString();
}
CheckLandersMac.checkMac
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package handling.login.handler;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
/**
*
* @author qhms
*/
public class CheckLandersMac {
public static boolean checkMac(String mac) {
String host = "127.0.0.1";
int port = 20130;
StringBuffer sb = new StringBuffer();
try {
//与服务端建立连接
Socket client = new Socket(host, port);
//建立连接后就可以往服务端写数据了
Writer writer = new OutputStreamWriter(client.getOutputStream());
final String wrms = "100|" + mac + "\r\n";
writer.write(wrms);
writer.flush();
//写完以后进行读操作
Reader reader = new InputStreamReader(client.getInputStream());
char chars[] = new char[64];
int len;
String temp;
int index;
while ((len = reader.read(chars)) != -1) {
temp = new String(chars, 0, len);
if ((index = temp.indexOf("eof")) != -1) {
sb.append(temp.substring(0, index));
break;
}
sb.append(temp);
}
writer.close();
reader.close();
client.close();
} catch (Exception e) {
System.err.println(e);
}
return sb.toString().endsWith("no");
}
}
本文来源于心动冒险岛(www.lnshengyou.com),如需转载[心动冒险岛原创核心技术,防万能网关源码],请写明出处
上一篇:关于游戏经常C++报错导致大掉的解决办法