#! /usr/bin/env perl
# vim: set filetype=perl ts=4 sw=4 sts=4 et:
use common::sense;

use IO::Socket;

use File::Basename;
use lib dirname(__FILE__) . '/../../../lib/perl';
use OVH::Result;
use OVH::Bastion;
use OVH::Bastion::Plugin qw( :DEFAULT help );

my $remainingOptions = OVH::Bastion::Plugin::begin(
    argv     => \@ARGV,
    header   => "simulate a netcat -z",
    options  => {"w=i" => \my $timeout},
    helptext => <<'EOF',
Check whether a remote TCP port is open

Usage: --osh SCRIPT_NAME [--host] HOST [--port] PORT [-w TIMEOUT]

  --host HOST  Host or IP to attempt to connect to
  --port PORT  TCP port to attempt to connect to
  -w SECONDS   Timeout in seconds (default: 3)
EOF
);

# be nice and try to guessify a host as first param and port as second param
# if user said --osh nc mymachine.example.org 22
if (    not $host
    and not $ip
    and not $port
    and ref $remainingOptions eq 'ARRAY'
    and @$remainingOptions == 2
    and $remainingOptions->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9.-]{1,}$/
    and $remainingOptions->[1] =~ /^\d+$/)
{
    ($host, $port) = @$remainingOptions;
}

#
# code
#
my $fnret;

if (not $host) {
    help();
    osh_exit 'ERR_MISSING_PARAMETER', "Missing required host parameter";
}

if (not $port) {
    help();
    osh_exit 'ERR_MISSING_PARAMETER', "Missing required port parameter";
}

$fnret = OVH::Bastion::is_valid_port(port => $port);
if (!$fnret) {
    help();
    osh_exit $fnret;
}

if ($host =~ m{/}) {
    help();
    osh_exit 'ERR_INVALID_PARAMETER', "Please use a single IP, not a subnet";
}

osh_info "Checking whether TCP port $port of $host is reachable...";

my $Sock = IO::Socket->new(
    Domain   => IO::Socket::AF_INET,
    Type     => IO::Socket::SOCK_STREAM,
    Proto    => 'tcp',
    Timeout  => ($timeout and $timeout > 0 and $timeout <= 3600) ? $timeout : 3,
    PeerAddr => $host,
    PeerPort => $port,
);

my $errmsg = $@;
$errmsg =~ s{IO::Socket(::[a-zA-Z0-9]*)?: }{};
$errmsg =~ s{connect: }{};

# try to guess what happened
my $answer = 'unknown';
if ($Sock) {
    $answer = 'open';
    osh_info("Port $port is open");
}
elsif ($errmsg =~ m{refused}i) {
    $answer = 'closed';
    osh_info("Port $port is closed");
}
elsif ($errmsg =~ m{timeout}i) {
    $answer = 'timeout';
    osh_info("Connection timed out");
}

osh_ok {
    host   => $host,
    port   => $port + 0,
    result => $answer,
};

