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 |
# node configuration |
26 |
aemp setseeds noderef... # set seednodes |
27 |
aemp addseed noderef # add a seednode |
28 |
aemp delseed noderef # remove seednode |
29 |
|
30 |
=head1 DESCRIPTION |
31 |
|
32 |
With aemp you can configure various aspects of AnyEvent::MP and it's protocol. |
33 |
|
34 |
=cut |
35 |
|
36 |
use common::sense; |
37 |
|
38 |
use Carp (); |
39 |
|
40 |
use AnyEvent; |
41 |
use AnyEvent::Util; |
42 |
|
43 |
use AnyEvent::MP::Config; |
44 |
use AnyEvent::MP; |
45 |
|
46 |
sub my_run_cmd { |
47 |
my ($cmd) = @_; |
48 |
|
49 |
my $cv = &run_cmd; |
50 |
my $status = $cv->recv; |
51 |
|
52 |
$status |
53 |
and die "@$cmd: command failed with exit status $status."; |
54 |
} |
55 |
|
56 |
sub gen_cert { |
57 |
my_run_cmd [qw(openssl req |
58 |
-new -nodes -x509 -days 3650 |
59 |
-newkey rsa:2048 -keyout /dev/fd/3 |
60 |
-batch -subj /CN=AnyEvent::MP |
61 |
)], |
62 |
"<", "/dev/null", |
63 |
">" , \my $cert, |
64 |
"3>", \my $key, |
65 |
"2>", "/dev/null"; |
66 |
|
67 |
"$cert$key" |
68 |
} |
69 |
|
70 |
our $cfg = \%AnyEvent::MP::Config::CFG; |
71 |
our $nodecfg = $cfg; |
72 |
|
73 |
sub resolve_port { |
74 |
my ($node, $port) = split /#/, $_[0], 2; |
75 |
|
76 |
$node = (resolve_node $node)->recv; |
77 |
"$node#$port" |
78 |
} |
79 |
|
80 |
our %CMD = ( |
81 |
snd => sub { |
82 |
my $port = resolve_port shift @ARGV; |
83 |
initialise_node "slave/", node_of $port; |
84 |
|
85 |
snd $port, @ARGV; |
86 |
|
87 |
my $cv = AE::cv; |
88 |
mon $port, $cv; |
89 |
my $reply = port { &$cv }; |
90 |
snd node_of $port, relay => $reply, "ok"; |
91 |
|
92 |
print join " ", $cv->recv, "\n"; |
93 |
}, |
94 |
|
95 |
mon => sub { |
96 |
my $port = resolve_port shift @ARGV; |
97 |
initialise_node "slave/", node_of $port; |
98 |
|
99 |
mon $port, my $cv = AE::cv; |
100 |
print join " ", $cv->recv, "\n"; |
101 |
}, |
102 |
|
103 |
setsecret => sub { |
104 |
@ARGV == 1 |
105 |
or die "shared secret missing\n"; |
106 |
|
107 |
$nodecfg->{secret} = shift @ARGV; |
108 |
++$cfg->{dirty}; |
109 |
}, |
110 |
gensecret => sub { |
111 |
$nodecfg->{secret} = AnyEvent::MP::Base::asciibits AnyEvent::MP::Base::nonce 64; |
112 |
++$cfg->{dirty}; |
113 |
}, |
114 |
clrsecret => sub { |
115 |
delete $nodecfg->{secret}; |
116 |
++$cfg->{dirty}; |
117 |
}, |
118 |
|
119 |
setcert => sub { |
120 |
@ARGV == 1 |
121 |
or die "key+certificate pem filename missing\n"; |
122 |
|
123 |
open my $fh, "<", $ARGV[0] |
124 |
or die "$ARGV[0]: $!"; |
125 |
|
126 |
local $/; |
127 |
$nodecfg->{cert} = <$fh>; |
128 |
++$cfg->{dirty}; |
129 |
}, |
130 |
gencert => sub { |
131 |
$nodecfg->{cert} = gen_cert; |
132 |
++$cfg->{dirty}; |
133 |
}, |
134 |
clrcert => sub { |
135 |
delete $nodecfg->{cert}; |
136 |
++$cfg->{dirty}; |
137 |
}, |
138 |
|
139 |
setseeds => sub { |
140 |
$cfg->{seeds} = [@ARGV]; |
141 |
@ARGV = (); |
142 |
++$cfg->{dirty}; |
143 |
}, |
144 |
addseed => sub { |
145 |
my $seed = shift @ARGV; |
146 |
@{ $cfg->{seeds} } = grep $_ ne $seed, @{ $cfg->{seeds} }; |
147 |
push @{ $cfg->{seeds} }, $seed; |
148 |
++$cfg->{dirty}; |
149 |
}, |
150 |
addseed => sub { |
151 |
my $seed = shift @ARGV; |
152 |
@{ $cfg->{seeds} } = grep $_ ne $seed, @{ $cfg->{seeds} }; |
153 |
++$cfg->{dirty}; |
154 |
}, |
155 |
); |
156 |
|
157 |
sub docmd { |
158 |
my $cmd = shift @ARGV; |
159 |
|
160 |
$CMD{$cmd} |
161 |
or die "$cmd: no such aemp command (try man aemp)"; |
162 |
|
163 |
$CMD{$cmd}(); |
164 |
} |
165 |
|
166 |
@ARGV |
167 |
or die "Usage: aemp subcommand ... (try man aemp)\n"; |
168 |
|
169 |
docmd; |
170 |
|
171 |
|