nesca/CheckKey_Th.cpp

97 lines
3.4 KiB
C++
Raw Normal View History

2015-02-24 14:00:19 +00:00
#pragma once
#include "CheckKey_Th.h"
#include "CheckProxy_Th.h"
#include "STh.h"
#include "externData.h"
#include "externFunctions.h"
2015-03-06 14:32:36 +00:00
#include <Connector.h>
#include <Utils.h>
2015-02-24 14:00:19 +00:00
2015-03-06 14:32:36 +00:00
void getSubStrEx(const char *src, char *startStr, char *endStr, char *dest, int szDest)
2015-02-24 14:00:19 +00:00
{
ZeroMemory(dest, szDest);
2015-03-02 14:27:38 +00:00
char *ptr1 = strstri((const char*)src, startStr);
2015-02-24 14:00:19 +00:00
if(ptr1 != NULL)
{
2015-03-02 14:27:38 +00:00
char *ptr2 = strstri((const char*)ptr1, endStr);
2015-02-24 14:00:19 +00:00
if(ptr2 != NULL)
{
int szStartStr = strlen(startStr);
int sz = ptr2 - ptr1 - szStartStr;
strncpy(dest, ptr1 + szStartStr, sz < szDest ? sz : szDest);
};
};
}
void getSubStr(char *src, char *startStr, char *endStr, char *dest, int szDest)
{
ZeroMemory(dest, szDest);
2015-03-02 14:27:38 +00:00
char *ptr1 = strstri((const char*)src, startStr);
2015-02-24 14:00:19 +00:00
if(ptr1 != NULL)
{
2015-03-02 14:27:38 +00:00
char *ptr2 = strstri((const char*)ptr1, endStr);
2015-02-24 14:00:19 +00:00
if(ptr2 != NULL)
{
int sz = ptr2 - ptr1;
strncpy(dest, ptr1, sz < szDest ? sz : szDest);
};
};
}
int emitIfOK = -1;
int KeyCheckerMain()
{
int kLen = strlen(trcPersKey);
if(kLen == 0)
{
stt->doEmitionRedFoundData("[Key check] Key field is empty.");
return -1;
}
else if(kLen < 32)
{
stt->doEmitionRedFoundData("[Key check] Key length is not valid.");
return -1;
};
2015-03-06 14:32:36 +00:00
std::vector<std::string> headerVector;
headerVector.push_back("X-Nescav3: True");
std::string buffer;
Connector::nConnect((std::string(trcSrv) + std::string(trcScr)).c_str(), std::stoi(trcSrvPortLine), &buffer, NULL, &headerVector);
int hostStringIndex = Utils::ci_find_substr(buffer, std::string("\r\n\r\n"));
if(hostStringIndex != -1) {
const char *ptr1 = buffer.c_str() + hostStringIndex + 4;
buffer.clear();
Connector::nConnect((std::string(ptr1) + std::string("/api/checkaccount?key=") + std::string(trcPersKey)).c_str(),
std::stoi(trcSrvPortLine), &buffer, NULL, &headerVector);
if(Utils::ci_find_substr(buffer, std::string("202 Accepted")) != -1) {
stt->doEmitionGreenFoundData("Key is valid.");
if(emitIfOK == 0) stt->doEmitionStartScanIP();
else if(emitIfOK == 1) stt->doEmitionStartScanDNS();
else if(emitIfOK == 2) stt->doEmitionStartScanImport();
return 1;
} else if(Utils::ci_find_substr(buffer, std::string("400 Bad Request")) != -1) {
QString errorDef = GetNSErrorDefinition(buffer.c_str(), "notify");
if(errorDef == "Invalid access key") stt->doEmitionYellowFoundData("[NS-Track] [Key is unauthorized] A valid key is required.");
else stt->doEmitionYellowFoundData("[Key checker] -FAIL! [400 Bad Request : " + GetNSErrorDefinition(buffer.c_str(), "notify") + "]");
return -1;
} else if(Utils::ci_find_substr(buffer, std::string("503 Bad Gateway")) != -1) {
stt->doEmitionYellowFoundData("[Key checker] 503 Backend not responding!");
return -1;
} else {
char header[64] = {0};
getSubStrEx(buffer.c_str(), "http/1.1 ", "\r\n", header, 64);
stt->doEmitionRedFoundData("[Key checker] -FAIL! An error occured. (" + QString::number(WSAGetLastError()) + ") Header: <u>" + QString::fromLocal8Bit(header) + "</u>");
if(gDebugMode) stt->doEmitionDebugFoundData(QString(buffer.c_str()));
return -1;
};
} else {
stt->doEmitionRedFoundData("[Key checker] Cannot acquire host string.");
}
}
2015-02-24 14:00:19 +00:00
void CheckKey_Th::run()
{
KeyCheckerMain();
2015-03-06 14:32:36 +00:00
}