| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use File::Basename; |
| 4 |
use Cwd 'abs_path'; |
| 5 |
use I18N::Langinfo qw'langinfo CODESET'; |
| 6 |
use PerlIO::encoding; |
| 7 |
|
| 8 |
# https://www.perlmonks.org/?node_id=840344 |
| 9 |
$PerlIO::encoding::fallback = Encode::STOP_AT_PARTIAL; |
| 10 |
|
| 11 |
# add fex lib |
| 12 |
unless ($FEXLIB = $ENV{FEXLIB}) { |
| 13 |
if ($ENV{FEXHOME}) { |
| 14 |
$FEXLIB = $ENV{FEXHOME}.'/lib'; |
| 15 |
} elsif (-f '/usr/share/fex/lib/fex.ph') { |
| 16 |
$FEXLIB = '/usr/share/fex/lib'; |
| 17 |
} else { |
| 18 |
$FEXLIB = dirname(dirname(abs_path($0))).'/lib'; |
| 19 |
} |
| 20 |
$ENV{FEXLIB} = $FEXLIB; |
| 21 |
} |
| 22 |
die "$0: no $FEXLIB\n" unless -d $FEXLIB; |
| 23 |
|
| 24 |
# import from fex.pp |
| 25 |
our (@logdir,$spooldir,$akeydir,$debug); |
| 26 |
|
| 27 |
# load common code, local config : $HOME/lib/fex.ph |
| 28 |
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; |
| 29 |
|
| 30 |
$CTYPE = langinfo(CODESET()); |
| 31 |
binmode(STDOUT,":encoding($CTYPE)"); |
| 32 |
|
| 33 |
$log = shift || $logdir[0].'/fexsrv.log'; |
| 34 |
|
| 35 |
$ignore = join('|',qw( |
| 36 |
(CONNECT|CONTINUE).*(crawl|msnbot) |
| 37 |
DISCONNECT:.no.HTTP.request |
| 38 |
GET.*(favicon|robots\.txt) |
| 39 |
GET./small_logo\.jpg |
| 40 |
GET./logo\.jpg |
| 41 |
GET./action-fex-camel\.gif |
| 42 |
GET./fup\?showstatus |
| 43 |
GET./FAQ/.*\.css |
| 44 |
GET./usecases/.*\.css |
| 45 |
GET./DOX/.*\.png |
| 46 |
GET./sharing/.*\.png |
| 47 |
GET.*/css/ |
| 48 |
GET./FAQ/jquery\.js |
| 49 |
GET.*Arrow\.gif |
| 50 |
GET./apple-touch |
| 51 |
GET./browserconfig\.xml |
| 52 |
User-Agent:.*(Webnote|FeedFetcher|\w+bot|bot/|Website.Watcher|crawler|spider|searchme|Yandex|Slurp|ScoutJet|findlinks|urlmon|nagios) |
| 53 |
User-Agent:.fnb.*quak |
| 54 |
User-Agent:.Google.favicon |
| 55 |
User-Agent:.smd |
| 56 |
From:.*(msnbot|yandex|googlebot|webcrawler) |
| 57 |
Referer:.*sex.*stream |
| 58 |
Referer:.*stream.*sex |
| 59 |
X-.*prefetch |
| 60 |
X-Purpose:.preview |
| 61 |
)); |
| 62 |
|
| 63 |
@weed = qw( |
| 64 |
.*keep-alive |
| 65 |
.*no-cache |
| 66 |
Connection: |
| 67 |
Cache-Control: |
| 68 |
Content-Type: |
| 69 |
Accept |
| 70 |
Sec-Fetch |
| 71 |
TE: |
| 72 |
UA-CPU: |
| 73 |
Pragma: |
| 74 |
DNT: |
| 75 |
Via: |
| 76 |
profile: |
| 77 |
Upgrade-Insecure-Requests: |
| 78 |
if-modified-since |
| 79 |
Surrogate-Capability |
| 80 |
Proxy-Authorization |
| 81 |
http\. |
| 82 |
Device-Stock |
| 83 |
NOKIA_ |
| 84 |
GPRS |
| 85 |
X-Proxy-ID |
| 86 |
X-Moz |
| 87 |
X.Wap |
| 88 |
X-FH |
| 89 |
X-FB |
| 90 |
X-WS |
| 91 |
X-Nokia |
| 92 |
X-UCBrowser |
| 93 |
X-NSN |
| 94 |
X-OperaMini |
| 95 |
x-Device |
| 96 |
x-source-id |
| 97 |
x.up |
| 98 |
X-Behavioral |
| 99 |
X-Do-Not-Track |
| 100 |
X-\S*Via |
| 101 |
x-Mobile |
| 102 |
X-Country |
| 103 |
X-ClickOnceSupport |
| 104 |
X-Newrelic |
| 105 |
X-IMForwards |
| 106 |
X-Clearswift |
| 107 |
X-MDS |
| 108 |
.*:\s*$ |
| 109 |
); |
| 110 |
|
| 111 |
$/ = "\n\n"; |
| 112 |
$| = 1; |
| 113 |
|
| 114 |
if (-t STDIN or $ENV{GATEWAY_INTERFACE}) { |
| 115 |
open L,$log or die "$0: $log - $!\n"; |
| 116 |
seek L,0,2; |
| 117 |
} else { |
| 118 |
*L = *STDIN; |
| 119 |
} |
| 120 |
# binmode(L,":encoding(UTF-8)"); |
| 121 |
|
| 122 |
for (;;) { |
| 123 |
while (<L>) { |
| 124 |
next if /(^|\n)($ignore)/i; |
| 125 |
s/[\x00-\x08\x0B-\x1F\x1F\x80-\x9F]/_/g; |
| 126 |
s/^\n//; |
| 127 |
foreach $weed (@weed) { |
| 128 |
while (s/\n$weed.*\n/\n/i) {} |
| 129 |
} |
| 130 |
$post = /\nPOST\s/; |
| 131 |
s/^(\n*(CONNECT|CONTINUE).*)\s\[([\d_]+)\]/$1/i and $pid = $3; |
| 132 |
if (/\n(POST|GET)\s+(\S+)/i) { |
| 133 |
$cgi = $2; |
| 134 |
$cgi =~ s:^http.+?//.+?/::; |
| 135 |
$cgi =~ s:^/+::; |
| 136 |
$cgi =~ s:/.*::; |
| 137 |
$cgi =~ s:\?.*::; |
| 138 |
} |
| 139 |
if (/Content-Length: (\d+)/i) { |
| 140 |
$d = $1; |
| 141 |
while ($d =~ s/(\d)(\d\d\d\b)/$1,$2/) {}; |
| 142 |
s/Content-Length: \d+/Content-Length: $d/i; |
| 143 |
} |
| 144 |
s/[\s\n]*$/\n\n/; |
| 145 |
print utf8decode($_) or exit; |
| 146 |
$from = ''; |
| 147 |
if (m:\nGET /fup/(\w{40,}):) { |
| 148 |
local $_ = decode_b64($1); |
| 149 |
if (/from=([\w\@.-]+)/) { |
| 150 |
$pp = $1; |
| 151 |
printf " FROM=\"%s\"\n",$pp; |
| 152 |
} |
| 153 |
} elsif (m:\nGET /fop/(\w+)/:) { |
| 154 |
$dkey = $1; |
| 155 |
my $ddir = "$spooldir/.dkeys/$dkey"; |
| 156 |
next unless -d $ddir; |
| 157 |
$pp = readlink $ddir or next; |
| 158 |
local $_ = $pp; |
| 159 |
(undef,$to,$from) = split('/'); |
| 160 |
printf " FROM=\"%s\"\n",$from; |
| 161 |
printf " TO=\"%s\"\n",$to; |
| 162 |
$cgi = ''; |
| 163 |
if ($comment = slurp("$ddir/comment")) { |
| 164 |
$comment =~ s/^\s*(.*)\s*$/$1/; |
| 165 |
# $comment =~ s/[^\x20-\x7e\xa0-\xff]/_/g; |
| 166 |
eval { $comment = utf8decode($comment)||'' }; |
| 167 |
printf " COMMENT=\"%s\"\n",$comment; |
| 168 |
} |
| 169 |
if (not -f "$ddir/data" and $_ = slurp("$ddir/error")) { |
| 170 |
s/\n.*//s; |
| 171 |
print " ERROR=\"$_\"\n"; |
| 172 |
} |
| 173 |
elsif ($size = -s "$ddir/data") { |
| 174 |
printf " SIZE=%s MB\n",int($size/1024/1024); |
| 175 |
} |
| 176 |
} elsif (m:\nGET /fup.*skey=(\w+):) { |
| 177 |
$pp = $1; |
| 178 |
read_skey($1); |
| 179 |
} |
| 180 |
# if (/\nCookie:.*akey=(\w+)/) { |
| 181 |
# $pp = $user = readlink("$akeydir/$1")||''; |
| 182 |
# $user =~ s:^../::; |
| 183 |
# print " USER=$user\n" if $user; |
| 184 |
# } |
| 185 |
if ($debug and $pid and $post) { |
| 186 |
$pp = &read_debug_log; |
| 187 |
print $pp if $pp; |
| 188 |
} |
| 189 |
print "\n" if $pp; |
| 190 |
$pp = $pid = $cgi = ''; |
| 191 |
} |
| 192 |
sleep 1; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
sub read_debug_log { |
| 197 |
my (@log,$log); |
| 198 |
local $/ = "\n"; |
| 199 |
local $_; |
| 200 |
my $pp = ''; |
| 201 |
|
| 202 |
# https://rt.cpan.org/Public/Bug/Display.html?id=88592 |
| 203 |
# local $^W; |
| 204 |
# no warnings "all"; |
| 205 |
no warnings 'utf8'; |
| 206 |
|
| 207 |
return if $cgi =~ /php$/; |
| 208 |
|
| 209 |
for (1..2) { |
| 210 |
sleep 1; |
| 211 |
@log = `ls -rt $logdir[0]/.debug/*_${pid}.$cgi 2>/dev/null`; |
| 212 |
if ($log = $log[-1] and open $log,$log) { |
| 213 |
# binmode($log,":utf8"); # crash on non-UTF8! |
| 214 |
while (<$log>) { |
| 215 |
s/\r//; |
| 216 |
s/[^\x09\x20-\xFF]/_/g; |
| 217 |
if (/^Content-Disposition:.*name="directory";\s*filename="([^\/]+).*"/i) { |
| 218 |
$pp .= " DIRECTORY=\"$1\"\n"; |
| 219 |
} elsif (/^Content-Disposition:.*filename="(.+)"/i) { |
| 220 |
my $file = urlencode($1); |
| 221 |
$pp .= " FILE=\"$file\"\n"; |
| 222 |
} elsif (/^Content-Disposition:.*name="(\w+)"/i) { |
| 223 |
my $p = $1; |
| 224 |
$_ = <$log>; |
| 225 |
my $v = <$log>||''; |
| 226 |
$v =~ s/[\r\n]+//; |
| 227 |
# if (length($v)) { |
| 228 |
my $vv = utf8decode($v)||$v; |
| 229 |
chomp $vv; |
| 230 |
$vv =~ s/[\x00-\x1F]/_/g; |
| 231 |
$vv =~ s/[\x80-\x9F]/_/g; |
| 232 |
$pp .= sprintf " %s=\"%s\"\n",$p,$vv; |
| 233 |
read_akey($v) if uc($p) eq 'AKEY'; |
| 234 |
read_skey($v) if uc($p) eq 'SKEY'; |
| 235 |
# } |
| 236 |
} elsif (/^(Param|Exp): (\w+=".+")/) { |
| 237 |
$pp .= " $2\n"; |
| 238 |
} |
| 239 |
} |
| 240 |
close $log; |
| 241 |
return $pp; |
| 242 |
} |
| 243 |
} |
| 244 |
return $pp; |
| 245 |
} |
| 246 |
|
| 247 |
sub read_akey { |
| 248 |
my $akey = "$spooldir/.akeys/" . shift; |
| 249 |
if (my $user = readlink($akey)) { |
| 250 |
$user =~ s:../::; |
| 251 |
print " USER=$user\n"; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
sub read_skey { |
| 257 |
local $_; |
| 258 |
my $skey = "$spooldir/.skeys/" . shift; |
| 259 |
if (open $skey,$skey) { |
| 260 |
while (<$skey>) { |
| 261 |
printf " FROM=\"%s\"\n",$1 if /from=(.+)/; |
| 262 |
printf " TO=\"%s\"\n",$1 if /to=(.+)/; |
| 263 |
} |
| 264 |
close $skey; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
sub utf8decode { |
| 270 |
local $_ = shift; |
| 271 |
s/([\xC0-\xDF])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg; |
| 272 |
return $_; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
# sub urlencode { |
| 277 |
# local $_ = shift; |
| 278 |
# s/([^ -~])/'%'.uc(unpack("H2",$1))/ge; |
| 279 |
# return $_; |
| 280 |
# } |