Пушнул последние правки
This commit is contained in:
parent
cb29d97370
commit
12dde17c01
|
|
@ -6,7 +6,6 @@ require_once dirname(__DIR__) . "/plugins/netsync/Netsync.php";
|
|||
try {
|
||||
|
||||
|
||||
|
||||
$rpc = new StaticRPC();
|
||||
$instance = $rpc->getPlugins()["netsync"] ?? null;
|
||||
if ($instance === null) {
|
||||
|
|
@ -16,9 +15,10 @@ try {
|
|||
* @var Netsync $instance
|
||||
*/
|
||||
|
||||
$config = $instance->getRoutingConfig();
|
||||
$outfile = $argv[1];
|
||||
file_put_contents($outfile, implode("\n", $config));
|
||||
$config = $instance->sync();
|
||||
|
||||
var_dump($config);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "\nError:" . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
|
|
|
|||
|
|
@ -39,4 +39,11 @@ abstract class Plugin implements IPluggable
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function saveConfig()
|
||||
{
|
||||
$wrapper = $this->context->getConfig();
|
||||
$wrapper[$this->context->getName()] = $this->config;
|
||||
$wrapper->save();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,11 @@ class RPC
|
|||
protected Config $config;
|
||||
protected array $plugins = [];
|
||||
|
||||
public function getPlugins(): array
|
||||
{
|
||||
return $this->plugins;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = new Config();
|
||||
|
|
|
|||
|
|
@ -7,8 +7,7 @@ class API extends Plugin
|
|||
{
|
||||
parent::onInit($context);
|
||||
if (!isset($this->config["key"])) {
|
||||
$this->config["key"] = sha1(rand() . uniqid());
|
||||
$this->context->getConfig()->save();
|
||||
$this->generateNewKey();
|
||||
}
|
||||
|
||||
$headers = getallheaders();
|
||||
|
|
@ -18,6 +17,13 @@ class API extends Plugin
|
|||
}
|
||||
}
|
||||
|
||||
public function generateNewKey(): string
|
||||
{
|
||||
$this->config["key"] = sha1(rand() . uniqid());
|
||||
$this->saveConfig();
|
||||
return $this->config["key"];
|
||||
}
|
||||
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->config["key"];
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ class Netsync extends Plugin
|
|||
throw new RuntimeException("API key is empty");
|
||||
}
|
||||
|
||||
$ch = curl_init($host);
|
||||
$ch = curl_init("http://" . $host . "/rpc");
|
||||
|
||||
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
|
|
@ -19,9 +19,10 @@ class Netsync extends Plugin
|
|||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
||||
"jsonrpc" => "2.0",
|
||||
"id" => "1",
|
||||
"method" => "getNetworks",
|
||||
"method" => "getConfig",
|
||||
"params" => []
|
||||
]));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
"X-Auth: " . md5($key),
|
||||
|
|
@ -29,10 +30,42 @@ class Netsync extends Plugin
|
|||
]);
|
||||
|
||||
$output = curl_exec($ch);
|
||||
if (curl_error($ch)) {
|
||||
return curl_error($ch);
|
||||
try {
|
||||
if ($err = curl_error($ch)) {
|
||||
throw new RuntimeException("Failed to fetch remote API: " . $err);
|
||||
}
|
||||
} finally {
|
||||
curl_close($ch);
|
||||
return $output;
|
||||
}
|
||||
|
||||
$header = @json_decode($output, true);
|
||||
|
||||
$result = $header["result"] ?? null;
|
||||
|
||||
$networks = $result["networks"] ?? null;
|
||||
if ($result === null or $networks === null) {
|
||||
throw new RuntimeException("Response has invalid data");
|
||||
}
|
||||
|
||||
$available = array_keys((new NetworkConfigReader())->getConfigs());
|
||||
|
||||
$remote_enabled = array_filter($networks, function ($e) use ($available) {
|
||||
return in_array($e, $available);
|
||||
});
|
||||
$wrapper = $this->context->getConfig();
|
||||
$local_enabled = $wrapper["networks"];
|
||||
$diff = array_diff($remote_enabled, $local_enabled);
|
||||
if (count($diff) > 0) {
|
||||
$wrapper["networks"] = $remote_enabled;
|
||||
$wrapper->save();
|
||||
}
|
||||
foreach ($this->context->getRPC()->getPlugins() as $plugin) {
|
||||
/**
|
||||
* @var IPluggable $plugin
|
||||
*/
|
||||
|
||||
$plugin->onSync();
|
||||
}
|
||||
return $diff;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue