Skip to content

Commit 2521af8

Browse files
author
Rahul Chatterjee
committed
Versino 1.0.4, fixed curl https check, and upload should now work smoothly
1 parent acc5293 commit 2521af8

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project(typtop)
55
# The version number.
66
set (typtop_VERSION_MAJOR 1)
77
set (typtop_VERSION_MINOR 0)
8-
set (typtop_VERSION_PATCH 3)
8+
set (typtop_VERSION_PATCH 4)
99

1010
## Set compiler flags
1111
if(CMAKE_BUILD_TYPE MATCHES "Debug")

src/typtop.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,21 +480,24 @@ void TypTop::set_typo_policy(int edit_cutoff, int abs_entcutoff, int rel_entcuto
480480
if (rel_entcutoff >= 0) tp->set_rel_entcutoff(rel_entcutoff);
481481
}
482482

483-
#include "upload.cpp"
484483
int TypTop::send_log(int test) {
485484
if (!is_initialized()) {
486485
LOG_INFO << "DB is not initialized. Will send logs later.";
487486
} else if (db.logs().l_size() < 5) {
488487
LOG_INFO << "Not many logs (" << db.logs().l_size() << "). Will send logs later.";
489488
} else {
490-
LOG_INFO << "sending logs \n";
489+
LOG_INFO << "Sending logs.";
491490
int ret = send_log_to_server(db.ch().install_id(),
492491
b64encode(db.logs().SerializeAsString()),
493492
test);
494493
if (ret == 1) {
494+
LOG_INFO << "Sending logs succeeded.";
495495
db.mutable_logs()->clear_l();
496496
return 1;
497+
} else {
498+
LOG_INFO << "Sending logs failed.";
497499
}
500+
498501
}
499502
return 0;
500503
}

src/typtop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "plog/Log.h"
1616
#include "typtopconfig.h"
1717

18-
1918
#ifndef TYPTOP_LOG_FILE
2019
#define TYPTOP_LOG_FILE "/tmp/typtop.log"
2120
#endif
@@ -86,5 +85,6 @@ class TypTop {
8685
inline const EncHeaderData& get_ench(){return ench;}
8786
};
8887

88+
int send_log_to_server(const string uid, const string log, int test=1);
8989

9090
#endif //TYPTOP_C_TYPTOP_H

src/upload.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
#include <curl/curl.h>
99
#include <string>
1010
#include "typtopconfig.h"
11-
#include "plog/Log.h"
11+
#include "typtop.h"
1212

1313
using namespace std;
1414

1515

1616
// const string url = "https://ec2-54-209-30-18.compute-1.amazonaws.com/submit";
1717
const string url = UPLOAD_URL;
18-
const string key = "a40648638b48abf2159f9331cbab9cb3ae81d8cd247c145942c3cce9c708ae89";
18+
// sha256 of the the private key of the server -- a shared secret
19+
// Doesn't provide much security, except some attacker who are just want to spoof the server.
20+
// The dedicated attacker can just read the source code and fill my data with garbage.
21+
const string key = "82e4ae0301ea4ef8db76945666f08d1b5ffef2ae0f27737889d2d21e32ee7e54";
22+
1923
/**
2024
* Sends the data to the amazon server. It assumes all the fields are url encoded,
2125
* and will not try to encode.
@@ -25,7 +29,7 @@ const string key = "a40648638b48abf2159f9331cbab9cb3ae81d8cd247c145942c3cce9c70
2529
* @return : success code
2630
*/
2731

28-
int send_log_to_server(const string uid, const string log, int test=1) {
32+
int send_log_to_server(const string uid, const string log, int test) {
2933
CURL *curl;
3034
CURLcode res = CURLE_SEND_ERROR;
3135
// struct curl_slist *headers = NULL; /* http headers to send with request */
@@ -47,22 +51,19 @@ int send_log_to_server(const string uid, const string log, int test=1) {
4751
curl = curl_easy_init();
4852
FILE *devnull = fopen("/dev/null", "w");
4953
if(curl) {
54+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
5055
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
56+
5157
/* Now specify the POST data */
5258
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, payload.size());
5359
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str());
54-
/* Add the certificate */
55-
// curl_easy_setopt(curl, CURLOPT_CAINFO, CAFILE);
56-
57-
// Till I can fix this weird bug
58-
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
59-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
6060

61+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
6162

6263
/* set content type */
63-
// headers = curl_slist_append(headers, "Accept: application/json");
64-
// headers = curl_slist_append(headers, "Content-Type: application/json");
65-
// curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
64+
/* headers = curl_slist_append(headers, "Accept: application/json"); */
65+
/* headers = curl_slist_append(headers, "Content-Type: application/json"); */
66+
/* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); */
6667

6768
/* set timeout */
6869
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
@@ -72,16 +73,12 @@ int send_log_to_server(const string uid, const string log, int test=1) {
7273
res = curl_easy_perform(curl);
7374
/* Check for errors */
7475
if(res != CURLE_OK){
75-
LOG_ERROR << "curl_easy_perform() failed: "<< curl_easy_strerror(res);
76-
// // try old cert once
77-
// curl_easy_setopt(curl, CURLOPT_CAINFO, OLD_CAFILE);
78-
// res = curl_easy_perform(curl);
79-
// if(res != CURLE_OK){
80-
// LOG_ERROR << "curl_easy_perform() failed: "<< curl_easy_strerror(res)
81-
// << "\nCAFILE: " << OLD_CAFILE << endl;
82-
// }
83-
} else
76+
LOG_DEBUG << "curl_easy_perform() failed: "<< curl_easy_strerror(res);
77+
} else {
8478
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &response_code);
79+
LOG_DEBUG << "CURL response code: " << response_code;
80+
}
81+
8582

8683
/* always cleanup */
8784
curl_easy_cleanup(curl);

test/test_typtop.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ TEST_CASE("Test TypTop DB") {
295295
}
296296
}
297297

298+
TEST_CASE("Upload"){
299+
SECTION("Simple upload") {
300+
REQUIRE(send_log_to_server("test-uid", "test-log-loerm-ipsum", 1));
301+
}
302+
}
303+
298304
TEST_CASE("Timing") {
299305
remove(_db_fname.c_str());
300306
TypTopTest tp;

0 commit comments

Comments
 (0)