lots
[lainsafe.git] / lainsafecli
1 #!/usr/bin/perl
2 # Lainsafe cli
3
4 # This file is part of lainsafe.
5
6 # lainsafe is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # lainsafe is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with lainsafe. If not, see <https://www.gnu.org/licenses/>.
18
19 # This script is kind of deprecated. Use clainsafecli instead.
20
21 use Getopt::Long;
22 use LWP::UserAgent;
23 my $ua = LWP::UserAgent->new;
24 use strict;
25 use warnings;
26 # variables
27 my $help;
28 my $tor;
29 my $i2p;
30 my $get_response;
31 my $DEFAULT_SERVER;
32 my $file;
33 my $DISPLAY_ASCII;
34 my $STORE_LINKS;
35 my $LINKS_FILE;
36 my $proxy_enabled = 1;
37 # Default options, if no specified.
38 $DEFAULT_SERVER = "https://lainsafe.delegao.moe";
39 $DISPLAY_ASCII = 1; # 0 if you don't want the ascii
40 $STORE_LINKS = 1; # 0 if you don't want to keep track of your upload
41 $LINKS_FILE = "$ENV{HOME}/.cache/lainsafelinks";
42
43 eval "use LWP::Protocol::socks; 1" or $proxy_enabled = 0;
44 my $ASCII_ART = <<'EOF';
45 _..-- ----- --.._
46 ,-'' `-.
47 , \
48 / \
49 / ` . \
50 ' / || ;
51 ; ^/| |/ | |
52 | /v /\`-'v√\'-|\ ,
53 | /v` ,--- ---- .^.| ;
54 : | /´@@`, ,@@`\ | ;
55 ' | '. @@ / \@@ / |\ |;
56 | ^| ----- --- | \/||
57 ` |` | /\ /
58 \ \ |/ |,
59 ' ; \ /| |
60 ` \ -- / | |
61 ` `. .-' | /
62 v,- `;._ _.; | |
63 `'`\ |-_ -^'^'| |
64 ------ |/
65
66 EOF
67
68 # Subs
69
70 print "This script is kind of deprecated. Use clainsafecli instead.\n";
71
72 sub help
73 {
74 print "lainsafecli, a command line interface for lainsafe.\n";
75 print "USAGE: lainsafecli [--tor | --i2p] [--server] FILE\n\n";
76 print "if --server not given, $DEFAULT_SERVER is used.\n";
77 print "--tor and --i2p are available\n" if $proxy_enabled;
78 print "--tor and --i2p are unavailable, flag are ignored\n" unless $proxy_enabled;
79 exit;
80 }
81
82 sub enable_tor
83 {
84 my $checker = $ua->proxy([qw(http https)] => 'socks://localhost:9050');
85 }
86
87 sub enable_i2p
88 {
89 my $checker = $ua->proxy([qw(http https)] => 'http://localhost:4444');
90 }
91
92 ## PROGRAM
93
94 GetOptions ("server=s" => \$DEFAULT_SERVER,
95 "help|" => \$help,
96 "tor"=> \$tor,
97 "i2p"=>\$i2p,
98 "get-response"=>\$get_response
99 );
100
101 &help if $help || not defined $ARGV[0];
102 if ($i2p and $tor) {
103 print "What are you trying to do? I don't really get you sometimes...\n";
104 exit;
105 }
106 &enable_tor if $tor and $proxy_enabled;
107 &enable_i2p if $i2p and $proxy_enabled;
108
109
110 # check if file is given
111
112 $file = $ARGV[@ARGV-1];
113
114 die "File does not exist\n" unless -e $file;
115
116
117 my $req;
118
119 # Fake user agent
120 $ua->agent("Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0");
121
122 # modify url if necessary
123 substr($DEFAULT_SERVER, 0, 0, 'https://') unless $DEFAULT_SERVER =~ /^(http|https):\/\//;
124 # check if server is running lainsafe
125
126 my $url_to_upload = $DEFAULT_SERVER . "/upload.cgi";
127 if (!$ua->get($url_to_upload)->is_success) {
128 print "$url_to_upload is not running lainsafe. (--get-response to check the error)\n";
129 print $ua->get($url_to_upload)->decoded_content if $get_response;
130 exit;
131 }
132
133
134 $req = $ua->post($url_to_upload,
135 Content_Type => 'form-data',
136 Content => [
137 "file" => [ $file ],
138 ],
139 );
140
141
142 print $ASCII_ART if $DISPLAY_ASCII;
143 if ($req->{_content} =~ /instance/) # If someone knows how to do it another way, I'm all ears
144 {
145 print $req->{_content} . "\n";
146 exit;
147 }
148 print $req->{_content} . "\n";
149
150 if ($STORE_LINKS) {
151 open FILE,'>>',$LINKS_FILE or die $!;
152 print FILE $req->{_content} . " $file" ."\n";
153 close FILE;
154 }
155