@@ -14,84 +14,94 @@ using namespace std;
1414
1515#define THREAD_NUM 200
1616
17- void print_usage () {
18- std::cout << PINK << " Usage: xpscan <IP> [options]\n "
19- << NC << BLUE << " Options:\n "
20- << " --help Print this message\n "
21- << " --json Export results to [IP].json\n "
22- << " --txt Export results to [IP].txt\n "
23- << " --full Scan all 65535 ports\n "
24- << NC ;
17+ void print_usage ()
18+ {
19+ std::cout << PINK << " Usage: xpscan <IP> [options]\n "
20+ << NC << BLUE << " Options:\n "
21+ << " --help Print this message\n "
22+ << " --json Export results to [IP].json\n "
23+ << " --txt Export results to [IP].txt\n "
24+ << " --full Scan all 65535 ports\n "
25+ << NC ;
2526}
2627
27- int main (int argc, char *argv[]) {
28- // check if at least one arg is passed
29- if (argc < 2 || std::strcmp (argv[1 ], " --help" ) == 0 ) {
30- print_usage ();
31- return 1 ;
32- }
33-
34- // Validate the ip
35- std::regex ip_regex (R"( ^(\d{1,3}\.){3}\d{1,3}$)" );
36- if (!std::regex_match (argv[1 ], ip_regex)) {
37- std::cerr << RED << " [x] Invalid IP address format." << NC << endl;
38- return 1 ;
39- }
40-
41- // Instatiate user input struct
42- struct UserInput input{};
43-
44- // Parse args
45- std::vector<std::string> args (argv + 2 , argv + argc);
46-
47- // Set IP attribute to the second arg passed
48- input.ip = argv[1 ];
49-
50- // Parse Flags
51- bool exportJson = (find (args.begin (), args.end (), " --json" ) != args.end ());
52- bool exportTxt = (find (args.begin (), args.end (), " --txt" ) != args.end ());
53- bool fullScan = (find (args.begin (), args.end (), " --full" ) != args.end ());
54-
55- Printer::printHeader ();
56-
57- // Check for fullscan flag
58- if (fullScan) {
59- input.start_port = START_PORT ;
60- input.end_port = END_PORT ;
61- } else {
62- std::cout << BLUE << " Enter start port:" << NC ;
63- std::cin >> input.start_port ;
64- std::cout << BLUE << " Enter end port:" << NC ;
65- std::cin >> input.end_port ;
66- }
67-
68- // Instatiate the scanner class with params
69- Scanner scanner (input.ip , THREAD_NUM );
70-
71- // Record start time
72- auto start = std::chrono::steady_clock::now ();
73-
74- // Proceed with the scanning logic
75- scanner.scanRange (input.start_port , input.end_port );
76-
77- // Record end time
78- auto end = std::chrono::steady_clock::now ();
79-
80- // Calculate elapsed time
81- std::chrono::duration<double > diff = end - start;
82- std::cout << " \n "
83- << BLUE << " [!] Scan finished in " << diff.count () << " s" << NC
84- << std::endl;
85-
86- Exporter exporter; // Uses default config path because i extracted the config file path as a parameter
87- if (exportJson) exporter.saveToJson (input.ip , scanner.getResults ());
88- if (exportTxt) exporter.saveToText (input.ip , scanner.getResults ());
89-
90- if (!exportJson && !exportTxt) {
91- std::cout << PINK
92- << " [!] No export flags set. Use --json or --txt to save results."
93- << NC << std::endl;
94- }
95-
96- return 0 ;
28+ int main (int argc, char * argv[])
29+ {
30+ // check if at least one arg is passed
31+ if (argc < 2 || std::strcmp (argv[1 ], " --help" ) == 0 )
32+ {
33+ print_usage ();
34+ return 1 ;
35+ }
36+
37+ // Validate the ip
38+ std::regex ip_regex (R"( ^(\d{1,3}\.){3}\d{1,3}$)" );
39+ if (!std::regex_match (argv[1 ], ip_regex))
40+ {
41+ std::cerr << RED << " [x] Invalid IP address format." << NC << endl;
42+ return 1 ;
43+ }
44+
45+ // Instatiate user input struct
46+ struct UserInput input{};
47+
48+ // Parse args
49+ std::vector<std::string> args (argv + 2 , argv + argc);
50+
51+ // Set IP attribute to the second arg passed
52+ input.ip = argv[1 ];
53+
54+ // Parse Flags
55+ bool exportJson = (find (args.begin (), args.end (), " --json" ) != args.end ());
56+ bool exportTxt = (find (args.begin (), args.end (), " --txt" ) != args.end ());
57+ bool fullScan = (find (args.begin (), args.end (), " --full" ) != args.end ());
58+
59+ Printer::printHeader ();
60+
61+ // Check for fullscan flag
62+ if (fullScan)
63+ {
64+ input.start_port = START_PORT ;
65+ input.end_port = END_PORT ;
66+ }
67+ else
68+ {
69+ std::cout << BLUE << " Enter start port:" << NC ;
70+ std::cin >> input.start_port ;
71+ std::cout << BLUE << " Enter end port:" << NC ;
72+ std::cin >> input.end_port ;
73+ }
74+
75+ // Instatiate the scanner class with params
76+ Scanner scanner (input.ip , THREAD_NUM );
77+
78+ // Record start time
79+ auto start = std::chrono::steady_clock::now ();
80+
81+ // Proceed with the scanning logic
82+ scanner.scanRange (input.start_port , input.end_port );
83+
84+ // Record end time
85+ auto end = std::chrono::steady_clock::now ();
86+
87+ // Calculate elapsed time
88+ std::chrono::duration<double > diff = end - start;
89+ std::cout << " \n "
90+ << BLUE << " [!] Scan finished in " << diff.count () << " s" << NC
91+ << std::endl;
92+
93+ Exporter exporter; // Uses default config path because i extracted the config file path as a parameter
94+ if (exportJson)
95+ exporter.saveToJson (input.ip , scanner.getResults ());
96+ if (exportTxt)
97+ exporter.saveToText (input.ip , scanner.getResults ());
98+
99+ if (!exportJson && !exportTxt)
100+ {
101+ std::cout << PINK
102+ << " [!] No export flags set. Use --json or --txt to save results."
103+ << NC << std::endl;
104+ }
105+
106+ return 0 ;
97107}
0 commit comments