Skip to content

Commit

Permalink
Merge pull request #7 from liut/feature/w42-options
Browse files Browse the repository at this point in the history
+ option
  • Loading branch information
liut authored Nov 11, 2021
2 parents defbc0e + c36018d commit dfa2680
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 135 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ CompactNamespaces: True
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: True
IncludeBlocks: Preserve
IndentAccessModifiers: False

PenaltyReturnTypeOnItsOwnLine: 36

PointerAlignment: Left
86 changes: 7 additions & 79 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

#include <boost/asio/signal_set.hpp>

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <libtorrent/config.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/version.hpp>
Expand All @@ -18,106 +15,37 @@ namespace po = boost::program_options;
#include "handlers.hpp"
#include "listener.hpp"
#include "log.hpp"
#include "option.hpp"
#include "sheath.hpp"
#include "util.hpp"

#include "plog/Initializers/RollingFileInitializer.h"

using namespace btd;

std::string mapper(std::string env_var)
{
// ensure the env_var is all caps
std::transform(env_var.begin(), env_var.end(), env_var.begin(), ::toupper);

if (env_var == ENV_PEERID_PREFIX || env_var == "TR_PEERID_PREFIX") return "peer-id";
if (env_var == ENV_BOOTSTRAP_NODES) return "dht-bootstrap-nodes";
if (env_var == ENV_MOVED_ROOT) return "moved-root";
if (env_var == ENV_STORE_ROOT) return "store-root";
if (env_var == ENV_WEBUI_ROOT) return "webui-root";
return "";
}

int main(int argc, char* argv[])
{
const std::string logMain(getLogsDir()+"/kedge-main.log");
const std::string logAlert(getLogsDir()+"/kedge-alert.log");
plog::init(plog::debug, logMain.c_str(), 1024*1024*32, 2); // Initialize the default logger instance.
plog::init<AlertLog>(plog::debug, logAlert.c_str(), 1024*1024*64, 2); // Initialize the 2nd logger instance.

std::string peerID = "";
std::string listens = "";
std::string movedRoot = "";
std::string storeRoot = "";
std::string webuiRoot = "";

po::options_description config("configuration");
config.add_options()
("help,h", "print usage message")
("listens,l", po::value<std::string>(&listens)->default_value("0.0.0.0:6881"), "listen_interfaces")
("moved-root", po::value<std::string>(&movedRoot), "moved root, env: " ENV_MOVED_ROOT)
("store-root,d", po::value<std::string>(&storeRoot)->default_value(getStoreDir()), "store root, env: " ENV_STORE_ROOT)
("webui-root", po::value<std::string>(&webuiRoot)->default_value(getWebUI()), "web UI root, env: " ENV_WEBUI_ROOT)
("peer-id", po::value<std::string>(&peerID)->default_value("-LT-"), "set prefix of fingerprint, env: " ENV_PEERID_PREFIX)
("dht-bootstrap-nodes", po::value<std::string>()->default_value("dht.transmissionbt.com:6881"), "a comma-separated list of Host port-pairs. env: " ENV_BOOTSTRAP_NODES)
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config), vm);
po::store(po::parse_environment(config, boost::function1<std::string, std::string>(mapper)), vm);
notify(vm);

if (vm.count("help")) {
std::cout << PROJECT_NAME << " " << PROJECT_VER << "\n";
std::cout << " Flags and " << config << "\n";
return 0;
}

using lt::session_handle;
using lt::settings_pack;

auto conf_dir = getConfDir();
if (! prepare_dirs(conf_dir))
option opt;
if (!opt.init_from(argc, argv))
{
return EXIT_FAILURE;
}
lt::session_params params;
load_sess_params(conf_dir, params);

// std::uint16_t peerPort = parse_port(listens);
if (vm.count("listens"))
{
LOG_DEBUG << "set listens " << listens << '|' << vm["listens"].as<std::string>();
params.settings.set_str(settings_pack::listen_interfaces, vm["listens"].as<std::string>());
}
if (vm.count("peer-id"))
{
LOG_DEBUG << "set peerID " << peerID;
params.settings.set_str(settings_pack::peer_fingerprint, peerID);
}
if (vm.count("moved-root"))
{
LOG_DEBUG << "set moved root " << movedRoot;
}
if (vm.count("store-root"))
{
LOG_DEBUG << "set store root " << storeRoot;
}
if (vm.count("dht-bootstrap-nodes"))
{
auto nodes = vm["dht-bootstrap-nodes"].as<std::string>();
LOG_DEBUG << "set dht-bootstrap-nodes " << nodes;
params.settings.set_str(settings_pack::dht_bootstrap_nodes, nodes);
}
const auto ses = std::make_shared<lt::session>(std::move(params));
lt::session_params params;
const auto ses = std::make_shared<lt::session>(std::move(opt.params));

