Skip to content

Commit d5da9a9

Browse files
committed
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.
1 parent 968a288 commit d5da9a9

File tree

6 files changed

+162
-91
lines changed

6 files changed

+162
-91
lines changed

sakisafecli/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
PROG += sakisafecli
32
SRCS += funcs.c sakisafecli.c config.c
43
MAN += sakisafecli.1 sakisafeclirc.5
5-
LDADD += -lssl -lz -lpthread -lnghttp2 -lcurl -lconfig -lcrypto -L/usr/local/lib
4+
LDADD += -lpthread -lcurl -lconfig -L/usr/local/lib -fPIC
65
PREFIX = /usr/local
76

87
# Use libbsd features if wanted

sakisafecli/config.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
#pragma once
2+
#include <curl/curl.h>
13
#include <libconfig.h>
4+
#include <stdbool.h>
5+
26

37
/* Parse the config file */
48
void
59
parse_config_file(FILE *config);
610

711
/* Print the current settings */
812
void
9-
print_config();
13+
print_config();
14+
15+
/* Internal variables */
16+
17+
extern CURL *easy_handle;
18+
19+
extern char *buffer;
20+
/* Config variables */
21+
22+
extern bool ipv6_flag, ipv4_flag, http_proxy_flag,
23+
socks_proxy_flag, silent_flag, paste_flag;
24+
25+
extern char *http_proxy_url, *socks_proxy_url;
26+
extern char *ssh_key_path;
27+
28+
extern char *server;
29+
extern const char *path;
30+

sakisafecli/funcs.c

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include <libconfig.h>
66
#include <curl/curl.h>
77
#include <stdbool.h>
8+
#include <sys/stat.h>
9+
#include <getopt.h>
810
#include "sakisafecli.h"
11+
#include "config.h"
912

1013
size_t
1114
write_data(void *buffer, size_t size, size_t nmemb, void *userp)
@@ -31,7 +34,7 @@ print_help()
3134
"-P|--http-proxy: http proxy to use e.g. http://127.0.0.1:4444",
3235
"-p|--socks-proxy: SOCK proxy to use e.g. 127.0.0.1:9050",
3336
"-6|--ipv6: uses IPv6 only",
34-
"-4|--ipv6: uses IPv4 only",
37+
"-4|--ipv6: uses IPv4 only",
3538
"-S|--silent: doesn't print progress",
3639
"-x|--paste: read file from stdin",
3740
"-C: print current settings",
@@ -40,8 +43,11 @@ print_help()
4043
}
4144

