From ab091194298a45b493cb6def06a6a67b701e64a3 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 22 Mar 2020 19:43:43 +0300 Subject: [PATCH] Add joyproxy prototype --- lib/joyproxy.pm | 103 ++++++++++++++++++++++++++++++++++++++++++++ myapi-joyproxy.psgi | 41 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 lib/joyproxy.pm create mode 100644 myapi-joyproxy.psgi diff --git a/lib/joyproxy.pm b/lib/joyproxy.pm new file mode 100644 index 0000000..198cb81 --- /dev/null +++ b/lib/joyproxy.pm @@ -0,0 +1,103 @@ +package joyproxy; + +use strict; +use warnings "all"; +use lib qw(./lib ./vendor_perl); +use Fcntl qw(O_RDONLY O_WRONLY); +use HTTP::Tiny; +use URI::URL; + +use Exporter qw(import); +use vars qw/$VERSION/; +$VERSION = "1.0"; +our @EXPORT = qw(joyproxy); + + +sub joyproxy ($) { + my $str = shift; + chomp($str); + + return ('500', 'text/plain', 'This is not reactor video') if ($str !~ /^img1\.reactor\.cc/); + + my ($file, $filesize) = __dlfunc("http://$str"); + + if (defined($file)) { + my $buf = ''; + + if (open(VID, '<', $file)) { + read(VID, $buf, $filesize); + close VID; + + if ($file =~ /\.mp4/i) { + return ('200', 'video/mp4', $buf); + } elsif ($file =~ /\.webm/i) { + return ('200', 'video/webm', $buf); + } else { + return ('200', 'video/mpeg', $buf); + } + } else { + unlink $file if (-f $file); + return ('500', 'text/plain', "Unable to open file in temporary loaction: $!\n"); + } + } else { + return ('500', 'text/plain', "Unable to get file from remote source!\n"); + } +} + +sub __urlencode($) { + my $str = shift; + my $urlobj = url $str; + $str = $urlobj->as_string; + undef $urlobj; + return $str; +} + + +sub __dlfunc($) { + my $url = shift; + + if (! -d "/tmp/joyproxy") { + warn "no /tmp/joyproxy"; + return (undef, undef) unless (mkdir("/tmp/joyproxy")); + } + + my @tmparray = split(/\//, $url); + my $file = $tmparray[@tmparray - 1]; + @tmparray = -1; undef @tmparray; + $file = "/tmp/joyproxy/" . $file; + $url = __urlencode($url); + + my $http = HTTP::Tiny->new(); + my $response = $http->mirror( + $url, + $file, + { + headers => { + "Accept" => '*/*', + "Accept-Encoding" => 'identity;q=1, *;q=0', + "Accept-Language" => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7', + "Range" => 'bytes=0-', + "Referer" => 'http://old.reactor.cc/all', + "User-Agent" => "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36" + } + } + ); + + if ($response->{success}) { + my $filesize = (stat($file))[7]; + + if ($filesize > 10485760) { + unlink $file; + return (undef, undef); + } else { + return ($file, $filesize); + } + } else { + return (undef, undef); + } +} + + +1; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/myapi-joyproxy.psgi b/myapi-joyproxy.psgi new file mode 100644 index 0000000..a2b3339 --- /dev/null +++ b/myapi-joyproxy.psgi @@ -0,0 +1,41 @@ +use strict; +use warnings "all"; + +# my plugins +use lib qw(./lib ./vendor_perl ./vendor_perl/lib/perl5); +use conf; +use joyproxy; + +my $CONF = loadConf(); + +if ($CONF->{api}->{prefix} eq '/') { $CONF->{api}->{prefix} = ''; } +my $prefix = $CONF->{api}->{prefix}; + +my $app = sub { + my $env = shift; + + my $msg = "Your Opinion is very important for us, please stand by.\n"; + my $status = '404'; + my $content = 'text/plain'; + + if ($env->{PATH_INFO} =~ /$prefix\/joyproxy\/(.+)/) { + my $joyproxyurl = $1; + ($status, $content, $msg) = ('400', $content, "Bad Request?\n"); + + if (($joyproxyurl =~ /\.mp4/i) or ($joyproxyurl =~ /\.webm/i)) { + ($status, $content, $msg) = joyproxy($joyproxyurl); + } + } + + + + return [ + $status, + [ 'Content-Type' => $content, 'Content-Length' => length($msg) ], + [ $msg ], + ]; +}; + + +__END__ +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: