Skip to content

Instantly share code, notes, and snippets.

@polaco1782
Created December 11, 2020 18:16
Show Gist options
  • Save polaco1782/b8acc512300170aceaeb64824bc2a810 to your computer and use it in GitHub Desktop.
Save polaco1782/b8acc512300170aceaeb64824bc2a810 to your computer and use it in GitHub Desktop.
Old Squid store-id integration
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <curl/curl.h>
namespace UT
{
CURL *handle;
CURLcode res;
std::string response;
void write_log(const std::string s)
{
std::cerr << s << std::endl;
}
// write CURL buffer to string
size_t write_string(void *ptr, size_t size, size_t count, void *stream)
{
((std::string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}
bool init_curl()
{
handle = curl_easy_init();
if(handle)
{
curl_easy_setopt(handle, CURLOPT_HEADER, 0);
// timeout values
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_easy_setopt(handle, CURLOPT_TIMEOUT, 5);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_string);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);
return true;
}
else
{
write_log("Failed to initialize CURL library!");
return false;
}
}
void shutdown()
{
curl_easy_cleanup(handle);
curl_global_cleanup();
}
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
{
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
void process_input(const std::string& line)
{
std::vector<std::string> tokens;
std::stringstream ss(line);
std::string token;
std::string url;
int res;
// split incoming data from squid
while (getline(ss, token, ' '))
tokens.push_back(token);
url.append("http://storeid.unveiltech.com/storeid/api-storeid.php?porn=1&url=");
url.append(base64_encode(reinterpret_cast<const unsigned char *>(tokens[0].c_str()), tokens[0].length()));
response.clear();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
res = curl_easy_perform(handle);
if(res==CURLE_OK)
std::cout << response << std::endl;
else
std::cout << "ERR" << std::endl;
}
}
int main(int argc, char** argv)
{
std::string line;
if(UT::init_curl())
{
while(getline(std::cin, line))
{
if(line.length()>0)
UT::process_input(line);
}
}
else
exit(-1);
UT::shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment