Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Logioniz / Mojolicious::Plugin::Coro.pm
Last active July 22, 2018 12:34
Coro and Mojolicious
package Mojolicious::Plugin::Coro;
use Mojo::Base 'Mojolicious::Plugin';
use Coro;
use Mojo::IOLoop;
# Wrap application in coroutine and reschedule main coroutine in event loop
sub register {
my ($self, $app) = @_;
my $subscribers = $app->plugins->subscribers('around_dispatch');
@Logioniz
Logioniz / Readme
Created June 22, 2016 14:01
Mojo client verification and get certificate info
Generate 2 certficate:
openssl req -nodes -new -x509 -keyout client.key -out client.cert
openssl req -nodes -new -x509 -keyout server.key -out server.cert
run server:
morbo server.pl -l 'https://*:3000?cert=./server.cert&key=./server.key&ca=client.cert'
run client:
openssl s_client -connect 127.0.0.1:3000 -cert client.cert -key client.key
GET / HTTP/1.0
@Logioniz
Logioniz / 1.pl
Created April 18, 2016 11:10
Eample with blocking task.
# to run simple execute: morbo 1.pl
# go to link htpp://127.0.0.1:3000/
use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::IOLoop::ReadWriteFork;
get '/' => 'index';
websocket '/find' => sub {
@Logioniz
Logioniz / bad_simple_proxy_connect_only.pl
Last active December 25, 2015 20:07
1-st not good variant of http/https proxy via CONNECT only method. 2-nd - is good simple http/https proxy.
#!/usr/bin/perl
use Mojo::Base -strict;
use Mojo::IOLoop;
use Mojo::UserAgent;
my %connections;
Mojo::IOLoop->server({port => 3128} => sub {
my ($loop, $stream_client, $client_id) = @_;
@Logioniz
Logioniz / redirect.pl
Last active November 17, 2021 17:17
Redirect from http to https with mojolicious
#!/usr/bin/perl
# To run need listen two ports: 80, 443
# sudo morbo 12.pl -l https://*:443?cert%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.crt%26key%3D%2Fhome%2Flogioniz%2Fcert%2Fserver.key%26verify%3D0x00 -l http://*:80
# go to url in browser http://your_ip/qwe/asd
use Mojo::Base -strict;
use Mojolicious::Lite;
@Logioniz
Logioniz / perl_ws_many_long_blocking_operations.pl
Last active December 9, 2015 08:00
Example how handle many long blocking operation when server asynchronous
#!/usr/bin/perl
use Mojo::Base -strict;
use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::IOLoop::ReadWriteFork;
plugin 'Mojolicious::Plugin::ForkCall';
@Logioniz
Logioniz / perl_ws_long_blocking_operation.pl
Created December 5, 2015 23:21
Example how handle long blocking operation when server asynchronous
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Mojolicious::Lite;
use Mojo::IOLoop;
use Mojo::IOLoop::ReadWriteFork;
@Logioniz
Logioniz / mojo_ioloop_dns.pl
Created September 29, 2015 11:05
Mojo::IOLoop::Net::DNS
#!/usr/bin/perl
package Mojo::IOLoop::Net::DNS;
use Mojo::Base -base;
use Net::DNS;
use Mojo::IOLoop;
use Scalar::Util 'weaken';
sub query {
@Logioniz
Logioniz / 1.pl
Created September 14, 2015 16:09
ipv4, ipv6 connect async ev
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo/;
use EV;
my $host = 'localhost';
my $port = '27017';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');
my ($err, @hosts) = getaddrinfo($host, $port, {protocol => $tcp_proto});
@Logioniz
Logioniz / hosts.pl
Created September 12, 2015 14:32
detect host by dnsname
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo unpack_sockaddr_in inet_pton getnameinfo inet_ntop/;
use EV;
use Data::Dumper;
my $host = 'localhost';
my $port = '3000';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');