forked from pierr3/ModeS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helpers.cpp
243 lines (204 loc) · 7.43 KB
/
Helpers.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
#include "stdafx.h"
#include "Helpers.h"
#ifdef USE_HTTPLIB
/* NEW HTTPLIB Client implementation */
string LoadUpdateString()
{
const string AGENT{ "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION};
httplib::Client cli(MY_PLUGIN_UPDATE_BASE);
auto res = cli.Get(MY_PLUGIN_UPDATE_ENDPOINT);
if (!res) {
throw std::runtime_error("Connection failed to verify the plugin version. Error: " + httplib::to_string(res.error()));
}
if (res->status != httplib::StatusCode::OK_200) {
throw std::runtime_error("Failed to verify the plugin version. HTTP status: " + std::to_string(res->status));
}
return res->body;
}
#else
string LoadUpdateString()
{
const string AGENT{ "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION };
HINTERNET connect = InternetOpen(AGENT.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!connect) {
throw error{ string {"Connection failed to verify the plugin version. Error: " + to_string(GetLastError()) } };
}
HINTERNET OpenAddress = InternetOpenUrl(connect, MY_PLUGIN_UPDATE_URL, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
if (!OpenAddress) {
InternetCloseHandle(connect);
throw error{ string { "Failed to load plugin version verification file. Error: " + to_string(GetLastError()) } };
}
char DataReceived[256];
DWORD NumberOfBytesRead{ 0 };
string answer;
while (InternetReadFile(OpenAddress, DataReceived, 256, &NumberOfBytesRead) && NumberOfBytesRead)
answer.append(DataReceived, NumberOfBytesRead);
InternetCloseHandle(OpenAddress);
InternetCloseHandle(connect);
return answer;
}
#endif
#ifdef USE_HTTPLIB
/* NEW HTTPLIB Client implementation */
string LoadWebSquawk(EuroScopePlugIn::CFlightPlan FP, EuroScopePlugIn::CController ATCO, vector<const char*> usedCodes, bool vicinityADEP, int ConnectionType)
{
string codes;
for (size_t i = 0; i < usedCodes.size(); i++)
{
if (i > 0)
codes += "~";
codes += usedCodes[i];
}
string query_string = "callsign=" + string(ATCO.GetCallsign());
if (FP.IsValid())
{
if (vicinityADEP)
{
query_string += "&orig=" + string(FP.GetFlightPlanData().GetOrigin());
}
query_string += "&dest=" + string(FP.GetFlightPlanData().GetDestination()) +
"&flightrule=" + string(FP.GetFlightPlanData().GetPlanType());
if (FP.GetCorrelatedRadarTarget().IsValid())
if (FP.GetCorrelatedRadarTarget().GetPosition().IsValid())
query_string += "&latitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Latitude) +
"&longitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Longitude);
query_string += "&connectiontype=" + to_string(ConnectionType);
#ifndef _DEBUG
if (ConnectionType > 2)
{
query_string += "&sim";
}
#endif
}
if (!codes.empty())
{
query_string += "&codes=" + codes;
}
httplib::Headers headers = {
{"User-Agent", "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION }
};
httplib::Client client(MY_PLUGIN_APP_BASE);
string uri = MY_PLUGIN_APP_ENDPOINT + string("?") + query_string;
auto res = client.Get(uri, headers);
if (!res || res->status != httplib::StatusCode::OK_200) {
return string{ httplib::to_string(res.error()) };
}
string answer = res->body;
trim(answer);
if (answer.empty())
return string{ "E411" };
return answer;
}
#else
string LoadWebSquawk(EuroScopePlugIn::CFlightPlan FP, EuroScopePlugIn::CController ATCO, vector<const char*> usedCodes, bool vicinityADEP, int ConnectionType)
{
//PluginData p;
//const string AGENT{ "EuroScope " + string { MY_PLUGIN_NAME } + "/" + string { MY_PLUGIN_VERSION } };
const string AGENT{ "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION };
HINTERNET connect = InternetOpen(AGENT.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!connect) {
#ifdef _DEBUG
//throw error { string { "Failed reach the CCAMS server. Error: " + to_string(GetLastError()) } };
#endif
return string{ "E404" };
}
//int statusCode;
char responseText[MAX_PATH]; // change to wchar_t for unicode
DWORD responseTextSize = sizeof(responseText);
//Check existance of page (for 404 error)
if (HttpQueryInfo(connect, HTTP_QUERY_STATUS_CODE, &responseText, &responseTextSize, NULL))
{
//statusCode = ;
if (atoi(responseText) != 200)
return string{ "E" + string(responseText) };
}
//cpr::Response r = cpr::Get(cpr::Url{ "http://localhost/webtools/CCAMS/squawk" },
// cpr::Parameters{ {"callsign", ATCO.GetCallsign()} });
//if (r.status_code != 200)
// return "CURL ERROR";
string codes;
for (size_t i = 0; i < usedCodes.size(); i++)
{
if (i > 0)
codes += ",";
codes += usedCodes[i];
}
//string build_url = "http://localhost/webtools/CCAMS/squawk?callsign=" + string(ATCO.GetCallsign());
string build_url = "https://ccams.kilojuliett.ch/squawk?callsign=" + string(ATCO.GetCallsign());
if (FP.IsValid())
{
if (vicinityADEP)
{
build_url += "&orig=" + string(FP.GetFlightPlanData().GetOrigin());
}
build_url += "&dest=" + string(FP.GetFlightPlanData().GetDestination()) +
"&flightrule=" + string(FP.GetFlightPlanData().GetPlanType());
if (FP.GetCorrelatedRadarTarget().IsValid())
if (FP.GetCorrelatedRadarTarget().GetPosition().IsValid())
build_url += "&latitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Latitude) +
"&longitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Longitude);
build_url += "&connectiontype=" + to_string(ConnectionType);
#ifndef _DEBUG
if (ConnectionType > 2)
{
build_url += "&sim";
}
#endif
}
if (codes.size() > 0)
{
build_url += "&codes=" + codes;
}
HINTERNET OpenAddress = InternetOpenUrl(connect, build_url.c_str(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
if (!OpenAddress) {
InternetCloseHandle(connect);
#ifdef _DEBUG
//throw error{ string { "Failed reach the CCAMS server. Error: " + to_string(GetLastError()) } };
#endif
return string{ "E406" };
}
char DataReceived[256];
DWORD NumberOfBytesRead{ 0 };
string answer;
while (InternetReadFile(OpenAddress, DataReceived, 256, &NumberOfBytesRead) && NumberOfBytesRead)
answer.append(DataReceived, NumberOfBytesRead);
InternetCloseHandle(OpenAddress);
InternetCloseHandle(connect);
trim(answer);
if (answer.length() == 0)
return string{ "E411" };
return answer;
}
#endif
string ESversion()
{
int EuroScopeVersion[4] = { 0, 0, 0, 0 };
DWORD verHandle = 0;
char szVersionFile[MAX_PATH];
GetModuleFileName(NULL, szVersionFile, MAX_PATH);
const DWORD verSize = GetFileVersionInfoSize(szVersionFile, &verHandle);
if (verSize != NULL && verHandle == 0)
{
char* verData = new char[verSize];
if (GetFileVersionInfo(szVersionFile, verHandle, verSize, verData))
{
VS_FIXEDFILEINFO* verInfo = nullptr;
unsigned int size = 0;
if (VerQueryValue(verData, "\\", (LPVOID*)&verInfo, &size))
{
if (size)
{
if (verInfo->dwSignature == 0xfeef04bd)
{
EuroScopeVersion[0] = (verInfo->dwFileVersionMS >> 16) & 0xffff;
EuroScopeVersion[1] = (verInfo->dwFileVersionMS >> 0) & 0xffff;
EuroScopeVersion[2] = (verInfo->dwFileVersionLS >> 16) & 0xffff;
EuroScopeVersion[3] = (verInfo->dwFileVersionLS >> 0) & 0xffff;
}
}
}
}
delete[] verData;
}
return to_string(EuroScopeVersion[0]) + "." + to_string(EuroScopeVersion[1]) + "." + to_string(EuroScopeVersion[2]) + "." + to_string(EuroScopeVersion[3]);
}