123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include <time.h>
- #include <string.h>
- #include <stdio.h>
- #include <sstream>
- #include <iomanip>
- #include "BBS2chProxyAuth.h"
- #include "DataStorage.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 char *api_ua_auth;
- extern char *x_2ch_ua_auth;
- 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)
- {
- DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
- size_t downloaded = size*nitems;
- data->appendBytes(buffer, downloaded);
- return downloaded;
- }
- bool BBS2chProxyAuth::updateSID(CURL *curl)
- {
- int ct = time(0);
- bool ret = false;
- pthread_mutex_lock(mutex);
- if(ct < expire) {
- ret = true;
- goto last;
- }
-
- if(curl) {
- CURLcode res;
- struct curl_slist *headers = NULL;
- DataStorage *dat = new DataStorage();
- char gateway[1024];
- snprintf(gateway,1024,"https://%s/v1/auth/",api_server);
- 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);
- curl_easy_setopt(curl, CURLOPT_ENCODING, "");
- 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_ua_auth) {
- curl_easy_setopt(curl, CURLOPT_USERAGENT, api_ua_auth);
- }
- else {
- if (user_agent && !strncmp(user_agent, "Monazilla/", strlen("Monazilla/")))
- curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent);
- else
- curl_easy_setopt(curl, CURLOPT_USERAGENT, "");
- }
- if(x_2ch_ua_auth) headers = curl_slist_append(headers, x_2ch_ua_auth);
- 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->appendBytes("",1);
- char *ptr = strchr(dat->bytes,':');
- if(ptr && !strncmp(dat->bytes,"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->bytes);
- }
- }
- 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);
- curl_easy_reset(curl);
- delete dat;
- }
- 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();
- }
|