123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include <time.h>
- #include <string.h>
- #include <stdio.h>
- #include <sstream>
- #include <iomanip>
- #include <vector>
- #include "BBS2chProxyAuth.h"
- #include "BBS2chProxyHttpHeaders.h"
- #include "hmac.h"
- #define SID_UPDATE_INTERVAL (60*60)
- extern char *proxy_server;
- extern long proxy_port;
- extern long proxy_type;
- extern long timeout;
- extern char *user_agent;
- extern char *appKey;
- extern char *hmacKey;
- extern BBS2chProxyHttpHeaders api_auth_headers;
- extern int force_ipv4;
- extern char *api_server;
- extern CURLSH *curl_share;
- extern void log_printf(int level, const char *format ...);
- static size_t write_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
- {
- std::vector<char> *data = static_cast<std::vector<char> *>(userdata);
- size_t downloaded = size*nitems;
- data->insert(data->end(), buffer, buffer+downloaded);
- return downloaded;
- }
- bool BBS2chProxyAuth::updateSID(CURL *curl)
- {
- int ct = time(0);
- bool ret = false;
- bool shouldReleaseCurlHandle = false;
- pthread_mutex_lock(&mutex);
- if(ct < expire) {
- ret = true;
- goto last;
- }
- if (!curl) {
- curl = curl_easy_init();
- shouldReleaseCurlHandle = true;
- }
- if(curl) {
- CURLcode res;
- struct curl_slist *headers = NULL;
- std::vector<char> dat;
- char gateway[1024];
- snprintf(gateway,1024,"https://%s/v1/auth/",api_server);
- std::set<std::string> excludes;
- if(curl_share) curl_easy_setopt(curl, CURLOPT_SHARE, curl_share);
- curl_easy_setopt(curl, CURLOPT_URL, gateway);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
- if (api_auth_headers.has("Accept-Encoding")) {
- excludes.insert("accept-encoding");
- curl_easy_setopt(curl, CURLOPT_ENCODING, api_auth_headers.get("Accept-Encoding").c_str());
- }
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback_download);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dat);
- curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- if(force_ipv4) curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- if(proxy_server) {
- curl_easy_setopt(curl, CURLOPT_PROXY, proxy_server);
- curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy_port);
- curl_easy_setopt(curl, CURLOPT_PROXYTYPE, proxy_type);
- }
- if (!api_auth_headers.has("User-Agent")) {
- curl_easy_setopt(curl, CURLOPT_USERAGENT, "");
- }
- if (!api_auth_headers.empty()) headers = api_auth_headers.appendToCurlSlist(headers, excludes);
- if(headers) curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
-
- std::stringstream postData;
- std::stringstream msg;
- postData << "ID=&PW=&KY=" << appKey << "&CT=" << ct << "&HB=";
- msg << appKey << ct;
-
- unsigned char digest[32];
- proxy2ch_HMAC_SHA256(hmacKey, strlen(hmacKey), msg.str().c_str(), msg.str().length(), digest);
- for(int i=0;i<32;i++) {
- postData << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)digest[i];
- }
- #if LIBCURL_VERSION_NUM >= 0x071101
- curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, postData.str().c_str());
- #else
- std::string postDataStr = postData.str();
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postDataStr.c_str());
- #endif
-
- res = curl_easy_perform(curl);
- if(res == CURLE_OK) {
- long statusCode;
- curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &statusCode);
- //fprintf(stderr,"%ld\n",statusCode);
- if(statusCode == 200) {
- dat.push_back('\0');
- char *ptr = strchr(&dat.front(),':');
- if(ptr && !strncmp(&dat.front(),"SESSION-ID=Monazilla",20)) {
- sid = std::string(ptr+1);
- expire = ct + SID_UPDATE_INTERVAL;
- ret = true;
- log_printf(1,"Updated SID: %s\n",sid.c_str());
- }
- else {
- log_printf(0,"Cannot update SID: %s\n",&dat.front());
- }
- }
- else {
- log_printf(0,"Cannot update SID: API gateway returned %ld\n",statusCode);
- }
- }
- else log_printf(0,"curl error while updating SID: %s\n",curl_easy_strerror(res));
- curl_slist_free_all(headers);
- if (shouldReleaseCurlHandle) curl_easy_cleanup(curl);
- else curl_easy_reset(curl);
- }
- last:
- pthread_mutex_unlock(&mutex);
- return ret;
- }
- std::string BBS2chProxyAuth::requestBodyForURL(const char *url, CURL *curl)
- {
- if (!updateSID(curl) || sid.empty()) return "";
- std::stringstream postData;
- std::stringstream msg;
- postData << "sid=" << sid << "&appkey=" << appKey << "&hobo=";
- msg << strstr(url,"/v1/") << sid << appKey;
-
- unsigned char digest[32];
- proxy2ch_HMAC_SHA256(hmacKey, strlen(hmacKey), msg.str().c_str(), msg.str().length(), digest);
- for(int i=0;i<32;i++) {
- postData << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)digest[i];
- }
-
- return postData.str();
- }
- const std::string& BBS2chProxyAuth::getSID()
- {
- updateSID(NULL);
- return sid;
- }
|