自動ID登録システム

Go down

自動ID登録システム

投稿 by 構築さん on 2014-05-30, 09:32

SQL


Code:
ALTER TABLE `accounts` ADD COLUMN `lastknownip` VARCHAR(30) NOT NULL DEFAULT '' AFTER `macs`;

AutoRegister.java

Code:
package org.character;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.ResultSet;import org.communication.MapleClient;import org.tools.DatabaseConnection;public class AutoRegister {   private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MapleClient.class);   private static final int ACCOUNTS_PER_IP = 3;   public static final boolean autoRegister = true;   public static boolean success = false;   public static boolean getAccountExists(String login) { boolean accountExists = false;   Connection con = DatabaseConnection.getConnection();   try { PreparedStatement ps = con.prepareStatement("SELECT name FROM accounts WHERE name = ?");    ps.setString(1, login);    ResultSet rs = ps.executeQuery();    if(rs.first()) accountExists = true;   } catch (Exception ex) {    log.error("ERROR", ex);   }    return accountExists;   }   public static void createAccount(String login, String pwd, String eip) { String sockAddr = eip;    Connection con;    try {    con = DatabaseConnection.getConnection();    } catch (Exception ex) {    log.error("ERROR", ex);    return;    } try {    PreparedStatement ipc = con.prepareStatement("SELECT lastknownip FROM accounts WHERE lastknownip = ?"); ipc.setString(1, sockAddr.substring(1, sockAddr.lastIndexOf(':')));    ResultSet rs = ipc.executeQuery();    if (rs.first() == false || rs.last() == true && rs.getRow() <ACCOUNTS_PER_IP> 0;    pin = rs.getString("pin");    greason = rs.getByte("greason");    tempban = getTempBanCalendar(rs);    if ((banned == 0 && !ipMacBanned) || banned == -1) { PreparedStatement ips = con.prepareStatement("INSERT INTO iplog (accountid, ip) VALUES (?, ?)");    ips.setInt(1, accId);    String sockAddr = session.getRemoteAddress().toString(); ips.setString(2, sockAddr.substring(1, sockAddr.lastIndexOf(':')));    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 {    boolean updatePasswordHash = false;    // Check if the passwords are correct here. :B    if (LoginCryptoLegacy.isLegacyPassword(passhash) && LoginCryptoLegacy.checkPassword(pwd, passhash)) {    // Check if a password upgrade is needed.    loginok = 0;    updatePasswordHash = true;    } else if (salt == null && LoginCrypto.checkSha1Hash(passhash, pwd)) {    loginok = 0;    updatePasswordHash = true;    } else if (LoginCrypto.checkSaltedSha512Hash(passhash, pwd, salt)) {    loginok = 0;    } else { loggedIn = false;    loginok = 4;    }    if (updatePasswordHash) {    PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `password` = ?, `salt` = ? WHERE id = ?"); try {    String newSalt = LoginCrypto.makeSalt();    pss.setString(1, LoginCrypto.makeSaltedSha512Hash(pwd, newSalt));    pss.setString(2, newSalt); pss.setInt(3, accId);    pss.executeUpdate();    } finally {    pss.close();    }    }    }    }    }    rs.close(); ps.close();    } catch (SQLException e) {    log.error("ERROR", e);    }    return loginok;   }


Code:
public int login(String login, String pwd, boolean ipMacBanned) { int loginok = 5; Connection con = DatabaseConnection.getConnection();    try {    PreparedStatement ps = con .prepareStatement("SELECT id,password,salt,tempban,banned,gm,macs,lastknownip,greason,pin 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");    String salt = rs.getString("salt");    gm = igm > 0;    pin = rs.getString("pin");    greason = rs.getByte("greason");    tempban = getTempBanCalendar(rs);    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();    } //update the lastknownip for the player on a successful login if the ip changes if (!rs.getString("lastknownip").equals(session.getRemoteAddress().toString())) { PreparedStatement lkip = con.prepareStatement("UPDATE accounts SET lastknownip = ? where id = ?"); String sockAddr = session.getRemoteAddress().toString();    lkip.setString(1, sockAddr.substring(1, sockAddr.lastIndexOf(':'))); lkip.setInt(2, accId); lkip.executeUpdate(); lkip.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 {    boolean updatePasswordHash = false;    // Check if the passwords are correct here. :B    if (LoginCryptoLegacy.isLegacyPassword(passhash) && LoginCryptoLegacy.checkPassword(pwd, passhash)) {    // Check if a password upgrade is needed.    loginok = 0;    updatePasswordHash = true;    } else if (salt == null && LoginCrypto.checkSha1Hash(passhash, pwd)) {    loginok = 0;    updatePasswordHash = true;    } else if (LoginCrypto.checkSaltedSha512Hash(passhash, pwd, salt)) {    loginok = 0;    } else { loggedIn = false;    loginok = 4;    }    if (updatePasswordHash) {    PreparedStatement pss = con.prepareStatement("UPDATE `accounts` SET `password` = ?, `salt` = ? WHERE id = ?"); try {    String newSalt = LoginCrypto.makeSalt();    pss.setString(1, LoginCrypto.makeSaltedSha512Hash(pwd, newSalt));    pss.setString(2, newSalt); pss.setInt(3, accId);    pss.executeUpdate();    } finally {    pss.close();    }    }    }    }    }    rs.close(); ps.close();    } catch (SQLException e) {    log.error("ERROR", e);    }    return loginok;   }
avatar
構築さん
Admin

Posts : 193
Join date : 2014/05/29

http://maplescience.forumjap.com

トップに戻る Go down

トップに戻る


 
Permissions in this forum:
返信投稿: 不可