refactor(landerctl): split some files

feature/50-one-time-keys
Jef Roosens 2024-10-03 10:00:44 +02:00
parent 8b7ce0085e
commit 16a7af6865
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 131 additions and 115 deletions

View File

@ -74,9 +74,17 @@ landerctl_err landerctl_post_short(landerctl_ctx *ctx);
landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_paste(landerctl_ctx *ctx);
landerctl_err landerctl_post_file(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx);
int landerctl_parse_args(landerctl_args *out, int argc, char **argv);
int landerctl_cmd_short(landerctl_args *args); int landerctl_cmd_short(landerctl_args *args);
int landerctl_cmd_paste(landerctl_args *args); int landerctl_cmd_paste(landerctl_args *args);
int landerctl_cmd_file(landerctl_args *args); int landerctl_cmd_file(landerctl_args *args);
int landerctl_parse_args(landerctl_args *out, int argc, char **argv);
int landerctl_curl_init(landerctl_curl *out);
int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args,
const char *key);
int landerctl_curl_perform(landerctl_curl *curl);
int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args);
void landerctl_curl_cleanup(landerctl_curl *curl);
#endif #endif

View File

@ -7,120 +7,6 @@
#include "landerctl.h" #include "landerctl.h"
int landerctl_curl_init(landerctl_curl *out) {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "Failed to initialize cURL client.\n");
return 8;
}
out->curl = curl;
out->headers = NULL;
return 0;
}
int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args,
const char *key) {
size_t url_len = strlen(args->cfg.server_url) + 4;
if (key != NULL) {
url_len += strlen(key);
}
char mode_char;
switch (args->mode) {
case landerctl_mode_short:
mode_char = 's';
break;
case landerctl_mode_paste:
mode_char = 'p';
break;
case landerctl_mode_file:
mode_char = 'f';
break;
// Shouldn't be able to happen
default:
return 10;
}
char url[url_len + 1];
if (key == NULL) {
sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char,
args->secure ? "l" : "");
} else {
sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char,
args->secure ? "l" : "", key);
}
curl_easy_setopt(curl->curl, CURLOPT_URL, url);
// Add API key header
char api_key_header[strlen(args->cfg.api_key) + 12];
sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key);
curl->headers = curl_slist_append(curl->headers, api_key_header);
curl_easy_setopt(curl->curl, CURLOPT_USERAGENT,
"landerctl/" LANDER_VERSION "");
if (args->verbose) {
curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L);
}
curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle);
curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg);
return 0;
}
int landerctl_curl_perform(landerctl_curl *curl) {
curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers);
int res = curl_easy_perform(curl->curl);
if (res != CURLE_OK) {
fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res,
curl->err_msg);
}
return res;
}
int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) {
long response_code;
curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code < 200 || response_code > 299) {
fprintf(stderr, "HTTP status code %li\n", response_code);
return 3;
} else {
struct curl_header *location_header;
if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1,
&location_header) == CURLHE_OK) {
printf("%s%s\n", args->cfg.server_url, location_header->value);
} else {
fprintf(stderr, "Server returned a 2xx without a Location header.\n");
return 5;
}
}
return 0;
}
void landerctl_curl_cleanup(landerctl_curl *curl) {
curl_easy_cleanup(curl->curl);
curl_slist_free_all(curl->headers);
}
int landerctl_cmd_short(landerctl_args *args) { int landerctl_cmd_short(landerctl_args *args) {
// TODO argument count check // TODO argument count check
int res; int res;

View File

@ -0,0 +1,122 @@
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include <magic.h>
#include "landerctl.h"
int landerctl_curl_init(landerctl_curl *out) {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "Failed to initialize cURL client.\n");
return 8;
}
out->curl = curl;
out->headers = NULL;
return 0;
}
int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args,
const char *key) {
size_t url_len = strlen(args->cfg.server_url) + 4;
if (key != NULL) {
url_len += strlen(key);
}
char mode_char;
switch (args->mode) {
case landerctl_mode_short:
mode_char = 's';
break;
case landerctl_mode_paste:
mode_char = 'p';
break;
case landerctl_mode_file:
mode_char = 'f';
break;
// Shouldn't be able to happen
default:
return 10;
}
char url[url_len + 1];
if (key == NULL) {
sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char,
args->secure ? "l" : "");
} else {
sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char,
args->secure ? "l" : "", key);
}
curl_easy_setopt(curl->curl, CURLOPT_URL, url);
// Add API key header
char api_key_header[strlen(args->cfg.api_key) + 12];
sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key);
curl->headers = curl_slist_append(curl->headers, api_key_header);
curl_easy_setopt(curl->curl, CURLOPT_USERAGENT,
"landerctl/" LANDER_VERSION "");
if (args->verbose) {
curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L);
}
curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle);
curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg);
return 0;
}
int landerctl_curl_perform(landerctl_curl *curl) {
curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers);
int res = curl_easy_perform(curl->curl);
if (res != CURLE_OK) {
fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res,
curl->err_msg);
}
return res;
}
int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) {
long response_code;
curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code < 200 || response_code > 299) {
fprintf(stderr, "HTTP status code %li\n", response_code);
return 3;
} else {
struct curl_header *location_header;
if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1,
&location_header) == CURLHE_OK) {
printf("%s%s\n", args->cfg.server_url, location_header->value);
} else {
fprintf(stderr, "Server returned a 2xx without a Location header.\n");
return 5;
}
}
return 0;
}
void landerctl_curl_cleanup(landerctl_curl *curl) {
curl_easy_cleanup(curl->curl);
curl_slist_free_all(curl->headers);
}