Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config option for mysql to use ssl or not. #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/fr/Alphart/BAT/BAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ public void loadDB(final Callback<Boolean> 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();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/fr/Alphart/BAT/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/fr/Alphart/BAT/database/DataSourceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.<Boolean>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");
Expand Down