Helper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\Server;
  4. use App\Models\ServerTrojan;
  5. use App\Models\User;
  6. class Helper
  7. {
  8. public static function guid($format = false)
  9. {
  10. if (function_exists('com_create_guid') === true) {
  11. return md5(trim(com_create_guid(), '{}'));
  12. }
  13. $data = openssl_random_pseudo_bytes(16);
  14. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  15. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  16. if ($format) {
  17. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  18. }
  19. return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . '-' . time());
  20. }
  21. public static function exchange($from, $to)
  22. {
  23. $result = file_get_contents('https://api.exchangeratesapi.io/latest?symbols=' . $to . '&base=' . $from);
  24. $result = json_decode($result, true);
  25. return $result['rates'][$to];
  26. }
  27. public static function randomChar($len, $special = false)
  28. {
  29. $chars = array(
  30. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  31. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  32. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  33. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  34. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  35. "3", "4", "5", "6", "7", "8", "9"
  36. );
  37. if ($special) {
  38. $chars = array_merge($chars, array(
  39. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  40. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  41. "}", "<", ">", "~", "+", "=", ",", "."
  42. ));
  43. }
  44. $charsLen = count($chars) - 1;
  45. shuffle($chars);
  46. $str = '';
  47. for ($i = 0; $i < $len; $i++) {
  48. $str .= $chars[mt_rand(0, $charsLen)];
  49. }
  50. return $str;
  51. }
  52. public static function buildTrojanLink(ServerTrojan $server, User $user)
  53. {
  54. $uri = "trojan://{$user->uuid}@{$server->host}:{$server->port}";
  55. $uri .= "\r\n";
  56. return $uri;
  57. }
  58. public static function buildVmessLink(Server $server, User $user)
  59. {
  60. $config = [
  61. "v" => "2",
  62. "ps" => $server->name,
  63. "add" => $server->host,
  64. "port" => $server->port,
  65. "id" => $user->uuid,
  66. "aid" => "2",
  67. "net" => $server->network,
  68. "type" => "none",
  69. "host" => "",
  70. "path" => "",
  71. "tls" => $server->tls ? "tls" : ""
  72. ];
  73. if ((string)$server->network === 'ws') {
  74. $wsSettings = json_decode($server->networkSettings);
  75. if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
  76. if (isset($wsSettings->headers->Host)) $config['host'] = $wsSettings->headers->Host;
  77. }
  78. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  79. }
  80. public static function multiPasswordVerify($algo, $password, $hash)
  81. {
  82. switch($algo) {
  83. case 'md5': return md5($password) === $hash;
  84. case 'sha256': return hash('sha256', $password) === $hash;
  85. default: return password_verify($password, $hash);
  86. }
  87. }
  88. public static function emailSuffixVerify($email, $suffixs)
  89. {
  90. $suffix = preg_split('/@/', $email)[1];
  91. if (!$suffix) return false;
  92. if (!is_array($suffixs)) {
  93. $suffixs = preg_split('/,/', $suffixs);
  94. }
  95. if (!in_array($suffix, $suffixs)) return false;
  96. return true;
  97. }
  98. public static function trafficConvert(int $byte)
  99. {
  100. $kb = 1024;
  101. $mb = 1048576;
  102. $gb = 1073741824;
  103. if ($byte > $gb) {
  104. return round($byte / $gb, 2) . ' GB';
  105. } else if ($byte > $mb) {
  106. return round($byte / $mb, 2) . ' MB';
  107. } else if ($byte > $kb) {
  108. return round($byte / $kb, 2) . ' KB';
  109. } else if ($byte < 0) {
  110. return 0;
  111. } else {
  112. return round($byte, 2) . ' B';
  113. }
  114. }
  115. }