Skip to content

Commit

Permalink
Support for all panels
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezax5 authored Feb 1, 2023
1 parent 683c432 commit c632903
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 0 deletions.
201 changes: 201 additions & 0 deletions src/Panel/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

namespace alirezax5\XuiApi\Panel;


class Base
{
protected $url, $username, $password, $id, $cookie;
protected $path = [
'login' => '/login',
'status' => '/server/status',
'restartXrayService' => '/server/restartXrayService',
'stopXrayService' => '/server/stopXrayService',
'getXrayVersion' => '/server/getXrayVersion',
'installXray' => 'server/installXray/{id}',
'allSetting' => '/xui/setting/all',
'updateSetting' => '/xui/setting/update',
'updateUser' => '/xui/setting/updateUser',
'listInbound' => '/xui/inbound/list',
'inbound' => '/xui/inbound/get/{id}',
'delInbound' => '/xui/inbound/del/{id}',
'updateInbound' => '/xui/inbound/update/{id}',
'addInbound' => '/xui/inbound/add',
];
protected $defaults = [
'sniffing' => [
"enabled" => true,
"destOverride" => [
"http",
"tls"
]
],
];

public function __construct($url, $username, $password)
{
$this->url = $url;
$this->cookie = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt";
$this->username = $username;
$this->password = $password;
}

protected function curl($path, $body = [], $isPost = true)
{
$ch = curl_init();
$options = [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->getUrl($path),
CURLOPT_POST => $isPost == true ? true : false,
CURLOPT_CUSTOMREQUEST => $isPost == true ? 'POST' : 'GET',
CURLOPT_POSTFIELDS => http_build_query($body),
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13',
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEFILE => $this->getCookie(),
CURLOPT_COOKIEJAR => $this->getCookie(),
];
curl_setopt_array($ch, $options);
$StatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$res = curl_exec($ch);
return json_decode($res, true);
}

protected function setId($id)
{
$this->id = $id;
return $this;
}

protected function getId(): int
{
return $this->id;
}

protected function getUrl($path): string
{

if (isset($this->path[$path])) {
$urlPath = $this->path[$path];
if ($path == 'inbound' || $path == 'delInbound' || $path == 'updateInbound' || $path == 'installXray') {
$urlPath = strtr($this->path[$path], ['{id}' => $this->getId()]);
}
return $this->url . $urlPath;
}

return $this->url;
}

public function setCookie($dir)
{
$this->cookie = $dir;
return $this;
}

public function getCookie()
{
return $this->cookie;
}

protected function initCookieFile()
{
if (!$this->checkCookieFile())
file_put_contents($this->cookie, '');

return $this;
}

protected function resetCookieFile()
{
unlink($this->cookie);
$this->initCookieFile();
return $this;
}

protected function checkCookieFile()
{
return file_exists($this->cookie);
}


protected function auth()
{
if (!$this->checkCookieFile()) {
$this->initCookieFile();
$this->curl('login', ['username' => $this->username, 'password' => $this->password]);
}
return $this;
}

protected function authForce()
{
$this->resetCookieFile();
$this->curl('login', ['username' => $this->username, 'password' => $this->password]);

return $this;
}


public function login($force = false)
{
if ($force == false)
$this->auth();
else
$this->authForce();

return $this;
}

public function status()
{
return $this->curl('status', [], true);
}

public function getXrayVersion()
{
return $this->curl('getXrayVersion', [], true);
}

public function installXray($version = 'v1.6.4')
{
$this->setId($version);
return $this->curl('installXray', compact('version'), true);
}

public function restartXrayService()
{
return $this->curl('restartXrayService', [], true);
}

public function stopXrayService()
{
return $this->curl('stopXrayService', [], true);
}

public function startXrayService()
{
return $this->curl('restartXrayService', [], true);
}


public function listInbound()
{
return $this->curl('listInbound', [], true);
}

public function allSetting()
{
return $this->curl('allSetting', [], true);
}


public function updateUser($oldUsername, $oldPassword, $newUsername, $newPassword)
{
return $this->curl('updateUser', compact('oldPassword', 'oldUsername', 'newPassword', 'newUsername'), true);
}

public function removeInbound($id)
{
$this->setId($id);
return $this->curl('delInbound', [], true);
}
}
32 changes: 32 additions & 0 deletions src/Panel/FranzKafkaYu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace alirezax5\XuiApi\Panel;

