2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utils;
|
|
|
|
|
2020-03-10 21:31:18 +08:00
|
|
|
use App\Models\Server;
|
2020-06-12 00:40:37 +08:00
|
|
|
use App\Models\ServerTrojan;
|
2020-03-10 21:31:18 +08:00
|
|
|
use App\Models\User;
|
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
class Helper
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public static function guid($format = false)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if (function_exists('com_create_guid') === true) {
|
|
|
|
return md5(trim(com_create_guid(), '{}'));
|
|
|
|
}
|
|
|
|
$data = openssl_random_pseudo_bytes(16);
|
|
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
|
|
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
|
|
if ($format) {
|
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . '-' . time());
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public static function exchange($from, $to)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
$result = file_get_contents('https://api.exchangeratesapi.io/latest?symbols=' . $to . '&base=' . $from);
|
|
|
|
$result = json_decode($result, true);
|
|
|
|
return $result['rates'][$to];
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public static function randomChar($len, $special = false)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
$chars = array(
|
|
|
|
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
|
|
|
|
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
|
|
|
|
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
|
|
|
|
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
|
|
|
|
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
|
|
|
|
"3", "4", "5", "6", "7", "8", "9"
|
|
|
|
);
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
if ($special) {
|
2019-10-29 15:33:36 +08:00
|
|
|
$chars = array_merge($chars, array(
|
|
|
|
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
|
|
|
|
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
|
|
|
|
"}", "<", ">", "~", "+", "=", ",", "."
|
|
|
|
));
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
$charsLen = count($chars) - 1;
|
|
|
|
shuffle($chars);
|
|
|
|
$str = '';
|
2020-01-11 13:36:52 +08:00
|
|
|
for ($i = 0; $i < $len; $i++) {
|
2019-10-29 15:33:36 +08:00
|
|
|
$str .= $chars[mt_rand(0, $charsLen)];
|
|
|
|
}
|
|
|
|
return $str;
|
|
|
|
}
|
2019-11-24 00:41:37 +08:00
|
|
|
|
2020-06-12 00:40:37 +08:00
|
|
|
public static function buildTrojanLink(ServerTrojan $server, User $user)
|
2020-06-12 00:35:35 +08:00
|
|
|
{
|
2020-06-12 17:16:21 +08:00
|
|
|
$server->name = rawurlencode($server->name);
|
2020-07-01 15:23:39 +08:00
|
|
|
$query = http_build_query([
|
|
|
|
'allowInsecure' => $server->allow_insecure,
|
2020-08-13 19:30:07 +08:00
|
|
|
'peer' => $server->server_name,
|
|
|
|
'sni' => $server->server_name
|
2020-07-01 15:23:39 +08:00
|
|
|
]);
|
|
|
|
$uri = "trojan://{$user->uuid}@{$server->host}:{$server->port}?{$query}#{$server->name}";
|
2020-06-12 00:35:35 +08:00
|
|
|
$uri .= "\r\n";
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
2020-03-10 21:31:18 +08:00
|
|
|
public static function buildVmessLink(Server $server, User $user)
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
2019-11-24 00:41:37 +08:00
|
|
|
$config = [
|
2019-12-28 12:31:47 +08:00
|
|
|
"v" => "2",
|
2020-03-10 21:31:18 +08:00
|
|
|
"ps" => $server->name,
|
|
|
|
"add" => $server->host,
|
|
|
|
"port" => $server->port,
|
2020-06-08 01:08:07 +08:00
|
|
|
"id" => $user->uuid,
|
2020-01-11 13:36:52 +08:00
|
|
|
"aid" => "2",
|
2020-03-10 21:31:18 +08:00
|
|
|
"net" => $server->network,
|
2020-01-11 13:36:52 +08:00
|
|
|
"type" => "none",
|
|
|
|
"host" => "",
|
|
|
|
"path" => "",
|
2020-03-10 21:31:18 +08:00
|
|
|
"tls" => $server->tls ? "tls" : ""
|
2019-11-24 00:41:37 +08:00
|
|
|
];
|
2020-03-10 21:31:18 +08:00
|
|
|
if ((string)$server->network === 'ws') {
|
|
|
|
$wsSettings = json_decode($server->networkSettings);
|
2019-12-31 13:45:50 +08:00
|
|
|
if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
|
|
|
|
if (isset($wsSettings->headers->Host)) $config['host'] = $wsSettings->headers->Host;
|
2019-11-24 00:41:37 +08:00
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
|
2019-11-24 00:41:37 +08:00
|
|
|
}
|
2020-02-02 20:44:52 +08:00
|
|
|
|
|
|
|
public static function multiPasswordVerify($algo, $password, $hash)
|
|
|
|
{
|
|
|
|
switch($algo) {
|
|
|
|
case 'md5': return md5($password) === $hash;
|
|
|
|
case 'sha256': return hash('sha256', $password) === $hash;
|
|
|
|
default: return password_verify($password, $hash);
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 18:01:06 +08:00
|
|
|
|
|
|
|
public static function emailSuffixVerify($email, $suffixs)
|
|
|
|
{
|
2020-02-09 18:20:33 +08:00
|
|
|
$suffix = preg_split('/@/', $email)[1];
|
2020-02-09 18:01:06 +08:00
|
|
|
if (!$suffix) return false;
|
2020-02-09 18:20:33 +08:00
|
|
|
if (!is_array($suffixs)) {
|
|
|
|
$suffixs = preg_split('/,/', $suffixs);
|
|
|
|
}
|
2020-02-09 18:01:06 +08:00
|
|
|
if (!in_array($suffix, $suffixs)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-25 16:39:35 +08:00
|
|
|
|
|
|
|
public static function trafficConvert(int $byte)
|
|
|
|
{
|
|
|
|
$kb = 1024;
|
|
|
|
$mb = 1048576;
|
|
|
|
$gb = 1073741824;
|
|
|
|
if ($byte > $gb) {
|
|
|
|
return round($byte / $gb, 2) . ' GB';
|
|
|
|
} else if ($byte > $mb) {
|
|
|
|
return round($byte / $mb, 2) . ' MB';
|
|
|
|
} else if ($byte > $kb) {
|
|
|
|
return round($byte / $kb, 2) . ' KB';
|
|
|
|
} else if ($byte < 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return round($byte, 2) . ' B';
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|