Skip to content
Snippets Groups Projects
Verified Commit afc76e9e authored by Eunhak Lee's avatar Eunhak Lee
Browse files

feat: 제출 기능 마무리

parent 2981bb8e
Branches
Tags
1 merge request!1서버에 자동으로 제출하도록
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <sstream>
#include <iomanip>
using namespace std; using namespace std;
typedef void* voidptr; typedef void* voidptr;
...@@ -237,32 +240,76 @@ std::string getDateTimeString() { ...@@ -237,32 +240,76 @@ std::string getDateTimeString() {
} }
std::string getCode(int argc, char *argv[]) { std::string getCode(int argc, char *argv[]) {
if (argc < 2) return std::string(""); if (argc < 1) return std::string("");
return std::string(argv[1]);
try {
string _argv = string(argv[0]);
size_t anchor = _argv.find("-");
int count = 0;
while ((anchor = _argv.find("-")) != string::npos) {
_argv = _argv.substr(anchor + 1);
count++;
}
if (count <= 4) return std::string("");
return _argv.substr(0, _argv.find("."));
}
catch (...) {
fwprintf(stderr, L"코드를 불러오는데 실패했습니다\n");
return std::string("");
}
}
std::string escape_json(const std::string& s);
char* escape_xml(const std::string& xml) {
std::string merged = string("{\"xml\": \"") + escape_json(xml) + string("\"}");
size_t bufferSize = sizeof(char) * (merged.size() + 1);
char* buffer = (char*)malloc(bufferSize);
snprintf(buffer, bufferSize, "%s", merged.c_str());
return buffer;
} }
bool validateCode(std::string code) { bool submitParts(std::string code, char* xml) {
CURL *handle = curl_easy_init(); CURL *handle = curl_easy_init();
struct curl_slist* list = NULL; struct curl_slist* list = NULL;
list = curl_slist_append(list, "Content-Type: application/json"); list = curl_slist_append(list, "Content-Type: application/json");
list = curl_slist_append(list, std::string("Authorization: Code " + code).c_str()); list = curl_slist_append(list, std::string("Authorization: Code " + code).c_str());
curl_easy_setopt(handle, CURLOPT_URL, "https://mirror.enak.kr/echo"); curl_easy_setopt(handle, CURLOPT_URL, "https://meanspec.enak.kr/my/");
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list); curl_easy_setopt(handle, CURLOPT_HTTPHEADER, list);
char* body = escape_xml(xml);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, body);
CURLcode res = curl_easy_perform(handle); CURLcode res = curl_easy_perform(handle);
size_t nread;
char buffer[1024] = { 0 };
curl_easy_recv(handle, buffer, sizeof(buffer), &nread);
if (res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} }
else { else {
size_t nread; long statusCode;
char buffer[1024] = { 0 }; curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &statusCode);
curl_easy_recv(handle, buffer, sizeof(buffer), &nread);
cout << "Content is\n" << buffer << '\n'; if (200 <= statusCode && statusCode < 400) {
wcout << L"제출에 성공했습니다\n";
}
else {
wcout << L"제출에 실패했습니다(" << statusCode << ")\n";
cout << buffer << '\n';
}
} }
curl_free(handle);
free(body);
return true; return true;
} }
...@@ -286,10 +333,6 @@ int main(int argc, char *argv[]) { ...@@ -286,10 +333,6 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
curl_global_init(CURL_GLOBAL_ALL);
bool result = validateCode(code);
curl_global_cleanup();
int _initRes = initWQLServices(); int _initRes = initWQLServices();
if (_initRes != 0) { if (_initRes != 0) {
wcout << L"WQL을 초기화하는데 실패했습니다. 문의를 남겨주시기 바랍니다.\n"; wcout << L"WQL을 초기화하는데 실패했습니다. 문의를 남겨주시기 바랍니다.\n";
...@@ -311,7 +354,7 @@ int main(int argc, char *argv[]) { ...@@ -311,7 +354,7 @@ int main(int argc, char *argv[]) {
"SELECT * FROM Win32_BaseBoard", "SELECT * FROM Win32_BaseBoard",
"SELECT * FROM MSFT_PhysicalDisk", "SELECT * FROM MSFT_PhysicalDisk",
"SELECT * FROM Win32_PhysicalMemory", "SELECT * FROM Win32_PhysicalMemory",
"SELECT * FROM win32_videocontroller" "SELECT * FROM Win32_VideoController"
}; };
IEnumWbemClassObject* pEnumerator = NULL; IEnumWbemClassObject* pEnumerator = NULL;
...@@ -324,7 +367,7 @@ int main(int argc, char *argv[]) { ...@@ -324,7 +367,7 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
fprintf(out, "<meanspec version=\"%s\"><time>%s</time>", VERSION, getDateTimeString()); fprintf(out, "<meanspec version=\"%s\"><time>%s</time>", VERSION, getDateTimeString().c_str());
wcout << L"\n데이터를 수집하는 중\n"; wcout << L"\n데이터를 수집하는 중\n";
...@@ -383,8 +426,9 @@ int main(int argc, char *argv[]) { ...@@ -383,8 +426,9 @@ int main(int argc, char *argv[]) {
case VT_BSTR: { case VT_BSTR: {
fwprintf(out, L"<%s>%s</%s>", bstrName, SafeXML(vtProp.bstrVal).c_str(), bstrName); fwprintf(out, L"<%s>%s</%s>", bstrName, SafeXML(vtProp.bstrVal).c_str(), bstrName);
if (wcscmp(bstrName, L"Name") == 0) { if (wcscmp(bstrName, L"Name") == 0 || wcscmp(bstrName, L"Caption") == 0) {
_gpuNameCandidate = vtProp.bstrVal; _gpuNameCandidate = vtProp.bstrVal;
wcout << " " << vtProp.bstrVal << '\n';
} }
if (wcscmp(bstrName, L"DriverVersion") == 0) { if (wcscmp(bstrName, L"DriverVersion") == 0) {
_gpuDriverVersionCandidate = vtProp.bstrVal; _gpuDriverVersionCandidate = vtProp.bstrVal;
...@@ -441,23 +485,36 @@ int main(int argc, char *argv[]) { ...@@ -441,23 +485,36 @@ int main(int argc, char *argv[]) {
fprintf(out, "</meanspec>\n"); fprintf(out, "</meanspec>\n");
fclose(out); fclose(out);
char path[1024] = { 0 }; FILE* in;
auto _t = _getcwd(path, sizeof(path)); fopen_s(&in, FILENAME, "r");
wcout << L"\n\n수집을 완료했습니다! meanspec-log.xml 파일을 제출해주세요\n"; if (in == NULL) {
wcout << L" 파일 위치: " << path << "\n\n"; fwprintf(stderr, L"수집에 성공하였으나 자료를 정리하는데 실패했습니다\n");
return 2;
}
snprintf(&path[strlen(path)], sizeof(path), "\\%s", FILENAME); fseek(in, 0, SEEK_END);
long lSize = ftell(in);
rewind(in);
size_t _bufferSize = sizeof(char) * (100 + lSize);
char* xmlBuffer = (char*)malloc(_bufferSize);
if (xmlBuffer == NULL) {
fwprintf(stderr, L"버퍼를 초기화하지 못했습니다\n");
return 3;
}
int pathSize = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0); memset((void*)xmlBuffer, 0, _bufferSize);
wchar_t* widePath = new wchar_t[pathSize];
MultiByteToWideChar(CP_UTF8, 0, path, -1, widePath, pathSize);
LPCWSTR lpctPath = widePath;
wstring _arg(L"/select,"); size_t read_bytes = fread((void*)xmlBuffer, 1, lSize, in);
_arg.append(lpctPath); if (read_bytes != lSize - 1) {
fwprintf(stderr, L"자료 파일이 손상되었습니다\n");
return 4;
}
ShellExecute(NULL, L"open", L"explorer", _arg.c_str(), NULL, SW_SHOW); curl_global_init(CURL_GLOBAL_ALL);
submitParts(code, xmlBuffer);
curl_global_cleanup();
wcout << L"문의: lee@enak.kr\n"; wcout << L"문의: lee@enak.kr\n";
wcout << L"엔터를 눌러 프로그램 종료\n"; wcout << L"엔터를 눌러 프로그램 종료\n";
...@@ -573,3 +630,27 @@ int initWQLServices() { ...@@ -573,3 +630,27 @@ int initWQLServices() {
return 0; return 0;
} }
std::string escape_json(const std::string& s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; break;
case '\b': o << "\\b"; break;
case '\f': o << "\\f"; break;
case '\n': o << "\\n"; break;
case '\r': o << "\\r"; break;
case '\t': o << "\\t"; break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u"
<< std::hex << std::setw(4) << std::setfill('0') << static_cast<int>(*c);
}
else {
o << *c;
}
}
}
return o.str();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment