📄 mapleclient.java
字号:
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation. You may not use, modify
or distribute this program under any other version of the
GNU Affero General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.odinms.client;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import javax.script.ScriptEngine;
import net.sf.odinms.client.messages.MessageCallback;
import net.sf.odinms.database.DatabaseConnection;
import net.sf.odinms.database.DatabaseException;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.net.login.LoginServer;
import net.sf.odinms.net.world.MaplePartyCharacter;
import net.sf.odinms.net.world.PartyOperation;
import net.sf.odinms.net.world.remote.WorldChannelInterface;
import net.sf.odinms.scripting.NPCScriptManager;
import net.sf.odinms.server.MapleTrade;
import net.sf.odinms.server.TimerManager;
import net.sf.odinms.tools.MapleAESOFB;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.IPAddressTool;
import org.apache.mina.common.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MapleClient {
public static final int LOGIN_NOTLOGGEDIN = 0;
public static final int LOGIN_SERVER_TRANSITION = 1;
public static final int LOGIN_LOGGEDIN = 2;
public static final String CLIENT_KEY = "CLIENT";
private static final Logger log = LoggerFactory.getLogger(MapleClient.class);
private MapleAESOFB send;
private MapleAESOFB receive;
private IoSession session;
private MapleCharacter player;
private int channel = 1;
private int accId = 1;
private boolean loggedIn = false;
private boolean serverTransition = false;
private Calendar birthday = null;
private String accountName;
private int world;
private long lastPong;
private boolean gm;
private Set<String> macs = new HashSet<String>();
private Map<String, ScriptEngine> engines = new HashMap<String, ScriptEngine>();
private ScheduledFuture<?> idleTask = null;
public MapleClient(MapleAESOFB send, MapleAESOFB receive, IoSession session) {
this.send = send;
this.receive = receive;
this.session = session;
}
public MapleAESOFB getReceiveCrypto() {
return receive;
}
public MapleAESOFB getSendCrypto() {
return send;
}
public IoSession getSession() {
return session;
}
public MapleCharacter getPlayer() {
return player;
}
public void setPlayer(MapleCharacter player) {
this.player = player;
}
public void sendCharList(int server) {
this.session.write(MaplePacketCreator.getCharList(this, server));
}
public List<MapleCharacter> loadCharacters(int serverId) { // TODO make
// this less
// costly zZz
List<MapleCharacter> chars = new LinkedList<MapleCharacter>();
for (CharNameAndId cni : loadCharactersInternal(serverId)) {
try {
chars.add(MapleCharacter.loadCharFromDB(cni.id, this, false));
} catch (SQLException e) {
log.error("Loading characters failed", e);
}
}
return chars;
}
public List<String> loadCharacterNames(int serverId) {
List<String> chars = new LinkedList<String>();
for (CharNameAndId cni : loadCharactersInternal(serverId)) {
chars.add(cni.name);
}
return chars;
}
private List<CharNameAndId> loadCharactersInternal(int serverId) {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps;
List<CharNameAndId> chars = new LinkedList<CharNameAndId>();
try {
ps = con.prepareStatement("SELECT id, name FROM characters WHERE accountid = ? AND world = ?");
ps.setInt(1, this.accId);
ps.setInt(2, serverId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id")));
}
rs.close();
ps.close();
} catch (SQLException e) {
log.error("THROW", e);
}
return chars;
}
public boolean isLoggedIn() {
return loggedIn;
}
public boolean hasBannedIP() {
boolean ret = false;
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')");
ps.setString(1, session.getRemoteAddress().toString());
ResultSet rs = ps.executeQuery();
rs.next();
if (rs.getInt(1) > 0) {
ret = true;
}
rs.close();
ps.close();
} catch (SQLException ex) {
log.error("Error checking ip bans", ex);
}
return ret;
}
public boolean hasBannedMac() {
if (macs.isEmpty())
return false;
boolean ret = false;
int i = 0;
try {
Connection con = DatabaseConnection.getConnection();
StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM macbans WHERE mac IN (");
for (i = 0; i < macs.size(); i++) {
sql.append("?");
if (i != macs.size() - 1)
sql.append(", ");
}
sql.append(")");
PreparedStatement ps = con.prepareStatement(sql.toString());
i = 0;
for (String mac : macs) {
i++;
ps.setString(i, mac);
}
ResultSet rs = ps.executeQuery();
rs.next();
if (rs.getInt(1) > 0) {
ret = true;
}
rs.close();
ps.close();
} catch (SQLException ex) {
log.error("Error checking mac bans", ex);
}
return ret;
}
private void loadMacsIfNescessary() throws SQLException {
if (macs.isEmpty()) {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT macs FROM accounts WHERE id = ?");
ps.setInt(1, accId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String[] macData = rs.getString("macs").split(", ");
for (String mac : macData) {
if (!mac.equals("")) {
macs.add(mac);
}
}
} else {
throw new RuntimeException("No valid account associated with this client.");
}
rs.close();
ps.close();
}
}
public void banMacs() {
Connection con = DatabaseConnection.getConnection();
try {
loadMacsIfNescessary();
List<String> filtered = new LinkedList<String>();
PreparedStatement ps = con.prepareStatement("SELECT filter FROM macfilters");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
filtered.add(rs.getString("filter"));
}
rs.close();
ps.close();
ps = con.prepareStatement("INSERT INTO macbans (mac) VALUES (?)");
for (String mac : macs) {
boolean matched = false;
for (String filter : filtered) {
if (mac.matches(filter)) {
matched = true;
break;
}
}
if (!matched) {
ps.setString(1, mac);
try {
ps.executeUpdate();
} catch (SQLException e) {
// can fail because of UNIQUE key, we dont care
}
}
}
ps.close();
} catch (SQLException e) {
log.error("Error banning MACs", e);
}
}
/**
* Returns 0 on success, a state to be used for
* {@link MaplePacketCreator#getLoginFailed(int)} otherwise.
*
* @param success
* @return The state of the login.
*/
public int finishLogin(boolean success) {
if (success) {
synchronized (MapleClient.class) {
if (getLoginState() > MapleClient.LOGIN_NOTLOGGEDIN) { // already
// loggedin
loggedIn = false;
return 7;
}
updateLoginState(MapleClient.LOGIN_LOGGEDIN);
}
return 0;
} else {
return 10;
}
}
public int login(String login, String pwd, boolean ipMacBanned) {
int loginok = 5;
Connection con = DatabaseConnection.getConnection();
try {
PreparedStatement ps = con
.prepareStatement("SELECT id,password,banned,gm,macs FROM accounts WHERE name = ?");
ps.setString(1, login);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int banned = rs.getInt("banned");
accId = rs.getInt("id");
int igm = rs.getInt("gm");
String passhash = rs.getString("password");
gm = igm > 0;
if ((banned == 0 && !ipMacBanned) || banned == -1) {
PreparedStatement ips = con.prepareStatement("INSERT INTO iplog (accountid, ip) VALUES (?, ?)");
ips.setInt(1, accId);
ips.setString(2, session.getRemoteAddress().toString());
ips.executeUpdate();
ips.close();
}
// do NOT track ALL mac addresses ever used
/*String[] macData = rs.getString("macs").split(", ");
for (String mac : macData) {
if (!mac.equals(""))
macs.add(mac);
}*/
ps.close();
// if (gm > 0) {
// session.write(MaplePacketCreator.getAuthSuccessRequestPin(getAccountName()));
// return finishLogin(true);
// }
if (banned == 1) {
loginok = 3;
} else {
// this is to simplify unbanning
// all known ip and mac bans associated with the current
// client
// will be deleted
if (banned == -1)
unban();
if (getLoginState() > MapleClient.LOGIN_NOTLOGGEDIN) { // already
// loggedin
loggedIn = false;
loginok = 7;
} else {
// Check if the passwords are correct here. :B
if (LoginCrypto.checkPassword(pwd, passhash)) {
// Check if a password upgrade is needed.
if (LoginCrypto.needsRehash(passhash)) {
PreparedStatement pss = con
.prepareStatement("UPDATE `accounts` SET `password` = ? WHERE id = ?");
pss.setString(1, LoginCrypto.hashPassword(pwd));
pss.setInt(2, accId);
pss.executeUpdate();
}
loginok = 0;
} else {
loginok = 4;
}
}
}
}
rs.close();
ps.close();
} catch (SQLException e) {
log.error("ERROR", e);
}
return loginok;
}
/**
* Gets the special server IP if the client matches a certain subnet.
*
* @param subnetInfo A <code>Properties</code> instance containing all the subnet info.
* @param clientIPAddress The IP address of the client as a dotted quad.
* @param channel The requested channel to match with the subnet.
* @return <code>0.0.0.0</code> if no subnet matched, or the IP if the subnet matched.
*/
public static String getChannelServerIPFromSubnet(String clientIPAddress, int channel) {
long ipAddress = IPAddressTool.dottedQuadToLong(clientIPAddress);
Properties subnetInfo = LoginServer.getInstance().getSubnetInfo();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -