|
|
@ -7,6 +7,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "DCC.h" |
|
|
|
#include "DCC.h" |
|
|
|
#include "IRCBase.h" |
|
|
|
#include "IRCBase.h" |
|
|
|
|
|
|
|
#include "NATUtils/PortMapping.h" |
|
|
|
|
|
|
|
#include "NATUtils/PublicAddress.h" |
|
|
|
|
|
|
|
#include "Utilities.h" |
|
|
|
#include <fmt/format.h> |
|
|
|
#include <fmt/format.h> |
|
|
|
#include <iostream> |
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
|
|
|
@ -89,24 +92,46 @@ struct DCCPriv |
|
|
|
if (!ec) return; // No errors.
|
|
|
|
if (!ec) return; // No errors.
|
|
|
|
std::cerr << fmt::format("'- DCC write error: {} ({})", SystemErrorToIRCError(ec), ec.message()) << std::endl; |
|
|
|
std::cerr << fmt::format("'- DCC write error: {} ({})", SystemErrorToIRCError(ec), ec.message()) << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void startListener() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
acceptor.open(endpoint.protocol()); |
|
|
|
|
|
|
|
acceptor.set_option(tcp::acceptor::reuse_address(true)); |
|
|
|
|
|
|
|
acceptor.bind(endpoint); |
|
|
|
|
|
|
|
acceptor.listen(1); |
|
|
|
|
|
|
|
acceptor.async_accept(socket, [this](const asio::error_code& ec){ |
|
|
|
|
|
|
|
if (ec == asio::error::operation_aborted) |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ec) |
|
|
|
|
|
|
|
disconnected(ec); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
connected(ec); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
DCC::DCC(IRCBase& ircctx, asio::io_context& ioctx) |
|
|
|
DCC::DCC(IRCBase& ircctx, asio::io_context& ioctx) |
|
|
|
: mp(std::make_unique<DCCPriv>(*this, ircctx, Direction::Initiator, ioctx, "", "0")) |
|
|
|
: mp(std::make_unique<DCCPriv>(*this, ircctx, Direction::Initiator, ioctx, "", "0")) |
|
|
|
{ |
|
|
|
{ |
|
|
|
mp->acceptor.open(mp->endpoint.protocol()); |
|
|
|
auto ipAddress = findOwnIpAddress(); |
|
|
|
mp->acceptor.set_option(tcp::acceptor::reuse_address(true)); |
|
|
|
|
|
|
|
mp->acceptor.bind(mp->endpoint); |
|
|
|
mp->startListener(); // also acquires a port for us
|
|
|
|
mp->acceptor.listen(1); |
|
|
|
|
|
|
|
mp->acceptor.async_accept(mp->socket, [this](const asio::error_code& ec){ |
|
|
|
if (isPrivateIp(ipAddress) && NATPortMapping::add(ipAddress, port(), port())) { |
|
|
|
if (ec == asio::error::operation_aborted) |
|
|
|
auto publicIp = NATPublicAddress::get(); |
|
|
|
return; |
|
|
|
if (publicIp.has_value()) { |
|
|
|
|
|
|
|
ipAddress = longIpToNormal(*publicIp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
NATPortMapping::remove(ipAddress, port(), port()); |
|
|
|
|
|
|
|
// Reset internal state, effectively stopping the listening.
|
|
|
|
|
|
|
|
// This DCC instance will be destroyed and replaced when we get a response with the port to use in reverse.
|
|
|
|
|
|
|
|
mp = std::make_unique<DCCPriv>(*this, ircctx, Direction::Initiator, ioctx, "", "0"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (ec) |
|
|
|
mp->ip = ipAddress; |
|
|
|
mp->disconnected(ec); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
mp->connected(ec); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
DCC::DCC(IRCBase& ircctx, asio::io_context& ioctx, const std::string& ip, const std::string& port) |
|
|
|
DCC::DCC(IRCBase& ircctx, asio::io_context& ioctx, const std::string& ip, const std::string& port) |
|
|
@ -127,23 +152,50 @@ bool DCC::isPending() const |
|
|
|
return mp->pending; |
|
|
|
return mp->pending; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IRCError DCC::accept() |
|
|
|
IRCError DCC::accept(const std::string& targetNickname) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mp->direction == Direction::Initiator) |
|
|
|
if (mp->direction == Direction::Initiator) |
|
|
|
return IRCError::DCC_NotATarget; // Only usable if we are a Target...
|
|
|
|
return IRCError::DCC_NotATarget; // Only usable if we are a Target...
|
|
|
|
|
|
|
|
|
|
|
|
mp->resolverResults = mp->resolver.resolve(mp->ip, mp->port); |
|
|
|
auto acceptNormally = [this] { |
|
|
|
|
|
|
|
mp->resolverResults = mp->resolver.resolve(mp->ip, mp->port); |
|
|
|
asio::async_connect(mp->socket, mp->resolverResults, |
|
|
|
|
|
|
|
[this](const asio::error_code& ec, const tcp::endpoint&) { |
|
|
|
asio::async_connect(mp->socket, mp->resolverResults, |
|
|
|
if (ec) |
|
|
|
[this](const asio::error_code& ec, const tcp::endpoint&) { |
|
|
|
mp->disconnected(ec); |
|
|
|
if (ec) |
|
|
|
|
|
|
|
mp->disconnected(ec); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
mp->connected(ec); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mp->pending = false; |
|
|
|
|
|
|
|
return IRCError::NoError; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto acceptReversed = [this, &targetNickname] { |
|
|
|
|
|
|
|
auto ipAddress = findOwnIpAddress(); |
|
|
|
|
|
|
|
mp->startListener(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isPrivateIp(ipAddress) && NATPortMapping::add(ipAddress, port(), port())) { |
|
|
|
|
|
|
|
auto publicIp = NATPublicAddress::get(); |
|
|
|
|
|
|
|
if (publicIp.has_value()) |
|
|
|
|
|
|
|
ipAddress = longIpToNormal(*publicIp); |
|
|
|
else |
|
|
|
else |
|
|
|
mp->connected(ec); |
|
|
|
NATPortMapping::remove(ipAddress, port(), port()); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mp->ircctx.ctcpRequest(targetNickname, "DCC", fmt::format("CHAT chat {} {}", |
|
|
|
|
|
|
|
normalIpToLong(ipAddress), |
|
|
|
|
|
|
|
port())); |
|
|
|
|
|
|
|
|
|
|
|
mp->pending = false; |
|
|
|
mp->pending = false; |
|
|
|
return IRCError::NoError; |
|
|
|
return IRCError::NoError; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mp->port == "0") |
|
|
|
|
|
|
|
return acceptReversed(); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return acceptNormally(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DCC::disconnect() |
|
|
|
void DCC::disconnect() |
|
|
@ -203,6 +255,11 @@ const std::string& DCC::ip() const |
|
|
|
return mp->ip; |
|
|
|
return mp->ip; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool DCC::isReversed() const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return mp->port == "0"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DCC::setCallbackRead(DCC::CallbackRead&& cb) |
|
|
|
void DCC::setCallbackRead(DCC::CallbackRead&& cb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
mp->cbRead = std::move(cb); |
|
|
|
mp->cbRead = std::move(cb); |
|
|
|