Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezax5 committed Jan 2, 2024
0 parents commit ef7578f
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 0 deletions.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "alirezax5/marzbanapiphp",
"description": "marzban api",
"type": "library",
"autoload": {
"psr-4": {
"alirezax5\\MarzbanApi\\": "src/"

}
},
"authors": [
{
"name": "alirezax5",
"email": "[email protected]"
}
],
"require": {
}
}
36 changes: 36 additions & 0 deletions src/Api/Admins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait Admins
{
public function getAdminToken()
{
return $this->request('/api/admin/token', ['username' => $this->getUsername(), 'password' => $this->getPassword()], self::POST);
}

public function getAdmin()
{
return $this->request('/api/admin');
}

public function getAdmins()
{
return $this->request('/api/admins');
}

public function createAdmin($username, $password, $is_sudo = false)
{
return $this->request('/api/admin', compact('username', 'password', 'is_sudo'), self::POST);
}

public function editAdmin($username, $password, $is_sudo = false)
{
return $this->request('/api/admin/' . $username, compact('password', 'is_sudo'), self::PUT);
}

public function deleteAdmin($username)
{
return $this->request('/api/admin/' . $username, [], self::DELETE);
}
}
26 changes: 26 additions & 0 deletions src/Api/Core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait Core
{
public function getCoreStat()
{
return $this->request('/api/core');
}

public function restartCore()
{
return $this->request('/api/core/restart', [], self::POST);
}

public function getCoreConfig()
{
return $this->request('/api/core/config');
}

public function editCoreConfig($body)
{
return $this->request('/api/core/config', $body, self::PUT);
}
}
48 changes: 48 additions & 0 deletions src/Api/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait Node
{
public function nodeSettings()
{
return $this->request('/api/node/settings');
}

public function getNodes()
{
return $this->request('/api/nodes');
}

public function usageNodes()
{
return $this->request('/api/nodes/usage');
}

public function addNode($name, $address, $port = '62050', $api_port = '62051', $add_as_new_host = true, $usage_coefficient = '1')
{
return $this->request('/api/node/{node_id}', compact('name', 'address', 'port', 'api_port' . 'add_as_new_host', 'usage_coefficient'), self::POST);
}

public function getNode($node_id)
{
return $this->request('/api/node/' . $node_id);
}

public function editNode($node_id, $name, $address, $port = '62050', $api_port = '62051', $add_as_new_host = true, $usage_coefficient = '1')
{
return $this->request('/api/node/' . $node_id, compact('name', 'address', 'port', 'api_port' . 'add_as_new_host', 'usage_coefficient'), self::PUT);
}

public function removeNode($node_id)
{
return $this->request('/api/node/' . $node_id, [], self::DELETE);
}

public function reconnectNode($node_id)
{
return $this->request('/api/node/' . $node_id . '/reconnect', [], self::POST);
}


}
23 changes: 23 additions & 0 deletions src/Api/Subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait Subscription
{
public function getSub($token, $useragent = 'V2ray')
{
return $this->request('/sub/' . $token . '/', [], self::GET);
}

public function getSubInfo($token)
{
return $this->request('/sub/' . $token . '/info', [], self::GET);
}

public function getSubByclient($token, $client_type = 'v2ray')
{
return $this->request('/sub/' . $token . '/' . $client_type, [], self::GET);
}


}
26 changes: 26 additions & 0 deletions src/Api/System.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait System
{
public function statsSystem()
{
return $this->request('/api/system');
}

public function inbounds()
{
return $this->request('/api/inbounds');
}

public function hosts()
{
return $this->request('/api/hosts');
}

public function editHosts($body)
{
return $this->request('/api/hosts', $body, self::PUT);
}
}
56 changes: 56 additions & 0 deletions src/Api/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait User
{
public function addUser($username, $expire, $data_limit, $proxies)
{
return $this->request('/api/user', compact('username', 'expire', 'data_limit', 'proxies'), self::POST);
}

public function getUser($username)
{
return $this->request('/api/user/' . $username, [], 'GET');
}

public function editUser($username, $expire, $data_limit, $proxies = '')
{
return $this->request('/api/user/' . $username, compact('expire', 'data_limit', 'proxies'), self::PUT);
}

public function removeUser($username)
{
return $this->request('/api/user/' . $username, [], self::DELETE);
}

public function resetUser($username)
{
return $this->request('/api/user/' . $username . '/reset', [], self::POST);
}

public function resetUsers()
{
return $this->request('/api/users/reset', [], self::POST);
}

public function revokeSubUser($username)
{
return $this->request('/api/user/' . $username . '/revoke_sub', [], self::POST);
}

public function getUsers($offset = 0, $limit = 0, $username = null, $status = null, $sort = null)
{
return $this->request('/api/users', compact('offset', 'limit', 'username', 'status', 'sort'), self::GET);
}

public function usageUser($username, $start = null, $end = null)
{
return $this->request('/api/user/' . $username . '/usage', compact('start', 'end'), self::GET);
}

public function removeExpired($passed_time = null)
{
return $this->request('/api/users/expired', compact('passed_time'), self::DELETE);
}
}
31 changes: 31 additions & 0 deletions src/Api/UserTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace alirezax5\MarzbanApi\Api;

