Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Created April 11, 2017 17:35
Show Gist options
  • Save Logioniz/76d637fc8017f35a61c5e11cec5a6a78 to your computer and use it in GitHub Desktop.
Save Logioniz/76d637fc8017f35a61c5e11cec5a6a78 to your computer and use it in GitHub Desktop.
Example tls server and client
#!/usr/bin/perl
use Mojo::Base;
use Mojo::IOLoop;
use Mojo::IOLoop::Client;
use Mojo::IOLoop::TLS;
use IO::Socket::IP;
my $s;
my $client = Mojo::IOLoop::Client->new;
$client->on(connect => sub {
my $handle = pop;
my $tls_remote = Mojo::IOLoop::TLS->new($handle);
$tls_remote->on(upgrade => sub {
$s = Mojo::IOLoop::Stream->new(pop());
$s->on(read => sub {
warn pop;
});
$s->on(close => sub {
warn "STREAM CLOSED";
});
$s->on(error => sub {
die pop;
});
$s->write("GET / HTTP/1.1\r\nHost: www.google.ru\r\n\r\n");
$s->start;
});
$tls_remote->on(error => sub { die pop; });
$tls_remote->negotiate;
});
$client->on(error => sub {
die pop();
});
$client->connect(address => 'www.google.ru', port => 443);
Mojo::IOLoop->start;
#!/usr/bin/perl
use Mojo::Base;
use Mojo::IOLoop;
use Mojo::IOLoop::Server;
use Mojo::IOLoop::TLS;
use IO::Socket::IP;
my $s;
my $server = Mojo::IOLoop::Server->new;
$server->on(accept => sub {
my $handle = pop;
my $tls_remote = Mojo::IOLoop::TLS->new($handle);
$tls_remote->on(upgrade => sub {
$s = Mojo::IOLoop::Stream->new(pop());
$s->on(read => sub {
warn pop;
$s->write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\n\r\nHi, Vasya");
});
$s->on(close => sub {
warn "STREAM CLOSED";
});
$s->on(error => sub {
die pop;
});
$s->start;
});
$tls_remote->on(error => sub { die pop; });
$tls_remote->negotiate(server => 1, tls_cert => '/home/logioniz/cert/cert.pem', tls_key => '/home/logioniz/cert/key.pem', tls_verify => 0x00);
});
$server->on(error => sub {
die pop();
});
$server->listen(port => 443);
$server->start;
# Mojo::IOLoop->server({port => 80, tls => 1} => sub {
# my $stream = @_[1];
# $stream->on(read => sub {
# warn pop;
# shift->write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\n\r\nHi, Vasya");
# });
# $stream->on(close => sub {
# warn "STREAM CLOSED";
# });
# $stream->on(error => sub {
# die pop;
# });
# });
Mojo::IOLoop->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment