Helper.php 3.8 KB

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