trait UserTemplate
{
public function getuserTemplates($offset = null, $limit = null)
{
return $this->request('/api/user_template', compact('offset', 'limit'), self::GET);
}

public function createuserTemplate($name, $data_limit = 0, $expire_duration = 0, $inbounds = '')
{
return $this->request('/api/user_template', compact('name', 'data_limit', 'expire_duration', 'inbounds'), self::POST);
}

public function geteuserTemplate($id)
{
return $this->request('/api/user_template/' . $id, [], self::POST);
}

public function editUserTemplate($id, $name, $data_limit = 0, $expire_duration = 0, $inbounds = '')
{
return $this->request('/api/user_template/' . $id, compact('name', 'data_limit', 'expire_duration', 'inbounds'), self::PUT);
}
public function remveUserTemplate($id)
{
return $this->request('/api/user_template/' . $id,[], self::PUT);
}

}
115 changes: 115 additions & 0 deletions src/Marzban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace alirezax5\MarzbanApi;

use alirezax5\MarzbanApi\Api\{Admins, Core, Node, Subscription, System, User, UserTemplate};

class Marzban
{
use Admins, Core, Node, Subscription, System, User, UserTemplate;

const DELETE = 'DELETE';
const GET = 'GET';
const PUT = 'PUT';
const POST = 'POST';
public $url = null;
private $password = null;
private $username = null;
private $token = null;
private $parameter = null;

public function __construct($url)
{
$this->url = $url;
return $this;
}

public function setUsername($username)
{
$this->username = $username;
return $this;
}

public function getUsername()
{
return $this->username;
}

public function setPassword($password)
{
$this->password = $password;
return $this;
}

public function getPassword()
{
return $this->password;
}

public function setToken($token)
{
$this->token = $token;
return $this;
}

public function getToken()
{
return $this->token;
}

public function setParameter($parameter)
{
$this->parameter = $parameter;
return $this;
}

public function getParameter()
{
return $this->parameter;
}

protected function request($path, $body = [], $httpMetHod = 'GET')
{
$data = $httpMetHod == 'POST' || $httpMetHod == 'PUT' ? json_encode($body) : http_build_query($body);
if ($this->getToken() != null)
$header = [
'Accept: application/json',
'Authorization: Bearer ' . $this->getToken(),
'Content-Type: application/json'
];

if ($path == '/api/admin/token') {
$header = [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
];
$data = http_build_query($body);
}


$ch = curl_init();
$options = [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->getUrl($path),
CURLOPT_POST => $httpMetHod == 'POST' ? true : false,
CURLOPT_CUSTOMREQUEST => $httpMetHod,
CURLOPT_POSTFIELDS => $data,
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_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
];

curl_setopt_array($ch, $options);
$res = curl_exec($ch);
return json_decode($res, true);
}

private function getUrl($path)
{

return $this->url . $path;
}

}

0 comments on commit ef7578f

Please sign in to comment.