Skip to content

Commit 7a509fe

Browse files
committed
add h2 database, authTime, and disconnectOnWrongPass
Version 1.2
1 parent a915ff6 commit 7a509fe

File tree

14 files changed

+430
-73
lines changed

14 files changed

+430
-73
lines changed

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ad1tya2</groupId>
88
<artifactId>AdiAuth</artifactId>
9-
<version>1.1</version>
9+
<version>1.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>AdiAuth</name>
@@ -40,6 +40,14 @@
4040
</goals>
4141
<configuration>
4242
<minimizeJar>true</minimizeJar>
43+
<filters>
44+
<filter>
45+
<artifact>com.h2database:h2</artifact>
46+
<includes>
47+
<includes>**</includes>
48+
</includes>
49+
</filter>
50+
</filters>
4351
<createDependencyReducedPom>false</createDependencyReducedPom>
4452
</configuration>
4553
</execution>
@@ -115,6 +123,12 @@
115123
</repositories>
116124

117125
<dependencies>
126+
<dependency>
127+
<groupId>com.h2database</groupId>
128+
<artifactId>h2</artifactId>
129+
<version>1.4.200</version>
130+
<scope>provided</scope>
131+
</dependency>
118132
<dependency>
119133
<groupId>net.md-5</groupId>
120134
<artifactId>bungeecord-proxy</artifactId>

src/main/java/ad1tya2/adiauth/Bungee/AdiAuth.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ad1tya2.adiauth.Bungee;
22

