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