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

Add mysql secure connection function (closes #277) #281

Merged
merged 2 commits into from
Mar 22, 2020
Merged
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
5 changes: 3 additions & 2 deletions docs/authenticator.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Authenticator

Trojan servers can authenticate users according to not only passwords in the config file but also entries in a MySQL (MariaDB) database. To turn this functionality on, set `enabled` field in the MySQL config to `true` and correctly configure the server address and credentials, etc:
Trojan servers can authenticate users according to not only passwords in the config file but also entries in a MySQL (MariaDB) database. To turn this functionality on, set `enabled` field in the MySQL config to `true` and correctly configure the server address, credentials, and etc. If you would like to connect to the database securely, you can to fill the `cafile` field indicating the CA file:

```json
"mysql": {
Expand All @@ -9,7 +9,8 @@ Trojan servers can authenticate users according to not only passwords in the con
"server_port": 3306,
"database": "trojan",
"username": "trojan",
"password": ""
"password": "",
"cafile": ""
}
```

Expand Down
3 changes: 2 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ The NAT config is for transparent proxy. You'll need to [setup iptables rules](h
"server_port": 3306,
"database": "trojan",
"username": "trojan",
"password": ""
"password": "",
"cafile": ""
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion examples/server.json-example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"server_port": 3306,
"database": "trojan",
"username": "trojan",
"password": ""
"password": "",
"cafile": ""
}
}
3 changes: 3 additions & 0 deletions src/core/authenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ using namespace std;
Authenticator::Authenticator(const Config &config) {
mysql_init(&con);
Log::log_with_date_time("connecting to MySQL server " + config.mysql.server_addr + ':' + to_string(config.mysql.server_port), Log::INFO);
if (config.mysql.cafile != "") {
mysql_ssl_set(&con, NULL, NULL, config.mysql.cafile.c_str(), NULL, NULL);
mem0rz marked this conversation as resolved.
Show resolved Hide resolved
}
if (mysql_real_connect(&con, config.mysql.server_addr.c_str(),
config.mysql.username.c_str(),
config.mysql.password.c_str(),
Expand Down
1 change: 1 addition & 0 deletions src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void Config::populate(const ptree &tree) {
mysql.database = tree.get("mysql.database", string("trojan"));
mysql.username = tree.get("mysql.username", string("trojan"));
mysql.password = tree.get("mysql.password", string());
mysql.cafile = tree.get("mysql.cafile", string());
}

bool Config::sip003() {
Expand Down
1 change: 1 addition & 0 deletions src/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Config {
std::string database;
std::string username;
std::string password;
std::string cafile;
} mysql;
void load(const std::string &filename);
void populate(const std::string &JSON);
Expand Down
3 changes: 2 additions & 1 deletion tests/LinuxSmokeTest/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"server_port": 0,
"database": "",
"username": "",
"password": ""
"password": "",
"cafile": ""
}
}