4245
size_t
43-
progress(
44-
void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
46+
progress(void *clientp,
47+
curl_off_t dltotal,
48+
curl_off_t dlnow,
49+
curl_off_t ultotal,
50+
curl_off_t ulnow)
4551
{
4652
/* I don't know why the fuck I have to do this */
4753
if(ultotal == 0) {
@@ -54,3 +60,93 @@ progress(
5460
fflush(stdout);
5561
return 0;
5662
}
63+
int
64+
get_protocol(char *server)
65+
{
66+
if(strstr(server, "http://") != NULL || strstr(server, "https://"))
67+
return CURLPROTO_HTTP;
68+
else if(strstr(server, "scp://") != NULL)
69+
return CURLPROTO_SCP;
70+
else
71+
return -1;
72+
}
73+
void
74+
die(const char *msg)
75+
{
76+
fprintf(stderr, "%i: %s", errno, msg);
77+
}
78+
79+
void
80+
init_sakisafe_options()
81+
{
82+
ipv6_flag = http_proxy_flag,
83+
socks_proxy_flag = paste_flag = silent_flag = false;
84+
85+
ipv4_flag = true;
86+
87+
ssh_key_path = NULL;
88+
server = "https://meth.cat";
89+
path = ".cache/sakisafelinks";
90+
}
91+
92+
int
93+
upload_file_http(int argc, char **argv)
94+
{
95+
96+
curl_mime *mime;
97+
mime = curl_mime_init(easy_handle);
98+
99+
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
100+
if(!mime) {
101+
fprintf(stderr, "Error initializing curl_mime\n");
102+
return -1;
103+
}
104+
105+
curl_mimepart *file_data;
106+
file_data = curl_mime_addpart(mime);
107+
char *filename = argv[optind - 1];
108+
/* Get file from stdin */
109+
if(paste_flag)
110+
filename = "/dev/stdin";
111+
curl_mime_filedata(file_data, filename);
112+
curl_mime_name(file_data, "file");
113+
if(paste_flag)
114+
curl_mime_filename(file_data, "-");
115+
curl_easy_perform(easy_handle);
116+
if(!silent_flag)
117+
putchar('\n');
118+
puts(buffer);
119+
curl_mime_free(mime);
120+
return 0;
121+
}
122+
123+
int
124+
upload_file_scp(int argc, char **argv)
125+
{
126+
curl_easy_setopt(easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
127+
128+
char path[256];
129+
char *filename = argv[optind];
130+
131+
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
132+
FILE *fp = fopen(filename, "r");
133+
if(fp == NULL) {
134+
fprintf(stderr, "%s", strerror(errno));
135+
exit(-1);
136+
}
137+
138+
struct stat st;
139+
stat(argv[optind], &st);
140+
snprintf(path, 256, "%s/%s", server, filename);
141+
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
142+
curl_easy_setopt(
143+
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
144+
145+
int ret = curl_easy_perform(easy_handle);
146+
putchar('\n');
147+
if(ret != 0) {
148+
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
149+
}
150+
fclose(fp);
151+
return 0;
152+
}

sakisafecli/funcs.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ progress(
2222
/* Print config */
2323
void
2424
print_config();
25+
26+
int
27+
init_sakisafe_options();
28+
29+
int
30+
get_protocol(char *server);
31+
32+
int
33+
die(char *msg);
34+
35+
int
36+
upload_file_http(int argc, char **argv);
37+
38+
int
39+
upload_file_scp(int argc, char **argv);

sakisafecli/sakisafecli.c

Lines changed: 25 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
#include "funcs.h"
1919
#include "sakisafecli.h"
2020

21+
CURL *easy_handle;
22+
23+
char *buffer;
2124
/* Config variables */
2225

23-
bool ipv6_flag = false, ipv4_flag = false, http_proxy_flag = false,
24-
socks_proxy_flag = false, silent_flag = false, paste_flag = false;
26+
bool ipv6_flag, ipv4_flag, http_proxy_flag,
27+
socks_proxy_flag, silent_flag, paste_flag;
2528

2629
char *http_proxy_url, *socks_proxy_url;
27-
char *ssh_key_path = NULL;
30+
char *ssh_key_path;
31+
32+
char *server;
33+
const char *path;
2834

29-
char *server = "https://lainsafe.delegao.moe";
30-
const char *path = ".cache/sakisafelinks";
3135

3236
int
3337
main(int argc, char **argv)
@@ -39,12 +43,10 @@ main(int argc, char **argv)
3943
}
4044
#endif
4145

42-
char *form_key = "file";
43-
44-
char *buffer = (char *)calloc(1024, sizeof(char));
46+
buffer = (char *)calloc(1024, sizeof(char));
4547

4648
if(buffer == NULL) {
47-
fprintf(stderr, "Error allocating memory!\n");
49+
fprintf(stderr, "Error allocating memory for buffer!\n");
4850
return -1;
4951
}
5052
char config_location[512];
@@ -71,7 +73,7 @@ main(int argc, char **argv)
7173
}
7274
/* libcurl initialization */
7375

74-
CURL *easy_handle = curl_easy_init();
76+
easy_handle = curl_easy_init();
7577

7678
if(!easy_handle) {
7779
fprintf(stderr, "Error initializing libcurl\n");
@@ -84,7 +86,7 @@ main(int argc, char **argv)
8486
curl_easy_cleanup(easy_handle);
8587
return -1;
8688
}
87-
89+
init_sakisafe_options();
8890
int option_index = 0;
8991
static struct option long_options[] = {
9092
{ "server", required_argument, 0, 's' },
@@ -159,12 +161,16 @@ main(int argc, char **argv)
159161
}
160162

161163
/* curl options */
162-
curl_easy_setopt(easy_handle, CURLOPT_USERAGENT, "curl");
163-
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
164-
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);
165-
166-
curl_easy_setopt(easy_handle, CURLOPT_URL, server);
167-
164+
if(curl_easy_setopt(easy_handle, CURLOPT_USERAGENT, "curl") != 0)
165+
die("Error setting CURLOPT_USERAGENT");
166+
if(curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data) != 0)
167+
die("Error setting CURLOPT_WRITEFUNCTION");
168+
if(curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer) != 0)
169+
die("error setting CURLOPT_WRITEDATA");
170+
171+
if(curl_easy_setopt(easy_handle, CURLOPT_URL, server) != 0)
172+
die("error setting CURLOPT_URL");
173+
168174
int protocol = get_protocol(server);
169175

170176
/* Proxy options */
@@ -207,61 +213,11 @@ main(int argc, char **argv)
207213
/* Process HTTP uploads */
208214

209215
if(protocol == CURLPROTO_HTTP) {
210-
curl_mime *mime;
211-
mime = curl_mime_init(easy_handle);
212-
213-
curl_easy_setopt(easy_handle, CURLOPT_MIMEPOST, mime);
214-
if(!mime) {
215-
fprintf(stderr, "Error initializing curl_mime\n");
216-
}
217-
218-
curl_mimepart *file_data;
219-
file_data = curl_mime_addpart(mime);
220-
char *filename = argv[optind];
221-
222-
if(paste_flag)
223-
filename = "/dev/stdin";
224-
225-
curl_mime_filedata(file_data, filename);
226-
curl_mime_name(file_data, form_key);
227-
if(paste_flag)
228-
curl_mime_filename(file_data, "-");
229-
230-
curl_easy_perform(easy_handle);
231-
if(!silent_flag)
232-
putchar('\n');
233-
puts(buffer);
234-
curl_mime_free(mime);
235-
216+
upload_file_http(argc, argv);
236217
}
237218
/* Process SCP uploads */
238219
else if(protocol == CURLPROTO_SCP) {
239-
curl_easy_setopt(
240-
easy_handle, CURLOPT_SSH_PRIVATE_KEYFILE, ssh_key_path);
241-
242-
char path[256];
243-
char *filename = argv[optind];
244-
245-
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, true);
246-
FILE *fp = fopen(filename, "r");
247-
if(fp == NULL) {
248-
fprintf(stderr, "%s", strerror(errno));
249-
exit(-1);
250-
}
251-
252-
struct stat st;
253-
stat(argv[optind], &st);
254-
snprintf(path, 256, "%s/%s", server, filename);
255-
curl_easy_setopt(easy_handle, CURLOPT_READDATA, fp);
256-
curl_easy_setopt(
257-
easy_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)st.st_size);
258-
259-
int ret = curl_easy_perform(easy_handle);
260-
putchar('\n');
261-
if(ret != 0) {
262-
fprintf(stderr, "%i: %s\n", ret, curl_easy_strerror(ret));
263-
}
264-
220+
upload_file_scp(argc, argv);
265221
} else {
266222
puts("Unsupported protocol");
267223
return -1;
@@ -273,13 +229,3 @@ main(int argc, char **argv)
273229
return 0;
274230
}
275231

276-
int
277-
get_protocol(char *server)
278-
{
279-
if(strstr(server, "http://") != NULL || strstr(server, "https://"))
280-
return CURLPROTO_HTTP;
281-
else if(strstr(server, "scp://") != NULL)
282-
return CURLPROTO_SCP;
283-
else
284-
return -1;
285-
}

sakisafecli/sakisafecli.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ store_link(const char *path, const char *buf);
2222
void
2323
print_help();
2424

25-
size_t
26-
progress(void *clientp,
27-
curl_off_t dltotal,
28-
curl_off_t dlnow,
29-
curl_off_t ultotal,
30-
curl_off_t ulnow);
3125

3226
void
3327
parse_config_file(FILE *config);

0 commit comments

Comments
 (0)