Helper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $server->name = rawurlencode($server->name);
  55. $uri = "trojan://{$user->uuid}@{$server->host}:{$server->port}#{$server->name}";
  56. $uri .= "\r\n";
  57. return $uri;
  58. }
  59. public static function buildVmessLink(Server $server, User $user)
  60. {
  61. $config = [
  62. "v" => "2",
  63. "ps" => $server->name,
  64. "add" => $server->host,
  65. "port" => $server->port,
  66. "id" => $user->uuid,
  67. "aid" => "2",
  68. "net" => $server->network,
  69. "type" => "none",
  70. "host" => "",
  71. "path" => "",
  72. "tls" => $server->tls ? "tls" : ""
  73. ];
  74. if ((string)$server->network === 'ws') {
  75. $wsSettings = json_decode($server->networkSettings);
  76. if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
  77. if (isset($wsSettings->headers->Host)) $config['host'] = $wsSettings->headers->Host;
  78. }
  79. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  80. }
  81. public static function multiPasswordVerify($algo, $password, $hash)
  82. {
  83. switch($algo) {
  84. case 'md5': return md5($password) === $hash;
  85. case 'sha256': return hash('sha256', $password) === $hash;
  86. default: return password_verify($password, $hash);
  87. }
  88. }
  89. public static function emailSuffixVerify($email, $suffixs)
  90. {
  91. $suffix = preg_split('/@/', $email)[1];
  92. if (!$suffix) return false;
  93. if (!is_array($suffixs)) {
  94. $suffixs = preg_split('/,/', $suffixs);
  95. }
  96. if (!in_array($suffix, $suffixs)) return false;
  97. return true;
  98. }
  99. public static function trafficConvert(int $byte)
  100. {
  101. $kb = 1024;
  102. $mb = 1048576;
  103. $gb = 1073741824;
  104. if ($byte > $gb) {
  105. return round($byte / $gb, 2) . ' GB';
  106. } else if ($byte > $mb) {
  107. return round($byte / $mb, 2) . ' MB';
  108. } else if ($byte > $kb) {
  109. return round($byte / $kb, 2) . ' KB';
  110. } else if ($byte < 0) {
  111. return 0;
  112. } else {
  113. return round($byte, 2) . ' B';
  114. }
  115. }
  116. }