lainsafe/clainsafecli.c

140 lines
2.8 KiB
C
Raw Normal View History

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
2021-02-02 22:28:17 +00:00
#include <curl/curl.h>
size_t static write_data(void *buffer, size_t size, size_t nmemb,
void *userp)
{
memcpy(userp, buffer, nmemb*size);
return 0;
}
2021-02-01 22:52:28 +00:00
void
print_usage()
{
printf("USAGE: clainsafecli [--tor|--i2p] [--server] file\n");
2021-02-01 22:52:28 +00:00
return;
}
void
print_help()
{
printf("--server <server>: specifies the lainsafe server\n%s\n%s\n%s",
"--tor: uses tor",
"--help: print this message\n",
"--i2p: uses i2p HTTP proxy"
);
2021-02-01 22:52:28 +00:00
return;
}
int
main(int argc, char **argv)
{
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
2021-02-02 22:28:17 +00:00
int tor_flag, i2p_flag;
tor_flag = i2p_flag = 0;
char *buffer = (char *)calloc(1024,sizeof(char));
2021-02-03 16:39:34 +00:00
if(buffer == NULL) {
fprintf(stderr,"Error allocating memory!\n");
}
char server[256] = "https://lainsafe.kalli.st";
CURL *easy_handle = curl_easy_init();
if(!easy_handle) {
fprintf(stderr,"Error initializing libcurl\n");
return -1;
}
if(argc == optind) {
2021-02-01 22:52:28 +00:00
print_usage();
return -1;
}
int option_index = 0;
static struct option long_options[] = {
{"server",required_argument,0,'s'},
2021-02-01 22:52:28 +00:00
{"help" ,no_argument ,0,'h'},
{"tor" ,no_argument ,0,'t'},
2021-02-02 22:28:17 +00:00
{"i2p" ,no_argument ,0,'i'},
{0 ,0 ,0, 0 }
};
int c = 0;
2021-02-02 22:28:17 +00:00
while((c = getopt_long(argc,argv, "htis:",
long_options,&option_index)) != -1) {
switch(c) {
case 's':
strncpy(server,optarg,256);
break;
2021-02-01 22:52:28 +00:00
case 'h':
print_help();
return 0;
break;
2021-02-02 22:28:17 +00:00
case 't':
tor_flag = 1;
break;
case 'i':
i2p_flag = 1;
break;
case '?':
2021-02-01 22:52:28 +00:00
print_usage();
return 0;
break;
default:
2021-02-01 22:52:28 +00:00
print_usage();
return 0;
break;
}
}
2021-02-02 22:28:17 +00:00
/* curl options */
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);
2021-02-01 22:52:28 +00:00
2021-02-02 22:28:17 +00:00
/* Proxy options */
if(tor_flag && i2p_flag) {
fprintf(stderr,"Tor and I2P can't be used at once\n");
return -1;
} else if(tor_flag) {
curl_easy_setopt(easy_handle,CURLOPT_PROXY,"127.0.0.1:9050");
curl_easy_setopt(easy_handle,CURLOPT_PROXYTYPE,
CURLPROXY_SOCKS5_HOSTNAME);
2021-02-02 22:28:17 +00:00
} else if(i2p_flag) {
curl_easy_setopt(easy_handle,CURLOPT_PROXY,"127.0.0.1:4444");
curl_easy_setopt(easy_handle,CURLOPT_PROXYTYPE,
CURLPROXY_HTTP);
}
2021-02-02 22:28:17 +00:00
2021-02-01 22:52:28 +00:00
/* Form parameters */
/* File name */
curl_formadd(&post,&last,
CURLFORM_COPYNAME, "file",
CURLFORM_FILE,argv[optind],
CURLFORM_END);
2021-02-01 22:52:28 +00:00
/* Actual file content */
curl_formadd(&post,&last,
CURLFORM_COPYNAME, "file",
CURLFORM_COPYCONTENTS,argv[optind],
CURLFORM_END);
curl_easy_setopt(easy_handle,CURLOPT_HTTPPOST,post);
curl_easy_perform(easy_handle);
puts(buffer);
free(buffer);
curl_formfree(post);
curl_easy_cleanup(easy_handle);
2021-02-02 22:28:17 +00:00
return 0;
}