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 proxy support #9

Open
wants to merge 8 commits 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
2 changes: 1 addition & 1 deletion scripts/btplay
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if not cmd:

mountpoint = tempfile.mkdtemp(prefix="btplay-")

ret = subprocess.call(("btfs", args.URI, mountpoint, ))
ret = subprocess.call(["btfs", args.URI, mountpoint, ] + sys.argv[1:])

def find_files(p):
for dirpath, dnames, fnames in os.walk(p):
Expand Down
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the c++11 thing. Might break support for older distros.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using it for static map initialization. If the older distros are important, I'm pretty sure I can do that differently.

btfs_LDADD = $(FUSE_LIBS) $(LIBTORRENT_LIBS) $(LIBCURL_LIBS)
101 changes: 93 additions & 8 deletions src/btfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,51 @@ btfs_init(struct fuse_conn_info *conn) {
se.announce_to_all_trackers = true;
se.announce_to_all_tiers = true;

if (params.proxy_hostname == NULL && params.proxy_type != NULL && strcmp(params.proxy_type, "i2p") == 0) {
params.proxy_hostname = "127.0.0.1";
params.proxy_port = 7656;
}

if (params.proxy_hostname != NULL && params.proxy_port == 0) {
params.proxy_port = 1080;
}

if (params.proxy_hostname != NULL) {
se.force_proxy = true;
if (params.proxy_type == NULL) {
params.proxy_type = "socks5h";
}
libtorrent::proxy_settings::proxy_type libtorrent_proxy_type = libtorrent_proxy_types[params.proxy_type];
if (strcmp(params.proxy_type, "i2p") != 0) {
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 = session->proxy();
proxy.hostname = params.proxy_hostname;
proxy.port = params.proxy_port;
if (params.proxy_username) {
proxy.username = params.proxy_username;
}
if (params.proxy_password) {
proxy.password = params.proxy_password;
}
if (strcmp(params.proxy_type, "i2p") == 0) {
session->set_i2p_proxy(proxy);
} else {
proxy.type = libtorrent_proxy_type;
session->set_proxy(proxy);
}
}

session->set_settings(se);
session->async_add_torrent(*p);

Expand Down Expand Up @@ -604,6 +649,31 @@ 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 && strcmp(params.proxy_type, "i2p") == 0) {
if (params.i2p_http_proxy) {
curl_easy_setopt(ch, CURLOPT_PROXY, params.i2p_http_proxy);
} else {
curl_easy_setopt(ch, CURLOPT_PROXY, "127.0.0.1:4444");
}
curl_easy_setopt(ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
} else if (params.proxy_hostname != NULL) {
if (params.proxy_type == NULL) {
params.proxy_type = "socks5h";
}
std::string proxy_string = "";
proxy_string += params.proxy_type;
proxy_string += "://";
proxy_string += params.proxy_hostname;
proxy_string += ":";
proxy_string += params.proxy_port;
curl_easy_setopt(ch, CURLOPT_PROXY, proxy_string.c_str());
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 @@ -658,14 +728,20 @@ 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("--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("--proxy-hostname=%s", proxy_hostname, 4),
BTFS_OPT("--proxy-port=%u", proxy_port, 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),
BTFS_OPT("--i2p-http-proxy=%s", i2p_http_proxy, 4),
FUSE_OPT_END
};

Expand Down Expand Up @@ -727,6 +803,15 @@ main(int argc, char *argv[]) {
printf(" --help -h show this message\n");
printf(" --browse-only -b download metadata only\n");
printf(" --keep -k keep files after unmount\n");
printf(" --proxy= -p= use a proxy with the given address\n");
printf(" should be in the form of host or host:port\n");
printf(" --proxy-type= set the type of proxy (defaults to socks5h)\n");
printf(" --proxy-username= login to the proxy with the given username\n");
printf(" --proxy-password= login to the proxy with the given password\n");
printf(" As a process argument, this is readable\n");
printf(" by any user by looking at a list of processes\n");
printf(" --i2p-http-proxy= provide an http proxy for use by curl in place of i2p\n");
printf(" Only used if the proxy type is set to i2p\n");
printf("\n");

// Let FUSE print more help
Expand Down
19 changes: 19 additions & 0 deletions src/btfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with BTFS. If not, see <http://www.gnu.org/licenses/>.
#define BTFS_H

#include <libtorrent/peer_request.hpp>
#include <libtorrent/session_settings.hpp>

namespace btfs
{
Expand Down Expand Up @@ -111,11 +112,29 @@ enum {
KEY_HELP,
};

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_hostname;
int proxy_port;
const char *proxy_type;
const char *proxy_username;
const char *proxy_password;
const char *i2p_http_proxy;
const char *metadata;
};

Expand Down