Skip to content

Commit 7e45c15

Browse files
committed
added .clang-format
1 parent 1490d3b commit 7e45c15

8 files changed

Lines changed: 241 additions & 192 deletions

File tree

.clang-format

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
TabWidth: 4
4+
UseTab: Never
5+
ColumnLimit: 0
6+
AllowShortIfStatementsOnASingleLine: false
7+
BreakBeforeBraces: Allman
8+
SpacesInParentheses: false
9+
SpaceAfterCStyleCast: true
10+
SpaceBeforeParens: ControlStatements
11+
PointerAlignment: Left
12+
AlignConsecutiveAssignments: true
13+
AlignConsecutiveDeclarations: true
14+
SortIncludes: true
15+
IncludeBlocks: Preserve
16+
Cpp11BracedListStyle: true
17+
Standard: Cpp11

include/Common.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define COMMON_H
33

44
#include <string>
5+
using namespace std;
56

6-
struct PortResult {
7-
int port;
8-
std::string service;
7+
struct PortResult
8+
{
9+
int port;
10+
string service;
911
};
1012

11-
#endif // !COMMON_H
13+
#endif // !COMMON_H

include/Exporter.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
#include "Common.h"
77

8-
class Exporter {
9-
public:
10-
static void saveToJson(const std::string& ip, const std::vector<PortResult>& results);
11-
static void saveToText(const std::string& ip, const std::vector<PortResult>& results);
8+
class Exporter
9+
{
10+
public:
11+
static void saveToJson(const std::string& ip, const std::vector<PortResult>& results);
12+
static void saveToText(const std::string& ip, const std::vector<PortResult>& results);
1213
};
1314
#endif

include/Printer.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
using namespace std;
88

9-
struct UserInput {
10-
string ip;
11-
int start_port;
12-
int end_port;
9+
struct UserInput
10+
{
11+
string ip;
12+
int start_port;
13+
int end_port;
1314
};
1415

15-
class Printer {
16-
public:
17-
static int getTerminalWidth();
18-
static void printCentered(const vector<string>& art, int terminalWidth);
19-
static void printHeader();
20-
static void clearScreen();
16+
class Printer
17+
{
18+
public:
19+
static int getTerminalWidth();
20+
static void printCentered(const vector<string>& art, int terminalWidth);
21+
static void printHeader();
22+
static void clearScreen();
2123
};
22-
#endif // !PRINTER_H
24+
#endif // !PRINTER_H

include/Scanner.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99

1010
using namespace std;
1111

12-
class Scanner {
13-
public:
14-
// constructor
15-
Scanner(std::string ip, int threads);
16-
void scanRange(int start, int end);
17-
std::vector<PortResult> getResults();
12+
class Scanner
13+
{
14+
public:
15+
// constructor
16+
Scanner(string ip, int threads);
17+
void scanRange(int start, int end);
18+
std::vector<PortResult> getResults();
1819

19-
private:
20-
void scanPort(int port);
20+
private:
21+
void scanPort(int port);
2122

22-
std::string target_ip;
23-
int max_threads;
24-
std::vector<PortResult> results;
25-
std::mutex result_mutex;
23+
string target_ip;
24+
int max_threads;
25+
vector<PortResult> results;
26+
mutex result_mutex;
2627
};
2728

2829
#endif

src/Printer.cpp

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,61 @@
88

99
using namespace std;
1010

11-
int Printer::getTerminalWidth() {
12-
// Instantiate a window size struct
13-
struct winsize w;
14-
// Setting some magic numbers for file descriptor and request. and passing the window size struct
15-
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
16-
// Returning the width columns
17-
return w.ws_col;
11+
int Printer::getTerminalWidth()
12+
{
13+
// Instantiate a window size struct
14+
struct winsize w;
15+
// Setting some magic numbers for file descriptor and request. and passing the window size struct
16+
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
17+
// Returning the width columns
18+
return w.ws_col;
1819
}
1920

20-
void Printer::clearScreen() {
21-
// \033[2J clears the screen, \033[H moves cursor to top
22-
std::cout << "\033[2J\033[H";
21+
void Printer::clearScreen()
22+
{
23+
// \033[2J clears the screen, \033[H moves cursor to top
24+
std::cout << "\033[2J\033[H";
2325
}
2426

25-
void Printer::printHeader() {
26-
Printer::clearScreen();
27-
28-
std::vector<std::string> myArt = {
29-
"****************************************************",
30-
"* ╻ ╻┏━┓ ┏━┓┏━╸┏━┓┏┓╻ *",
31-
"* ┏╋┛┣━┛ ┗━┓┃ ┣━┫┃┗┫ *",
32-
"* ╹ ╹╹ ┗━┛┗━╸╹ ╹╹ ╹ *",
33-
"* -------------------------------------------- *",
34-
"* 01101000 01110101 01101101 01100001 01101110 *",
35-
"* -------------------------------------------- *",
36-
"****************************************************\n\n",
37-
};
38-
39-
int width = Printer::getTerminalWidth();
40-
Printer::printCentered(myArt, width);
27+
void Printer::printHeader()
28+
{
29+
Printer::clearScreen();
30+
31+
std::vector<std::string> myArt = {
32+
"****************************************************",
33+
"* ╻ ╻┏━┓ ┏━┓┏━╸┏━┓┏┓╻ *",
34+
"* ┏╋┛┣━┛ ┗━┓┃ ┣━┫┃┗┫ *",
35+
"* ╹ ╹╹ ┗━┛┗━╸╹ ╹╹ ╹ *",
36+
"* -------------------------------------------- *",
37+
"* 01101000 01110101 01101101 01100001 01101110 *",
38+
"* -------------------------------------------- *",
39+
"****************************************************\n\n",
40+
};
41+
42+
int width = Printer::getTerminalWidth();
43+
Printer::printCentered(myArt, width);
4144
}
4245

43-
void Printer::printCentered(const vector<string>& art, int terminalWidth) {
44-
size_t maxWidth = 0;
45-
const std::string COLOR = "\033[33m";
46-
const std::string RESET = "\033[0m";
46+
void Printer::printCentered(const vector<string>& art, int terminalWidth)
47+
{
48+
size_t maxWidth = 0;
49+
const std::string COLOR = "\033[33m";
50+
const std::string RESET = "\033[0m";
4751

48-
// Step 1: Find the widest line in the art
49-
for (const std::string& line : art) {
50-
maxWidth = std::max(maxWidth, line.length());
51-
}
52+
// Step 1: Find the widest line in the art
53+
for (const std::string& line : art)
54+
{
55+
maxWidth = std::max(maxWidth, line.length());
56+
}
5257

53-
// Step 2: Calculate padding based on the widest line
54-
int padding = (terminalWidth - (int)maxWidth) / 2;
55-
if (padding < 0) padding = 0; // Prevent errors if terminal is too narrow
58+
// Step 2: Calculate padding based on the widest line
59+
int padding = (terminalWidth - (int) maxWidth) / 2;
60+
if (padding < 0)
61+
padding = 0; // Prevent errors if terminal is too narrow
5662

57-
// Step 3: Print every line using the SAME padding
58-
for (const std::string& line : art) {
59-
std::cout << std::string(padding, ' ') << COLOR << line << RESET << std::endl;
60-
}
63+
// Step 3: Print every line using the SAME padding
64+
for (const std::string& line : art)
65+
{
66+
std::cout << std::string(padding, ' ') << COLOR << line << RESET << std::endl;
67+
}
6168
}

src/Scanner.cpp

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,71 @@
1111

1212
Scanner::Scanner(std::string ip, int threads) : target_ip(ip), max_threads(threads) {}
1313

14-
void Scanner::scanPort(int port) {
15-
int sock = socket(AF_INET, SOCK_STREAM, 0);
16-
if (sock < 0) return;
14+
void Scanner::scanPort(int port)
15+
{
16+
int sock = socket(AF_INET, SOCK_STREAM, 0);
17+
if (sock < 0)
18+
return;
1719

18-
// Set to Non-Blocking mode
19-
fcntl(sock, F_SETFL, O_NONBLOCK);
20+
// Set to Non-Blocking mode
21+
fcntl(sock, F_SETFL, O_NONBLOCK);
2022

21-
struct sockaddr_in addr;
22-
addr.sin_family = AF_INET;
23-
addr.sin_port = htons(port);
24-
inet_pton(AF_INET, target_ip.c_str(), &addr.sin_addr);
23+
struct sockaddr_in addr;
24+
addr.sin_family = AF_INET;
25+
addr.sin_port = htons(port);
26+
inet_pton(AF_INET, target_ip.c_str(), &addr.sin_addr);
2527

26-
// In a non blocking context, the return of connect() is neglicted
27-
// In this case stored to a variable to avoid unexpected behavior
28-
int connect_output = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
28+
// In a non blocking context, the return of connect() is neglicted
29+
// In this case stored to a variable to avoid unexpected behavior
30+
int connect_output = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
2931

30-
// Use select to wait exactly 500ms
31-
fd_set fdset;
32-
FD_ZERO(&fdset);
33-
FD_SET(sock, &fdset);
34-
struct timeval tv;
35-
tv.tv_sec = 0;
36-
// 500ms timeout
37-
tv.tv_usec = 500000;
32+
// Use select to wait exactly 500ms
33+
fd_set fdset;
34+
FD_ZERO(&fdset);
35+
FD_SET(sock, &fdset);
36+
struct timeval tv;
37+
tv.tv_sec = 0;
38+
// 500ms timeout
39+
tv.tv_usec = 500000;
3840

39-
if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1) {
40-
int so_error;
41-
socklen_t len = sizeof(so_error);
42-
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
41+
if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1)
42+
{
43+
int so_error;
44+
socklen_t len = sizeof(so_error);
45+
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
4346

44-
if (so_error == 0) {
45-
struct servent* serv = getservbyport(htons(port), "tcp");
46-
std::string s_name = (serv) ? serv->s_name : "unknown";
47+
if (so_error == 0)
48+
{
49+
struct servent* serv = getservbyport(htons(port), "tcp");
50+
std::string s_name = (serv) ? serv->s_name : "unknown";
4751

48-
std::lock_guard<std::mutex> lock(result_mutex);
49-
results.push_back({port, s_name});
50-
std::cout << "\r\033[K\033[32m[+] Port " << port << " OPEN (" << s_name << ")\033[0m" << std::endl;
52+
std::lock_guard<std::mutex> lock(result_mutex);
53+
results.push_back({port, s_name});
54+
std::cout << "\r\033[K\033[32m[+] Port " << port << " OPEN (" << s_name << ")\033[0m" << std::endl;
55+
}
5156
}
52-
}
53-
close(sock);
57+
close(sock);
5458
}
5559

56-
void Scanner::scanRange(int start, int end) {
57-
std::vector<std::thread> pool;
58-
for (int p = start; p <= end; ++p) {
59-
pool.emplace_back(&Scanner::scanPort, this, p);
60+
void Scanner::scanRange(int start, int end)
61+
{
62+
std::vector<std::thread> pool;
63+
for (int p = start; p <= end; ++p)
64+
{
65+
pool.emplace_back(&Scanner::scanPort, this, p);
6066

61-
if (pool.size() >= max_threads) {
62-
for (auto& t : pool)
63-
if (t.joinable()) t.join();
64-
pool.clear();
65-
std::cout << "\r\033[33m[*] Scanning: " << p << "/" << end << "...\033[0m" << std::flush;
67+
if (pool.size() >= max_threads)
68+
{
69+
for (auto& t : pool)
70+
if (t.joinable())
71+
t.join();
72+
pool.clear();
73+
std::cout << "\r\033[33m[*] Scanning: " << p << "/" << end << "...\033[0m" << std::flush;
74+
}
6675
}
67-
}
68-
for (auto& t : pool)
69-
if (t.joinable()) t.join();
76+
for (auto& t : pool)
77+
if (t.joinable())
78+
t.join();
7079
}
7180

7281
std::vector<PortResult> Scanner::getResults() { return results; }

0 commit comments

Comments
 (0)