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

Move table names to config #2

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
8 changes: 8 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void config::init() {
add("site_password", "00000000000000000000000000000000");
add("report_password", "00000000000000000000000000000000");

// Database table names
add("torrents", "torrents");
add("files_users", "xbt_files_users");
add("users_main", "users_main");
add("users_freeleeches", "users_freeleeches");
add("client_whitelist", "xbt_client_whitelist");
add("snatched", "xbt_snatched");

// Debugging
add("readonly", false);
}
Expand Down
34 changes: 20 additions & 14 deletions db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ void mysql::load_config(config * conf) {
mysql_username = conf->get_str("mysql_username");
mysql_password = conf->get_str("mysql_password");
readonly = conf->get_bool("readonly");
torrents_table = conf->get_str("torrents");
files_users = conf->get_str("files_users");
users_main = conf->get_str("users_main");
users_freeleeches = conf->get_str("users_freeleeches");
client_whitelist = conf->get_str("client_whitelist");
snatched = conf->get_str("snatched");
}

void mysql::reload_config(config * conf) {
Expand All @@ -56,11 +62,11 @@ bool mysql::connected() {

void mysql::clear_peer_data() {
try {
mysqlpp::Query query = conn.query("TRUNCATE xbt_files_users;");
mysqlpp::Query query = conn.query("TRUNCATE " + files_users + ";");
if (!query.exec()) {
std::cerr << "Unable to truncate xbt_files_users!" << std::endl;
std::cerr << "Unable to truncate " + files_users + "!" << std::endl;
}
query = conn.query("UPDATE torrents SET Seeders = 0, Leechers = 0;");
query = conn.query("UPDATE " + torrents + " SET Seeders = 0, Leechers = 0;");
if (!query.exec()) {
std::cerr << "Unable to reset seeder and leecher count!" << std::endl;
}
Expand All @@ -72,7 +78,7 @@ void mysql::clear_peer_data() {
}

void mysql::load_torrents(torrent_list &torrents) {
mysqlpp::Query query = conn.query("SELECT ID, info_hash, freetorrent, Snatched FROM torrents ORDER BY ID;");
mysqlpp::Query query = conn.query("SELECT ID, info_hash, freetorrent, Snatched FROM " + torrents + " ORDER BY ID;");
try {
mysqlpp::StoreQueryResult res = query.store();
std::unordered_set<std::string> cur_keys;
Expand Down Expand Up @@ -140,7 +146,7 @@ void mysql::load_torrents(torrent_list &torrents) {
}

void mysql::load_users(user_list &users) {
mysqlpp::Query query = conn.query("SELECT ID, can_leech, torrent_pass, (Visible='0' OR IP='127.0.0.1') AS Protected FROM users_main WHERE Enabled='1';");
mysqlpp::Query query = conn.query("SELECT ID, can_leech, torrent_pass, (Visible='0' OR IP='127.0.0.1') AS Protected FROM " + users_main + " WHERE Enabled='1';");
try {
mysqlpp::StoreQueryResult res = query.store();
size_t num_rows = res.num_rows();
Expand Down Expand Up @@ -184,7 +190,7 @@ void mysql::load_users(user_list &users) {
}

void mysql::load_tokens(torrent_list &torrents) {
mysqlpp::Query query = conn.query("SELECT uf.UserID, t.info_hash FROM users_freeleeches AS uf JOIN torrents AS t ON t.ID = uf.TorrentID WHERE uf.Expired = '0';");
mysqlpp::Query query = conn.query("SELECT uf.UserID, t.info_hash FROM " + users_freeleeches + " AS uf JOIN " + torrents + " AS t ON t.ID = uf.TorrentID WHERE uf.Expired = '0';");
int token_count = 0;
try {
mysqlpp::StoreQueryResult res = query.store();
Expand All @@ -209,7 +215,7 @@ void mysql::load_tokens(torrent_list &torrents) {


void mysql::load_whitelist(std::vector<std::string> &whitelist) {
mysqlpp::Query query = conn.query("SELECT peer_id FROM xbt_client_whitelist;");
mysqlpp::Query query = conn.query("SELECT peer_id FROM " + client_whitelist + ";");
try {
mysqlpp::StoreQueryResult res = query.store();
size_t num_rows = res.num_rows();
Expand Down Expand Up @@ -307,7 +313,7 @@ void mysql::flush_users() {
if (update_user_buffer == "") {
return;
}
sql = "INSERT INTO users_main (ID, Uploaded, Downloaded) VALUES " + update_user_buffer +
sql = "INSERT INTO " + users_main + " (ID, Uploaded, Downloaded) VALUES " + update_user_buffer +
" ON DUPLICATE KEY UPDATE Uploaded = Uploaded + VALUES(Uploaded), Downloaded = Downloaded + VALUES(Downloaded)";
user_queue.push(sql);
update_user_buffer.clear();
Expand All @@ -332,14 +338,14 @@ void mysql::flush_torrents() {
if (update_torrent_buffer == "") {
return;
}
sql = "INSERT INTO torrents (ID,Seeders,Leechers,Snatched,Balance) VALUES " + update_torrent_buffer +
sql = "INSERT INTO " + torrents + " (ID,Seeders,Leechers,Snatched,Balance) VALUES " + update_torrent_buffer +
" ON DUPLICATE KEY UPDATE Seeders=VALUES(Seeders), Leechers=VALUES(Leechers), " +
"Snatched=Snatched+VALUES(Snatched), Balance=VALUES(Balance), last_action = " +
"IF(VALUES(Seeders) > 0, NOW(), last_action)";
torrent_queue.push(sql);
update_torrent_buffer.clear();
sql.clear();
sql = "DELETE FROM torrents WHERE info_hash = ''";
sql = "DELETE FROM " + torrents + " WHERE info_hash = ''";
torrent_queue.push(sql);
if (t_active == false) {
std::thread thread(&mysql::do_flush_torrents, this);
Expand All @@ -361,7 +367,7 @@ void mysql::flush_snatches() {
if (update_snatch_buffer == "" ) {
return;
}
sql = "INSERT INTO xbt_snatched (uid, fid, tstamp, IP) VALUES " + update_snatch_buffer;
sql = "INSERT INTO " + snatched + " (uid, fid, tstamp, IP) VALUES " + update_snatch_buffer;
snatch_queue.push(sql);
update_snatch_buffer.clear();
if (s_active == false) {
Expand Down Expand Up @@ -396,7 +402,7 @@ void mysql::flush_peers() {
if (qsize >= 1000) {
peer_queue.pop();
}
sql = "INSERT INTO xbt_files_users (uid,fid,active,uploaded,downloaded,upspeed,downspeed,remaining,corrupt," +
sql = "INSERT INTO " + files_users + " (uid,fid,active,uploaded,downloaded,upspeed,downspeed,remaining,corrupt," +
std::string("timespent,announced,ip,peer_id,useragent,mtime) VALUES ") + update_heavy_peer_buffer +
" ON DUPLICATE KEY UPDATE active=VALUES(active), uploaded=VALUES(uploaded), " +
"downloaded=VALUES(downloaded), upspeed=VALUES(upspeed), " +
Expand All @@ -412,7 +418,7 @@ void mysql::flush_peers() {
if (qsize >= 1000) {
peer_queue.pop();
}
sql = "INSERT INTO xbt_files_users (uid,fid,timespent,announced,peer_id,mtime) VALUES " +
sql = "INSERT INTO " + files_users + " (uid,fid,timespent,announced,peer_id,mtime) VALUES " +
update_light_peer_buffer +
" ON DUPLICATE KEY UPDATE upspeed=0, downspeed=0, timespent=VALUES(timespent), " +
"announced=VALUES(announced), mtime=VALUES(mtime)";
Expand Down Expand Up @@ -441,7 +447,7 @@ void mysql::flush_tokens() {
if (update_token_buffer == "") {
return;
}
sql = "INSERT INTO users_freeleeches (UserID, TorrentID, Downloaded) VALUES " + update_token_buffer +
sql = "INSERT INTO " + users_freeleeches + " (UserID, TorrentID, Downloaded) VALUES " + update_token_buffer +
" ON DUPLICATE KEY UPDATE Downloaded = Downloaded + VALUES(Downloaded)";
token_queue.push(sql);
update_token_buffer.clear();
Expand Down
2 changes: 2 additions & 0 deletions db.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class mysql {
std::queue<std::string> snatch_queue;
std::queue<std::string> token_queue;

std::string torrents_table, files_users, users_main, users_freeleeches, client_whitelist, snatched;

std::string mysql_db, mysql_host, mysql_username, mysql_password;
bool u_active, t_active, p_active, s_active, tok_active;
bool readonly;
Expand Down
7 changes: 7 additions & 0 deletions ocelot.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ mysql_username =
mysql_password =
mysql_db =

torrents = torrents
files_users = xbt_files_users
users_main = users_main
users_freeleeches = users_freeleeches
client_whitelist = xbt_client_whitelist
snatched = xbt_snatched

# The passwords must be 32 characters and match the Gazelle config
report_password = 00000000000000000000000000000000
site_password = 00000000000000000000000000000000
Expand Down