| 1 | package GuacLite::Plugin::Guacd; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | sub register { | ||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 6 | 1 | my ($plugin, $app, $conf) = @_; | |||||||||
| 7 | 1 | $app->helper('guacd.tunnel' => \&_tunnel); | |||||||||
| 8 | } | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | sub _tunnel { | ||||||||
|
Subroutine
_tunnel: called CC=1 SCAR=0.8
| |||||||||||
| 11 | 1 | my ($c, $client) = @_; | |||||||||
| 12 | |||||||||||
| 13 | 1 | my $tx = $c->tx; | |||||||||
| 14 | 1 | $tx->with_protocols('guacamole'); | |||||||||
| 15 | 1 | $tx->with_compression; | |||||||||
| 16 | 1 | $tx->max_websocket_size(10485760); | |||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | $c->on(finish => sub { $client->close; undef $c; undef $tx; undef $client }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
| |||||||||||
| 19 | 0 | ▶ | $client->on(close => sub { $c->finish }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 20 | |||||||||||
| 21 | return $client->connect_p | ||||||||||
| 22 | 1 | ▶ | ->then(sub { $client->handshake_p }) | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | ->then(sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||
| 24 | 1 | my $id = shift; | |||||||||
| 25 | |||||||||||
| 26 | 1 | my $ws_stream = Mojo::IOLoop->stream($c->tx->connection); | |||||||||
| 27 | 1 | my $guacd_stream = $client->stream; | |||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | $client->on(instruction => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 30 | 0 | $c->send({text => $_[1]}); | |||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | return if $ws_stream->can_write; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | _backpressure($guacd_stream => $ws_stream); | |||||||||
| 34 | 1 | }); | |||||||||
| 35 | 1 | ▶ | $c->on(text => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=16.0
| |||||||||||
| 36 | 1 | my (undef, $bytes) = @_; | |||||||||
| 37 | |||||||||||
| 38 | # OOB messages are sent with empty instruction, for now assume its a ping | ||||||||||
| 39 | 1 | ▶ | if(substr($bytes, 0, 2) eq '0.') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | return $c->send({text => $bytes}); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 0 | $client->write($bytes); | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | return if $guacd_stream->can_write; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | _backpressure($ws_stream => $guacd_stream); | |||||||||
| 47 | 1 | }); | |||||||||
| 48 | # initiate by sending the id, except the frontend doesn't want the $ | ||||||||||
| 49 | 1 | $id =~ s/^\$//; | |||||||||
| 50 | 1 | $c->send({text => GuacLite::Client::Guacd::encode(['', $id])}); | |||||||||
| 51 | 1 | }); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # handle backpressure, but it assumes there already is some don't call unles | ||||||||||
| 55 | # you've checked can_write, that said, don't put it in here for efficiency's sake | ||||||||||
| 56 | 0 | ▶ | sub _backpressure { | ||||||||
|
Subroutine
_backpressure: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | 0 | my ($in, $out) = @_; | |||||||||
| 58 | 0 | $in->stop; | |||||||||
| 59 | 0 | ▶ | $out->once(drain => sub { $in->start }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | 1; | |||||||||
| 63 | |||||||||||