class FranzKafkaYu extends Base
{

public function updateSetting($webPort, $webCertFile, $webKeyFile, $webBasePath, $xrayTemplateConfig, bool $tgBotEnable = false, $tgNotifyExpireTimeDiff = 0, $tgNotifyTrafficDiff = 0, $cpulimitNotifyConfig = 0, string $tgBotToken = null, $tgBotChatId = null, $tgRunTime = null, $timeLocation = 'Asia/Tehran', $webListen = '')
{
$com = compact('webPort', 'webCertFile', 'webKeyFile', 'webBasePath', 'xrayTemplateConfig', 'tgBotEnable', 'tgNotifyExpireTimeDiff', 'tgNotifyTrafficDiff', 'cpulimitNotifyConfig', 'tgBotToken', 'tgBotChatId', 'tgRunTime', 'timeLocation', 'webListen');
return $this->curl('updateSetting', $com, true);
}

public function addInbound($remark, $port, $protocol, $settings, $streamSettings, $autoreset = false, $ipalert = false, $iplimit = 0, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
return $this->curl('addInbound', compact('remark', 'port', 'protocol', 'settings', 'streamSettings', 'autoreset', 'ipalert', 'iplimit', 'sniffing', 'expiryTime', 'listen'), true);
}

public function editInbound($enable, $id, $remark, $port, $protocol, $settings, $streamSettings, $autoreset = false, $ipalert = false, $iplimit = 0, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
$this->setId($id);
return $this->curl('updateInbound', compact('enable', 'remark', 'port', 'protocol', 'settings', 'streamSettings', 'autoreset', 'ipalert', 'iplimit', 'sniffing', 'expiryTime', 'listen'), true);
}
}
31 changes: 31 additions & 0 deletions src/Panel/HexaSoftwareTech.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace alirezax5\XuiApi\Panel;

class HexaSoftwareTech extends Base
{
public function updateSetting($webPort, $webCertFile, $webKeyFile, $webBasePath, $xrayTemplateConfig, bool $tgBotEnable = false, $tgNotifyExpireTimeDiff = 0, $tgNotifyTrafficDiff = 0, string $tgBotToken = null, $tgBotChatId = null, $tgRunTime = null, $timeLocation = 'Asia/Tehran', $webListen = '')
{
$com = compact('webPort', 'webCertFile', 'webKeyFile', 'webBasePath', 'xrayTemplateConfig', 'tgBotEnable', 'tgNotifyExpireTimeDiff', 'tgNotifyTrafficDiff', 'tgBotToken', 'tgBotChatId', 'tgRunTime', 'timeLocation', 'webListen');
return $this->curl('updateSetting', $com, true);
}

public function addInbound($remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
return $this->curl('addInbound', compact('remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}

public function editInbound($enable, $id, $remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
$this->setId($id);
return $this->curl('updateInbound', compact('enable', 'remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}
}
32 changes: 32 additions & 0 deletions src/Panel/NidukaAkalanka.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace alirezax5\XuiApi\Panel;

class NidukaAkalanka extends Base
{

public function updateSetting($webPort, $webCertFile, $webKeyFile, $webBasePath, $xrayTemplateConfig, bool $tgBotEnable = false, $tgNotifyExpireTimeDiff = 0, $tgNotifyTrafficDiff = 0, string $tgBotToken = null, $tgBotChatId = null, $tgRunTime = null, $timeLocation = 'Asia/Tehran', $webListen = '')
{
$com = compact('webPort', 'webCertFile', 'webKeyFile', 'webBasePath', 'xrayTemplateConfig', 'tgBotEnable', 'tgNotifyExpireTimeDiff', 'tgNotifyTrafficDiff', 'tgBotToken', 'tgBotChatId', 'tgRunTime', 'timeLocation', 'webListen');
return $this->curl('updateSetting', $com, true);
}

public function addInbound($remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
return $this->curl('addInbound', compact('remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}

public function editInbound($enable, $id, $remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
$this->setId($id);
return $this->curl('updateInbound', compact('enable', 'remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}
}
32 changes: 32 additions & 0 deletions src/Panel/Vaxilu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace alirezax5\XuiApi\Panel;

class Vaxilu extends Base
{

public function updateSetting($webPort, $webCertFile, $webKeyFile, $webBasePath, $xrayTemplateConfig, $timeLocation = 'Asia/Tehran', $webListen = '')
{
$com = compact('webPort', 'webCertFile', 'webKeyFile', 'webBasePath', 'xrayTemplateConfig', 'timeLocation', 'webListen');
return $this->curl('updateSetting', $com, true);
}

public function addInbound($remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
return $this->curl('addInbound', compact('remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}

public function editInbound($enable, $id, $remark, $port, $protocol, $settings, $streamSettings, $sniffing = null, $expiryTime = 0, $listen = '')
{
$sniffing = $sniffing == null ? $this->defaults['sniffing'] : $sniffing;
$sniffing = json_encode($sniffing);
$settings = json_encode($settings);
$streamSettings = json_encode($streamSettings);
$this->setId($id);
return $this->curl('updateInbound', compact('enable', 'remark', 'port', 'protocol', 'settings', 'streamSettings', 'sniffing', 'expiryTime', 'listen'), true);
}
}

0 comments on commit c632903

Please sign in to comment.