-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (129 loc) · 4.99 KB
/
main.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
// Name: Alexandros Nicolaides
// Purpose: Cross-platform utility to redirect domain name/s to a non-routable address.
// File: main.cpp (Application Interface)
// Dependencies: hosts.hpp, hosts.cpp, dirtywork.hpp
#include "hosts.hpp"
#include "hosts.cpp" /* needed to compile in cloud9 IDE */
#include "dirtywork.hpp"
void menu() {
cout << "\n-------------------System Administrator's Utility-------------------\n"
<< "1 - Block a specific domain name.\n"
<< "2 - Block domain name/s specified in a file.\n"
<< "3 - Display domain name/s currently being blocked.\n"
<< "4 - Remove a specific domain name from hosts file.\n"
<< "5 - Create a backup of the hosts file to the current directory.\n"
<< "6 - Unblock all domains names.\n"
<< "R - Restore from a backup.\n"
<< "Q - To Quit.\n" << endl;
}
char userChoice() {
char choice;
string input;
cout << "Please enter one of the above: ";
getline(cin, input);
stringstream(input) >> choice;
return choice;
}
int main(int argc, char** argv) {
Dirtywork helper(argv);
Hosts file(helper.getHostsPath());
menu();
char choice = userChoice();
while (choice != 'q' && choice != 'Q') {
string domain, filePath;
stringstream ss;
switch (choice) {
case '1':
cout << "Enter a domain name to block (e.g facebook.com): ";
getline(cin, domain);
// assuming smallest domain name is of length 4 (e.g "a.co")
while (domain.length() < 4) {
cerr << "\nERROR: The domain name is too small, try again (e.g facebook.com): ";
domain.clear();
getline(cin, domain);
}
try {
if (file.backupHostsFile()) file.blockDomain(domain);
}
catch(const std::runtime_error &error) {
cerr << error.what() << endl;}
break;
case '2':
cout << "\nPlease enter the path to the file to extract data from: (file.txt): ";
getline(cin, filePath);
while (!filePath.length()) {
cout << "\nPlease enter a valid file path or file name: ";
filePath.clear();
getline(cin, filePath);
}
try {
if(file.backupHostsFile()) file.appendFile(filePath);
}
catch (const std::runtime_error &error) {
cerr << error.what() << endl;
}
break;
case '3':
file.printDomains();
break;
case '4':
cout << "Enter the domain name to be removed in the form (e.g facebook.com): ";
getline(cin, domain);
while (!domain.length()) {
cerr << "\nERROR: Your domain is too small, try again (e.g facebook.com): ";
domain.clear();
getline(cin, domain);
}
try {
if (file.backupHostsFile() && file.removeDomain(domain, false))
cout << "\nThe domain name " + domain + " has been removed successfully." << endl;
else
cerr << "\nERROR: Couldn't remove the domain from hosts file." << endl;
}
catch (const std::runtime_error &error) {
cerr << error.what() << endl;
}
break;
case '5':
try {
if (file.backupHostsFile()) cout << "\nThe backup file can be found at: " << helper.getCurDir()
<< '/' << file.getBackupFileName() << endl;
}
catch (const std::runtime_error &error) {
cout << error.what() << endl;
}
break;
case '6':
try {
if (file.backupHostsFile() && file.clearHosts())
cout << "All domain names have been removed. You are all set!" << endl;
else cerr << "\nThere are no domains currently being blocked. You are all set!" << endl;
}
catch (const std::runtime_error &error) {
cout << error.what() << endl;
}
break;
case 'r':
case 'R':
cout << "\nEnter the file name or path of the file that's to replace the hosts file (e.g hosts.bak): ";
filePath.clear();
getline(cin, filePath);
while (!filePath.length()) {
cerr << "ERROR: Your file name or path is too small, try again (e.g hosts.bak): ";
filePath.clear();
getline(cin, filePath);
}
if (file.overwriteHosts(filePath))
cout << "\nThe file has been replaced successfully!" << endl;
else
cerr << "ERROR: Could not replace the hosts file." << endl;
break;
default:
cerr << "\nERROR: Wrong input." << endl;
break;
}
menu();
choice = userChoice();
}
return 0;
}