Skip to content

Instantly share code, notes, and snippets.

@bostrot
Created December 6, 2018 09:22
Show Gist options
  • Save bostrot/0f17d6ddcf3357eb10a3d7d614730eb0 to your computer and use it in GitHub Desktop.
Save bostrot/0f17d6ddcf3357eb10a3d7d614730eb0 to your computer and use it in GitHub Desktop.
Posts empty body to site with cookies using libcurl to retrieve e.g. daily login bonus. Site must return code 200 when logged and another when not e.g. 404.
#include <stdio.h>
#include <curl/curl.h>
#include <iostream>
#include <unistd.h>
size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s)
{
size_t newLength = size * nmemb;
size_t oldLength = s->size();
try
{
s->resize(oldLength + newLength);
}
catch (std::bad_alloc &e)
{
// TODO: handle memory problem
return 0;
}
std::copy((char *)contents, (char *)contents + newLength, s->begin() + oldLength);
return size * nmemb;
}
void starting();
void callSite()
{
CURL *curl;
CURLcode res;
struct curl_slist *headerlist = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
std::string s;
long http_code = 0;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
// write response to function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
// disable logs
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
// set headers
headerlist = curl_slist_append(headerlist, "referer: https://example.com/");
headerlist = curl_slist_append(headerlist, "dnt: 1");
headerlist = curl_slist_append(headerlist, "sec-metadata: cause=\"forced\", destination=\"document\", site=\"cross-site\"");
headerlist = curl_slist_append(headerlist, "upgrade-insecure-requests: 1");
headerlist = curl_slist_append(headerlist, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
headerlist = curl_slist_append(headerlist, "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
headerlist = curl_slist_append(headerlist, "accept-encoding: gzip, deflate, br");
headerlist = curl_slist_append(headerlist, "accept-language: de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US;q=0.6");
headerlist = curl_slist_append(headerlist, "cache-control: max-age=0");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
// set cookie
curl_easy_setopt(curl, CURLOPT_COOKIE, "cookie1=test1; cookie2=test2;");
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// check for errors
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
// check status code
if (http_code == 200)
{
printf("Success\n");
}
else
{
printf("Failed\n");
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
// print response
std::cout<<s<<std::endl;
// every 6 hours
sleep(6 * 60 * 60);
// start over
starting();
}
void starting()
{
printf("Calling Site - ");
callSite();
}
int main(void)
{
starting();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment