-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrivate-Proxy.cpp
More file actions
152 lines (137 loc) · 5.08 KB
/
Private-Proxy.cpp
File metadata and controls
152 lines (137 loc) · 5.08 KB
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
#define _CRT_SECURE_NO_WARNINGS
#include "Headers/Private-Proxy.h"
#include "Headers/callServer.h"
#include "Headers/checkStatus.h"
#include "Headers/getPort.h"
#include "Headers/stopApp.h"
#include "Headers/readConfig.h"
#include "Headers/createDefault.h"
void displayHelp() {
std::cout << ""
<< "Usage: Private-Proxy [options]\n\n"
<< "Author: Stephen Chryn\n"
<< "Version: 1.0.0 \n\n"
<< "Options:\n"
<< "Program name command alias description\n"
<< "Private-Proxy --start (-s) Start the program\n"
<< "Private-Proxy --stop (-x) Stop the program\n"
<< "Private-Proxy --on-start Execute task in startup-mode\n"
<< "Private-Proxy --version (-v) Show the current version\n"
<< "Private-Proxy --help (-h) Show help menu\n";
}
int startServer(int port) {
if (checkStatus("127.0.0.1", port) == 1) {
std::cout << "Appication is already running on 127.0.0.1:" << port << std::endl;
return 0;
}
else {
std::cout << "Appication is not running on 127.0.0.1:" << port << std::endl;
std::cout << "Attemplting to start server...\n";
callServer();
std::cout << "Appication has successfully shut down!\n";
}
}
void AttachToConsole() {
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
AllocConsole();
}
FILE* stream;
freopen_s(&stream, "CONOUT$", "w", stdout);
freopen_s(&stream, "CONOUT$", "w", stderr);
std::cout << "Attached to console\n";
}
void DetachFromConsole() {
FreeConsole();
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
) {
int port = getPort();
for (int i = 0; i < __argc; i++) {
if (port == -1) {
std::cout << "Failed to get port number from config file...\n";
if (createDefault() == 1) {
return 0;
}
std::cout << "Default config's created.\n";
}
std::vector<std::string> args(__argv, __argv + __argc);
if (std::find(args.begin(), args.end(), "--start") != args.end()) {
std::cout << "Attempting to start application...\n";
startServer(port);
return 0;
}
if (std::find(args.begin(), args.end(), "-s") != args.end()) {
std::cout << "Attempting to start application...\n";
startServer(port);
return 0;
}
if (std::find(args.begin(), args.end(), "--stop") != args.end()) {
std::cout << "Attemplting to stop appication...\n";
if (stopApp() == 0) {
std::cout << "Application has successfully shut down!\n";
}
return 0;
}
if (std::find(args.begin(), args.end(), "-x") != args.end()) {
std::cout << "Attemplting to stop appication...\n";
if (stopApp() == 0) {
std::cout << "Application has successfully shut down!\n";
}
return 0;
}
if (std::find(args.begin(), args.end(), "--on-start") != args.end()) {
std::ifstream input("C:/ProgramData/Private Proxy/config.json");
nlohmann::json config = readConfig("C:/ProgramData/Private Proxy/config.json");
if (config["on-start"] == true) {
}
else if (config["on-start"] == false) {
std::cout << "On-Start in Config.json is set to false...\n";
std::cout << "Applicagtion not starting.\n";
return 0;
}
else {
std::cout << "Error in: On-Start config. On-Start must be true or false.\n";
std::cout << "Assuming On-Start to true...\n";
}
if (checkStatus("127.0.0.1", port) == 1) {
std::cout << "Appication is already running on 127.0.0.1:" << port << std::endl;
}
else {
std::cout << "Appication is not running on 127.0.0.1:" << port << std::endl;
std::cout << "Attemplting to start server...\n";
callServer();
std::cout << "Appication has successfully shut down!\n";
}
std::ofstream outfile("config.json");
if (outfile.is_open()) {
outfile << config.dump(4);
outfile.close();
}
return 0;
}
AttachToConsole();
if (std::find(args.begin(), args.end(), "--help") != args.end()) {
displayHelp();
return 0;
}
if (std::find(args.begin(), args.end(), "-h") != args.end()) {
displayHelp();
return 0;
}
if (std::find(args.begin(), args.end(), "--version") != args.end()) {
std::cout << "Version 1.0.0";
return 0;
}
if (std::find(args.begin(), args.end(), "-v") != args.end()) {
std::cout << "Version 1.0.0";
return 0;
}
}
std::cout << "No argument selected...\n\n";
displayHelp();
return 0;
}