Added a header for clainsafecli which contains the functions. Also
added the default curl progress bar. Added deprecation note for lainsafecli.
This commit is contained in:
parent
e87c6c9b1c
commit
acb47dd5bb
3 changed files with 98 additions and 78 deletions
|
@ -5,47 +5,10 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
#include "clainsafecli.h"
|
||||||
size_t static write_data(void *buffer, size_t size, size_t nmemb,
|
|
||||||
void *userp)
|
|
||||||
{
|
|
||||||
memcpy(userp, buffer, nmemb*size);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
print_usage()
|
|
||||||
{
|
|
||||||
printf("USAGE: clainsafecli [--tor|--i2p] [--server] file\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
store_link(const char *path, const char *buf)
|
|
||||||
{
|
|
||||||
FILE *fp = fopen(path,"a+");
|
|
||||||
if(fp == NULL) {
|
|
||||||
fprintf(stderr,"Error opening file %i: %s\n",errno,
|
|
||||||
strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fwrite(buf,strlen(buf),1,fp);
|
|
||||||
fputc('\n',fp);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
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"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
|
@ -55,22 +18,23 @@ main(int argc, char **argv)
|
||||||
|
|
||||||
int tor_flag, i2p_flag;
|
int tor_flag, i2p_flag;
|
||||||
tor_flag = i2p_flag = 0;
|
tor_flag = i2p_flag = 0;
|
||||||
|
|
||||||
char *buffer = (char *)calloc(1024,sizeof(char));
|
char *buffer = (char *)calloc(1024,sizeof(char));
|
||||||
if(buffer == NULL) {
|
if(buffer == NULL) {
|
||||||
fprintf(stderr,"Error allocating memory!\n");
|
fprintf(stderr,"Error allocating memory!\n");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
CURL *easy_handle = curl_easy_init();
|
CURL *easy_handle = curl_easy_init();
|
||||||
|
|
||||||
if(!easy_handle) {
|
if(!easy_handle) {
|
||||||
fprintf(stderr,"Error initializing libcurl\n");
|
fprintf(stderr,"Error initializing libcurl\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(argc == optind) {
|
if(argc == optind) {
|
||||||
print_usage();
|
print_usage();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
{"server",required_argument,0,'s'},
|
{"server",required_argument,0,'s'},
|
||||||
|
@ -113,14 +77,14 @@ main(int argc, char **argv)
|
||||||
fprintf(stderr,"Error opening file\n");
|
fprintf(stderr,"Error opening file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* curl options */
|
/* curl options */
|
||||||
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data);
|
curl_easy_setopt(easy_handle,CURLOPT_WRITEFUNCTION, write_data);
|
||||||
curl_easy_setopt(easy_handle,CURLOPT_WRITEDATA,buffer);
|
curl_easy_setopt(easy_handle,CURLOPT_WRITEDATA,buffer);
|
||||||
curl_easy_setopt(easy_handle,CURLOPT_URL,server);
|
curl_easy_setopt(easy_handle,CURLOPT_URL,server);
|
||||||
|
|
||||||
/* Proxy options */
|
/* Proxy options */
|
||||||
|
|
||||||
if(tor_flag && i2p_flag) {
|
if(tor_flag && i2p_flag) {
|
||||||
fprintf(stderr,"Tor and I2P can't be used at once\n");
|
fprintf(stderr,"Tor and I2P can't be used at once\n");
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -147,12 +111,20 @@ main(int argc, char **argv)
|
||||||
CURLFORM_COPYCONTENTS,argv[optind],
|
CURLFORM_COPYCONTENTS,argv[optind],
|
||||||
CURLFORM_END);
|
CURLFORM_END);
|
||||||
|
|
||||||
|
/* Progress bar
|
||||||
|
*
|
||||||
|
* TODO: Use a custom progress bar rather than
|
||||||
|
* default curl progress bar
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
curl_easy_setopt(easy_handle,CURLOPT_NOPROGRESS,0L);
|
||||||
curl_easy_setopt(easy_handle,CURLOPT_HTTPPOST,post);
|
curl_easy_setopt(easy_handle,CURLOPT_HTTPPOST,post);
|
||||||
|
|
||||||
curl_easy_perform(easy_handle);
|
curl_easy_perform(easy_handle);
|
||||||
|
|
||||||
puts(buffer);
|
puts(buffer);
|
||||||
|
|
||||||
curl_formfree(post);
|
curl_formfree(post);
|
||||||
curl_easy_cleanup(easy_handle);
|
curl_easy_cleanup(easy_handle);
|
||||||
|
|
||||||
|
@ -165,3 +137,31 @@ main(int argc, char **argv)
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t static write_data(void *buffer, size_t size, size_t nmemb,
|
||||||
|
void *userp)
|
||||||
|
{
|
||||||
|
memcpy(userp, buffer, nmemb*size);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
print_usage()
|
||||||
|
{
|
||||||
|
printf("USAGE: clainsafecli [--tor|--i2p] [--server] file\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
store_link(const char *path, const char *buf)
|
||||||
|
{
|
||||||
|
FILE *fp = fopen(path,"a+");
|
||||||
|
if(fp == NULL) {
|
||||||
|
fprintf(stderr,"Error opening file %i: %s\n",errno,
|
||||||
|
strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fwrite(buf,strlen(buf),1,fp);
|
||||||
|
fputc('\n',fp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
17
clainsafecli/clainsafecli.h
Normal file
17
clainsafecli/clainsafecli.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
size_t
|
||||||
|
static write_data(void *buffer, size_t size, size_t nmemb,
|
||||||
|
void *userp);
|
||||||
|
|
||||||
|
void
|
||||||
|
print_usage();
|
||||||
|
|
||||||
|
int progress_func(void* ptr, double TotalToDownload, double NowDownloaded,
|
||||||
|
double TotalToUpload, double NowUploaded);
|
||||||
|
|
||||||
|
int
|
||||||
|
store_link(const char *path, const char *buf);
|
||||||
|
|
||||||
|
void
|
||||||
|
print_help();
|
63
lainsafecli
63
lainsafecli
|
@ -16,6 +16,8 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with lainsafe. If not, see <https://www.gnu.org/licenses/>.
|
# along with lainsafe. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# This script is kind of deprecated. Use clainsafecli instead.
|
||||||
|
|
||||||
use Getopt::Long;
|
use Getopt::Long;
|
||||||
use LWP::UserAgent;
|
use LWP::UserAgent;
|
||||||
my $ua = LWP::UserAgent->new;
|
my $ua = LWP::UserAgent->new;
|
||||||
|
@ -66,24 +68,24 @@ EOF
|
||||||
# Subs
|
# Subs
|
||||||
|
|
||||||
sub help
|
sub help
|
||||||
{
|
{
|
||||||
print "lainsafecli, a command line interface for lainsafe.\n";
|
print "lainsafecli, a command line interface for lainsafe.\n";
|
||||||
print "USAGE: lainsafecli [--tor | --i2p] [--server] FILE\n\n";
|
print "USAGE: lainsafecli [--tor | --i2p] [--server] FILE\n\n";
|
||||||
print "if --server not given, $DEFAULT_SERVER is used.\n";
|
print "if --server not given, $DEFAULT_SERVER is used.\n";
|
||||||
print "--tor and --i2p are available\n" if $proxy_enabled;
|
print "--tor and --i2p are available\n" if $proxy_enabled;
|
||||||
print "--tor and --i2p are unavailable, flag are ignored\n" unless $proxy_enabled;
|
print "--tor and --i2p are unavailable, flag are ignored\n" unless $proxy_enabled;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub enable_tor
|
sub enable_tor
|
||||||
{
|
{
|
||||||
my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050');
|
my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050');
|
||||||
}
|
}
|
||||||
|
|
||||||
sub enable_i2p
|
sub enable_i2p
|
||||||
{
|
{
|
||||||
my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444');
|
my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444');
|
||||||
}
|
}
|
||||||
|
|
||||||
## PROGRAM
|
## PROGRAM
|
||||||
|
|
||||||
|
@ -92,12 +94,12 @@ GetOptions ("server=s" => \$DEFAULT_SERVER,
|
||||||
"tor"=> \$tor,
|
"tor"=> \$tor,
|
||||||
"i2p"=>\$i2p,
|
"i2p"=>\$i2p,
|
||||||
"get-response"=>\$get_response
|
"get-response"=>\$get_response
|
||||||
);
|
);
|
||||||
|
|
||||||
&help if $help || not defined $ARGV[0];
|
&help if $help || not defined $ARGV[0];
|
||||||
if ($i2p and $tor) {
|
if ($i2p and $tor) {
|
||||||
print "What are you trying to do? I don't really get you sometimes...\n";
|
print "What are you trying to do? I don't really get you sometimes...\n";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
&enable_tor if $tor and $proxy_enabled;
|
&enable_tor if $tor and $proxy_enabled;
|
||||||
&enable_i2p if $i2p and $proxy_enabled;
|
&enable_i2p if $i2p and $proxy_enabled;
|
||||||
|
@ -121,30 +123,31 @@ substr($DEFAULT_SERVER, 0, 0, 'https://') unless $DEFAULT_SERVER =~ /^(http|http
|
||||||
|
|
||||||
my $url_to_upload = $DEFAULT_SERVER . "/upload.cgi";
|
my $url_to_upload = $DEFAULT_SERVER . "/upload.cgi";
|
||||||
if (!$ua->get($url_to_upload)->is_success) {
|
if (!$ua->get($url_to_upload)->is_success) {
|
||||||
print "$url_to_upload is not running lainsafe. (--get-response to check the error)\n";
|
print "$url_to_upload is not running lainsafe. (--get-response to check the error)\n";
|
||||||
print $ua->get($url_to_upload)->decoded_content if $get_response;
|
print $ua->get($url_to_upload)->decoded_content if $get_response;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$req = $ua->post($url_to_upload,
|
$req = $ua->post($url_to_upload,
|
||||||
Content_Type => 'form-data',
|
Content_Type => 'form-data',
|
||||||
Content => [
|
Content => [
|
||||||
"file" => [ $file ],
|
"file" => [ $file ],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
print $ASCII_ART if $DISPLAY_ASCII;
|
print $ASCII_ART if $DISPLAY_ASCII;
|
||||||
if ($req->{_content} =~ /instance/) # If someone knows how to do it another way, I'm all ears
|
if ($req->{_content} =~ /instance/) # If someone knows how to do it another way, I'm all ears
|
||||||
{
|
{
|
||||||
print $req->{_content} . "\n";
|
print $req->{_content} . "\n";
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
print $req->{_content} . "\n";
|
print $req->{_content} . "\n";
|
||||||
|
|
||||||
if ($STORE_LINKS) {
|
if ($STORE_LINKS) {
|
||||||
open FILE,'>>',$LINKS_FILE or die $!;
|
open FILE,'>>',$LINKS_FILE or die $!;
|
||||||
print FILE $req->{_content} . " $file" ."\n";
|
print FILE $req->{_content} . " $file" ."\n";
|
||||||
close FILE;
|
close FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue