Cleanup for AccountData class.
This commit is contained in:
parent
0876c15543
commit
3c3e0ec3e6
@ -17,35 +17,35 @@
|
|||||||
*/
|
*/
|
||||||
package org.l2jmobius.loginserver;
|
package org.l2jmobius.loginserver;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.l2jmobius.Config;
|
||||||
|
|
||||||
public class AccountData
|
public class AccountData
|
||||||
{
|
{
|
||||||
|
private static Logger _log = Logger.getLogger(AccountData.class.getName());
|
||||||
private static final String SHA = "SHA";
|
private static final String SHA = "SHA";
|
||||||
private static final String UTF_8 = "UTF-8";
|
private static final String UTF_8 = "UTF-8";
|
||||||
private static Logger _log = Logger.getLogger(AccountData.class.getName());
|
private static final Map<String, byte[]> _logPass = new ConcurrentHashMap<>();
|
||||||
private final Map<String, byte[]> _logPass = new HashMap<>();
|
private static final Map<String, Integer> _accessLevels = new ConcurrentHashMap<>();
|
||||||
private final Map<String, Integer> _accessLevels = new HashMap<>();
|
private static final Map<String, Integer> _hackProtection = new ConcurrentHashMap<>();
|
||||||
private final Map<String, Integer> _hackProtection = new HashMap<>();
|
|
||||||
private final boolean _autoCreate;
|
|
||||||
|
|
||||||
public AccountData(boolean autoCreate)
|
public AccountData(boolean autoCreate)
|
||||||
{
|
{
|
||||||
_autoCreate = autoCreate;
|
_log.config("Automatically creating new accounts: " + Config.AUTO_CREATE_ACCOUNTS);
|
||||||
_log.config("Automatically creating new accounts: " + _autoCreate);
|
|
||||||
File loginFile = new File("data/accounts.txt");
|
File loginFile = new File("data/accounts.txt");
|
||||||
if (loginFile.exists())
|
if (loginFile.exists())
|
||||||
{
|
{
|
||||||
@ -74,6 +74,7 @@ public class AccountData
|
|||||||
_log.warning("Hacking detected from ip:" + address.getHostAddress() + " .. adding IP to banlist.");
|
_log.warning("Hacking detected from ip:" + address.getHostAddress() + " .. adding IP to banlist.");
|
||||||
throw new HackingException(address.getHostAddress());
|
throw new HackingException(address.getHostAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
MessageDigest md = MessageDigest.getInstance(SHA);
|
MessageDigest md = MessageDigest.getInstance(SHA);
|
||||||
@ -82,7 +83,7 @@ public class AccountData
|
|||||||
byte[] expected = _logPass.get(user);
|
byte[] expected = _logPass.get(user);
|
||||||
if (expected == null)
|
if (expected == null)
|
||||||
{
|
{
|
||||||
if (_autoCreate)
|
if (Config.AUTO_CREATE_ACCOUNTS)
|
||||||
{
|
{
|
||||||
_logPass.put(user, hash);
|
_logPass.put(user, hash);
|
||||||
_accessLevels.put(user, 0);
|
_accessLevels.put(user, 0);
|
||||||
@ -109,6 +110,7 @@ public class AccountData
|
|||||||
_log.warning("Could not check password:" + e);
|
_log.warning("Could not check password:" + e);
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
int failedCount = 1;
|
int failedCount = 1;
|
||||||
@ -122,17 +124,17 @@ public class AccountData
|
|||||||
{
|
{
|
||||||
_hackProtection.remove(address.getHostAddress());
|
_hackProtection.remove(address.getHostAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readFromDisk(File loginFile) throws NumberFormatException, IOException
|
private void readFromDisk(File loginFile) throws NumberFormatException, IOException
|
||||||
{
|
{
|
||||||
BufferedReader lnr = null;
|
|
||||||
_logPass.clear();
|
_logPass.clear();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
String line = null;
|
String line = null;
|
||||||
lnr = new LineNumberReader(new FileReader(loginFile));
|
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(loginFile)));
|
||||||
while ((line = ((LineNumberReader) lnr).readLine()) != null)
|
while ((line = lnr.readLine()) != null)
|
||||||
{
|
{
|
||||||
StringTokenizer st = new StringTokenizer(line, "\t\n\r");
|
StringTokenizer st = new StringTokenizer(line, "\t\n\r");
|
||||||
if (!st.hasMoreTokens())
|
if (!st.hasMoreTokens())
|
||||||
@ -141,7 +143,7 @@ public class AccountData
|
|||||||
}
|
}
|
||||||
String name = st.nextToken().toLowerCase();
|
String name = st.nextToken().toLowerCase();
|
||||||
String password = st.nextToken();
|
String password = st.nextToken();
|
||||||
_logPass.put(name, java.util.Base64.getDecoder().decode(password));
|
_logPass.put(name, Base64.getDecoder().decode(password));
|
||||||
if (st.hasMoreTokens())
|
if (st.hasMoreTokens())
|
||||||
{
|
{
|
||||||
String access = st.nextToken();
|
String access = st.nextToken();
|
||||||
@ -154,25 +156,15 @@ public class AccountData
|
|||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
_log.config("Found " + i + " accounts on disk.");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
lnr.close();
|
lnr.close();
|
||||||
return;
|
_log.config("Found " + i + " accounts on disk.");
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToDisk()
|
public void saveToDisk()
|
||||||
{
|
{
|
||||||
File loginFile = new File("data/accounts.txt");
|
|
||||||
FileWriter save = null;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
save = new FileWriter(loginFile);
|
FileWriter save = new FileWriter(new File("data/accounts.txt"));
|
||||||
Iterator<String> iter = _logPass.keySet().iterator();
|
Iterator<String> iter = _logPass.keySet().iterator();
|
||||||
while (iter.hasNext())
|
while (iter.hasNext())
|
||||||
{
|
{
|
||||||
@ -185,24 +177,11 @@ public class AccountData
|
|||||||
save.write("" + _accessLevels.get(name));
|
save.write("" + _accessLevels.get(name));
|
||||||
save.write("\r\n");
|
save.write("\r\n");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
_log.warning("Could not store accouts file." + e);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (save != null)
|
|
||||||
{
|
|
||||||
save.close();
|
save.close();
|
||||||
}
|
}
|
||||||
}
|
catch (Exception e)
|
||||||
catch (Exception e1)
|
|
||||||
{
|
{
|
||||||
}
|
_log.warning("Could not store accouts file." + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user