1 |
#!/opt/bin/perl |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
aemp - AnyEvent:MP utility |
6 |
|
7 |
=head1 SYNOPSIS |
8 |
|
9 |
aemp command args... |
10 |
|
11 |
# protocol commands |
12 |
aemp snd <port> <arg...> # send a message |
13 |
aemp mon <port> # wait till port is killed |
14 |
|
15 |
# node configuration: secret |
16 |
aemp gensecret # generate a random shared secret |
17 |
aemp setsecret <secret> # set the shared secret |
18 |
aemp clrsecret # remove the secret |
19 |
|
20 |
# node configuration: TLS |
21 |
aemp setcert <file> # set a certificate (key.pem + certificate.pem) |
22 |
aemp clrcert # remove certificate |
23 |
aemp gencert # generate a random certificate |
24 |
|
25 |
=head1 DESCRIPTION |
26 |
|
27 |
With aemp you can configure various aspects of AnyEvent::MP and it's protocol. |
28 |
|
29 |
=cut |
30 |
|
31 |
use common::sense; |
32 |
|
33 |
use Carp (); |
34 |
|
35 |
use AnyEvent; |
36 |
use AnyEvent::Util; |
37 |
|
38 |
use AnyEvent::MP::Config; |
39 |
use AnyEvent::MP; |
40 |
|
41 |
sub my_run_cmd { |
42 |
my ($cmd) = @_; |
43 |
|
44 |
my $cv = &run_cmd; |
45 |
my $status = $cv->recv; |
46 |
|
47 |
$status |
48 |
and die "@$cmd: command failed with exit status $status."; |
49 |
} |
50 |
|
51 |
sub gen_cert { |
52 |
my_run_cmd [qw(openssl req |
53 |
-new -nodes -x509 -days 3650 |
54 |
-newkey rsa:2048 -keyout /dev/fd/3 |
55 |
-batch -subj /CN=AnyEvent::MP |
56 |
)], |
57 |
"<", "/dev/null", |
58 |
">" , \my $cert, |
59 |
"3>", \my $key, |
60 |
"2>", "/dev/null"; |
61 |
|
62 |
"$cert$key" |
63 |
} |
64 |
|
65 |
our $cfg = \%AnyEvent::MP::Config::CFG; |
66 |
our $nodecfg = $cfg; |
67 |
|
68 |
sub resolve_port { |
69 |
my ($node, $port) = split /#/, $_[0], 2; |
70 |
|
71 |
$node = (resolve_node $node)->recv; |
72 |
"$node#$port" |
73 |
} |
74 |
|
75 |
our %CMD = ( |
76 |
snd => sub { |
77 |
my $port = resolve_port shift @ARGV; |
78 |
initialise_node "slave/", node_of $port; |
79 |
|
80 |
snd $port, @ARGV; |
81 |
|
82 |
my $cv = AE::cv; |
83 |
mon $port, $cv; |
84 |
my $reply = port { &$cv }; |
85 |
snd node_of $port, relay => $reply, "ok"; |
86 |
|
87 |
print join " ", $cv->recv, "\n"; |
88 |
}, |
89 |
|
90 |
mon => sub { |
91 |
my $port = resolve_port shift @ARGV; |
92 |
initialise_node "slave/", node_of $port; |
93 |
|
94 |
mon $port, my $cv = AE::cv; |
95 |
print join " ", $cv->recv, "\n"; |
96 |
}, |
97 |
|
98 |
setsecret => sub { |
99 |
@ARGV == 1 |
100 |
or die "shared secret missing\n"; |
101 |
|
102 |
$nodecfg->{secret} = shift @ARGV; |
103 |
++$cfg->{dirty}; |
104 |
}, |
105 |
gensecret => sub { |
106 |
$nodecfg->{secret} = AnyEvent::MP::Base::asciibits AnyEvent::MP::Base::nonce 64; |
107 |
++$cfg->{dirty}; |
108 |
}, |
109 |
clrsecret => sub { |
110 |
delete $nodecfg->{secret}; |
111 |
++$cfg->{dirty}; |
112 |
}, |
113 |
|
114 |
setcert => sub { |
115 |
@ARGV == 1 |
116 |
or die "key+certificate pem filename missing\n"; |
117 |
|
118 |
open my $fh, "<", $ARGV[0] |
119 |
or die "$ARGV[0]: $!"; |
120 |
|
121 |
local $/; |
122 |
$nodecfg->{cert} = <$fh>; |
123 |
++$cfg->{dirty}; |
124 |
}, |
125 |
gencert => sub { |
126 |
$nodecfg->{cert} = gen_cert; |
127 |
++$cfg->{dirty}; |
128 |
}, |
129 |
clrcert => sub { |
130 |
delete $nodecfg->{cert}; |
131 |
++$cfg->{dirty}; |
132 |
}, |
133 |
); |
134 |
|
135 |
sub docmd { |
136 |
my $cmd = shift @ARGV; |
137 |
|
138 |
$CMD{$cmd} |
139 |
or die "$cmd: no such aemp command (try man aemp)"; |
140 |
|
141 |
$CMD{$cmd}(); |
142 |
} |
143 |
|
144 |
@ARGV |
145 |
or die "Usage: aemp subcommand ... (try man aemp)\n"; |
146 |
|
147 |
docmd; |
148 |
|
149 |
|