-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_hosts.cpp
676 lines (606 loc) · 20.9 KB
/
quick_hosts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* Name: qs
*
* Description: quickly change entries in your hosts file
* using profiles.
*
* Author: Kyluke McDougall
*
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <term.h>
using namespace std;
// Menus
void menu(); // Prints out a menu with options
void menu_option(int option); // Handles the options from the menus
void arguments(string ts, string a, int test); // When arguments are passed to the program from a cli
void wait_for_menu(); // Used to remove redundant lines of code when waiting for user input after functions
// Hosts file
string check_host_path(); // Get the location of the host file after it has been setup
string view_hosts(); // Display/Print out the contents of the host file
int setup(string switch_host_file); // Setup all the necessary files and folders
// Profiles
void switch_profile(string profile); // Actually switching the profile
void choose_profile(); // To choose a profile before switching
void view_profiles(); // View the profiles
void remove_profile(string r_profile); // Remove one of the profiles in the list
void add_profile(string add_profile); // Adding a new profile with constraints
// Check networks
string network_check();
// Misc
void ClearScreen();
void no_profiles();
int first_run();
// Start of program
int main(int argc, char const *argv[])
{
string the_switch; // Holds the actual switch which determines which function is used
string argument; // Holds the variable passed to the program
int quick_incr = 0; // Used to increment within the for loop. Required to store argument as it is switch + 1
if (first_run() == 0)
{
ClearScreen();
setup("1");
}
// Get the user ID
if (getuid() != 0)
{
cout << "This program needs to be run as root." << endl;
cout << "Please run this program as root." << endl;
exit(0);
}
if (argc % 2 != 0) // If the argument is an odd number, it is either incomplete or non-existant
{
for(int i = 1; i < argc; i = i + 2) // This gets all the switches and arguments (Only 1 of each instance at the moment)
{
quick_incr = i + 1;
the_switch = argv[i]; // The first variable passed is for the switch
argument = argv[quick_incr]; // The second variable passed is for the argument
}
if (the_switch == "") // If there is no switch or argument, proceed straight to the menu
{
menu();
}
else
arguments(the_switch.c_str(),argument.c_str(),argc); // Otherwise, direct to the function which deals with arguments
}
else
exit(1); // If nothing here is right, exit to avoid any damage or data loss
// Wait for user input before doing anything
std::cin.ignore(1024, '\n');
std::cout << "press enter to continue ";
std::cin.get();
return 0;
}
void arguments(string ts, string a, int test) // Handles which function is called based on the switch value
{
if (ts == "-p") // -p is to switch the active profile
switch_profile(a.c_str());
else if (ts == "-c") // -c is to add a new profile
add_profile(a);
else if (ts == "-r") // -r is to remove a profile
remove_profile(a);
else if (ts == "-h") // -h is to setup the config files
setup(a);
else if (ts != "-h" && ts != "-c" && ts != "-r" && ts != "-p") // If none of these are right, terminate
exit(1);
}
void menu() // Function to display the menu
{
int option; // Stores the input of the user
ClearScreen(); // Clear screen initially
cout << "Main Menu" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
cout << "1. Switch profile" << endl;
cout << "2. Read hosts file" << endl;
cout << "3. View profiles" << endl;
cout << "4. Add a new profile" << endl;
cout << "5. Remove a profile" << endl;
cout << "6. Check current network" << endl;
cout << "9. Setup" << endl;
cout << "0. Exit" << endl;
cout << endl << "Option: ";
cin >> option; // Gets the input of the user
menu_option(option); // Calls the function which will direct to the user defined function
}
void menu_option(int option) // Once a choice has een made, this function will call the defined function
{
switch(option) {
case 1:
ClearScreen();
choose_profile(); // Change active profile
wait_for_menu(); // Waits for user input before displaying the menu again
break;
case 2:
ClearScreen();
cout << view_hosts(); // Display contents of hosts file
wait_for_menu();
break;
case 3:
ClearScreen();
view_profiles(); // View the available profiles
wait_for_menu();
break;
case 4:
ClearScreen();
add_profile("1"); // Add new profile
wait_for_menu();
break;
case 5:
ClearScreen();
remove_profile("1"); // Remove a profile
wait_for_menu();
break;
case 6:
ClearScreen();
network_check(); // Remove a profile
wait_for_menu();
break;
case 9:
ClearScreen();
setup("1"); // Setup the software config files
wait_for_menu();
break;
case 0:
ClearScreen();
exit(0);
default:
ClearScreen();
menu();
}
}
void wait_for_menu() // Waits for a user to press enter before displaying the menu
{
//Used to wait for user input before returning to the main menu
std::cin.ignore(1024, '\n');
std::cout << "press enter to continue ";
std::cin.get();
ClearScreen();
menu(); // Calls the menu function
}
string view_hosts() // Used to view the hosts files
{
string file; // Used as a container to save each line
string host; // Used as a temporary storage variable for the host file information
string dir; // Stores the location of the host file
dir = check_host_path(); // Gets the string location of the hosts file
ifstream hosts; // Initializes a variable for opening a file
hosts.open (dir.c_str()); // Opens the path given by the variable dir
cout << endl;
while(!hosts.eof()) // Loops until the end of the file
{
getline(hosts,file); // Saves each line to the variable called file.
host = host+file+"\n"; // Adds a new line after each line so that the output is readable
}
hosts.close(); // Closes the file
return host; // Returns the entire output of the file
}
int setup(string switch_host_file) // Sets up the config files for the software
{
string file; // Stores each line of the hosts file
string store_path; // Stores the path of the hosts file
string get_line; // Temporary storage for each line of the hosts file
string header = "#quick_hosts_profile"; // A header which defines the beginning of the host_switch configs in the hosts file
bool check = false; // False until the header is found in the hosts file
mkdir("/etc/qs",0777);
chdir("/etc/qs");
if (switch_host_file == "1") // If the hosts file needs to be setup, run this.
{
cout << "Please enter the path to your hosts file: " << endl; // Requests path to hosts file
cin >> store_path; // Stores the path of the hosts file
}
else // Otherwise it has been setup already and will get the path from the variable passed to the function
store_path = switch_host_file; // Stores the variable in store_path
ifstream given_host_file; // Initializes a variable to open files
given_host_file.open(store_path.c_str()); // Opens the file in the path indicated by store_path
while(!given_host_file.eof()) // Loops until the end of the file
{
getline(given_host_file,get_line); // Reads each line individually
if (get_line == header) // Checks to see if the header line is already present
check = true; // Sets check to true so that it doesn't add the header again later
}
if (check == false) // If the header wasn't found
{
ofstream ahf; // Opens the variable ahf to append the file
ahf.open(store_path.c_str(),ios::app); // Open the actual file indicated by the path in store_path
ahf << "" << endl; // Creates a new line at the end of the file
ahf << header << endl; // Adds the header at the end of the file
ahf.close(); // Closes the variable
}
given_host_file.close(); // Closes the variable for the hosts file
ofstream store_hosts("/etc/qs/host_path.ky"); // Opens the file host_path.ky for editing
store_hosts << store_path << endl; // Adds the path to the config file
store_hosts.close(); // closes store_hosts
cout << "You have selected this to be your hosts file: " << endl;
ifstream host_file; // Initializes the variable for display
host_file.open ("/etc/qs/host_path.ky"); // Opens the config file
while(!host_file.eof()) // Loops until the end of the file
{
getline(host_file,file); // Saves the each line in file.
cout << file << endl; // Prints out file.
}
host_file.close(); // Closes the config file
mkdir("/etc/qs/profiles",0777); // Creates a folder with permissions for everyone to read and write to later save profiles in
return 0;
}
void switch_profile(string profile)
{
string line; // Stores each line
string host = view_hosts(); // Stores the host file
string profile_replace; // Stores the replacement profile
string new_line = "\n"; // New line
string host_path = check_host_path(); // Stores the path of the hosts file
string header = "#quick_hosts_profile"; // Header
// Load the profile directory
chdir("/etc/qs/profiles"); // Changes dir
string working_dir = getcwd(NULL,0); // Get current working dir
//Get the contents of the new profile
ifstream new_profile; // Initialize variable for opening files
new_profile.open(profile.c_str()); // Open the profile
while(!new_profile.eof()) // Loop until end of file
{
getline(new_profile,line); // Store each line
profile_replace = profile_replace+new_line+line; // Adds a new line after each line was saved
}
new_profile.close(); // Close the variable
chdir(".."); // Move to parent folder
working_dir = getcwd(NULL,0); // Get current working dir
profile_replace = header+new_line+profile_replace; // Replace the profile
// let's replace the first needle:
host.replace(host.find(header),host.length(),profile_replace);
ofstream replace; // Initialize a variable for editing files
replace.open(host_path.c_str()); // Open the hosts file
replace << host << endl; // Replace the hosts file with new hosts file
replace.close(); // Close the variable
cout << host << endl; // Print the new hosts file
}
string check_host_path() // Get the path to the hosts file
{
string get_path; // Temporary stoarage for the path
string path; // Variable to store the actual path
ifstream host_path; // Initialize the variable for opening files
host_path.open ("/etc/qs/host_path.ky"); // Open the config file
if (!host_path.fail()) // If it does not fail opening the config file
{
while(!host_path.eof()) // Loop until end of file
{
getline(host_path,get_path); // Store the path in get_path
if (get_path != "") // If it is not empty
{
path = get_path; // Store the result in path
}
}
}
else // Otherwise print error
{
cout << "Can't open config file file" << endl;
menu();
}
return path;
}
void choose_profile() // Choose the profile
{
string contents[50]; // Contents of the profile
int control = 0;
string control_unit;
int host = 0;
string get;
cout << "Profile Switching" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
DIR *dir;
struct dirent *ent;
dir = opendir ("/etc/qs/profiles");
if (dir != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
control_unit = ent->d_name;
if (control_unit == "." || control_unit == "..")
continue;
else
{
contents[control] = ent->d_name;
control++;
}
}
if (control == 0)
{
no_profiles();
}
for (int i = 0; i < control; ++i)
{
cout << i << ". " << contents[i] << endl;
}
cout << endl << "Option: ";
cin >> host;
closedir (dir);
switch_profile(contents[host].c_str());
}
else
{
setup("1");
}
}
void add_profile(string add_profile)
{
string profile;
string p_name;
string view_line;
cout << "Add profile" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
cout << "Please enter a name for your new profile: " << endl;
cin >> p_name;
if (add_profile != "1")
profile = add_profile;
else
{
cout << "Please enter the profile constraints (end with % and press enter to finish): " << endl;
char new_input[1024];
cin.getline(new_input, 1024, '%');
profile=new_input;//cast char array to string
}
chdir("/etc/qs/profiles");
ofstream create_profile(p_name.c_str());
create_profile << profile << endl;
create_profile.close();
cout << "\n";
cout << "Your new profile: " << endl;
cout << "Name: " << p_name << endl;
cout << "Contents: ";
ifstream view;
view.open(p_name.c_str());
while(!view.eof())
{
getline(view,view_line);
cout << view_line+"\n";
}
view.close();
chdir("..");
}
void remove_profile(string r_profile)
{
string profile_remove;
string contents[50];
string remove_this_one;
int control = 0;
int option;
int chosen;
string control_unit;
cout << "Profile removal" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
if (r_profile == "1")
{
cout << "1. Remove profile" << endl;
cout << "2. View all profiles" << endl;
cout << "Option: ";
cin >> option;
cout << "\n";
ClearScreen();
if (option == 2)
{
cout << "Please pick a profile to remove" << endl << endl;
DIR *dir;
struct dirent *ent;
dir = opendir ("/etc/qs/profiles");
while ((ent = readdir (dir)) != NULL) {
control_unit = ent->d_name;
if (control_unit == "." || control_unit == "..")
continue;
else
{
contents[control] = ent->d_name;
control++;
}
}
if (control == 0)
{
no_profiles();
}
for (int i = 0; i < control; ++i)
{
cout << i << ". " << contents[i] << endl;
}
cout << endl << "Option: ";
cin >> chosen;
chdir("/etc/qs/profiles");
remove_this_one = contents[chosen];
if (remove(remove_this_one.c_str()) != 0)
{
ClearScreen();
cout << "Remove failed. Please try again" << endl;
chdir("..");
std::cin.ignore(1024, '\n');
std::cout << "press enter to continue ";
std::cin.get();
ClearScreen();
remove_profile("1");
}
else
{
cout << "Remove successful" << endl;
chdir("..");
}
}
else if (option == 1)
{
cout << "Please enter the name of the profile you would like to remove (case sensitive): ";
cin >> remove_this_one;
chdir("/etc/qs/profiles");
if (remove(remove_this_one.c_str()) != 0)
{
cout << "Remove failed. Please try again" << endl;
chdir("..");
remove_profile("1");
}
else
{
ClearScreen();
cout << "Remove successful" << endl;
chdir("..");
}
}
}
else
{
chdir("/etc/qs/profiles");
if (remove(r_profile.c_str()) != 0)
{
cout << "Remove failed. Please try again" << endl;
chdir("..");
remove_profile("1");
}
else
{
cout << "Remove successful" << endl;
chdir("..");
}
}
//remove(profile_remove.c_str());
//Used to wait for user input before returning to the main menu
std::cin.ignore(1024, '\n');
std::cout << "press enter to continue ";
std::cin.get();
ClearScreen();
menu();
}
void view_profiles()
{
string contents[50];
int control = 0;
string control_unit;
cout << "Available profiles" << endl;
cout << "-----------------------------------------------------------" << endl << endl;
DIR *dir;
struct dirent *ent;
dir = opendir ("/etc/qs/profiles");
while ((ent = readdir (dir)) != NULL)
{
control_unit = ent->d_name;
if (control_unit == "." || control_unit == "..")
continue;
else
{
contents[control] = ent->d_name;
control++;
}
}
if (control == 0)
{
no_profiles();
}
for (int i = 0; i < control; ++i)
{
cout << i << ". " << contents[i] << endl;
}
chdir("..");
cout << endl;
}
/* Detects the network address
*
* For future use
*/
string network_check()
{
string network;
string network_device;
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
// printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
network_device = ifa->ifa_name;
if (network_device != "lo")
{
network = addressBuffer;
}
} else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
// printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
}
if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
return network;
}
void ClearScreen()
{
cout << "\033[2J\033[1;1H";
}
int first_run()
{
const char* pzPath;
pzPath = "/etc/qs";
if ( pzPath == NULL) return false;
DIR *pDir;
bool bExists = 0;
pDir = opendir (pzPath);
if (pDir != NULL)
{
bExists = 1;
(void) closedir (pDir);
}
return bExists;
}
void no_profiles()
{
int add_new_profile = 0;
cout << "You currently have no profiles, would you like to add one?" << endl;
cout << "0. Yes" << endl;
cout << "1. No" << endl;
do
{
add_new_profile = 0;
cout << endl << "Option: ";
cin >> add_new_profile;
}
while (add_new_profile > 1);
if (add_new_profile == 0)
{
ClearScreen();
add_profile("1");
wait_for_menu();
menu();
}
else if (add_new_profile == 1)
{
ClearScreen();
menu();
}
}