Skip to content

Commit

Permalink
Revert "feat: ip spoofing"
Browse files Browse the repository at this point in the history
This reverts commit 6542d7d.
  • Loading branch information
Bobscorn committed Mar 22, 2022
1 parent fe6ebea commit f3d2800
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 282 deletions.
14 changes: 0 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,11 @@ elseif(UNIX)
set(srcs ${srcs} "src/linux_socket.cpp" "src/linux_platform.cpp")
endif(WIN32)

option(PTOP_PRINT_DEBUG "Print extra debugging information to the standard output" OFF)
option(PTOP_SPOOF_IP "Insert fake ip addresses into the terminal instead of actual ones" OFF)

if(PTOP_PRINT_DEBUG)
add_compile_definitions(DATA_COUT=1)
endif()

if(PTOP_SPOOF_IP)
add_compile_definitions(PTOP_SPOOF_IP=1 PTOP_SPOOF_SERVER="111.111.111.111" PTOP_SPOOF_PUBLIC="123.456.789.101" PTOP_SPOOF_PRIVATE="10.0.0.1" PTOP_SPOOF_ME="1.0.0.1")
else()
add_compile_definitions(PTOP_SPOOF_SERVER="" PTOP_SPOOF_PUBLIC="" PTOP_SPOOF_PRIVATE="")
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)
add_executable(ptop ${PROJECT_SOURCE_DIR}/src/clientmain.cpp ${srcs})
add_executable(ptop_rendezvous ${PROJECT_SOURCE_DIR}/src/servermain.cpp ${srcs})


