Skip to content

Commit

Permalink
Added proxy authentication support
Browse files Browse the repository at this point in the history
Also enabled C++11 for static map initialization
  • Loading branch information
PlasmaPower committed Jan 10, 2016
1 parent 879309e commit b16ad04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin_PROGRAMS = btfs
btfs_SOURCES = btfs.cc btfs.h
btfs_CPPFLAGS = -Wall $(FUSE_CFLAGS) $(LIBTORRENT_CFLAGS) $(LIBCURL_CFLAGS)
btfs_CPPFLAGS = -Wall -std=c++11 $(FUSE_CFLAGS) $(LIBTORRENT_CFLAGS) $(LIBCURL_CFLAGS)
btfs_LDADD = $(FUSE_LIBS) $(LIBTORRENT_LIBS) $(LIBCURL_LIBS)
62 changes: 37 additions & 25 deletions src/btfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,19 @@ btfs_init(struct fuse_conn_info *conn) {
params.proxy_type = "socks5h";
}
bool found_proxy_type = false;
libtorrent::proxy_settings::proxy_type libtorrent_proxy_type;
for (unsigned int i = 0; i < sizeof(proxy_types) / sizeof(proxy_types[0]); i++) {
proxy_type type = proxy_types[i];
if (type.libcurl_name == params.proxy_type) {
found_proxy_type = true;
libtorrent_proxy_type = type.libtorrent_type;
break;
}
}
if (!found_proxy_type) {
libtorrent::proxy_settings::proxy_type libtorrent_proxy_type = libtorrent_proxy_types[params.proxy_type];
if (libtorrent_proxy_type == libtorrent::proxy_settings::none) { // None is the default value
fprintf(stderr, "Unkown proxy type");
exit(1);
}
if (params.proxy_username != NULL && params.proxy_password != NULL) {
libtorrent::proxy_settings::proxy_type new_type = libtorrent_authed_proxy_types[libtorrent_proxy_type];
if (new_type != libtorrent::proxy_settings::none) {
libtorrent_proxy_type = new_type;
}
}

libtorrent::proxy_settings proxy = libtorrent::proxy_settings();
libtorrent::proxy_settings proxy = session->proxy();
std::string proxyString = std::string(params.proxy);
int index = proxyString.find(':');
if (index != std::string::npos) {
Expand All @@ -498,6 +496,12 @@ btfs_init(struct fuse_conn_info *conn) {
proxy.hostname = params.proxy;
proxy.port = 1080;
}
if (params.proxy_username) {
proxy.username = params.proxy_username;
}
if (params.proxy_password) {
proxy.password = params.proxy_password;
}
proxy.type = libtorrent_proxy_type;
session->set_proxy(proxy);
}
Expand Down Expand Up @@ -598,12 +602,18 @@ populate_metadata(libtorrent::add_torrent_params& p, const char *arg) {
curl_easy_setopt(ch, CURLOPT_WRITEDATA, (void *) &output);
curl_easy_setopt(ch, CURLOPT_USERAGENT, "btfs/" VERSION);
curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 1);
if (params.proxy_type == NULL) {
params.proxy_type = "socks5h";
}
if (params.proxy != NULL) {
curl_easy_setopt(ch, CURLOPT_PROXY, params.proxy);
if (params.proxy_type == NULL) {
params.proxy_type = "socks5h";
}
curl_easy_setopt(ch, CURLOPT_PROXYTYPE, params.proxy_type);
if (params.proxy_username != NULL) {
curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, params.proxy_username);
}
if (params.proxy_password != NULL) {
curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, params.proxy_password);
}
}

CURLcode res = curl_easy_perform(ch);
Expand Down Expand Up @@ -659,17 +669,19 @@ populate_metadata(libtorrent::add_torrent_params& p, const char *arg) {
#define BTFS_OPT(t, p, v) { t, offsetof(struct btfs_params, p), v }

static const struct fuse_opt btfs_opts[] = {
BTFS_OPT("-v", version, 1),
BTFS_OPT("--version", version, 1),
BTFS_OPT("-h", help, 1),
BTFS_OPT("--help", help, 1),
BTFS_OPT("-b", browse_only, 1),
BTFS_OPT("--browse-only", browse_only, 1),
BTFS_OPT("-k", keep, 1),
BTFS_OPT("-p=%s", proxy, 4),
BTFS_OPT("-proxy=%s", proxy, 4),
BTFS_OPT("--proxy-type=%s", proxy_type, 4),
BTFS_OPT("--keep", keep, 1),
BTFS_OPT("-v", version, 1),
BTFS_OPT("--version", version, 1),
BTFS_OPT("-h", help, 1),
BTFS_OPT("--help", help, 1),
BTFS_OPT("-b", browse_only, 1),
BTFS_OPT("--browse-only", browse_only, 1),
BTFS_OPT("-k", keep, 1),
BTFS_OPT("--keep", keep, 1),
BTFS_OPT("-p=%s", proxy, 4),
BTFS_OPT("-proxy=%s", proxy, 4),
BTFS_OPT("--proxy-type=%s", proxy_type, 4),
BTFS_OPT("--proxy-username=%s", proxy_username, 4),
BTFS_OPT("--proxy-password=%s", proxy_password, 4),
FUSE_OPT_END
};

Expand Down
15 changes: 9 additions & 6 deletions src/btfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,27 @@ enum {
KEY_HELP,
};

struct proxy_type {
const char *libcurl_name;
libtorrent::proxy_settings::proxy_type libtorrent_type;
};

proxy_type proxy_types[] = {
std::map<std::string, libtorrent::proxy_settings::proxy_type> libtorrent_proxy_types = {
{"socks4", libtorrent::proxy_settings::socks4},
{"socks5h", libtorrent::proxy_settings::socks5},
{"socks5", libtorrent::proxy_settings::socks5},
{"http", libtorrent::proxy_settings::http}
};

std::map<libtorrent::proxy_settings::proxy_type, libtorrent::proxy_settings::proxy_type> libtorrent_authed_proxy_types = {
{libtorrent::proxy_settings::socks5, libtorrent::proxy_settings::socks5_pw},
{libtorrent::proxy_settings::http, libtorrent::proxy_settings::http_pw}
};

struct btfs_params {
int version;
int help;
int browse_only;
int keep;
const char *proxy;
const char *proxy_type;
const char *proxy_username;
const char *proxy_password;
const char *metadata;
};

Expand Down

0 comments on commit b16ad04

Please sign in to comment.