const auto ctx = std::make_shared<sheath>(ses, storeRoot, movedRoot);
const auto ctx = std::make_shared<sheath>(ses, opt.storeRoot, opt.movedRoot);

std::thread ctx_start_loader([&ctx] {
ctx->start();
});

const auto caller = std::make_shared<httpCaller>(ctx, webuiRoot);
const auto caller = std::make_shared<httpCaller>(ctx, opt.webuiRoot);

// main: web server

Expand Down
161 changes: 161 additions & 0 deletions src/option.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#pragma once

#include <string>

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <libtorrent/config.hpp>
#include <libtorrent/session.hpp>

#include "const.hpp"
#include "util.hpp"

namespace btd {

void
load_sess_params(std::string const& cd, lt::session_params& params);

struct option {

std::string peerID = "";
std::string listens = "";
std::string movedRoot = "";
std::string storeRoot = "";
std::string webuiRoot = "";

lt::session_params params;

bool
init_from(int argc, char* argv[]);
};

std::string mapper(std::string env_var)
{
// ensure the env_var is all caps
std::transform(env_var.begin(), env_var.end(), env_var.begin(), ::toupper);

if (env_var == ENV_PEERID_PREFIX || env_var == "TR_PEERID_PREFIX") return "peer-id";
if (env_var == ENV_BOOTSTRAP_NODES) return "dht-bootstrap-nodes";
if (env_var == ENV_MOVED_ROOT) return "moved-root";
if (env_var == ENV_STORE_ROOT) return "store-root";
if (env_var == ENV_WEBUI_ROOT) return "webui-root";
return "";
}

bool
option::init_from(int argc, char* argv[])
{
po::options_description config("configuration");
config.add_options()
("help,h", "print usage message")
("listens,l", po::value<std::string>(&listens)->default_value("0.0.0.0:6881"), "listen_interfaces")
("moved-root", po::value<std::string>(&movedRoot), "moved root, env: " ENV_MOVED_ROOT)
("store-root,d", po::value<std::string>(&storeRoot)->default_value(getStoreDir()), "store root, env: " ENV_STORE_ROOT)
("webui-root", po::value<std::string>(&webuiRoot)->default_value(getWebUI()), "web UI root, env: " ENV_WEBUI_ROOT)
("peer-id", po::value<std::string>(&peerID)->default_value("-LT-"), "set prefix of fingerprint, env: " ENV_PEERID_PREFIX)
("dht-bootstrap-nodes", po::value<std::string>()->default_value("dht.transmissionbt.com:6881"), "a comma-separated list of Host port-pairs. env: " ENV_BOOTSTRAP_NODES)
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config), vm);
po::store(po::parse_environment(config, boost::function1<std::string, std::string>(mapper)), vm);
notify(vm);

if (vm.count("help")) {
std::cout << PROJECT_NAME << " " << PROJECT_VER << "\n";
std::cout << " Flags and " << config << "\n";
return false;
}

auto conf_dir = getConfDir();
if (!prepare_dirs(conf_dir)) return false;

load_sess_params(conf_dir, params);

using lt::session_handle;
using lt::settings_pack;

// std::uint16_t peerPort = parse_port(listens);
if (vm.count("listens"))
{
LOG_DEBUG << "set listens " << listens << '|' << vm["listens"].as<std::string>();
params.settings.set_str(settings_pack::listen_interfaces, vm["listens"].as<std::string>());
}
if (vm.count("peer-id"))
{
LOG_DEBUG << "set peerID " << peerID;
params.settings.set_str(settings_pack::peer_fingerprint, peerID);
}
if (vm.count("moved-root"))
{
LOG_DEBUG << "set moved root " << movedRoot;
}
if (vm.count("store-root"))
{
LOG_DEBUG << "set store root " << storeRoot;
}
if (vm.count("dht-bootstrap-nodes"))
{
auto nodes = vm["dht-bootstrap-nodes"].as<std::string>();
LOG_DEBUG << "set dht-bootstrap-nodes " << nodes;
params.settings.set_str(settings_pack::dht_bootstrap_nodes, nodes);
}

return true;
}

