Helper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Utils;
  3. class Helper
  4. {
  5. public static function guid($format = false)
  6. {
  7. if (function_exists('com_create_guid') === true) {
  8. return md5(trim(com_create_guid(), '{}'));
  9. }
  10. $data = openssl_random_pseudo_bytes(16);
  11. $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
  12. $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
  13. if ($format) {
  14. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  15. }
  16. return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . '-' . time());
  17. }
  18. public static function exchange($from, $to)
  19. {
  20. $result = file_get_contents('https://api.exchangeratesapi.io/latest?symbols=' . $to . '&base=' . $from);
  21. $result = json_decode($result, true);
  22. return $result['rates'][$to];
  23. }
  24. public static function randomChar($len, $special = false)
  25. {
  26. $chars = array(
  27. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  28. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  29. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  30. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  31. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  32. "3", "4", "5", "6", "7", "8", "9"
  33. );
  34. if ($special) {
  35. $chars = array_merge($chars, array(
  36. "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
  37. "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
  38. "}", "<", ">", "~", "+", "=", ",", "."
  39. ));
  40. }
  41. $charsLen = count($chars) - 1;
  42. shuffle($chars);
  43. $str = '';
  44. for ($i = 0; $i < $len; $i++) {
  45. $str .= $chars[mt_rand(0, $charsLen)];
  46. }
  47. return $str;
  48. }
  49. public static function buildVmessLink($item, $user)
  50. {
  51. $config = [
  52. "v" => "2",
  53. "ps" => $item->name,
  54. "add" => $item->host,
  55. "port" => $item->port,
  56. "id" => $user->v2ray_uuid,
  57. "aid" => "2",
  58. "net" => $item->network,
  59. "type" => "none",
  60. "host" => "",
  61. "path" => "",
  62. "tls" => $item->tls ? "tls" : ""
  63. ];
  64. if ($item->network == 'ws') {
  65. $wsSettings = json_decode($item->settings);
  66. if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
  67. if (isset($wsSettings->headers->Host)) $config['host'] = $wsSettings->headers->Host;
  68. }
  69. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  70. }
  71. }