1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Devgiants\Command; |
4
|
|
|
|
5
|
|
|
use Buzz\Message\Request; |
6
|
|
|
use Devgiants\Model\ApplicationCommand; |
7
|
|
|
use Devgiants\Model\NatRule; |
8
|
|
|
use Devgiants\Configuration\ConfigurationManager; |
9
|
|
|
use Devgiants\Configuration\ApplicationConfiguration as AppConf; |
10
|
|
|
use Pimple\Container; |
11
|
|
|
use Symfony\Component\Console\Exception\InvalidOptionException; |
12
|
|
|
use Symfony\Component\Console\Exception\LogicException; |
13
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class NatDeleteCommand extends ApplicationCommand |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const OPTION_ID = 'id'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Wan command constructor. |
26
|
|
|
* |
27
|
|
|
* @param null|string $name |
28
|
|
|
* @param Container $container |
29
|
|
|
*/ |
30
|
|
|
public function __construct($name, Container $container) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($name, $container); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->setName('nat:delete') |
42
|
|
|
->setDescription('Delete NAT entry') |
43
|
|
|
->addOption(static::OPTION_ID, static::OPTION_ID, InputOption::VALUE_REQUIRED, 'Id of the NAT rule') |
44
|
|
|
->setHelp("This command allows you to remove livebox NAT entry by ID"); |
45
|
|
|
|
46
|
|
|
parent::configure(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
$ymlFile = $this->getConfigurationFile($input); |
55
|
|
|
|
56
|
|
|
if ($ymlFile !== NULL && is_file($ymlFile)) { |
57
|
|
|
|
58
|
|
|
// Structures check and configuration loading |
59
|
|
|
$configurationManager = new ConfigurationManager($ymlFile); |
60
|
|
|
$configuration = $configurationManager->load(); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
// Authentication |
64
|
|
|
$this->tools->authenticate( |
65
|
|
|
$configuration[AppConf::HOST[AppConf::NODE_NAME]], |
66
|
|
|
$configuration[AppConf::USER[AppConf::NODE_NAME]], |
67
|
|
|
$configuration[AppConf::PASSWORD] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$ruleId = $input->getOption(static::OPTION_ID); |
71
|
|
|
|
72
|
|
|
$commandInput = new ArrayInput([ |
73
|
|
|
'command' => 'nat:infos', |
74
|
|
|
'--file' => $input->getOption('file') |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$response = $this->runAnotherCommand($commandInput, $input); |
78
|
|
|
$json = json_decode($response); |
79
|
|
|
|
80
|
|
|
if (!empty($json->result) && !empty($json->result->status)) { |
81
|
|
|
$natRules = $json->result->status; |
82
|
|
|
$fullName = NatRule::ORIGIN . '_' . $ruleId; |
83
|
|
|
if (isset($natRules->$fullName)) { |
84
|
|
|
$natRule = NatRule::buildFrom($natRules->$fullName); |
85
|
|
|
|
86
|
|
|
// Execute request |
87
|
|
|
$response = $this->tools->createRequest( |
88
|
|
|
Request::METHOD_POST, |
89
|
|
|
"{$configuration[ AppConf::HOST[ AppConf::NODE_NAME ] ]}/ws", |
90
|
|
|
[ |
91
|
|
|
"service" => "Firewall", |
92
|
|
|
"method" => "deletePortForwarding", |
93
|
|
|
"parameters" => $natRule->getOutputForDelete() |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
$output->write($response->getContent()); |
97
|
|
|
} else { |
98
|
|
|
throw new InvalidOptionException("Id argument invalid, can't find it in existing NAT rules."); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
throw new LogicException('Wrong format from nat:infos command'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Handle post command stuff |
105
|
|
|
parent::execute($input, $output); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |