]>
git.proxmox.com Git - pve-xtermjs.git/blob - src/PVE/CLI/termproxy.pm
1 package PVE
::CLI
::termproxy
;
7 use PVE
::JSONSchema
qw(get_standard_option);
13 use base
qw(PVE::CLIHandler);
15 use constant MAX_QUEUE_LEN
=> 16*1024;
16 use constant DEFAULT_PATH
=> '/';
17 use constant DEFAULT_PERM
=> 'Sys.Console';
20 my ($ticket, $user, $path, $perm) = @_;
22 my $ua = LWP
::UserAgent-
>new();
24 my $res = $ua->post ('http://localhost:85/api2/json/access/ticket', Content
=> {
30 if (!$res->is_success) {
31 die "Authentication failed: '$res->status_line'\n";
35 sub listen_and_authenticate
{
36 my ($port, $timeout, $path, $perm) = @_;
41 Proto
=> &Socket
::IPPROTO_TCP
,
42 GetAddrInfoFlags
=> 0,
43 LocalAddr
=> 'localhost',
47 my $socket = IO
::Socket
::IP-
>new(%$params) or die "failed to open socket: $!\n";
50 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
52 my $client = $socket->accept; # Wait for a client
57 my $n = sysread($client, $queue, 4096);
58 if ($n && $queue =~ s/^([^:]+):(.+)\n//) {
62 verify_ticket
($ticket, $user, $path, $perm);
64 die "aknowledge failed\n"
65 if !syswrite($client, "OK");
68 die "malformed authentication string\n";
71 return ($queue, $client);
75 my ($cmd, $webhandle, $queue) = @_;
77 foreach my $k (keys %ENV) {
78 next if $k eq 'PATH' || $k eq 'USER' || $k eq 'HOME' || $k eq 'LANG' || $k eq 'LANGUAGE';
79 next if $k =~ m/^LC_/;
83 $ENV{TERM
} = 'xterm-256color';
85 my $pty = PVE
::PTY-
>new();
88 die "fork: $!\n" if !defined($pid);
90 $pty->make_controlling_terminal();
91 exec {$cmd->[0]} @$cmd
95 $pty->set_size(80,20);
97 read_write_loop
($webhandle, $pty->master, $queue, $pty);
104 sub read_write_loop
{
105 my ($webhandle, $cmdhandle, $queue, $pty) = @_;
107 my $select = new IO
::Select
;
109 $select->add($webhandle);
110 $select->add($cmdhandle);
114 # we may have already messages from the first read
115 $queue = process_queue
($queue, $cmdhandle, $pty);
119 while($select->count && scalar(@handles = $select->can_read($timeout))) {
120 foreach my $h (@handles) {
122 my $n = $h->sysread($buf, 4096);
124 if ($h == $webhandle) {
125 if ($n && (length($queue) + $n) < MAX_QUEUE_LEN
) {
126 $queue = process_queue
($queue.$buf, $cmdhandle, $pty);
130 } elsif ($h == $cmdhandle) {
132 syswrite($webhandle, $buf);
142 my ($queue, $handle, $pty) = @_;
145 while(length($queue)) {
146 ($queue, $msg) = remove_message
($queue, $pty);
147 last if !defined($msg);
148 syswrite($handle, $msg);
154 # we try to remove a whole message
155 # if we succeed, we return the remaining queue and the msg
156 # if we fail, the message is undef and the queue is not changed
158 my ($queue, $pty) = @_;
161 my $type = substr $queue, 0, 1;
165 my ($length) = $queue =~ m/^0:(\d+):/;
166 my $begin = 3 + length($length);
167 if (defined($length) && length($queue) >= ($length + $begin)) {
168 $msg = substr $queue, $begin, $length;
170 # msg contains now $length chars after 0:$length:
171 $queue = substr $queue, $begin + $length;
174 } elsif ($type eq '1') {
176 my ($cols, $rows) = $queue =~ m/^1:(\d+):(\d+):/;
177 if (defined($cols) && defined($rows)) {
178 $queue = substr $queue, (length($cols) + length ($rows) + 4);
179 eval { $pty->set_size($cols, $rows) if defined($pty) };
183 } elsif ($type eq '2') {
185 $queue = substr $queue, 1;
189 $queue = substr $queue, 1;
193 return ($queue, $msg);
196 __PACKAGE__-
>register_method ({
200 description
=> "Connects a TCP Socket with a commandline",
202 additionalProperties
=> 0,
206 description
=> "The port to listen on."
210 description
=> "The Authentication path. (default: '".DEFAULT_PATH
."')",
211 default => DEFAULT_PATH
,
215 description
=> "The Authentication Permission. (default: '".DEFAULT_PERM
."')",
216 default => DEFAULT_PERM
,
218 'extra-args' => get_standard_option
('extra-args'),
221 returns
=> { type
=> 'null'},
226 if (defined($param->{'extra-args'})) {
227 $cmd = [@{$param->{'extra-args'}}];
229 die "No command given\n";
232 my $path = $param->{path
} // DEFAULT_PATH
;
233 my $perm = $param->{perm
} // DEFAULT_PERM
;
234 my ($queue, $handle) = listen_and_authenticate
($param->{port
}, 10, $path, $perm);
236 run_pty
($cmd, $handle, $queue);
241 our $cmddef = [ __PACKAGE__
, 'exec', ['port', 'extra-args' ]];