#!/usr/bin/perl

BEGIN {
    unshift @INC, 'blib/lib', 'blib/arch';
}

use Socket::Class      ();
use Socket::Class::SSL ();

my $pem = '-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQC5hCJbBaDK3vhJN51DaVwUdUQkb7fnbYV36F1cYSQ0XcuUhWSb
wB1Kq9BKzuYW/qaIIH08n9W41GMbQcZGLGe6pn0eZsvv/TrXLV8a2Yf6QzWw4ufH
aJc1NLDvq8P2N0jxhiWmGf1u5ZJw6nGShqbrNEnB5WJZDvmdMgzGp6G7DwIDAQAB
AoGBAJsmsPzi9hj05T2Gr5WjVgkeEcFPVcTNSeSAhyQtcfQBxbMO5JeF0nmSu/70
jmYIzwnl8hdzrXCI3+H53nLtzElXPM8FJ10SOAsklHZexjvV3fCC3U7Qo5su2R1e
cKsRfNYx3X++kODuKGHzOi+fi9V0hiZtHgs3s9Lb4Gr2VKoRAkEA3sBUz70DEkBd
UHfCh1kRRph4yUtytx9EETaewQ5kc94QHHuUk8G61zlBoItbPU+bQAJGzwap0PNe
EJCqLe6ctQJBANU0/asX/q/mALPZqjE5XpzbXY4d8ZdKxJjLaEzjBHSyw4bGAD4W
Q/k7E7b8r/M5wGdipBvSFbCvS2qE2c/bVzMCQQCu5u/xKeV2eEmM/GwnIF17RA9b
Zz2M4iTtKykeR3HCtPOLmdGA71YI1nFcYO/kRVSOvvrgZcgDRIRwl1a4uCodAkEA
y8T1nIw2Uo8UpM+npZwbHPdblvRvbhV7iDz/1lwyagZgcXLT0IMfPBiGYyFmWKQd
i7Hu/tfu+wrOnWOTeOE9mQJAQaMngYlDh0SLABUiMk6MuBtZArrCL735ostnWnpf
efsi37gWPlbX0Yy+4HyDPIcFpXJ46cZecFVlOq2NZSSEAg==
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIDRTCCAq6gAwIBAgIBADANBgkqhkiG9w0BAQQFADB7MQswCQYDVQQGEwJVUzEQ
MA4GA1UECBMHVW5rbm93bjEQMA4GA1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( getpwuid($>) )[7];
if ( open( my $pem_fh, '>', $homedir . '/mypem_ssl_test.pem' ) ) {
    print {$pem_fh} $pem;
    close($pem_fh);
}

my %SSLARGS = (
    'private_key' => $homedir . '/mypem_ssl_test.pem',
    'certificate' => $homedir . '/mypem_ssl_test.pem',

    #'SSL_cipher_list' => 'ALL:!ADH:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP'
);

my $port = 10001;
my %ARGS = (
    'listen'     => '45',
    'proto'      => 'tcp',
    'local_port' => $port,
    'reuseaddr'  => '1',
);
my $fd = Socket::Class->new(%ARGS);

while (1) {
    $fd->select( $read_set = 1, undef, undef, 5 );
    if ($read_set) {
        $socket = $fd->accept() || next;
        if ( fork() ) {

            # parent

            $socket->close();
        }
        else {

            # child
            my $sslsocket = Socket::Class::SSL->starttls(
                $socket,
                'server' => 1,
                %SSLARGS
            ) || die "Could not start ssl";
            $sslsocket->read( $constring, 2 );
            $sslsocket->write("SSL SERVER CONNETED OK\n");
            exit();
        }
    }
}
