JD2022-TU1/main/extern/Camcam/TestApp/camcam_httpclient.cpp

527 lines
17 KiB
C++
Raw Blame History

#include <windows.h>
#include "Defines.h"
#include "camcam_httpclient.h"
#include "fileio.h"
#include "Scene.h"
//#include "account.h"
#ifdef ONLINE_MODE
// Version routing
#ifdef CAMCAM_BRANCH_DEV
#define CAMCAM_ENVIRONMENT 50
#elif defined CAMCAM_BRANCH_TEST
#define CAMCAM_ENVIRONMENT 50
#elif defined CAMCAM_BRANCH_LIVE
#define CAMCAM_ENVIRONMENT 100
#else
#error "CamCam branch not defined"
#endif
// Server environments
#ifndef CAMCAM_ENVIRONMENT
#error "CamCam environment not defined"
#endif
static char RootCertificateFilename [] = "\31\12\7\124\114\12\63\103"; //"cert.pem";
static int RootCertificateFilenameLen = 8;
#if CAMCAM_ENVIRONMENT == 100
char UserAgent [] = "\71\16\30\143\3\27\6"; //"CamCamP"; // Prod Server
int UserAgentLen = 7;
char WSBaseURL [] = "\22\33\1\120\130\125\171\131\64\65\142\72\72\27\100\106\32\25\32\4\155\47\42\106\27"; //"http://www.youup.com/www/";
int WSBaseURLLen = 25;
char WebBaseURL [] = "\22\33\1\120\130\125\171\131\64\65\142\72\72\27\100\106\32\25\32\4\155"; //"http://www.youup.com/";
int WebBaseURLLen = 21;
char WSCredentials[2][12] = {
"\31\16\30\103\3\27",
"\31\133\30\103\126\27\144\36\163\172"
}; //{ "camcam", "c4mc4m2008" };
int WSCredentialsLen[2] = { 6, 10 };
char SecureWSBaseURL[] = "\22\33\1\120\21\100\171\1\60\47\57\66\47\7\33\103\126\37\133\12\55\75\172\110\127\234\21\135\27"; //"https://secure.ubi.com/youup/";
char SecureWSBaseURLLen = 29;
#elif CAMCAM_ENVIRONMENT == 50
char UserAgent [] = "\71\16\30\143\3\27\32"; //"CamCamL"; // Beta/UAT Server
int UserAgentLen = 7;
char WSBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306\23\132\117\33"; //"http://camcam.ubisoft.com/www/";
int WSBaseURLLen = 30;
char WebBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306"; //"http://camcam.ubisoft.com/";
int WebBaseURLLen = 26;
char WSCredentials[2][12] = {
"\31\16\30\103\3\27",
"\31\133\30\103\126\27\144\36\163\172"
}; //{ "camcam", "c4mc4m2008" };
int WSCredentialsLen[2] = { 6, 10 };
char SecureWSBaseURL[] = "\22\33\1\120\21\100\171\1\40\43\41\40\64\17\33\103\126\37\6\6\44\44\173\122\127\204\113\136\113\130\107"; //"https://camcam.ubisoft.com/ssl/";
int SecureWSBaseURLLen = 31;
#elif CAMCAM_ENVIRONMENT == 51
char UserAgent [] = "\71\16\30\143\3\27\2"; //"CamCamT"; // Internal Test Server
int UserAgentLen = 7;
char WSBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306\20\110\113\100\15\6\14\100\2\127\25\125"; //"http://camcam.ubisoft.com/testenv/www/";
int WSBaseURLLen = 38;
char WebBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306\20\110\113\100\15\6\14\100"; //"http://camcam.ubisoft.com/testenv/";
int WebBaseURLLen = 34;
char WSCredentials[2][12] = {
"\31\16\30\103\3\27",
"\31\133\30\103\126\27\144\36\163\172"
}; //{ "camcam", "c4mc4m2008" };
int WSCredentialsLen[2] = { 6, 10 };
#elif CAMCAM_ENVIRONMENT == 52
char UserAgent [] = "\71\16\30\143\3\27\2\3"; //"CamCamT2"; // Internal Test Server 2
int UserAgentLen = 8;
char WSBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306\20\110\113\100\15\6\14\135\132\127\25\15\171"; //"http://camcam.ubisoft.com/testenv2/www/";
int WSBaseURLLen = 39;
char WebBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306\20\110\113\100\15\6\14\135\132"; //"http://camcam.ubisoft.com/testenv2/";
int WebBaseURLLen = 35;
char WSCredentials[2][12] = {
"\31\16\30\103\3\27",
"\31\133\30\103\126\27\144\36\163\172"
}; //{ "camcam", "c4mc4m2008" };
int WSCredentialsLen[2] = { 6, 10 };
#elif CAMCAM_ENVIRONMENT == 10
char UserAgent [] = "\71\16\30\143\3\27\22"; //"CamCamD"; // Internal Dev Server
int UserAgentLen = 7;
char WSBaseURL [] = "\22\33\1\120\130\125\171\132\64\51\141\54\66\3\121\131\33\25\24\4\41\61\70\36"; //"http://twk-ocado/camcam/";
int WSBaseURLLen = 24;
char WebBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306"; //"http://camcam.ubisoft.com/";
int WebBaseURLLen = 26;
char WSCredentials[2][12] = {
"",
""
}; //{ "", "" };
int WSCredentialsLen[2] = { 0, 0 };
#elif CAMCAM_ENVIRONMENT == 11
char UserAgent [] = "\71\16\30\143\3\27\34\133\57\53\51\55"; //"CamCamJulien";
int UserAgentLen = 12;
char WSBaseURL [] = "\22\33\1\120\130\125\171\103\67\62\141\51\42\15\131\133\121\4\132\12\43\75\66\120\125\30"; //"http://mtp-jwolmer/camcam/";
int WSBaseURLLen = 26;
char WebBaseURL [] = "\22\33\1\120\130\125\171\115\42\57\57\42\70\114\100\124\135\5\32\17\66\176\66\136\125\306"; //"http://camcam.ubisoft.com/";
int WebBaseURLLen = 26;
char WSCredentials[2][12] = {
"",
""
}; //{ "", "" };
int WSCredentialsLen[2] = { 0, 0 };
const char UserAgent [] = "CamCamJulien";
const char WSBaseURL [] = "http://mtp-jwolmer/camcam/";
const char WebBaseURL [] = "http://camcam.ubisoft.com/";
const char WSCredentials [2][1] = { "", "" };
#endif
u32 ProtocolVersionNumber = 9;
enum LanguageId {
LanguageEn,
LanguageDefault = LanguageEn,
LanguageFr,
NumLanguages
};
char *ErrorMsgTable[NumLanguages][NUM_ACCOUNT_ERROR_MESSAGES] =
{
{
"",
"",
"Could not log-in",
"Could not check refresh status",
"Could not get asset list",
"Could not log-out",
"Could not create article",
"Could not get auth token",
"%s (could not find server)",
"%s (connection failed)",
"%s (error code %u)",
"\nYour application will be patched now.",
"Your account is being used on another computer. This application will restart now.",
"Failed script request",
"This feature is not available while another application uses the video ouput (e.g. during a video chat conversation)",
"Your account has been deleted. If you need more information, please contact youup@ubisoft.com",
"Your account has been banned.",
"Could not check article list",
"Could not cancel article"
},
{
"",
"",
"Echec de connexion",
"Impossible d'obtenir l'<27>tat de mise <20> jour",
"Impossible d'obtenir la liste de cartes",
"Echec de d<>connexion",
"Impossible de cr<63>er un article",
"Impossible d'obtenir un jeton d'authentification",
"%s (serveur non trouv<75>)",
"%s (<28>chec de connexion)",
"%s (code d'erreur %u)",
"\nL'application va maintenant se mettre <20> jour.",
"Votre compte est actuellement utilis<69> sur un autre ordinateur. L'application va red<65>marrer.",
"Echec de requ<71>te de script",
"Cette fonctionnalit<69> n'est pas disponible pendant qu'une autre application utilise la sortie vid<69>o (ex: pendant une conversation vid<69>o)",
"Votre compte a <20>t<EFBFBD> supprim<69>. Si vous avez besoin de plus d\'informations, veuillez contacter youup@ubisoft.com",
"Votre compte a <20>t<EFBFBD> banni.",
"Impossible d'acc<63>der <20> la liste des vid<69>os",
"Impossible d'annuler votre envoi de vid<69>o"
}
};
extern char *CryptBuffer(char *buff, u32 size);
//Vivek start
extern WORLD *TheScene;
extern char AppDataPath[];
bool firsrRun = true;
bool proxySaved = false;
//Vivek end
// FOR Getting Crypted files
/*void GetCrypt(char *base)
{
char crypt[1024];
char cryptHoho[1024];
char *cryptHohoPtr = cryptHoho;
memset(cryptHoho, 0, sizeof(cryptHoho));
strcpy(crypt, base);
CryptBuffer(crypt, strlen(base));
for (int i=0; i<strlen(base); i++)
{
sprintf(cryptHohoPtr, "\\%o", (unsigned char)crypt[i]);
cryptHohoPtr += strlen(cryptHohoPtr);
}
sprintf(cryptHohoPtr, " %i", strlen(base));
return;
}*/
void UncryptAll()
{
CryptBuffer(RootCertificateFilename, RootCertificateFilenameLen);
CryptBuffer(UserAgent, UserAgentLen);
CryptBuffer(WSBaseURL, WSBaseURLLen);
CryptBuffer(WebBaseURL, WebBaseURLLen);
CryptBuffer(WSCredentials[0], WSCredentialsLen[0]);
CryptBuffer(WSCredentials[1], WSCredentialsLen[1]);
CryptBuffer(SecureWSBaseURL, SecureWSBaseURLLen);
}
SimpleString<2> CamCamHttpClient::_LanguageCode;
// ***************************************************************************
bool CamCamHttpClient::init(const SimpleString<16> *pIdAccount)
{
// Initialize new Curl handle
if (!_HttpClient.init())
{
_ErrorString.sprintf("Could not init HTTP Client");
logStr(__FUNCTION__"-error: %s", _ErrorString.c_str());
return false;
}
initUserPassword();
_HttpClient.setUserAgent(UserAgent);
_IdAccount = pIdAccount;
return true;
}
// ***************************************************************************
void CamCamHttpClient::initUserPassword()
{
if (WSCredentials[0] != '\0')
_HttpClient.setCredentials(WSCredentials[0], WSCredentials[1]);
}
// ***************************************************************************
void CamCamHttpClient::setCookie(const char *cookieName, const char *cookieValue)
{
_HttpClient.setCookie(cookieName, cookieValue);
}
// ***************************************************************************
void CamCamHttpClient::resetCookie()
{
_HttpClient.resetCookie();
}
// ***************************************************************************
void CamCamHttpClient::setDomainCookie(std::string domain)
{
_HttpClient.resetCookie();
std::string tempC;
std::string temp = _Cookie[domain];
if(temp.empty())
return;
temp = temp.substr(1) + std::string(";"); // delete first ";"
while (temp.find(";") != std::string::npos)
{
tempC = temp.substr(0, temp.find(";"));
setCookie(tempC.substr(0, tempC.find("=")).c_str(), tempC.substr(tempC.find("=") + 1).c_str());
temp = temp.substr(tempC.size()+1);
}
}
// ***************************************************************************
void CamCamHttpClient::addDomainCookie(std::string domain, std::string cookie, bool eraseIfExist)
{
bool find = (_Cookie.find(domain) != _Cookie.end());
if(eraseIfExist && find)
_Cookie.erase (_Cookie.find(domain));
if(!find || eraseIfExist)
_Cookie.insert ( std::make_pair(domain, cookie) );
}
// ***************************************************************************
const char *CamCamHttpClient::getLocErrorMsg(ErrorMsgTextId msgId)
{
if (strncmp(_LanguageCode.c_str(), "fr", 2) == 0)
return ErrorMsgTable[LanguageFr][msgId];
else
return ErrorMsgTable[LanguageDefault][msgId];
}
// ***************************************************************************
bool CamCamHttpClient::doRequest(const char *action, const char *args, ErrorMsgTextId errorMsgId, const char *func, const char *moreErrorInfo, bool expectErrorCode, bool actionIsFullURL, bool useSecureLine)
{
_WSErrorCode = 0;
bool res;
const char *pageURL;
char PROXY_FILE[2048];
char proxy[2048];
bool scriptFile = false;
if(firsrRun)
{
firsrRun = false;
strncpy(PROXY_FILE, AppDataPath, 2047);
#ifdef USER_MODE
strcat(PROXY_FILE, "YouUpProxySettings.cfgx");
#else
strcat(PROXY_FILE, "YouUpProxySettings.cfg");
#endif
// This first checks the connetion through saved proxy configuration.
// If fails then set proxy to NULL to check the proxy through HTTP_PROXY in next if stmt.
if (isFilePresent(PROXY_FILE))
{
#ifdef USER_MODE
PROXY_FILE[strlen(PROXY_FILE)-1] = 0; // IniFileManager will readd the x suffix in USER_MODE
#endif
scriptFile = TheScene->GetIniValue(PROXY_FILE, "proxyaddress", proxy);
}
if(scriptFile)
{
if(strcmp(proxy, "NO_PROXY") == 0)
strncpy(proxy, "", 1);
_HttpClient.setProxy(proxy);
}
}
for(int i = 0; i < 2; i++)
{
if (actionIsFullURL)
{
pageURL = action;
//_HttpClient.setCookie("userName", "accmantest%40gmail.com");
res = _HttpClient.sendRequest(pageURL, args, true);
}
else
{
SimpleString<2048> pageURLbuf, paramStr;
if (useSecureLine)
{
#if CAMCAM_ENVIRONMENT >= 100
// This check ensures we're not communicating with a non-official free-content server (default)
_HttpClient.setVerifyServer(true, RootCertificateFilename);
pageURLbuf.sprintf("%s%s.php", SecureWSBaseURL, action);
#else
// This allows to connecting to a server without a valid certificate
_HttpClient.setVerifyServer(false);
pageURLbuf.sprintf("%s%s.php", WSBaseURL, action);
#endif
}
else
pageURLbuf.sprintf("%s%s.php", WSBaseURL, action);
#ifdef USER_MODE
paramStr.sprintf("v=%u%s&x=1&ia=%s%s%s", ProtocolVersionNumber, /*TEMP*/(ProtocolVersionNumber == 8) ? "b" : "", _IdAccount->c_str(), ((args[0]!='\0') ? "&" : ""), args);
#else
paramStr.sprintf("v=%u%s&ia=%s%s%s", ProtocolVersionNumber, /*TEMP*/(ProtocolVersionNumber == 8) ? "b" : "", _IdAccount->c_str(), ((args[0]!='\0') ? "&" : ""), args);
#endif
pageURL = pageURLbuf.c_str();
//logStr("%s?%s", pageURL, paramStr.c_str());
res = _HttpClient.sendRequest(pageURL, paramStr.c_str(), true);
}
if( res || !scriptFile ||
(_HttpClient.getRequestCode() != CHttpClient::ErrorCouldNotConnect
&& _HttpClient.getRequestCode() != 403
&& _HttpClient.getRequestCode() != 503
&& _HttpClient.getRequestCode() != 5)
)
{
break;
}
else
{
// file config fails so check connection through HTTP_PROXY
_HttpClient.setProxy(NULL);
}
}
//Vivek end
if (!res || (_HttpClient.getRequestCode() >= 400))
{
if (_HttpClient.isAborting())
{
_ErrorString.sprintf("Request aborted");
logStr("%s-info: %s", func, _ErrorString.c_str());
return false;
}
// HTTP problem?
const char *errorText = getLocErrorMsg(errorMsgId);
std::string moreInfo;
if (errorMsgId == FAILED_SCRIPT_REQUEST)
{
moreInfo = std::string(errorText) + std::string(" while accessing ") + std::string(pageURL);
errorText = moreInfo.c_str();
}
else if (errorMsgId == USE_MORE_INFO)
{
moreInfo = std::string(moreErrorInfo);
errorText = moreInfo.c_str();
}
switch (_HttpClient.getRequestCode())
{
case CHttpClient::ErrorCouldNotResolve:
_ErrorString.sprintf(getLocErrorMsg(S_COULD_NOT_RESOLVE), errorText);
break;
case CHttpClient::ErrorCouldNotConnect:
case 403: // obtained when trying to access a local host through Ubisoft's proxy (China)
case 503: // obtained when trying to access a local host through Ubisoft's proxy (EMEA)
case 5: /*CURLE_COULDNT_RESOLVE_PROXY*/
// Time to launch proxy auto-configuration, and retry!
if (_HttpClient.nextTryAutoConfigureProxy(pageURL)) // needs XP SP1 or newer
{
return doRequest(action, args, errorMsgId, func, moreErrorInfo, expectErrorCode, actionIsFullURL, useSecureLine);
}
_ErrorString.sprintf(getLocErrorMsg(S_CONNECTION_FAILED), errorText);
break;
default:
_ErrorString.sprintf(getLocErrorMsg(S_ERROR_CODE), errorText, _HttpClient.getRequestCode());
break;
}
//logStr("Result: %s", _HttpClient.getResponsePage().c_str());
if (_HttpClient.getRequestCode() >= 400)
_ErrorString.appendStr(_HttpClient.getResponsePage().c_str(), ~0);
if (errorMsgId != SILENT)
logStr("%s-error: %s", func, _ErrorString.c_str());
return false;
}
// Vivek start
// Save proxy settings when connetion succeed.
if (!proxySaved)
{
if(_HttpClient.getProxy())
{
strcpy(proxy, _HttpClient.getProxy());
if(strcmp(proxy, "") == 0)
strcpy(proxy, "NO_PROXY");
strncpy(PROXY_FILE, AppDataPath, 2047);
strcat(PROXY_FILE, "YouUpProxySettings.cfg");
TheScene->SetIniValue(PROXY_FILE, "proxyaddress", proxy);
TheScene->SaveIniFile(PROXY_FILE);
}
proxySaved = true;
}
// vivek end
if (_HttpClient.isAborting())
{
_ErrorString.sprintf("Request aborted");
if (errorMsgId != SILENT)
logStr("%s-info: %s", func, _ErrorString.c_str());
return false;
}
_ErrorString.clear();
if (!expectErrorCode)
return true;
// Did the page return an error?
const HttpPage& buffer = _HttpClient.getResponsePage();
if ((buffer.c_str()[0] != '0') ||
(buffer.c_str()[1] != '|'))
{
return onPageError(getLocErrorMsg(errorMsgId), func);
}
return true;
}
// ***************************************************************************
bool CamCamHttpClient::writeDownloadedFile(const char *path)
{
//if (isFilePresent(path))
// return false;
extern void REC_CreateDirectory(char *Name);
REC_CreateDirectory(const_cast<char*>(path));
FILE *f = CC_fopen(path, "wb");
if (!f)
return false;
fwrite(_HttpClient.getResponsePage().data(), 1, _HttpClient.getResponsePage().size(), f);
CC_fclose(f);
return true;
}
// ***************************************************************************
std::string getNextResultField(char const**ppSrc, char delimiter)
{
const char *pStart = *ppSrc;
const char *pEnd = strchr(pStart, delimiter);
if (pEnd != NULL)
{
*ppSrc = pEnd+1;
return std::string(pStart, pEnd-pStart);
}
else
{
*ppSrc = NULL;
return std::string(pStart);
}
}
// ***************************************************************************
std::string getNextResultField(char **ppSrc, char delimiter)
{
char *pStart = *ppSrc;
char *pEnd = strchr(pStart, delimiter);
if (pEnd != NULL)
{
*ppSrc = pEnd+1;
return std::string(pStart, pEnd-pStart);
}
else
{
*ppSrc = NULL;
return std::string(pStart);
}
}
// ***************************************************************************
bool isFilePresent(const char *filename)
{
FILE *f = CC_fopen(filename, "r"); // TODO: find a better way to find file existence
if (f)
{
CC_fclose(f);
return true;
}
return false;
}
#endif