Пушнул последние правки
This commit is contained in:
parent
cb29d97370
commit
12dde17c01
|
|
@ -6,7 +6,6 @@ require_once dirname(__DIR__) . "/plugins/netsync/Netsync.php";
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$rpc = new StaticRPC();
|
$rpc = new StaticRPC();
|
||||||
$instance = $rpc->getPlugins()["netsync"] ?? null;
|
$instance = $rpc->getPlugins()["netsync"] ?? null;
|
||||||
if ($instance === null) {
|
if ($instance === null) {
|
||||||
|
|
@ -16,9 +15,10 @@ try {
|
||||||
* @var Netsync $instance
|
* @var Netsync $instance
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$config = $instance->getRoutingConfig();
|
$config = $instance->sync();
|
||||||
$outfile = $argv[1];
|
|
||||||
file_put_contents($outfile, implode("\n", $config));
|
var_dump($config);
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
echo "\nError:" . $e->getMessage() . "\n";
|
echo "\nError:" . $e->getMessage() . "\n";
|
||||||
exit(1);
|
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 Config $config;
|
||||||
protected array $plugins = [];
|
protected array $plugins = [];
|
||||||
|
|
||||||
|
public function getPlugins(): array
|
||||||
|
{
|
||||||
|
return $this->plugins;
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->config = new Config();
|
$this->config = new Config();
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,7 @@ class API extends Plugin
|
||||||
{
|
{
|
||||||
parent::onInit($context);
|
parent::onInit($context);
|
||||||
if (!isset($this->config["key"])) {
|
if (!isset($this->config["key"])) {
|
||||||
$this->config["key"] = sha1(rand() . uniqid());
|
$this->generateNewKey();
|
||||||
$this->context->getConfig()->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers = getallheaders();
|
$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
|
public function getKey(): string
|
||||||
{
|
{
|
||||||
return $this->config["key"];
|
return $this->config["key"];
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class Netsync extends Plugin
|
||||||
throw new RuntimeException("API key is empty");
|
throw new RuntimeException("API key is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init($host);
|
$ch = curl_init("http://" . $host . "/rpc");
|
||||||
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||||
|
|
@ -19,9 +19,10 @@ class Netsync extends Plugin
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
||||||
"jsonrpc" => "2.0",
|
"jsonrpc" => "2.0",
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
"method" => "getNetworks",
|
"method" => "getConfig",
|
||||||
"params" => []
|
"params" => []
|
||||||
]));
|
]));
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
"X-Auth: " . md5($key),
|
"X-Auth: " . md5($key),
|
||||||
|
|
@ -29,10 +30,42 @@ class Netsync extends Plugin
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$output = curl_exec($ch);
|
$output = curl_exec($ch);
|
||||||
if (curl_error($ch)) {
|
try {
|
||||||
return curl_error($ch);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue