forked from KrzysztofKowalski/satan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.pl
143 lines (116 loc) · 3.34 KB
/
agent.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl -l
#
# Satan (agent)
# Shell account service manager
# Rootnode http://rootnode.net
#
# Copyright (C) 2009-2012 Marcin Hlybin
# All rights reserved.
#
use warnings;
use strict;
use JSON::XS;
use IO::Socket;
use YAML qw(LoadFile);
use FindBin qw($Bin);
use Data::Dumper;
no Smart::Comments;
use lib $Bin;
# Satan modules
use Satan::Admin;
use Satan::Dns;
use Satan::Mysql;
use Satan::Vhost;
our $MINLEN = undef;
our $MAXLEN = undef;
# json serialization
my $json = JSON::XS->new->utf8;
# get service name
my $service_name = shift or die "Service name not specified!\n";
# load agent configuration
my $agent_conf = YAML::LoadFile("$Bin/../config/agent.yaml");
my $agent = $agent_conf->{$service_name};
# open log files
if (!@ARGV) {
open STDOUT,">>","$Bin/../logs/access.log";
open STDERR,">>","$Bin/../logs/error.log";
chmod 0600,"$Bin/../logs/access.log";
chmod 0600,"$Bin/../logs/error.log";
}
# create socket
my $s_agent = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => $agent->{port},
Proto => 'tcp',
Listen => 5,
ReuseAddr => 1,
) or die "Cannot create socket! $!\n";
# accept connections
while(my $s_server = $s_agent->accept()) {
# buffering is lame
$s_server->autoflush(1);
my ($client, $response);
SERVER:
while(<$s_server>) {
chomp;
# get json request and split into client info and request
eval {
$client = $json->decode($_);
}
# parse error
or do {
$response = { status => 503, message => 'Cannot parse JSON' };
last SERVER;
};
### Client: $client
# store request as array
my @request = @{ $client->{request} };
delete $client->{request};
# check if requested service name is the same as agent's
my $request_service_name = shift @request;
if ($service_name ne $request_service_name) {
$response = { status => 503, message => "Wrong agent for service $service_name" };
last SERVER;
}
# get command name
my $command_name = shift @request || 'list';
# agent module name
$agent->{module} = 'Satan::' . ucfirst $service_name;
# create object
my $service = $agent->{module}->new($client);
# get available command names
my %export_ok = $service->get_export;
### Request: @request
# send command
if ($export_ok{$command_name}) {
$agent->{response} = $service->$command_name(@request);
}
else {
my $available_commands = join q{, }, sort keys %export_ok;
my $help_message = "Command \033[1m$command_name\033[0m is NOT available. "
. "Available commands are: \033[1;32m$available_commands\033[0m\n"
. "Run \033[1;34satan $service_name help\033[0m for help or visit "
. "http://rootnode.net/satan/$service_name for details.";
$response = { status => 400, message => $help_message };
last SERVER;
}
# command finished successfully
if(!$agent->{response}) {
$response = { status => 0, message => 'OK' };
# get data if exists
if ($agent->{data} = $service->get_data) {
$response->{data} = $agent->{data};
}
}
# user error occured
else {
$response = { status => 400, message => $agent->{response} }
}
# send response
print $s_server $json->encode($response);
}
# send status
print $s_server $json->encode($response);
close ($s_server);
}
exit;