1bf5ac567Sbluhm#!/usr/bin/perl 2*d43d768fSbluhm# $OpenBSD: remote.pl,v 1.9 2016/08/25 22:56:13 bluhm Exp $ 3bf5ac567Sbluhm 4aa8f1300Sbluhm# Copyright (c) 2010-2014 Alexander Bluhm <bluhm@openbsd.org> 5bf5ac567Sbluhm# 6bf5ac567Sbluhm# Permission to use, copy, modify, and distribute this software for any 7bf5ac567Sbluhm# purpose with or without fee is hereby granted, provided that the above 8bf5ac567Sbluhm# copyright notice and this permission notice appear in all copies. 9bf5ac567Sbluhm# 10bf5ac567Sbluhm# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11bf5ac567Sbluhm# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12bf5ac567Sbluhm# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13bf5ac567Sbluhm# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14bf5ac567Sbluhm# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15bf5ac567Sbluhm# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16bf5ac567Sbluhm# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17bf5ac567Sbluhm 18bf5ac567Sbluhmuse strict; 19bf5ac567Sbluhmuse warnings; 20bf5ac567Sbluhmuse File::Basename; 21bf5ac567Sbluhmuse File::Copy; 22bf5ac567Sbluhmuse Socket; 23bf5ac567Sbluhmuse Socket6; 24bf5ac567Sbluhm 25bf5ac567Sbluhmuse Client; 26bf5ac567Sbluhmuse Relayd; 27bf5ac567Sbluhmuse Server; 28bf5ac567Sbluhmuse Remote; 29bf5ac567Sbluhmrequire 'funcs.pl'; 30bf5ac567Sbluhm 31bf5ac567Sbluhmsub usage { 32bf5ac567Sbluhm die <<"EOF"; 33bf5ac567Sbluhmusage: 34bf5ac567Sbluhm remote.pl localport remoteaddr remoteport [test-args.pl] 35bf5ac567Sbluhm Run test with local client and server. Remote relayd 36bf5ac567Sbluhm forwarding from remoteaddr remoteport to server localport 37bf5ac567Sbluhm has to be started manually. 38bf5ac567Sbluhm remote.pl copy|splice listenaddr connectaddr connectport [test-args.pl] 39bf5ac567Sbluhm Only start remote relayd. 40bf5ac567Sbluhm remote.pl copy|splice localaddr remoteaddr remotessh [test-args.pl] 41bf5ac567Sbluhm Run test with local client and server. Remote relayd is 42bf5ac567Sbluhm started automatically with ssh on remotessh. 43bf5ac567SbluhmEOF 44bf5ac567Sbluhm} 45bf5ac567Sbluhm 46aa8f1300Sbluhmmy $testfile; 47bf5ac567Sbluhmour %args; 48bf5ac567Sbluhmif (@ARGV and -f $ARGV[-1]) { 49aa8f1300Sbluhm $testfile = pop; 50aa8f1300Sbluhm do $testfile 51aa8f1300Sbluhm or die "Do test file $testfile failed: ", $@ || $!; 52bf5ac567Sbluhm} 53bf5ac567Sbluhmmy $mode = 54bf5ac567Sbluhm @ARGV == 3 && $ARGV[0] =~ /^\d+$/ && $ARGV[2] =~ /^\d+$/ ? "manual" : 55bf5ac567Sbluhm @ARGV == 4 && $ARGV[1] !~ /^\d+$/ && $ARGV[3] =~ /^\d+$/ ? "relay" : 56bf5ac567Sbluhm @ARGV == 4 && $ARGV[1] !~ /^\d+$/ && $ARGV[3] !~ /^\d+$/ ? "auto" : 57bf5ac567Sbluhm usage(); 58bf5ac567Sbluhm 59f8f8530cSbluhmmy($s, $r, $c); 60bf5ac567Sbluhmif ($mode eq "relay") { 61bf5ac567Sbluhm my($rport) = find_ports(num => 1); 62bf5ac567Sbluhm $r = Relayd->new( 63bf5ac567Sbluhm forward => $ARGV[0], 64bf5ac567Sbluhm %{$args{relayd}}, 65bf5ac567Sbluhm listendomain => AF_INET, 66bf5ac567Sbluhm listenaddr => $ARGV[1], 67bf5ac567Sbluhm listenport => $rport, 68bf5ac567Sbluhm connectdomain => AF_INET, 69bf5ac567Sbluhm connectaddr => $ARGV[2], 70bf5ac567Sbluhm connectport => $ARGV[3], 71bf5ac567Sbluhm logfile => dirname($0)."/remote.log", 72bf5ac567Sbluhm conffile => dirname($0)."/relayd.conf", 73aa8f1300Sbluhm testfile => $testfile, 74bf5ac567Sbluhm ); 75bf5ac567Sbluhm open(my $log, '<', $r->{logfile}) 76bf5ac567Sbluhm or die "Remote log file open failed: $!"; 77bf5ac567Sbluhm $SIG{__DIE__} = sub { 78bf5ac567Sbluhm die @_ if $^S; 79bf5ac567Sbluhm copy($log, \*STDERR); 80bf5ac567Sbluhm warn @_; 81bf5ac567Sbluhm exit 255; 82bf5ac567Sbluhm }; 83bf5ac567Sbluhm copy($log, \*STDERR); 84bf5ac567Sbluhm $r->run; 85bf5ac567Sbluhm copy($log, \*STDERR); 86bf5ac567Sbluhm $r->up; 87bf5ac567Sbluhm copy($log, \*STDERR); 88bf5ac567Sbluhm print STDERR "listen sock: $ARGV[1] $rport\n"; 89bf5ac567Sbluhm <STDIN>; 90bf5ac567Sbluhm copy($log, \*STDERR); 91bf5ac567Sbluhm print STDERR "stdin closed\n"; 92bf5ac567Sbluhm $r->kill_child; 93bf5ac567Sbluhm $r->down; 94bf5ac567Sbluhm copy($log, \*STDERR); 95bf5ac567Sbluhm 96bf5ac567Sbluhm exit; 97bf5ac567Sbluhm} 98bf5ac567Sbluhm 99cab60319Sbluhmmy $redo = $args{lengths} && @{$args{lengths}}; 100cab60319Sbluhm$redo = 0 if $args{client}{http_vers}; # run only one persistent connection 101f8f8530cSbluhm$s = Server->new( 10287fd5723Sbluhm forward => $ARGV[0], 103bf5ac567Sbluhm func => \&read_char, 10487fd5723Sbluhm redo => $redo, 105bf5ac567Sbluhm %{$args{server}}, 106bf5ac567Sbluhm listendomain => AF_INET, 107bf5ac567Sbluhm listenaddr => ($mode eq "auto" ? $ARGV[1] : undef), 108bf5ac567Sbluhm listenport => ($mode eq "manual" ? $ARGV[0] : undef), 109aa8f1300Sbluhm testfile => $testfile, 110*d43d768fSbluhm client => \$c, 111a3c410c1Sbluhm) unless $args{server}{noserver}; 112bf5ac567Sbluhmif ($mode eq "auto") { 113bf5ac567Sbluhm $r = Remote->new( 114bf5ac567Sbluhm forward => $ARGV[0], 115bf5ac567Sbluhm logfile => "relayd.log", 11614344d13Sbluhm %{$args{relayd}}, 117bf5ac567Sbluhm remotessh => $ARGV[3], 118bf5ac567Sbluhm listenaddr => $ARGV[2], 119bf5ac567Sbluhm connectaddr => $ARGV[1], 120a3c410c1Sbluhm connectport => $s ? $s->{listenport} : 1, 121aa8f1300Sbluhm testfile => $testfile, 122bf5ac567Sbluhm ); 123bf5ac567Sbluhm $r->run->up; 124bf5ac567Sbluhm} 125f8f8530cSbluhm$c = Client->new( 12687fd5723Sbluhm forward => $ARGV[0], 127bf5ac567Sbluhm func => \&write_char, 128bf5ac567Sbluhm %{$args{client}}, 129bf5ac567Sbluhm connectdomain => AF_INET, 130bf5ac567Sbluhm connectaddr => ($mode eq "manual" ? $ARGV[1] : $r->{listenaddr}), 131bf5ac567Sbluhm connectport => ($mode eq "manual" ? $ARGV[2] : $r->{listenport}), 132aa8f1300Sbluhm testfile => $testfile, 133*d43d768fSbluhm server => \$s, 13414344d13Sbluhm) unless $args{client}{noclient}; 135bf5ac567Sbluhm 136a3c410c1Sbluhm$s->run unless $args{server}{noserver}; 13714344d13Sbluhm$c->run->up unless $args{client}{noclient}; 138a3c410c1Sbluhm$s->up unless $args{server}{noserver}; 139bf5ac567Sbluhm 14014344d13Sbluhm$c->down unless $args{client}{noclient}; 141a3c410c1Sbluhm$s->down unless $args{server}{noserver}; 142bf5ac567Sbluhm$r->close_child; 143bf5ac567Sbluhm$r->down; 144bf5ac567Sbluhm 145a7ce14b0Sbluhmcheck_logs($c, $r, $s, %args); 146