I started addressing the issue I created 2 years ago about this shit

being unreadable. Hopefully this makes the thing more clear and
readable.

Some improvements could be done to the upload_file_* thing. As there's
really no need to pass argc and argv to those. And passing argv to
that is an overkill

Solution: To create a const char* variable and save the filename in
there and pass that to the functions. Simple as that. But i'm a lazy
motherfucker and I'm tired. Boss.
This commit is contained in:
(≧◡≦) 2025-01-21 23:04:41 +01:00
parent 968a288866
commit d5da9a9574
6 changed files with 161 additions and 90 deletions

View file

@ -1,8 +1,7 @@
PROG += sakisafecli
SRCS += funcs.c sakisafecli.c config.c
MAN += sakisafecli.1 sakisafeclirc.5
LDADD += -lssl -lz -lpthread -lnghttp2 -lcurl -lconfig -lcrypto -L/usr/local/lib
LDADD += -lpthread -lcurl -lconfig -L/usr/local/lib -fPIC
PREFIX = /usr/local
# Use libbsd features if wanted

View file

@ -1,4 +1,8 @@
#pragma once
#include <curl/curl.h>
#include <libconfig.h>
#include <stdbool.h>
/* Parse the config file */
void
@ -6,4 +10,21 @@ parse_config_file(FILE *config);
/* Print the current settings */
void
print_config();
print_config();
/* Internal variables */
extern CURL *easy_handle;
extern char *buffer;
/* Config variables */
extern bool ipv6_flag, ipv4_flag, http_proxy_flag,
socks_proxy_flag, silent_flag, paste_flag;
extern char *http_proxy_url, *socks_proxy_url;
extern char *ssh_key_path;
extern char *server;
extern const char *path;

View file

