diff --git a/src/main/java/fr/Alphart/BAT/BAT.java b/src/main/java/fr/Alphart/BAT/BAT.java index 5c3446d..d380293 100644 --- a/src/main/java/fr/Alphart/BAT/BAT.java +++ b/src/main/java/fr/Alphart/BAT/BAT.java @@ -159,13 +159,15 @@ public void loadDB(final Callback dbState) { final String database = config.getMysql_database(); final String port = config.getMysql_port(); final String host = config.getMysql_host(); + final boolean use_ssl = config.isMysql_ssl(); // BoneCP can accept no database and we want to avoid that Preconditions.checkArgument(!"".equals(database), "You must set the database."); ProxyServer.getInstance().getScheduler().runAsync(this, new Runnable() { @Override public void run() { try{ - dsHandler = new DataSourceHandler(host, port, database, username, password); + dsHandler = new DataSourceHandler(host, port, database, username, password, use_ssl); + final Connection c = dsHandler.getConnection(); if (c != null) { c.close(); diff --git a/src/main/java/fr/Alphart/BAT/Configuration.java b/src/main/java/fr/Alphart/BAT/Configuration.java index be7fb32..fa5c05a 100644 --- a/src/main/java/fr/Alphart/BAT/Configuration.java +++ b/src/main/java/fr/Alphart/BAT/Configuration.java @@ -56,8 +56,12 @@ public Configuration(){ @Path(value = "mysql.host") private String mysql_host = "localhost"; @Comment("If you don't know it, just leave it like this (3306 = default mysql port)") - @Path(value = "mysql.port") + @Path(value = "mysql.port") private String mysql_port = "3306"; + @Comment("Set this to true, if you configured mysql server to use ssl and provided truststore for server " + + "certificate verification (Just leave it as it is, if you don't know what I am talking about.)") + @Path(value = "mysql.ssl") + private boolean mysql_ssl = false; public Locale getLocale() { if (language.length() != 2) { BAT.getInstance().getLogger().severe("Incorrect language set ... The language was set to english."); diff --git a/src/main/java/fr/Alphart/BAT/database/DataSourceHandler.java b/src/main/java/fr/Alphart/BAT/database/DataSourceHandler.java index 414f5ab..fa0bafc 100644 --- a/src/main/java/fr/Alphart/BAT/database/DataSourceHandler.java +++ b/src/main/java/fr/Alphart/BAT/database/DataSourceHandler.java @@ -17,6 +17,7 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.logging.Level; +import java.util.logging.Logger; import com.zaxxer.hikari.HikariDataSource; import net.md_5.bungee.api.ProxyServer; @@ -40,6 +41,7 @@ public class DataSourceHandler { private String database; private String port; private String host; + private Boolean use_ssl; private static boolean sqlite = false; // If sqlite is used or not private Connection SQLiteConn; @@ -52,21 +54,31 @@ public class DataSourceHandler { * @param database * @param username * @param password + * @param use_ssl True, if connection to mysql server is ssl protected; false otherwise. * @throws SQLException */ - public DataSourceHandler(final String host, final String port, final String database, final String username, final String password) throws SQLException{ + public DataSourceHandler(final String host, final String port, final String database, final String username, + final String password, final Boolean use_ssl) throws SQLException{ // Check database's informations and init connection this.host = Preconditions.checkNotNull(host); this.port = Preconditions.checkNotNull(port); this.database = Preconditions.checkNotNull(database); this.username = Preconditions.checkNotNull(username); this.password = Preconditions.checkNotNull(password); + this.use_ssl = Preconditions.checkNotNull(use_ssl); - BAT.getInstance().getLogger().config("Initialization of HikariCP in progress ..."); + Logger logger = BAT.getInstance().getLogger(); + logger.config("Initialization of HikariCP in progress ..."); BasicConfigurator.configure(new NullAppender()); ds = new HikariDataSource(); - ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + - "?useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID()); + ds.setJdbcUrl(String.format( + "jdbc:mysql://%s:%s/%s" + + "?useLegacyDatetimeCode=false" + + "&serverTimezone=" + TimeZone.getDefault().getID() + + "&useSSL=%s" + , this.host, this.port, this.database, this.use_ssl)); + + logger.config("Using jdbc connection url: " + ds.getJdbcUrl()); ds.setUsername(this.username); ds.setPassword(this.password); ds.addDataSourceProperty("cachePrepStmts", "true");