-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
148 lines (128 loc) · 4.58 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
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "iostream"
#include <iostream>
#include <string>
#include "cpr/cpr.h"
#include "cstring"
using namespace std;
char text[120];
vector<string> registration(const string& key) {
sprintf(text,R"({"key":"value"})", key.c_str());
cpr::Response r = cpr::Post(cpr::Url{"You URL =)"},
cpr::Body{text},
cpr::Header{{"Content-Type", "application/json"}});
vector<string>aqua;
if (r.text[21] == 't'){
int private_key_n = r.text.find("private_key");
int server_public_key_n = r.text.find("server_public_key");
int public_key_n = r.text.find("\"public_key");
int server_address_n = r.text.find("serverAddress");
int ip_client_n = r.text.find("ip-client");
int allowed_ips_n = r.text.find("allowed-ips");
int pk = r.text.substr(private_key_n+14).find('\"');
string private_key = r.text.substr(private_key_n+14,pk);
aqua.push_back(private_key);
int pub_k = r.text.substr(public_key_n+14).find('\"');
string public_key = r.text.substr(public_key_n+14,pub_k);
aqua.push_back(public_key);
int spk = r.text.substr(server_public_key_n+20).find('\"');
string server_public_key = r.text.substr(server_public_key_n+20,spk);
aqua.push_back(server_public_key);
int sa = r.text.substr(server_address_n+20).find('\"');
string server_address = r.text.substr(server_address_n+20,sa);
aqua.push_back(server_address);
int ic = r.text.substr(ip_client_n+20).find('\"');
string ip_client = r.text.substr(ip_client_n+12,sa);
aqua.push_back(ip_client);
int ai = r.text.substr(allowed_ips_n+20).find('\"');
string allowed_ips = r.text.substr(allowed_ips_n+16,sa);
aqua.push_back(allowed_ips);
return aqua;
}else{
cout << "Регистрация завершилась ошибкой!" << endl;
}
return {};
}
void create_file(const string& private_key){
std::ofstream outfile ("/home/privatekey");
outfile << private_key << std::endl;
outfile.close();
}
void authorization(const string& ip,const basic_string<char>& deviceId){
sprintf(text,R"({"key":"%s","ip":"%s"})", deviceId.c_str(),ip.c_str());
cpr::Response r = cpr::Post(cpr::Url{"You URL =)"},
cpr::Body{text},
cpr::Header{{"Content-Type", "application/json"}});
if (r.text[21] == 't'){
cout << "Авторизация прошла успешно!" << endl;
}else{
cout << "Авторизация завершилась ошибкой!" << endl;
}
}
basic_string<char> readConfig(const string& path){
ifstream file (path);
string line;
if ( file.is_open() ) {
while ( file ) {
std::getline (file, line);
if (line.length() > 8){
if(line[0] == 's' && line[1] == 'e' && line[2] =='r' && line[6] == 'n'){
string serialNum = line.erase(0, 9);
cout << serialNum << endl;
return serialNum;
}
}
}
}
return {};
}
string fileFind(){
string path = "Camera config";
basic_string<char> serialNum = readConfig(path);
if (!serialNum.empty()){
cout << "Пришло: " << serialNum << endl;
return serialNum;
}else{
cout << "Серийный номер не был получен [Неправильный адрес]" << endl;
}
return {};
}
std::string execCommand(const std::string& cmd, int& out_exitStatus)
{
out_exitStatus = 0;
auto pPipe = ::popen(cmd.c_str(), "r");
if(pPipe == nullptr)
{
throw std::runtime_error("Cannot open pipe");
}
std::array<char, 256> buffer{};
std::string result;
while(not std::feof(pPipe))
{
auto bytes = std::fread(buffer.data(), 1, buffer.size(), pPipe);
result.append(buffer.data(), bytes);
}
auto rc = ::pclose(pPipe);
if(WIFEXITED(rc))
{
out_exitStatus = WEXITSTATUS(rc);
}
return result;
}
void runBin(vector<string> data) {
int exitStatus = 0;
string command = "Root for VPN " + data[0] + data[1] + "ip" + "allowed-ips" + data[4];
auto result = execCommand(command, exitStatus);
cout << result;
}
int main() {
std::string serialNum = fileFind();
vector<string> camId = registration("Camera number");
for (auto elem : camId){
cout << elem << endl;
}
if (!camId.empty()){
runBin(camId);
authorization(camId,serialNum);
}
return 0;
}