if(UNIX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
Expand Down
53 changes: 0 additions & 53 deletions CMakePresets.json

This file was deleted.

34 changes: 8 additions & 26 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,32 @@ std::unique_ptr<IDataSocketWrapper>& client_init_kit::get_server_socket() {

void client_init_kit::set_server_socket(std::unique_ptr<IDataSocketWrapper>&& input) {
_server_socket = std::move(input);

if(input == nullptr)
std::cout << "client_init_kit: connection socket set to nullptr mmk?" << std::endl;
}

client_peer_kit::client_peer_kit() {

}

void client_peer_kit::set_peer_data(client_init_kit& init_kit, const char* data, int next_data_index, MESSAGE_LENGTH_T data_len) {
public_info = read_peer_data(data, next_data_index, data_len);
private_info = read_peer_data(data, next_data_index, data_len);
#ifdef PTOP_SPOOF_IP
std::cout << "Target is: " << PTOP_SPOOF_PRIVATE << ":" << private_info.port << "/" << PTOP_SPOOF_PUBLIC << ":" << public_info.port << " priv/pub" << std::endl;
#else
std::cout << "Target is: " << private_info.ip_address << ":" << private_info.port << "/" << public_info.ip_address << ":" << public_info.port << " priv/pub" << std::endl;
#endif

old_privatename = init_kit.get_server_socket()->get_myname_raw();
std::cout << "Closing socket to server" << std::endl;
init_kit.set_server_socket(nullptr); //need to close the server socket HERE to maintain the same session in the peer sockets

std::cout << "Creating the listen socket..." << std::endl;
if (init_kit.protocol.is_udp())
listen_sock = std::make_unique<UDPListener>(old_privatename, init_kit.protocol, "HolePunch-Listen");
else
listen_sock = std::make_unique<NonBlockingListener>(old_privatename, init_kit.protocol, "HolePunch-Listen");

listen_sock->listen();

std::cout << "Creating Connectors to the public and private targets..." << std::endl;
public_connector = std::make_unique<NonBlockingConnector>(old_privatename, public_info.ip_address, public_info.port, init_kit.protocol, "HolePunch-Public", PTOP_SPOOF_PUBLIC);
private_connector = std::make_unique<NonBlockingConnector>(old_privatename, private_info.ip_address, private_info.port, init_kit.protocol, "HolePunch-Private", PTOP_SPOOF_PRIVATE);
public_connector = std::make_unique<NonBlockingConnector>(old_privatename, public_info.ip_address, public_info.port, init_kit.protocol, "HolePunch-Public");
private_connector = std::make_unique<NonBlockingConnector>(old_privatename, private_info.ip_address, private_info.port, init_kit.protocol, "HolePunch-Private");


peer_connect_start_time = std::chrono::system_clock::now();
Expand All @@ -79,7 +75,7 @@ EXECUTION_STATUS connect_public(client_init_kit& init_kit, client_peer_kit& peer
{
std::cout << "Public Connection Failed, Retrying connection..." << std::endl;
peer_kit.public_connector = nullptr;
peer_kit.public_connector = std::make_unique<NonBlockingConnector>(peer_kit.old_privatename, peer_kit.public_info.ip_address, peer_kit.public_info.port, init_kit.protocol, "HolePunch-Public", PTOP_SPOOF_PUBLIC);
peer_kit.public_connector = std::make_unique<NonBlockingConnector>(peer_kit.old_privatename, peer_kit.public_info.ip_address, peer_kit.public_info.port, init_kit.protocol, "HolePunch-Public");
}
return EXECUTION_STATUS::HOLE_PUNCH;
}
Expand All @@ -98,7 +94,7 @@ EXECUTION_STATUS connect_private(client_init_kit& init_kit, client_peer_kit& pee
{
std::cout << "Private Connection attempt failed, retrying..." << std::endl;
peer_kit.private_connector = nullptr;
peer_kit.private_connector = std::make_unique<NonBlockingConnector>(peer_kit.old_privatename, peer_kit.private_info.ip_address, peer_kit.private_info.port, init_kit.protocol, "HolePunch-Private", PTOP_SPOOF_PRIVATE);
peer_kit.private_connector = std::make_unique<NonBlockingConnector>(peer_kit.old_privatename, peer_kit.private_info.ip_address, peer_kit.private_info.port, init_kit.protocol, "HolePunch-Private");
}
return EXECUTION_STATUS::HOLE_PUNCH;
}
Expand Down Expand Up @@ -144,12 +140,6 @@ EXECUTION_STATUS hole_punch(client_init_kit& init_kit, client_peer_kit& peer_kit
return EXECUTION_STATUS::FAILED;
}

if (status == EXECUTION_STATUS::PEER_CONNECTED)
{
std::cout << "We have hole punched to the peer!" << std::endl;
std::cout << "Try sending a message with 'msg: [text]' or a file with 'file: [filename]'" << std::endl;
}

return status;
}
catch (const std::exception& e)
Expand Down Expand Up @@ -178,7 +168,6 @@ EXECUTION_STATUS process_server_data(client_init_kit& init_kit, client_peer_kit&
{
case MESSAGE_TYPE::CONNECT_TO_PEER:
{
std::cout << "Rendezvous server has responded, we are now attempting to hole punch..." << std::endl;
if (init_kit.do_delay)
{
std::cout << "Delaying hole punching by 5s..." << std::endl;
Expand Down Expand Up @@ -289,9 +278,7 @@ EXECUTION_STATUS process_peer_data(const Message& mess, const std::unique_ptr<ID

case MESSAGE_TYPE::NONE:
default:
#ifdef DATA_COUT
std::cout << __func__ << "(" << __LINE__ << "): Ignoring Message with Type: " << mt_to_string(msg_type) << std::endl;
#endif
return EXECUTION_STATUS::PEER_CONNECTED;
}
return EXECUTION_STATUS::PEER_CONNECTED;
Expand Down Expand Up @@ -333,11 +320,7 @@ bool do_user_input(thread_queue& message_queue, std::unique_lock<std::shared_mut
void client_loop(std::string server_address_pair, Protocol input_protocol)
{
std::cout << "Starting ptop!" << std::endl;
#ifdef PTOP_SPOOF_IP
std::cout << "Connecting to rendezvous server: " << PTOP_SPOOF_SERVER << std::endl;
#else
std::cout << "Connecting to rendezvous server: " << server_address_pair << std::endl;
#endif
client_init_kit init_kit{ server_address_pair, input_protocol };
client_peer_kit peer_kit{};
auto& connection_socket = init_kit.get_server_socket();
Expand All @@ -349,7 +332,6 @@ void client_loop(std::string server_address_pair, Protocol input_protocol)
std::unique_lock<std::shared_mutex> take_message_lock(message_queue.queue_mutex, std::defer_lock);

init_kit.status = EXECUTION_STATUS::RENDEZVOUS;
std::cout << "Connected to rendezvous server, now awaiting another peer..." << std::endl;

while (init_kit.status != EXECUTION_STATUS::COMPLETE && init_kit.status != EXECUTION_STATUS::FAILED) //listen at the start of protocol
{
Expand Down
36 changes: 13 additions & 23 deletions src/clientmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,26 @@

using namespace std::chrono;

int clientmain()
{
int main(int argc, char** argv) {

try
{
#if defined(WIN32) | defined(_WIN64) // windows_internet uses RAII to ensure WSAStartup and WSACleanup get called in the proper order
windows_internet wsa_wrapper(MAKEWORD(2, 2));
windows_internet wsa_wrapper(MAKEWORD(2, 2));
#endif

std::cout << "Please enter the rendezvous server's IP:" << std::endl;
std::string raw_ip{};
std::cout << "Please enter the rendezvous server's IP:" << std::endl;
std::string raw_ip{};

std::cin >> raw_ip;
std::getline(std::cin, std::string()); // Discard the newline that confuses the next input
std::cin >> raw_ip;

if (raw_ip == "") {
std::this_thread::sleep_for(100ms); //epic optimization
return 0;
}
Protocol validated{ "udp" };

client_loop(raw_ip, validated);
}
if (raw_ip == "") {
std::this_thread::sleep_for(100ms); //epic optimization
return 0;
}
Protocol validated{ "udp" };

int main(int argc, char** argv) {

if (argc > 1 && !strcmp(argv[1], "no-catch"))
return clientmain();

try
{
clientmain();
client_loop(raw_ip, validated);
}
catch (const std::exception& e)
{
Expand Down
72 changes: 72 additions & 0 deletions src/linux_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ readable_ip_info Platform::get_peer_data() const
return out;
}

raw_name_data Platform::get_peername_raw() const
{
return _socket.get_peer_raw();
}

raw_name_data Platform::get_myname_raw() const
{
return _socket.get_name_raw();
}

PtopSocket listen_construct(std::string port, Protocol input_proto, std::string name)
{
std::cout << "[Listen] Creating new Socket on port (with localhost, named: " << name << "): " << port << std::endl;
Expand Down Expand Up @@ -344,4 +354,66 @@ std::unique_ptr<IDataSocketWrapper> NonBlockingListener::accept_connection()
return std::make_unique<PlatformAnalyser>(std::move(new_sock));
}

PtopSocket reuse_connection_construct(raw_name_data data, Protocol proto, std::string name)
{
auto readable = convert_to_readable(data);
std::cout << "[DataReuseNoB] Creating Connection socket '" << name << "' bound to : " << readable.ip_address << ":" << readable.port << std::endl;
auto conn_socket = PtopSocket(proto, name);

if (conn_socket.is_invalid())
throw_new_exception("[DataReuseNoB] Failed to create nonblocking socket: " + linux_error(), LINE_CONTEXT);

conn_socket.set_non_blocking(true);
conn_socket.set_socket_reuse();

conn_socket.bind_socket(data, std::string("[DataReuseNoB] (") + name + ") Failed to bind");
std::cout << "[DataReuseNoB] Successfully bound Data socket (" << name << ") to: " << readable.ip_address << ":" << readable.port << std::endl;

return conn_socket;
}

NonBlockingConnector::NonBlockingConnector(raw_name_data data, std::string ip_address, std::string port, Protocol proto, std::string name)
: Platform(reuse_connection_construct(data, proto, name))
{
// if tcp?
try
{
this->connect(ip_address, port);
}
catch (const std::exception& e)
{
throw_with_context(e, LINE_CONTEXT);
}
}

void NonBlockingConnector::connect(std::string ip_address, std::string port)
{
try
{
std::cout << "[DataReuseNoB] (" << get_name() << ") Trying to connect to : " << ip_address << ":" << port << std::endl;
struct addrinfo* results, hints;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

int iResult = 0;

iResult = getaddrinfo(ip_address.c_str(), port.c_str(), &hints, &results);
if (iResult != 0)
throw_new_exception("Socket '" + get_name() + "' Failed to getaddrinfo, error: " + std::to_string(iResult), LINE_CONTEXT);

if (results == nullptr)
throw_new_exception(("No possible sockets found for '") + ip_address + ":" + port + "' (socket '" + get_name() + "')", LINE_CONTEXT);

_socket.connect(results->ai_addr, results->ai_addrlen);
std::cout << "[DataReuseNoB] (" << get_name() << ") Successfully BEGUN Connection to : " << ip_address << ":" << port << std::endl;
try_update_endpoint_info();
}
catch (const std::exception& e)
{
throw_with_context(e, LINE_CONTEXT);
}
}

#endif
4 changes: 1 addition & 3 deletions src/linux_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ PtopSocket::~PtopSocket()

if (is_valid())
{
auto lock = std::unique_lock<std::shared_mutex>(*_handle_mutex);
#ifdef DATA_COUT
auto lock = std::unique_lock(*_handle_mutex);
std::cout << "Closing socket" << std::endl;
#endif
close(*_handle);
*_handle = REALLY_INVALID_SOCKET;
_handle = nullptr;
Expand Down
Loading

0 comments on commit f3d2800

Please sign in to comment.