简体   繁体   中英

How to apply a proxy in libtorrent?

I am creating a torrent client and would like to add a proxy to it. I just can't seem to get it to work.i am using " http://ipmagnet.services.cbcdn.com " to check what IP address my client is handing out to peers and trackers.How can I fix my code to apply my proxy to the client correctly?

import libtorrent as lt
import time
import os

ses = lt.session()
ses.listen_on(6881, 6891)
r = lt.proxy_settings()
r.proxy_hostnames = True
r.proxy_peer_connections = True
r.hostname = "*myproxyinfo*"
r.username = "*myproxyinfo*"
r.password = "*myproxyinfo*"
r.proxy_port = 1080
r.proxy_type = lt.proxy_type().socks5_pw
#print lt.proxy_type().socks5_pw
ses.set_dht_proxy(r)
ses.set_peer_proxy(r)
ses.set_tracker_proxy(r)
ses.set_web_seed_proxy(r)
ses.set_proxy(r)
t = ses.settings()
t.force_proxy = True
t.proxy_hostnames = True
t.proxy_peer_connections = True
t.proxy_tracker_connections = True
#t.anonymous_mode = True
#ses.set_settings(t)
#print ses.get_settings()
ses.dht_proxy()
ses.peer_proxy()
ses.tracker_proxy()
ses.web_seed_proxy()
ses.proxy()
ses.set_settings(t)


magnet_link = "magnet:?xt=urn:btih:1931ced5c4e20047091742905f30f8d0b69c9ca9&dn=ipMagnet+Tracking+Link&tr=http%3A%2F%2Fipmagnet.services.cbcdn.com%3A80%2F"

params = {"save_path": os.getcwd() + r"\torrents",
               "storage_mode": lt.storage_mode_t.storage_mode_sparse,
              "url": magnet_link}
h = ses.add_torrent(params)

s = h.status()
while (not s.is_seeding):
        s = h.status()

        state_str = ['queued', 'checking', 'downloading metadata', \
            'downloading', 'finished', 'seeding', 'allocating']
        print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d)         %s' % \
            (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
            s.num_peers, state_str[s.state])

    time.sleep(1)

I would suggest you look at alerts generated by your session, to see if anything is failing to use the proxy.

By default, libtorrent considers the proxy to be best-effort. If it fails for any reason, libtorrent will fall back to attempting direct connections.

If you would like to force using the proxy, and fail if the proxy fails, set the force_proxy setting to true.

Took me hours to figure out this, this worked for me

ses = lt.session()
r = lt.proxy_settings()
r.hostname = "*myproxyinfo*"
r.username = "*myproxyinfo*"
r.password = "*myproxyinfo*"
r.port = 1080
r.type = lt.proxy_type_t.socks5_pw
ses.set_peer_proxy(r)
ses.set_web_seed_proxy(r)
ses.set_proxy(r)
t = ses.settings()
t.force_proxy = True
t.proxy_peer_connections = True
t.anonymous_mode = True
ses.set_settings(t)
print(ses.get_settings())
ses.peer_proxy()
ses.web_seed_proxy()
ses.set_settings(t)

I changed r.proxy_port to r.port and r.proxy_type = lt.proxy_type().socks5_pw to r.type = lt.proxy_type_t.socks5_pw

I have confirmed this to work using ipmagnet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM