MACBAN 自動ID登録システム

Go down

MACBAN 自動ID登録システム

投稿 by 構築さん on 2014-05-31, 17:04

Code:
public class AutoRegister {
private static final Map<String> accountList = new HashMap<String>();

public static boolean getAccountExists(String login) {
if (accountList.isEmpty()) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT name, lastknownip FROM accounts");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
accountList.put(rs.getString("name").toLowerCase(), rs.getString("lastknownip"));
}
rs.close();
ps.close();
} catch (Exception e) {
System.out.println("Error loading the account list: " + e + ".");
}
}
return accountList.containsKey(login);
}

public static boolean createAccount(String login, String pwd, String eip) {
boolean success = false;
int countIP = 0;
String eIP = eip.substring(1, eip.lastIndexOf(':'));
for (String ips : accountList.values()) {
if (ips.equals(eIP)) {
countIP++;
}
}
if (5 > countIP) {
try {
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO accounts (name, password, email, birthday, macs, lastknownip) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, login);
ps.setString(2, sha1(pwd));
ps.setString(3, "no@email.provided");
ps.setString(4, "0000-00-00");
ps.setString(5, "00-00-00-00-00-00");
ps.setString(6, eIP);
ps.executeUpdate();
ps.close();
success = true;
accountList.put(login.toLowerCase(), eIP);
} catch (Exception e) {
System.out.println("Error creating an account (" + login + " | " + pwd + " | " + eip + ").");
}
}
return success;
}

private static String sha1(String in) {
try {
MessageDigest Digester = MessageDigest.getInstance("SHA-1");
Digester.update(in.getBytes("UTF-8"), 0, in.length());
byte[] sha1Hash = Digester.digest();
return HexTool.toString(sha1Hash).replace(" ", "").toLowerCase();
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("Hashing the password failed: ", ex);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Encoding the string failed: ", e);
}
}
}

LoginPasswordHandler.java

Code:
loginok = c.login(login, pwd, ipBan || macBan);

Code:
if (AutoRegister.getAccountExists(login.toLowerCase())) {
loginok = c.login(login, pwd, ipBan || macBan);
} else if ((!ipBan || !macBan)) {
if (AutoRegister.createAccount(login, pwd, c.getSession().getRemoteAddress().toString())) {
loginok = c.login(login, pwd, ipBan || macBan);
} else {
loginok = 10;
}
} else {
loginok = 5;
}

SQL

Code:
ALTER TABLE `accounts` ADD COLUMN `lastknownip` TINYTEXT NULL;
avatar
構築さん
Admin

Posts : 193
Join date : 2014/05/29

http://maplescience.forumjap.com

トップに戻る Go down

トップに戻る


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