void
load_sess_params(std::string const& cd, lt::session_params& params)
{
using lt::settings_pack;

std::vector<char> in;
if (load_file(path_cat(cd, SESS_FILE), in))
{
lt::error_code ec;
lt::bdecode_node e = lt::bdecode(in, ec);
lt::session::save_state_flags_t sft = lt::session::save_settings;
#ifndef TORRENT_DISABLE_DHT
params.dht_settings.privacy_lookups = true;
sft |= lt::session::save_dht_state;
#endif

if (!ec) params = read_session_params(e, sft);
}

auto& settings = params.settings;
settings.set_int(settings_pack::cache_size, -1);
settings.set_int(settings_pack::choking_algorithm, settings_pack::rate_based_choker);

// if (!settings.has_val(settings_pack::user_agent) || settings.get_str(settings_pack::user_agent) == "" )
// {
settings.set_str(settings_pack::user_agent, PROJECT_NAME "/" PROJECT_VER " lt/" LIBTORRENT_VERSION);
// }

settings.set_int(settings_pack::alert_mask
, lt::alert_category::error
| lt::alert_category::peer
| lt::alert_category::port_mapping
| lt::alert_category::storage
| lt::alert_category::tracker
| lt::alert_category::connect
| lt::alert_category::status
| lt::alert_category::ip_block
| lt::alert_category::performance_warning
| lt::alert_category::dht
// | lt::alert_category::session_log
// | lt::alert_category::torrent_log
| lt::alert_category::incoming_request
| lt::alert_category::dht_operation
| lt::alert_category::port_mapping_log
| lt::alert_category::file_progress);

settings.set_bool(settings_pack::enable_upnp, false);
settings.set_bool(settings_pack::enable_natpmp, false);
settings.set_bool(settings_pack::enable_dht, false);
settings.set_bool(settings_pack::enable_lsd, false);
settings.set_bool(settings_pack::validate_https_trackers, false);
}

} // namespace btd
53 changes: 0 additions & 53 deletions src/sheath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,6 @@ parse_endpoint(lt::tcp::endpoint & ep, std::string addr)
return false;
}

void
load_sess_params(std::string const& cd, lt::session_params& params)
{
using lt::settings_pack;

std::vector<char> in;
if (load_file(path_cat(cd, SESS_FILE), in))
{
lt::error_code ec;
lt::bdecode_node e = lt::bdecode(in, ec);
lt::session::save_state_flags_t sft = lt::session::save_settings;
#ifndef TORRENT_DISABLE_DHT
params.dht_settings.privacy_lookups = true;
sft |= lt::session::save_dht_state;
#endif

if (!ec) params = read_session_params(e, sft);
}

auto& settings = params.settings;
settings.set_int(settings_pack::cache_size, -1);
settings.set_int(settings_pack::choking_algorithm, settings_pack::rate_based_choker);

// if (!settings.has_val(settings_pack::user_agent) || settings.get_str(settings_pack::user_agent) == "" )
// {
settings.set_str(settings_pack::user_agent, PROJECT_NAME "/" PROJECT_VER " lt/" LIBTORRENT_VERSION);
// }

settings.set_int(settings_pack::alert_mask
, lt::alert_category::error
| lt::alert_category::peer
| lt::alert_category::port_mapping
| lt::alert_category::storage
| lt::alert_category::tracker
| lt::alert_category::connect
| lt::alert_category::status
| lt::alert_category::ip_block
| lt::alert_category::performance_warning
| lt::alert_category::dht
// | lt::alert_category::session_log
// | lt::alert_category::torrent_log
| lt::alert_category::incoming_request
| lt::alert_category::dht_operation
| lt::alert_category::port_mapping_log
| lt::alert_category::file_progress);

settings.set_bool(settings_pack::enable_upnp, false);
settings.set_bool(settings_pack::enable_natpmp, false);
settings.set_bool(settings_pack::enable_dht, false);
settings.set_bool(settings_pack::enable_lsd, false);
settings.set_bool(settings_pack::validate_https_trackers, false);
}


json::object
torrent_status_to_json_obj(lt::torrent_status const& st)
Expand Down
3 changes: 0 additions & 3 deletions src/sheath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ namespace json = boost::json;
namespace btd {
namespace fs = std::filesystem;

void
load_sess_params(std::string const& cd, lt::session_params& params);

json::object
torrent_status_to_json_obj(lt::torrent_status const& st);

Expand Down

0 comments on commit dfa2680

Please sign in to comment.