33
import ad1tya2.adiauth.Bungee.commands.*;
4-
import ad1tya2.adiauth.Bungee.data.mysql;
4+
import ad1tya2.adiauth.Bungee.data.database;
55
import ad1tya2.adiauth.Bungee.data.servers;
66
import ad1tya2.adiauth.Bungee.data.storage;
77
import ad1tya2.adiauth.Bungee.events.Handler;
@@ -23,7 +23,10 @@ public void onEnable() {
2323
// Plugin startup logic
2424
instance = this;
2525

26-
load();
26+
Config.load();
27+
database.load();
28+
storage.load();
29+
servers.load();
2730

2831
getProxy().getPluginManager().registerListener(this, new Handler());
2932
getProxy().getLogger().setFilter(new Filter() {
@@ -50,9 +53,10 @@ public boolean isLoggable(LogRecord record) {
5053
getProxy().getPluginManager().registerCommand(this, new unregister());
5154
getProxy().getPluginManager().registerCommand(this, new forcechangepass());
5255
servers.serversStatusChecker();
56+
5357
}
5458

55-
public static void load(){
59+
public static void reload(){
5660
Config.load();
5761
storage.load();
5862
servers.load();
@@ -64,7 +68,7 @@ public static void runAsync(Runnable task){
6468

6569
@Override
6670
public void onDisable() {
67-
mysql.close();
71+
database.close();
6872
executor.shutdownNow();
6973
// Plugin shutdown logic
7074
}

src/main/java/ad1tya2/adiauth/Bungee/Config.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@
1414
import java.io.*;
1515
import java.util.ArrayList;
1616
import java.util.List;
17+
import java.util.Locale;
1718
import java.util.logging.Level;
1819

1920
public class Config {
2021
public static int SessionTime = 120;
22+
public enum StorageType {
23+
Mysql, H2
24+
}
25+
public static StorageType storageType;
2126

2227
public static List<ServerInfo> lobbies = new ArrayList<ServerInfo>();
2328
public static List<ServerInfo> auths = new ArrayList<ServerInfo>();
2429
public static List<String> whitelistedCommands;
2530

26-
public static String pluginMsgConf;
2731
public static int serverCheckInterval = 30,
28-
maxPremiumAccounts, maxCrackedAccounts, maxTotalAccounts;
29-
public static String backupServer, secondAttempt;
30-
public static boolean forceBackupServer, convertOldCrackedToPremium, backupServerEnabled, secondAttemptEnabled;
32+
maxPremiumAccounts, maxCrackedAccounts, maxTotalAccounts, authTime;
33+
34+
public static String backupServer, secondAttempt, pluginMsgConf;
35+
36+
public static boolean forceBackupServer, convertOldCrackedToPremium, backupServerEnabled, secondAttemptEnabled, disconnectOnWrongPass;
3137
public static class Messages {
3238
public static String registerError, loginAndRegisterSuccess, alreadyLoggedIn, loginNotRegistered,
3339
loginWrongPass, loginError, alreadyRegistered, noServersAvailable, registerMessage,
3440
loginMessage, logoutMessage, changePassError, genericPremiumError, successfulChangePass,
35-
tooManyAccounts;
41+
tooManyAccounts, loginRegisterBossBar, authTimeExceeded;
3642
public static Title loginAndRegisterSuccessTitle, registerTitle, loginTitle;
3743
}
3844
public enum MYSQL {
@@ -45,12 +51,15 @@ public enum MYSQL {
4551

4652

4753
public static Configuration config;
54+
private static Configuration defaultConf;
4855

4956

5057
public static void load() {
5158
saveDefaultConf();
5259
try {
53-
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(AdiAuth.instance.getDataFolder(), "config.yml"));
60+
//Loading the default config
61+
defaultConf = ConfigurationProvider.getProvider(YamlConfiguration.class).load(AdiAuth.instance.getResourceAsStream("bungeeconfig.yml"));
62+
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(AdiAuth.instance.getDataFolder(), "config.yml"), defaultConf);
5463
Configuration mysqlConf = config.getSection("mysql");
5564
for (MYSQL x : MYSQL.values()) {
5665
x.value = mysqlConf.getString(x.name());
@@ -95,14 +104,28 @@ public static void load() {
95104
Messages.tooManyAccounts = getMsgString("tooManyAccounts");
96105
secondAttempt = tools.getColoured(config.getString("secondAttempt"));
97106
Config.secondAttemptEnabled = !secondAttempt.equals("");
107+
disconnectOnWrongPass = config.getBoolean("disconnectOnWrongPass");
108+
authTime = config.getInt("authTime");
109+
Messages.loginRegisterBossBar = getMsgString("loginRegisterBossBar");
110+
Messages.authTimeExceeded = getMsgString("authTimeExceeded");
111+
String database = config.getString("database").toLowerCase();
112+
switch (database){
113+
case "mysql":
114+
storageType = StorageType.Mysql;
115+
break;
116+
case "h2":
117+
storageType = StorageType.H2;
118+
break;
119+
default:
120+
storageType = StorageType.H2;
121+
break;
122+
}
98123

99124
}
100125
catch (Exception e){
101126
e.printStackTrace();
102127
tools.log(Level.SEVERE, "Config wasnt loaded properly!");
103128
}
104-
105-
106129
}
107130

108131

@@ -115,11 +138,16 @@ private static Title createTitle(String msg){
115138
title.fadeIn(50);
116139
return title;
117140
}
141+
118142
private static void saveDefaultConf(){
119143
File configFile = new File(AdiAuth.instance.getDataFolder(), "config.yml");
120144
if (!AdiAuth.instance.getDataFolder().exists()) {
121145
AdiAuth.instance.getDataFolder().mkdir();
122146
}
147+
File libFolder = new File(configFile.getParentFile().toString()+"/libs");
148+
if(!libFolder.exists()){
149+
libFolder.mkdir();
150+
}
123151
if (!configFile.exists()) {
124152
try {
125153
configFile.createNewFile();

src/main/java/ad1tya2/adiauth/Bungee/UserProfile.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package ad1tya2.adiauth.Bungee;
22

3+
import ad1tya2.adiauth.Bungee.utils.BossBar;
4+
import ad1tya2.adiauth.Bungee.utils.tools;
5+
import net.md_5.bungee.api.connection.ProxiedPlayer;
6+
import net.md_5.bungee.protocol.DefinedPacket;
7+
import net.md_5.bungee.protocol.ProtocolConstants;
8+
39
import java.util.UUID;
10+
import java.util.concurrent.ScheduledFuture;
11+
import java.util.concurrent.TimeUnit;
412

513
public class UserProfile {
614
public String username;
715
public UUID premiumUuid;
816
public UUID uuid;
917
public String password;
1018
public String lastIp;
11-
19+
private ScheduledFuture<?> titleTask;
1220
public boolean fullJoined = false;
1321
//Full joined is set when a person completely logs into the server for the first time
1422

@@ -18,7 +26,13 @@ public boolean isPremium(){
1826
return premiumUuid != null;
1927
}
2028

21-
public void startSession(){
29+
public void startSession(ProxiedPlayer p){
30+
if(titleTask != null)
31+
{
32+
titleTask.cancel(true);
33+
BossBar.removeTitle(p);
34+
titleTask = null;
35+
}
2236
sessionEnd = System.currentTimeMillis() + Config.SessionTime*60000L;
2337
}
2438

@@ -47,7 +61,21 @@ public void loginProcessCompleted(){
4761
loggingIn = false;
4862
}
4963

50-
51-
52-
64+
public void startTitleTask(ProxiedPlayer p){
65+
float maxTime = Config.authTime;
66+
final float[] timeLeft = {maxTime};
67+
titleTask = AdiAuth.executor.scheduleAtFixedRate(new Runnable() {
68+
@Override
69+
public void run() {
70+
if(timeLeft[0] > 0 && p.isConnected()) {
71+
timeLeft[0]--;
72+
BossBar.sendTitleAndHealth(
73+
Config.Messages.loginRegisterBossBar.replaceAll("TIMELEFT",
74+
String.valueOf((int) timeLeft[0])), timeLeft[0] /maxTime, p);
75+
} else {
76+
p.disconnect(Config.Messages.authTimeExceeded);
77+
}
78+
}
79+
}, 2, 1, TimeUnit.SECONDS);
80+
}
5381
}

src/main/java/ad1tya2/adiauth/Bungee/commands/login.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ else if(profile.password == null){
3636
else if(profile.password.equals(pass)){
3737
p.sendTitle(Config.Messages.loginAndRegisterSuccessTitle);
3838
p.sendMessage(Config.Messages.loginAndRegisterSuccess);
39-
profile.startSession();
39+
profile.startSession(p);
4040
loggedInPlayer(p);
4141
}
4242
else {
43-
p.sendMessage(Config.Messages.loginWrongPass);
43+
if(Config.disconnectOnWrongPass){
44+
p.disconnect(Config.Messages.loginWrongPass);
45+
}
46+
else {
47+
p.sendMessage(Config.Messages.loginWrongPass);
48+
}
4449
}
4550
}
4651

src/main/java/ad1tya2/adiauth/Bungee/commands/register.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ else if(profile.password != null){
3939
storage.updatePlayer(profile);
4040
p.sendTitle(Config.Messages.loginAndRegisterSuccessTitle);
4141
p.sendMessage(Config.Messages.loginAndRegisterSuccess);
42-
profile.startSession();
42+
profile.startSession(p);
4343
login.loggedInPlayer(p);
4444
}
4545
}

src/main/java/ad1tya2/adiauth/Bungee/commands/reload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public reload() {
1313

1414
@Override
1515
public void execute(CommandSender sender, String[] args) {
16-
AdiAuth.load();
16+
AdiAuth.reload();
1717
sender.sendMessage(tools.getColoured("&bAdiAuth was successfully Reloaded!"));
1818
}
1919
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package ad1tya2.adiauth.Bungee.data;
2+
3+
import ad1tya2.adiauth.Bungee.AdiAuth;
4+
import ad1tya2.adiauth.Bungee.Config;
5+
import ad1tya2.adiauth.Bungee.utils.URLClassLoaderAccess;
6+
import ad1tya2.adiauth.Bungee.utils.tools;
7+
import com.zaxxer.hikari.HikariConfig;
8+
import com.zaxxer.hikari.HikariDataSource;
9+
10+
import javax.sql.DataSource;
11+
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.net.MalformedURLException;
16+
import java.net.URL;
17+
import java.net.URLClassLoader;
18+
import java.nio.channels.Channels;
19+
import java.nio.channels.ReadableByteChannel;
20+
import java.nio.file.Files;
21+
import java.nio.file.Paths;
22+
import java.sql.Connection;
23+
import java.sql.SQLException;
24+
import java.util.Properties;
25+
26+
public class database {
27+
private static HikariDataSource ds;
28+
private static final String h2Url = "https://repo1.maven.org/maven2/com/h2database/h2/1.4.200/h2-1.4.200.jar";
29+
private static final String h2DriverName = "plugins/AdiAuth/libs/h2-1.4.200.jar";
30+
public static void load(){
31+
try {
32+
HikariConfig config = new HikariConfig();
33+
if(Config.storageType == Config.StorageType.Mysql) {
34+
config.setUsername(Config.MYSQL.username.value);
35+
config.setPassword(Config.MYSQL.password.value);
36+
config.setJdbcUrl("jdbc:mysql://" + Config.MYSQL.host.value + "/" + Config.MYSQL.database.value);
37+
Properties optimizedProps = new Properties();
38+
optimizedProps.load(AdiAuth.instance.getResourceAsStream("mysql.props"));
39+
config.setDataSourceProperties(optimizedProps);
40+
} else {
41+
File library = new File(h2DriverName);
42+
if(!library.exists()){
43+
tools.log("&eDownloading h2 driver..");
44+
downloadLibrary(h2Url, h2DriverName);
45+
tools.log("&2Downloaded!");
46+
}
47+
loadLibrary(library);
48+
// config.setDriverClassName("org.h2.jdbcx.JdbcDataSource");
49+
config.setDriverClassName("org.h2.Driver");
50+
config.setJdbcUrl("jdbc:h2:./"+AdiAuth.instance.getDataFolder()+"/users;MODE=MYSQL");
51+
}
52+
config.setPoolName("AdiAuthDatabase");
53+
ds = new HikariDataSource(config);
54+
} catch (Exception e){
55+
e.printStackTrace();
56+
tools.log("&cStorage failed to initialize!, Shutting down server.");
57+
AdiAuth.instance.getProxy().stop();
58+
}
59+
}
60+
61+
62+
public static void downloadLibrary(String link, String fileName) throws IOException {
63+
URL url = new URL(link);
64+
InputStream in = url.openStream();
65+
Files.copy(in, Paths.get(fileName));
66+
}
67+
68+
public static void loadLibrary(File jar) throws MalformedURLException {
69+
URLClassLoaderAccess access = URLClassLoaderAccess.create((URLClassLoader) AdiAuth.instance.getClass().getClassLoader());
70+
access.addURL(jar.toURI().toURL());
71+
}
72+
73+
public static void close(){
74+
try {
75+
ds.close();
76+
} catch (Exception ignored){}
77+
}
78+
79+
public static Connection getConnection() throws SQLException {
80+
return ds.getConnection();
81+
}
82+
83+
}

0 commit comments

Comments
 (0)