Helpers.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\CouponLog;
  5. use App\Http\Models\Level;
  6. use App\Http\Models\NotificationLog;
  7. use App\Http\Models\SsConfig;
  8. use App\Http\Models\User;
  9. use App\Http\Models\UserSubscribe;
  10. use App\Http\Models\UserTrafficModifyLog;
  11. class Helpers
  12. {
  13. // 不生成的端口
  14. private static $denyPorts = [
  15. 1068, 1109, 1434, 3127, 3128,
  16. 3129, 3130, 3332, 4444, 5554,
  17. 6669, 8080, 8081, 8082, 8181,
  18. 8282, 9996, 17185, 24554, 35601,
  19. 60177, 60179
  20. ];
  21. // 加密方式
  22. public static function methodList()
  23. {
  24. return SsConfig::type(1)->get();
  25. }
  26. // 协议
  27. public static function protocolList()
  28. {
  29. return SsConfig::type(2)->get();
  30. }
  31. // 混淆
  32. public static function obfsList()
  33. {
  34. return SsConfig::type(3)->get();
  35. }
  36. // 等级
  37. public static function levelList()
  38. {
  39. return Level::query()->get()->sortBy('level');
  40. }
  41. // 生成用户的订阅码
  42. public static function makeSubscribeCode()
  43. {
  44. $code = makeRandStr(5);
  45. if(UserSubscribe::query()->where('code', $code)->exists()){
  46. $code = self::makeSubscribeCode();
  47. }
  48. return $code;
  49. }
  50. /**
  51. * 添加用户
  52. *
  53. * @param string $email 用户邮箱
  54. * @param string $password 用户密码
  55. * @param string $transfer_enable 可用流量
  56. * @param int $data 可使用天数
  57. * @param int $referral_uid 邀请人
  58. * @return int
  59. */
  60. public static function addUser($email, $password, $transfer_enable, $data, $referral_uid = 0)
  61. {
  62. $user = new User();
  63. $user->username = $email;
  64. $user->email = $email;
  65. $user->password = $password;
  66. // 生成一个可用端口
  67. $user->port = self::systemConfig()['is_rand_port']? Helpers::getRandPort() : Helpers::getOnlyPort();
  68. $user->passwd = makeRandStr();
  69. $user->vmess_id = createGuid();
  70. $user->enable = 1;
  71. $user->method = Helpers::getDefaultMethod();
  72. $user->protocol = Helpers::getDefaultProtocol();
  73. $user->protocol_param = '';
  74. $user->obfs = Helpers::getDefaultObfs();
  75. $user->obfs_param = '';
  76. $user->usage = 1;
  77. $user->transfer_enable = $transfer_enable;
  78. $user->enable_time = date('Y-m-d');
  79. $user->expire_time = date('Y-m-d', strtotime("+".$data." days"));
  80. $user->reg_ip = getClientIp();
  81. $user->referral_uid = $referral_uid;
  82. $user->reset_time = NULL;
  83. $user->status = 0;
  84. $user->save();
  85. return $user->id;
  86. }
  87. // 获取系统配置
  88. public static function systemConfig()
  89. {
  90. $config = Config::query()->get();
  91. $data = [];
  92. foreach($config as $vo){
  93. $data[$vo->name] = $vo->value;
  94. }
  95. return $data;
  96. }
  97. // 获取一个随机端口
  98. public static function getRandPort()
  99. {
  100. $config = self::systemConfig();
  101. $port = mt_rand($config['min_port'], $config['max_port']);
  102. $exists_port = User::query()->pluck('port')->toArray();
  103. if(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  104. $port = self::getRandPort();
  105. }
  106. return $port;
  107. }
  108. // 获取一个随机端口
  109. public static function getOnlyPort()
  110. {
  111. $config = self::systemConfig();
  112. $port = $config['min_port'];
  113. $exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
  114. while(in_array($port, $exists_port) || in_array($port, self::$denyPorts)){
  115. $port = $port+1;
  116. }
  117. return $port;
  118. }
  119. // 获取默认加密方式
  120. public static function getDefaultMethod()
  121. {
  122. $config = SsConfig::default()->type(1)->first();
  123. return $config? $config->name : 'aes-256-cfb';
  124. }
  125. // 获取默认协议
  126. public static function getDefaultProtocol()
  127. {
  128. $config = SsConfig::default()->type(2)->first();
  129. return $config? $config->name : 'origin';
  130. }
  131. // 获取默认混淆
  132. public static function getDefaultObfs()
  133. {
  134. $config = SsConfig::default()->type(3)->first();
  135. return $config? $config->name : 'plain';
  136. }
  137. /**
  138. * 添加通知推送日志
  139. *
  140. * @param string $title 标题
  141. * @param string $content 内容
  142. * @param int $type 发送类型
  143. * @param string $address 收信方
  144. * @param int $status 投递状态
  145. * @param string $error 投递失败时记录的异常信息
  146. *
  147. * @return int
  148. */
  149. public static function addNotificationLog($title, $content, $type, $address = 'admin', $status = 1, $error = '')
  150. {
  151. $log = new NotificationLog();
  152. $log->type = $type;
  153. $log->address = $address;
  154. $log->title = $title;
  155. $log->content = $content;
  156. $log->status = $status;
  157. $log->error = $error;
  158. $log->save();
  159. return $log->id;
  160. }
  161. /**
  162. * 添加优惠券操作日志
  163. *
  164. * @param int $couponId 优惠券ID
  165. * @param int $goodsId 商品ID
  166. * @param int $orderId 订单ID
  167. * @param string $desc 备注
  168. *
  169. * @return int
  170. */
  171. public static function addCouponLog($couponId, $goodsId, $orderId, $desc = '')
  172. {
  173. $log = new CouponLog();
  174. $log->coupon_id = $couponId;
  175. $log->goods_id = $goodsId;
  176. $log->order_id = $orderId;
  177. $log->desc = $desc;
  178. return $log->save();
  179. }
  180. /**
  181. * 记录流量变动日志
  182. *
  183. * @param int $userId 用户ID
  184. * @param string $oid 订单ID
  185. * @param int $before 记录前的值
  186. * @param int $after 记录后的值
  187. * @param string $desc 描述
  188. *
  189. * @return int
  190. */
  191. public static function addUserTrafficModifyLog($userId, $oid, $before, $after, $desc = '')
  192. {
  193. $log = new UserTrafficModifyLog();
  194. $log->user_id = $userId;
  195. $log->order_id = $oid;
  196. $log->before = $before;
  197. $log->after = $after;
  198. $log->desc = $desc;
  199. return $log->save();
  200. }
  201. }