bind SOCKS5 connection to correct outgoing interface

This commit is contained in:
arvidn 2020-01-13 19:18:18 +01:00 committed by Arvid Norberg
parent 95df88e7a3
commit 571952fd19
4 changed files with 32 additions and 1 deletions

View File

@ -370,6 +370,8 @@ void bind_alert()
.value("partfile_write", operation_t::partfile_write) .value("partfile_write", operation_t::partfile_write)
.value("hostname_lookup", operation_t::hostname_lookup) .value("hostname_lookup", operation_t::hostname_lookup)
.value("symlink", operation_t::symlink) .value("symlink", operation_t::symlink)
.value("handshake", operation_t::handshake)
.value("sock_option", operation_t::sock_option)
; ;
def("operation_name", static_cast<char const*(*)(operation_t)>(&lt::operation_name)); def("operation_name", static_cast<char const*(*)(operation_t)>(&lt::operation_name));

View File

@ -169,6 +169,9 @@ namespace libtorrent {
// handshake with a peer or server // handshake with a peer or server
handshake, handshake,
// set socket option
sock_option,
}; };
// maps an operation id (from peer_error_alert and peer_disconnected_alert) // maps an operation id (from peer_error_alert and peer_disconnected_alert)

View File

@ -896,6 +896,7 @@ namespace {
case o::hostname_lookup: return -1; case o::hostname_lookup: return -1;
case o::symlink: return -1; case o::symlink: return -1;
case o::handshake: return -1; case o::handshake: return -1;
case o::sock_option: return -1;
} }
return -1; return -1;
} }
@ -1562,7 +1563,8 @@ namespace {
"partfile_write", "partfile_write",
"hostname_lookup", "hostname_lookup",
"symlink", "symlink",
"handshake" "handshake",
"sock_option"
}; };
int const idx = static_cast<int>(op); int const idx = static_cast<int>(op);

View File

@ -556,9 +556,33 @@ void socks5::on_name_lookup(error_code const& e, tcp::resolver::iterator i)
error_code ec; error_code ec;
m_socks5_sock.open(is_v4(m_proxy_addr) ? tcp::v4() : tcp::v6(), ec); m_socks5_sock.open(is_v4(m_proxy_addr) ? tcp::v4() : tcp::v6(), ec);
if (ec)
{
if (m_alerts.should_post<socks5_alert>())
m_alerts.emplace_alert<socks5_alert>(m_proxy_addr, operation_t::sock_open, ec);
return;
}
// enable keepalives // enable keepalives
m_socks5_sock.set_option(boost::asio::socket_base::keep_alive(true), ec); m_socks5_sock.set_option(boost::asio::socket_base::keep_alive(true), ec);
if (ec)
{
if (m_alerts.should_post<socks5_alert>())
m_alerts.emplace_alert<socks5_alert>(m_proxy_addr, operation_t::sock_option, ec);
return;
}
tcp::endpoint const bind_ep(m_listen_socket.get_local_endpoint().address(), 0);
m_socks5_sock.bind(bind_ep, ec);
if (ec)
{
if (m_alerts.should_post<socks5_alert>())
m_alerts.emplace_alert<socks5_alert>(m_proxy_addr, operation_t::sock_bind, ec);
return;
}
// TODO: perhaps an attempt should be made to bind m_socks5_sock to the
// device of m_listen_socket
ADD_OUTSTANDING_ASYNC("socks5::on_connected"); ADD_OUTSTANDING_ASYNC("socks5::on_connected");
m_socks5_sock.async_connect(m_proxy_addr m_socks5_sock.async_connect(m_proxy_addr