Skip to content

Commit

Permalink
CWP: Resolve client IP and port
Browse files Browse the repository at this point in the history
  • Loading branch information
PR3C14D0 committed Jul 31, 2024
1 parent 2f0d0fc commit 359462c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/CWP.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "CWP.h"

void ProtocolThread() {
DebugPrint(DEBUG_TYPE::INFO_MSG, "Started CWP protocol worker thread.");

int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (sock < 0) {
Expand All @@ -13,5 +15,27 @@ void ProtocolThread() {
saIn.sin_family = AF_INET;
inet_pton(AF_INET, LHOST, &saIn.sin_addr.s_addr);


if (bind(sock, (const struct sockaddr*)&saIn, sizeof(saIn)) < 0) {
DebugPrint(DEBUG_TYPE::ERROR_MSG, std::string("Error binding protocol TCP socket on port: ") + std::to_string(PROTOCOL_PORT));
exit(1);
}

if (listen(sock, MAX_CONN) < 0) {
DebugPrint(DEBUG_TYPE::ERROR_MSG, "Error starting TCP listened");
exit(1);
}

DebugPrint(DEBUG_TYPE::INFO_MSG, std::string("WCP Protocol listening on port: ") + std::to_string(PROTOCOL_PORT));

while (true) {
sockaddr_in clientAddr = { };
socklen_t clientSize = sizeof(sockaddr_in);
int client = accept(sock, (sockaddr*)&clientAddr, &clientSize);

char clientIp[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &clientAddr.sin_addr.s_addr, clientIp, sizeof(clientIp));
UINT clientPort = ntohs(clientAddr.sin_port);

DebugPrint(DEBUG_TYPE::INFO_MSG, std::string("(CWP) New connection from: ") + std::string(clientIp) + ':' + std::to_string(clientPort));
}
}
4 changes: 4 additions & 0 deletions src/CWP.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#ifdef WIN32
#include <winsock2.h>
#include <WS2tcpip.h>
Expand All @@ -11,9 +12,12 @@
typedef unsigned int UINT;
#endif

#include <string>

#include "ConsoleUtils.h"

#define LHOST "127.0.0.1"
#define PROTOCOL_PORT 18450
#define MAX_CONN 10

void ProtocolThread();
2 changes: 1 addition & 1 deletion src/ConsoleUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ inline void DebugPrint(DEBUG_TYPE type, std::string msg) {
break;
}

std::cout << printedMsg << std::endl;
std::cout << printedMsg + "\n";
}
8 changes: 7 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef unsigned int UINT;

#include "ConsoleUtils.h"
#include "BlockList.h"
#include "CWP.h"

#define VERSION 0.1

Expand All @@ -29,6 +30,8 @@ char PRIMARY_DNS[] = "8.8.8.8";

BlockList* g_blockList = nullptr;

std::thread g_cwpThread;

int main() {
std::cout << "CleanWave. The most powerful DNS for privacy and ad blocking. Version: " << VERSION << std::endl;
std::cout << "Made by Preciado" << std::endl << std::endl;
Expand All @@ -43,7 +46,10 @@ int main() {
}
#endif

DebugPrint(DEBUG_TYPE::INFO_MSG, "Initializing socket");
g_cwpThread = std::thread(ProtocolThread);
g_cwpThread.detach();

DebugPrint(DEBUG_TYPE::INFO_MSG, "Initializing DNS server socket");
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
DebugPrint(DEBUG_TYPE::FATAL_MSG, "Error creating the socket");
Expand Down

0 comments on commit 359462c

Please sign in to comment.