Feature: New frps api server

合并了原来的独立 API,直接调用 configuration.php 里面的配置,不再需要单独配置一次
增加配置文件获取接口 getconf,请求地址:`/api/?action=getconf&user=用户名&token=访问密钥&node=节点ID`
This commit is contained in:
Akkariin Meiko 2020-01-21 11:09:43 +08:00 committed by GitHub
parent e1f3a683b3
commit 5a26258cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,223 +1,205 @@
<?php <?php
error_reporting(0); namespace SakuraPanel;
Header("Content-type: text/plain");
$conn = mysqli_connect( use SakuraPanel;
/* 数据库地址 */ "localhost",
/* 数据库账号 */ "root",
/* 数据库密码 */ "123456",
/* 数据库名称 */ "spanel",
/* 数据库端口 */ 3306
) or die("Database error");
// API 密码,需要和 Frps.ini 里面设置的一样 // API 密码,需要和 Frps.ini 里面设置的一样
define("API_TOKEN", "SakuraFrpToken"); define("API_TOKEN", "SakuraFrpToken");
define("ROOT", realpath(__DIR__ . "/../"));
// 输出禁止错误 Header if(ROOT === false) {
function ServerForbidden($msg) { exit("Please place this file on /api/ folder");
Header("HTTP/1.1 403 {$msg}");
echo json_encode(Array(
'status' => 403,
'message' => $msg
), JSON_UNESCAPED_UNICODE);
exit;
} }
// 输出未找到错误 Header include(ROOT . "/configuration.php");
function ServerNotFound($msg) { include(ROOT . "/core/Database.php");
Header("HTTP/1.1 404 {$msg}"); include(ROOT . "/core/Regex.php");
echo json_encode(Array( include(ROOT . "/core/Utils.php");
'status' => 404,
'message' => $msg
), JSON_UNESCAPED_UNICODE);
exit;
}
// 输出未找到错误 Header $conn = null;
function ServerBadRequest($msg) { $db = new SakuraPanel\Database();
Header("HTTP/1.1 400 {$msg}");
echo json_encode(Array(
'status' => 400,
'message' => $msg
), JSON_UNESCAPED_UNICODE);
exit;
}
// 输出正常消息 include(ROOT . "/core/UserManager.php");
function LoginSuccessful($msg) { include(ROOT . "/core/NodeManager.php");
Header("Content-type: text/plain", true, 200); include(ROOT . "/core/ProxyManager.php");
echo json_encode(Array(
'status' => 200,
'success' => true,
'message' => $msg
), JSON_UNESCAPED_UNICODE);
exit;
}
// 输出正常消息 $pm = new ProxyManager();
function CheckSuccessful($msg) { $nm = new NodeManager();
Header("Content-type: text/plain", true, 200);
echo json_encode(Array(
'status' => 200,
'success' => true,
'message' => $msg
), JSON_UNESCAPED_UNICODE);
exit;
}
// Json 格式消息输出
function Println($data) {
Header("Content-type: text/plain", true, 200);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
function getBoolean($str) {
return $str == "true";
}
// 服务端 API 部分 // 服务端 API 部分
// 先进行 Frps 鉴权 // 先进行 Frps 鉴权
if(isset($_GET['apitoken']) && $_GET['apitoken'] == API_TOKEN) { if((isset($_GET['apitoken']) && $_GET['apitoken'] == API_TOKEN) || (isset($_GET['action']) && $_GET['action'] == "getconf")) {
if(isset($_GET['action'])) { switch($_GET['action']) {
switch($_GET['action']) { case "getconf":
if(isset($_GET['user'], $_GET['token'], $_GET['node'])) {
// 检查客户端是否合法 if(Regex::isUserName($_GET['user']) && Regex::isLetter($_GET['token']) && Regex::isNumber($_GET['node'])) {
case "checktoken": $rs = Database::querySingleLine("tokens", [
if(isset($_GET['user'])) { "username" => $_GET['user'],
if(preg_match("/^[A-Za-z0-9]{1,32}$/", $_GET['user'])) { "token" => $_GET['token']
$userToken = mysqli_real_escape_string($conn, $_GET['user']); ]);
$rs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `tokens` WHERE `token`='{$userToken}'")); if($rs && $nm->isNodeExist($_GET['node'])) {
if($rs) { $rs = $pm->getUserProxiesConfig($_GET['user'], $_GET['node']);
LoginSuccessful("Login successful, welcome!"); if(is_string($rs)) {
Header("Content-Type: text/plain");
exit($rs);
} else { } else {
ServerForbidden("Login failed"); Utils::sendServerNotFound("User or node not found");
} }
} else { } else {
ServerForbidden("Invalid username"); Utils::sendServerNotFound("User or node not found");
} }
} else { } else {
ServerForbidden("Username cannot be empty"); Utils::sendServerNotFound("Invalid username or token");
} }
break; } else {
Utils::sendServerNotFound("Invalid request");
// 检查隧道是否合法 }
case "checkproxy": break;
if(isset($_GET['user'])) {
if(preg_match("/^[A-Za-z0-9]{1,32}$/", $_GET['user'])) { // 检查客户端是否合法
$proxyName = str_replace("{$_GET['user']}.", "", $_GET['proxy_name']); case "checktoken":
$proxyType = $_GET['proxy_type'] ?? "tcp"; if(isset($_GET['user'])) {
$remotePort = Intval($_GET['remote_port']) ?? ""; if(Regex::isLetter($_GET['user']) && strlen($_GET['user']) == 16) {
$userToken = mysqli_real_escape_string($conn, $_GET['user']); $userToken = Database::escape($_GET['user']);
$rs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `tokens` WHERE `token`='{$userToken}'")); $rs = Database::querySingleLine("tokens", ["token" => $userToken]);
if($rs) { if($rs) {
if($proxyType == "tcp" || $proxyType == "udp" || $proxyType == "stcp" || $proxyType == "xtcp") { Utils::sendLoginSuccessful("Login successful, welcome!");
if(isset($remotePort) && preg_match("/^[0-9]{1,5}$/", $remotePort)) { } else {
$username = mysqli_real_escape_string($conn, $rs['username']); Utils::sendServerForbidden("Login failed");
// 这里只对远程端口做限制,可根据自己的需要修改 }
$rs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `proxies` WHERE `username`='{$username}' AND `remote_port`='{$remotePort}' AND `proxy_type`='{$proxyType}'")); } else {
if($rs) { Utils::sendServerForbidden("Invalid username");
if($rs['status'] !== "0") { }
ServerForbidden("Proxy disabled"); } else {
} Utils::sendServerForbidden("Username cannot be empty");
CheckSuccessful("Proxy exist"); }
} else { break;
ServerNotFound("Proxy not found");
// 检查隧道是否合法
case "checkproxy":
if(isset($_GET['user'])) {
if(Regex::isLetter($_GET['user']) && strlen($_GET['user']) == 16) {
$proxyName = str_replace("{$_GET['user']}.", "", $_GET['proxy_name']);
$proxyType = $_GET['proxy_type'] ?? "tcp";
$remotePort = Intval($_GET['remote_port']) ?? "";
$userToken = Database::escape($_GET['user']);
$rs = Database::querySingleLine("tokens", ["token" => $userToken]);
if($rs) {
if($proxyType == "tcp" || $proxyType == "udp" || $proxyType == "stcp" || $proxyType == "xtcp") {
if(isset($remotePort) && Regex::isNumber($remotePort)) {
$username = Database::escape($rs['username']);
// 这里只对远程端口做限制,可根据自己的需要修改
$rs = Database::querySingleLine("proxies", [
"username" => $username,
"remote_port" => $remotePort,
"proxy_type" => $proxyType
]);
if($rs) {
if($rs['status'] !== "0") {
Utils::sendServerForbidden("Proxy disabled");
} }
Utils::sendCheckSuccessful("Proxy exist");
} else { } else {
ServerBadRequest("Invalid request"); Utils::sendServerNotFound("Proxy not found");
}
} elseif($proxyType == "http" || $proxyType == "https") {
if(isset($_GET['domain']) || isset($_GET['subdomain'])) {
// 目前只验证域名和子域名
$domain = $_GET['domain'] ?? "null";
$subdomain = $_GET['subdomain'] ?? "null";
$username = mysqli_real_escape_string($conn, $rs['username']);
$domain = mysqli_real_escape_string($conn, $domain);
$subdomain = mysqli_real_escape_string($conn, $subdomain);
$domainsql = (isset($_GET['domain']) && !empty($_GET['domain'])) ? "`domain`='{$domain}'" : "`subdomain`='{$subdomain}'";
$rs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `proxies` WHERE `username`='{$username}' AND {$domainsql} AND `proxy_type`='{$proxyType}'"));
if($rs) {
if($rs['status'] !== "0") {
ServerForbidden("Proxy disabled");
}
CheckSuccessful("Proxy exist");
} else {
ServerNotFound("Proxy not found");
}
} else {
ServerBadRequest("Invalid request");
} }
} else { } else {
ServerBadRequest("Invalid request"); Utils::sendServerBadRequest("Invalid request");
} }
} else { } elseif($proxyType == "http" || $proxyType == "https") {
ServerNotFound("User not found"); if(isset($_GET['domain']) || isset($_GET['subdomain'])) {
} // 目前只验证域名和子域名
} else { $domain = $_GET['domain'] ?? "null";
ServerBadRequest("Invalid request"); $subdomain = $_GET['subdomain'] ?? "null";
} $username = Database::escape($rs['username']);
} else { $domain = Database::escape($domain);
ServerForbidden("Invalid username"); $subdomain = Database::escape($subdomain);
} $domainSQL = (isset($_GET['domain']) && !empty($_GET['domain'])) ? ["domain" => $domain] : ["subdomain" => $subdomain];
break; $querySQL = [
case "getlimit": "username" => $username,
Header("Content-type: text/plain", true, 200); "proxy_type" => $proxyType
if(isset($_GET['user'])) { ];
if(preg_match("/^[A-Za-z0-9]{1,32}$/", $_GET['user'])) { $querySQL = Array_merge($querySQL, $domainSQL);
$userToken = mysqli_real_escape_string($conn, $_GET['user']); $rs = Database::querySingleLine("proxies", $querySQL);
$rs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `tokens` WHERE `token`='{$userToken}'")); if($rs) {
if($rs) { if($rs['status'] !== "0") {
$username = mysqli_real_escape_string($conn, $rs['username']); Utils::sendServerForbidden("Proxy disabled");
$ls = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `limits` WHERE `username`='{$username}'"));
if($ls) {
exit(json_encode(Array(
'status' => 200,
'max-in' => Floatval($ls['inbound']),
'max-out' => Floatval($ls['outbound'])
)));
} else {
$uinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `users` WHERE `username`='{$username}'"));
if($uinfo) {
if($rs['group'] == "admin") {
exit(json_encode(Array(
'status' => 200,
'max-in' => 1000000,
'max-out' => 1000000
)));
}
$group = mysqli_real_escape_string($conn, $rs['group']);
$gs = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `groups` WHERE `name`='{$group}'"));
if($gs) {
exit(json_encode(Array(
'status' => 200,
'max-in' => Floatval($gs['inbound']),
'max-out' => Floatval($gs['outbound'])
)));
} else {
exit(json_encode(Array(
'status' => 200,
'max-in' => 1024,
'max-out' => 1024
)));
} }
Utils::sendCheckSuccessful("Proxy exist");
} else {
Utils::sendServerNotFound("Proxy not found");
} }
} else {
Utils::sendServerBadRequest("Invalid request");
} }
} else { } else {
ServerForbidden("Login failed"); Utils::sendServerBadRequest("Invalid request");
} }
} else { } else {
ServerForbidden("Invalid username"); Utils::sendServerNotFound("User not found");
} }
} else { } else {
ServerForbidden("Username cannot be empty"); Utils::sendServerBadRequest("Invalid request");
} }
break; } else {
default: Utils::sendServerForbidden("Invalid username");
ServerNotFound("Undefined action"); }
} break;
} else { case "getlimit":
ServerNotFound("Invalid request"); if(isset($_GET['user'])) {
if(Regex::isLetter($_GET['user']) && strlen($_GET['user']) == 16) {
$userToken = Database::escape($_GET['user']);
$rs = Database::querySingleLine("tokens", ["token" => $userToken]);
if($rs) {
$username = Database::escape($rs['username']);
$ls = Database::querySingleLine("limits", ["username" => $username]);
if($ls) {
Utils::sendJson(Array(
'status' => 200,
'max-in' => Floatval($ls['inbound']),
'max-out' => Floatval($ls['outbound'])
));
} else {
$uinfo = Database::querySingleLine("users", ["username" => $username]);
if($uinfo) {
if($uinfo['group'] == "admin") {
Utils::sendJson(Array(
'status' => 200,
'max-in' => 1000000,
'max-out' => 1000000
));
}
$group = Database::escape($uinfo['group']);
$gs = Database::querySingleLine("groups", ["name" => $group]);
if($gs) {
Utils::sendJson(Array(
'status' => 200,
'max-in' => Floatval($gs['inbound']),
'max-out' => Floatval($gs['outbound'])
));
} else {
Utils::sendJson(Array(
'status' => 200,
'max-in' => 1024,
'max-out' => 1024
));
}
} else {
Utils::sendServerForbidden("User not exist");
}
}
} else {
Utils::sendServerForbidden("Login failed");
}
} else {
Utils::sendServerForbidden("Invalid username");
}
} else {
Utils::sendServerForbidden("Username cannot be empty");
}
break;
default:
Utils::sendServerNotFound("Undefined action");
} }
} else { } else {
ServerNotFound("Invalid request"); Utils::sendServerNotFound("Invalid request");
} }