| 1 | package GuacLite::Client::Web; | ||
| 2 | |||
| 3 | use Mojo::Base 'Mojolicious'; | ||
| 4 | |||
| 5 | use Mojo::File; | ||
| 6 | use Mojo::JSON; | ||
| 7 | use Mojo::URL; | ||
| 8 | |||
| 9 | use GuacLite::Client::Guacd; | ||
| 10 | |||
| 11 | use File::ShareDir; | ||
| 12 | |||
| 13 | has dist_dir => sub { | ||
| 14 | my $cpanfile = Mojo::File::curfile->dirname->dirname->dirname->sibling('cpanfile'); | ||
| 15 | return $cpanfile->sibling('share') if -e $cpanfile; | ||
| 16 | return Mojo::File->new(File::ShareDir::dist_dir('GuacLite')); | ||
| 17 | }; | ||
| 18 | |||
| 19 | sub startup { | ||
| 20 | my $app = shift; | ||
| 21 | |||
| 22 | $app->commands->namespaces([ | ||
| 23 | 'Mojolicious::Command', | ||
| 24 | 'GuacLite::Client::Web::Command', | ||
| 25 | ]); | ||
| 26 | |||
| 27 | $app->renderer->paths([ | ||
| 28 | $app->dist_dir->child('templates'), | ||
| 29 | ]); | ||
| 30 | $app->static->paths([ | ||
| 31 | $app->dist_dir->child('public'), | ||
| 32 | ]); | ||
| 33 | |||
| 34 | $app->plugin('GuacLite::Plugin::Guacd'); | ||
| 35 | |||
| 36 | my $r = $app->routes; | ||
| 37 | $r->get('/')->to(template => 'index'); | ||
| 38 | $r->websocket('/tunnel')->to(cb => \&_tunnel)->name('tunnel'); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _tunnel { | ||
| 42 | my $c = shift; | ||
| 43 | |||
| 44 | my $json = $c->req->params->pairs->[0]; | ||
| 45 | my $params = Mojo::JSON::from_json($json); | ||
| 46 | my $target = Mojo::URL->new($params->{target}); | ||
| 47 | |||
| 48 | my $connection = $target->query->to_hash; | ||
| 49 | $connection->{hostname} ||= $target->host if $target->host; | ||
| 50 | $connection->{port} ||= $target->port if $target->port; | ||
| 51 | |||
| 52 | my $client = GuacLite::Client::Guacd->new( | ||
| 53 | $params->{client}{audio} ? (audio_mimetypes => $params->{client}{audio} ) : (), | ||
| 54 | $params->{client}{video} ? (video_mimetypes => $params->{client}{video} ) : (), | ||
| 55 | $params->{client}{image} ? (image_mimetypes => $params->{client}{image} ) : (), | ||
| 56 | $params->{client}{timezone} ? (timezone => $params->{client}{timezone}) : (), | ||
| 57 | $params->{client}{width} ? (width => $params->{client}{width} ) : (), | ||
| 58 | $params->{client}{height} ? (height => $params->{client}{height} ) : (), | ||
| 59 | $params->{client}{dpi} ? (dpi => $params->{client}{dpi} ) : (), | ||
| 60 | $target->protocol ? (protocol => $target->protocol ) : (), | ||
| 61 | connection_args => $connection, | ||
| 62 | ); | ||
| 63 | |||
| 64 | return $c->guacd->tunnel($client); | ||
| 65 | } | ||
| 66 | |||
| 67 | { | ||
| 68 | package GuacLite::Client::Web::Command::pack; | ||
| 69 | |||
| 70 | use Mojo::Base 'Mojolicious::Command'; | ||
| 71 | |||
| 72 | has description => 'Bundle guacamole-common-js assets'; | ||
| 73 | has usage => <<"END"; | ||
| 74 | Usage: $0 pack path/to/guacamole-client/guacamole-common-js/src/main/webapp/modules/ | ||
| 75 | |||
| 76 | Bundle the assets in a given directory and write it to STDOUT. | ||
| 77 | The resulting javascript file exposes a Guacamole global with an "initialize" method used to load the rest of the library. | ||
| 78 | This "initialize" allows deferring loading the audio system until after a user action which is required by modern browsers. | ||
| 79 | END | ||
| 80 | |||
| 81 | sub run { | ||
| 82 | my ($command, $path) = @_; | ||
| 83 | warn $path; | ||
| 84 | |||
| 85 | die "a path is required" unless $path; | ||
| 86 | die "$path does not exist or is not a directory" unless -d $path; | ||
| 87 | |||
| 88 | require GuacLite::Util; | ||
| 89 | print GuacLite::Util::pack_js($path); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 |