Helper.php 2.5 KB

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