Skip to content

Commit

Permalink
Allow to load remote config with curl
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Aug 24, 2015
1 parent 72c4c5e commit 1305ed9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Version 1.2
- Add cryptonight-light (Aeon)
- Add Lyra2REv2 algo (Vertcoin)
- Allow to load a remote config with curl
- Algorithm parameter is now case insensitive
- Drop anime algo (dead coin)
- Checkup on arm, tested ok on Tegra K1 (CyanogenMod 12.1)

version 1.1 (Tanguy Pruvot)
Expand Down
7 changes: 6 additions & 1 deletion cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,12 @@ void parse_arg(int key, char *arg)
break;
case 'c': {
json_error_t err;
json_t *config = JSON_LOAD_FILE(arg, &err);
json_t *config;
if (arg && strstr(arg, "://")) {
config = json_load_url(arg, &err);
} else {
config = JSON_LOADF(arg, &err);
}
if (!json_is_object(config)) {
if (err.line < 0)
fprintf(stderr, "%s\n", err.text);
Expand Down
6 changes: 4 additions & 2 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ static inline void le16enc(void *pp, uint16_t x)

#if JANSSON_MAJOR_VERSION >= 2
#define JSON_LOADS(str, err_ptr) json_loads(str, 0, err_ptr)
#define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, 0, err_ptr)
#define JSON_LOADF(path, err_ptr) json_load_file(path, 0, err_ptr)
#else
#define JSON_LOADS(str, err_ptr) json_loads(str, err_ptr)
#define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, err_ptr)
#define JSON_LOADF(path, err_ptr) json_load_file(path, err_ptr)
#endif

json_t* json_load_url(char* cfg_url, json_error_t *err);

void sha256_init(uint32_t *state);
void sha256_transform(uint32_t *state, const uint32_t *block, int swap);
void sha256d(unsigned char *hash, const unsigned char *data, int len);
Expand Down
46 changes: 46 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,52 @@ json_t *json_rpc_call(CURL *curl, const char *url,
return NULL;
}

/* used to load a remote config */
json_t* json_load_url(char* cfg_url, json_error_t *err)
{
char err_str[CURL_ERROR_SIZE] = { 0 };
struct data_buffer all_data = { 0 };
int rc = 0; json_t *cfg = NULL;
CURL *curl = curl_easy_init();
if (unlikely(!curl)) {
applog(LOG_ERR, "Remote config init failed!");
return NULL;
}
curl_easy_setopt(curl, CURLOPT_URL, cfg_url);
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, err_str);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
if (opt_proxy) {
curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
} else if (getenv("http_proxy")) {
if (getenv("all_proxy"))
curl_easy_setopt(curl, CURLOPT_PROXY, getenv("all_proxy"));
else if (getenv("ALL_PROXY"))
curl_easy_setopt(curl, CURLOPT_PROXY, getenv("ALL_PROXY"));
else
curl_easy_setopt(curl, CURLOPT_PROXY, "");
}
rc = curl_easy_perform(curl);
if (rc) {
applog(LOG_ERR, "Remote config read failed: %s", err_str);
goto err_out;
}
if (!all_data.buf || !all_data.len) {
applog(LOG_ERR, "Empty data received for config");
goto err_out;
}

cfg = JSON_LOADS((char*)all_data.buf, err);
err_out:
curl_easy_cleanup(curl);
return cfg;
}

void bin2hex(char *s, const unsigned char *p, size_t len)
{
for (size_t i = 0; i < len; i++)
Expand Down

0 comments on commit 1305ed9

Please sign in to comment.