diff --git a/bin/sync-networks b/bin/sync-networks index 16b4f50..54db7f4 100755 --- a/bin/sync-networks +++ b/bin/sync-networks @@ -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); diff --git a/classes/Plugin.php b/classes/Plugin.php index f9e123c..d8089bf 100644 --- a/classes/Plugin.php +++ b/classes/Plugin.php @@ -39,4 +39,11 @@ abstract class Plugin implements IPluggable } } } + + protected function saveConfig() + { + $wrapper = $this->context->getConfig(); + $wrapper[$this->context->getName()] = $this->config; + $wrapper->save(); + } } \ No newline at end of file diff --git a/classes/RPC.php b/classes/RPC.php index a5f707b..113d150 100644 --- a/classes/RPC.php +++ b/classes/RPC.php @@ -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(); diff --git a/plugins/api/API.php b/plugins/api/API.php index f35b590..7a922cb 100644 --- a/plugins/api/API.php +++ b/plugins/api/API.php @@ -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"]; diff --git a/plugins/netsync/Netsync.php b/plugins/netsync/Netsync.php index 95bfa6d..6871836 100644 --- a/plugins/netsync/Netsync.php +++ b/plugins/netsync/Netsync.php @@ -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); } - 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; } } \ No newline at end of file