]>
git.proxmox.com Git - pve-xtermjs.git/blob - src/PVE/CLI/termproxy.pm
c45eb509fe7a9bb23cc2186cd921a5c540e55b99
1 package PVE
::CLI
::termproxy
;
6 use PVE
::RPCEnvironment
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::AccessControl
;
14 use base
qw(PVE::CLIHandler);
16 use constant MAX_QUEUE_LEN
=> 16*1024;
18 sub setup_environment
{
19 PVE
::RPCEnvironment-
>setup_default_cli_env();
22 sub listen_and_authenticate
{
23 my ($port, $timeout) = @_;
28 Proto
=> &Socket
::IPPROTO_TCP
,
29 GetAddrInfoFlags
=> 0,
30 LocalAddr
=> 'localhost',
34 my $socket = IO
::Socket
::IP-
>new(%$params) or die "failed to open socket: $!\n";
37 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
39 my $client = $socket->accept; # Wait for a client
44 my $n = sysread($client, $queue, 4096);
45 if ($n && $queue =~ s/^([^:]+):([^:]+):(.+)\n//) {
50 die "authentication failed\n"
51 if !PVE
::AccessControl
::verify_vnc_ticket
($ticket, $user, $path);
53 die "aknowledge failed\n"
54 if !syswrite($client, "OK");
57 die "malformed authentication string\n";
60 return ($queue, $client);
64 my ($cmd, $webhandle, $queue) = @_;
66 foreach my $k (keys %ENV) {
67 next if $k eq 'PATH' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE';
68 next if $k =~ m/^LC_/;
72 $ENV{TERM
} = 'xterm-256color';
74 my $pty = PVE
::PTY-
>new();
77 die "fork: $!\n" if !defined($pid);
79 $pty->make_controlling_terminal();
80 exec {$cmd->[0]} @$cmd
84 $pty->set_size(80,20);
86 read_write_loop
($webhandle, $pty->master, $queue, $pty);
94 my ($webhandle, $cmdhandle, $queue, $pty) = @_;
96 my $select = new IO
::Select
;
98 $select->add($webhandle);
99 $select->add($cmdhandle);
103 # we may have already messages from the first read
104 $queue = process_queue
($queue, $cmdhandle, $pty);
108 while($select->count && scalar(@handles = $select->can_read($timeout))) {
109 foreach my $h (@handles) {
111 my $n = $h->sysread($buf, 4096);
113 if ($h == $webhandle) {
114 if ($n && (length($queue) + $n) < MAX_QUEUE_LEN
) {
115 $queue = process_queue
($queue.$buf, $cmdhandle, $pty);
119 } elsif ($h == $cmdhandle) {
121 syswrite($webhandle, $buf);
131 my ($queue, $handle, $pty) = @_;
134 while(length($queue)) {
135 ($queue, $msg) = remove_message
($queue, $pty);
136 last if !defined($msg);
137 syswrite($handle, $msg);
143 # we try to remove a whole message
144 # if we succeed, we return the remaining queue and the msg
145 # if we fail, the message is undef and the queue is not changed
147 my ($queue, $pty) = @_;
150 my $type = substr $queue, 0, 1;
154 my ($length) = $queue =~ m/^0:(\d+):/;
155 my $begin = 3 + length($length);
156 if (defined($length) && length($queue) >= ($length + $begin)) {
157 $msg = substr $queue, $begin, $length;
159 # msg contains now $length chars after 0:$length:
160 $queue = substr $queue, $begin + $length;
163 } elsif ($type eq '1') {
165 my ($cols, $rows) = $queue =~ m/^1:(\d+):(\d+):/;
166 if (defined($cols) && defined($rows)) {
167 $queue = substr $queue, (length($cols) + length ($rows) + 4);
168 eval { $pty->set_size($cols, $rows) if defined($pty) };
172 } elsif ($type eq '2') {
174 $queue = substr $queue, 1;
178 $queue = substr $queue, 1;
182 return ($queue, $msg);
185 __PACKAGE__-
>register_method ({
189 description
=> "Connects a TCP Socket with a commandline",
191 additionalProperties
=> 0,
195 description
=> "The port to listen on."
197 'extra-args' => get_standard_option
('extra-args'),
200 returns
=> { type
=> 'null'},
205 if (defined($param->{'extra-args'})) {
206 $cmd = [@{$param->{'extra-args'}}];
208 die "No command given\n";
211 my ($queue, $handle) = listen_and_authenticate
($param->{port
}, 10);
213 run_pty
($cmd, $handle, $queue);
218 our $cmddef = [ __PACKAGE__
, 'exec', ['port', 'extra-args' ]];