@ -5,7 +5,10 @@
#include <libconfig.h>
#include <curl/curl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <getopt.h>
#include "sakisafecli.h"
#include "config.h"
size_t
write_data(void *buffer, size_t size, size_t nmemb, void *userp)
@ -31,7 +34,7 @@ print_help()
"-P|--http-proxy: http proxy to use e.g. http://127.0.0.1:4444",
"-p|--socks-proxy: SOCK proxy to use e.g. 127.0.0.1:9050",
"-6|--ipv6: uses IPv6 only",
"-4|--ipv6: uses IPv4 only",
"-4|--ipv6: uses IPv4 only",
"-S|--silent: doesn't print progress",
"-x|--paste: read file from stdin",
"-C: print current settings",
@ -40,8 +43,11 @@ print_help()
}
size_t
progress(
void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
progress(void *clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow)
{
/* I don't know why the fuck I have to do this */
if(ultotal == 0) {
@ -54,3 +60,93 @@ progress(
fflush(stdout);
return 0;
}
int
get_protocol(char *server)
{
if(strstr(server, "http://") != NULL || strstr(server, "https://"))
return CURLPROTO_HTTP;
else if(strstr(server, "scp://") != NULL)
return CURLPROTO_SCP;
else
return -1;
}
void
die(const char *msg)
{
fprintf(stderr, "%i: %s", errno, msg);
}
void
init_sakisafe_options()
{
ipv6_flag = http_proxy_flag,
socks_proxy_flag = paste_flag = silent_flag = false;
ipv4_flag = true;
ssh_key_path = NULL;
server = "https://meth.cat";
path = ".cache/sakisafelinks";
}
int
upload_file_http(int argc, char **argv)
{
curl_mime *mime;
mime = curl_mime_init(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
if(!mime) {
fprintf(stderr, "Error initializing curl_mime\n");
return -1;
}
curl_mimepart *file_data;
file_data = curl_mime_addpart(mime);
char *filename = argv[optind - 1];
/* Get file from stdin */
if(paste_flag)
filename = "/dev/stdin";
curl_mime_filedata(file_data, filename);
curl_mime_name(file_data, "file");
if(paste_flag)
curl_mime_filename(file_data, "-");
curl_easy_perform(easy_handle);
if(!silent_flag)
putchar('\n');
puts(buffer);
curl_mime_free(mime);
return 0;
}
int
upload_file_scp(int argc, char **argv)
{
curl_easy_setopt(easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
char path[256];
char *filename = argv[optind];
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
FILE *fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "%s", strerror(errno));
exit(-1);
}
struct stat st;
stat(argv[optind], &st);
snprintf(path, 256, "%s/%s", server, filename);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt(
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
int ret = curl_easy_perform(easy_handle);
putchar('\n');
if(ret != 0) {
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
}
fclose(fp);
return 0;
}

View file

@ -22,3 +22,18 @@ progress(
/* Print config */
void
print_config();
int
init_sakisafe_options();
int
get_protocol(char *server);
int
die(char *msg);
int
upload_file_http(int argc, char **argv);
int
upload_file_scp(int argc, char **argv);

View file

@ -18,16 +18,20 @@
#include "funcs.h"
#include "sakisafecli.h"
CURL *easy_handle;
char *buffer;
/* Config variables */
bool ipv6_flag = false, ipv4_flag = false, http_proxy_flag = false,
socks_proxy_flag = false, silent_flag = false, paste_flag = false;
bool ipv6_flag, ipv4_flag, http_proxy_flag,
socks_proxy_flag, silent_flag, paste_flag;
char *http_proxy_url, *socks_proxy_url;
char *ssh_key_path = NULL;
char *ssh_key_path;
char *server;
const char *path;
char *server = "https://lainsafe.delegao.moe";
const char *path = ".cache/sakisafelinks";
int
main(int argc, char **argv)
@ -39,12 +43,10 @@ main(int argc, char **argv)
}
#endif
char *form_key = "file";
char *buffer = (char *)calloc(1024, sizeof(char));
buffer = (char *)calloc(1024, sizeof(char));
if(buffer == NULL) {
fprintf(stderr, "Error allocating memory!\n");
fprintf(stderr, "Error allocating memory for buffer!\n");
return -1;
}
char config_location[512];
@ -71,7 +73,7 @@ main(int argc, char **argv)
}
/* libcurl initialization */
CURL *easy_handle = curl_easy_init();
easy_handle = curl_easy_init();
if(!easy_handle) {
fprintf(stderr, "Error initializing libcurl\n");
@ -84,7 +86,7 @@ main(int argc, char **argv)
curl_easy_cleanup(easy_handle);
return -1;
}
init_sakisafe_options();
int option_index = 0;
static struct option long_options[] = {
{ "server", required_argument, 0, 's' },
@ -159,12 +161,16 @@ main(int argc, char **argv)
}
/* curl options */
curl_easy_setopt(easy_handle, CURLOPT_USERAGENT, "curl");
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
curl_easy_setopt(easy_handle, CURLOPT_URL, server);
if(curl_easy_setopt(easy_handle, CURLOPT_USERAGENT, "curl") != 0)
die("Error setting CURLOPT_USERAGENT");
if(curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data) != 0)
die("Error setting CURLOPT_WRITEFUNCTION");
if(curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer) != 0)
die("error setting CURLOPT_WRITEDATA");
if(curl_easy_setopt(easy_handle, CURLOPT_URL, server) != 0)
die("error setting CURLOPT_URL");
int protocol = get_protocol(server);
/* Proxy options */
@ -207,61 +213,11 @@ main(int argc, char **argv)
/* Process HTTP uploads */
if(protocol == CURLPROTO_HTTP) {
curl_mime *mime;
mime = curl_mime_init(easy_handle);
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
if(!mime) {
fprintf(stderr, "Error initializing curl_mime\n");
}
curl_mimepart *file_data;
file_data = curl_mime_addpart(mime);
char *filename = argv[optind];
if(paste_flag)
filename = "/dev/stdin";
curl_mime_filedata(file_data, filename);
curl_mime_name(file_data, form_key);
if(paste_flag)
curl_mime_filename(file_data, "-");
curl_easy_perform(easy_handle);
if(!silent_flag)
putchar('\n');
puts(buffer);
curl_mime_free(mime);
upload_file_http(argc, argv);
}
/* Process SCP uploads */
else if(protocol == CURLPROTO_SCP) {
curl_easy_setopt(
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
char path[256];
char *filename = argv[optind];
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
FILE *fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "%s", strerror(errno));
exit(-1);
}
struct stat st;
stat(argv[optind], &st);
snprintf(path, 256, "%s/%s", server, filename);
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
curl_easy_setopt(
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
int ret = curl_easy_perform(easy_handle);
putchar('\n');
if(ret != 0) {
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
}
upload_file_scp(argc, argv);
} else {
puts("Unsupported protocol");
return -1;
@ -273,13 +229,3 @@ main(int argc, char **argv)
return 0;
}
int
get_protocol(char *server)
{
if(strstr(server, "http://") != NULL || strstr(server, "https://"))
return CURLPROTO_HTTP;
else if(strstr(server, "scp://") != NULL)
return CURLPROTO_SCP;
else
return -1;
}

View file

@ -22,12 +22,6 @@ store_link(const char *path, const char *buf);
void
print_help();
size_t
progress(void *clientp,
curl_off_t dltotal,
curl_off_t dlnow,
curl_off_t ultotal,
curl_off_t ulnow);
void
parse_config_file(FILE *config);