Skip to content

Commit

Permalink
添加节点地图 + 修复系统设置页面元素错误+代码简写规范化
Browse files Browse the repository at this point in the history
  • Loading branch information
ZBrettonYe committed Jul 27, 2020
1 parent 267e302 commit 54ee807
Show file tree
Hide file tree
Showing 88 changed files with 1,030 additions and 429 deletions.
13 changes: 4 additions & 9 deletions app/Components/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,20 @@ public static function addUser($email, $password, $transfer_enable, $data, $refe

// 获取系统配置
public static function systemConfig(): array {
$config = Config::query()->get();
$data = [];
foreach($config as $vo){
foreach(Config::all() as $vo){
$data[$vo->name] = $vo->value;
}

$data['is_onlinePay'] = ($data['is_AliPay'] || $data['is_QQPay'] || $data['is_WeChatPay'] || $data['is_otherPay'])?: 0;

return $data;
}

// 获取一个随机端口
public static function getRandPort() {
$config = self::systemConfig();
$port = random_int($config['min_port'], $config['max_port']);
$port = random_int(self::systemConfig()['min_port'], self::systemConfig()['max_port']);

$exists_port = User::query()->pluck('port')->toArray();
if(in_array($port, $exists_port, true) || in_array($port, self::$denyPorts)){
if(in_array($port, $exists_port, true) || in_array($port, self::$denyPorts, true)){
$port = self::getRandPort();
}

Expand All @@ -128,8 +124,7 @@ public static function getRandPort() {

// 获取一个随机端口
public static function getOnlyPort() {
$config = self::systemConfig();
$port = $config['min_port'];
$port = (int) self::systemConfig()['min_port'];

$exists_port = User::query()->where('port', '>=', $port)->pluck('port')->toArray();
while(in_array($port, $exists_port, true) || in_array($port, self::$denyPorts, true)){
Expand Down
4 changes: 1 addition & 3 deletions app/Components/IPIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class IPIP {
*/
public static function ip($ip): ?array {
$filePath = database_path('ipip.ipdb');
$loc = new City($filePath);

return $loc->findMap($ip, 'CN');
return (new City($filePath))->findMap($ip, 'CN');
}
}
3 changes: 1 addition & 2 deletions app/Components/Namesilo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ private function send($operation, $data = []) {

$content = '请求操作:['.$operation.'] --- 请求数据:['.http_build_query($query).']';

$client = new Client(['timeout' => 10]);
$request = $client->get(self::$host.$operation.'?'.http_build_query($query));
$request = (new Client(['timeout' => 10]))->get(self::$host.$operation.'?'.http_build_query($query));
$result = XML2Array::createArray(json_decode($request->getBody(), true));

if($request->getStatusCode() != 200){
Expand Down
8 changes: 3 additions & 5 deletions app/Components/NetworkDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class NetworkDetection {
public static function networkCheck($ip, $type, $port = null) {
$url = 'https://api.50network.com/china-firewall/check/ip/'.($type? 'icmp/' : ($port? 'tcp_port/' : 'tcp_ack/')).$ip.($port? '/'.$port : '');
$checkName = $type? 'ICMP' : 'TCP';
$client = new Client(['timeout' => 10]);
$request = $client->get($url);
$request = (new Client(['timeout' => 10]))->get($url);
$result = json_decode($request->getBody(), true);

if($request->getStatusCode() == 200){
Expand Down Expand Up @@ -66,9 +65,8 @@ public static function networkCheck($ip, $type, $port = null) {
*/
public static function ping($ip) {
$url = 'https://api.oioweb.cn/api/hostping.php?host='.$ip;//https://api.iiwl.cc/api/ping.php?host=
$client = new Client(['timeout' => 20]);
$request = $client->get($url);
$message = json_decode($client->get($url)->getBody(), true);
$request = (new Client(['timeout' => 20]))->get($url);
$message = json_decode($request->getBody(), true);

// 发送成功
if($request->getStatusCode() == 200){
Expand Down
6 changes: 2 additions & 4 deletions app/Components/PushNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static function send($title, $content) {
*/
private static function ServerChan($title, $content) {
// TODO:一天仅可发送不超过500条
$client = new Client(['timeout' => 5]);
$request = $client->get('https://sc.ftqq.com/'.Helpers::systemConfig()['server_chan_key'].'.send?text='.$title.'&desp='.urlencode($content));
$request = (new Client(['timeout' => 5]))->get('https://sc.ftqq.com/'.Helpers::systemConfig()['server_chan_key'].'.send?text='.$title.'&desp='.urlencode($content));
$message = json_decode($request->getBody(), true);
Log::debug($message);
// 发送成功
Expand All @@ -58,8 +57,7 @@ private static function ServerChan($title, $content) {
* @return mixed
*/
private static function Bark($title, $content) {
$client = new Client(['timeout' => 5]);
$request = $client->get('https://api.day.app/'.Helpers::systemConfig()['bark_key'].'/'.$title.'/'.$content);
$request = (new Client(['timeout' => 5]))->get('https://api.day.app/'.Helpers::systemConfig()['bark_key'].'/'.$title.'/'.$content);
$message = json_decode($request->getBody(), true);

if($request->getStatusCode() == 200){
Expand Down
23 changes: 9 additions & 14 deletions app/Components/QQInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ class QQInfo {
public static function getName(string $qq): string {
//向接口发起请求获取json数据
$url = 'https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?get_nick=1&uins='.$qq;
$client = new Client(['timeout' => 10]);
$request = $client->get($url);
$request = (new Client(['timeout' => 10]))->get($url);
$message = mb_convert_encoding($request->getBody(), "UTF-8", "GBK");

// 接口是否异常
if($request->getStatusCode() == 200){
if(strpos($message, $qq) !== false){
//对获取的json数据进行截取并解析成数组
$message = json_decode(substr($message, 17, -1), true);
if($request->getStatusCode() == 200 && str_contains($message, $qq)){
//对获取的json数据进行截取并解析成数组
$message = json_decode(substr($message, 17, -1), true);

return stripslashes($message[$qq][6]);
}
return stripslashes($message[$qq][6]);
}

return $qq;
Expand All @@ -28,12 +25,11 @@ public static function getName(string $qq): string {
public static function getName2(string $qq): string {
//向接口发起请求获取json数据
$url = 'https://api.toubiec.cn/qq?qq='.$qq.'&size=100';
$client = new Client(['timeout' => 10]);
$request = $client->get($url);
$request = (new Client(['timeout' => 10]))->get($url);
$message = json_decode($request->getBody(), true);

// 接口是否异常
if($request->getStatusCode() == 200 && $message && $message['code'] == 200){
if($message && $message['code'] == 200 && $request->getStatusCode() == 200){
return $message['name'];
}

Expand All @@ -43,12 +39,11 @@ public static function getName2(string $qq): string {
public static function getName3(string $qq): string {
//向接口发起请求获取json数据
$url = 'https://api.unipay.qq.com/v1/r/1450000186/wechat_query?cmd=1&pf=mds_storeopen_qb-__mds_qqclub_tab_-html5&pfkey=pfkey&from_h5=1&from_https=1&openid=openid&openkey=openkey&session_id=hy_gameid&session_type=st_dummy&qq_appid=&offerId=1450000186&sandbox=&provide_uin='.$qq;
$client = new Client(['timeout' => 10]);
$request = $client->get($url);
$request = (new Client(['timeout' => 10]))->get($url);
$message = json_decode($request->getBody(), true);

// 接口是否异常
if($request->getStatusCode() == 200 && $message && $message['ret'] == 0){
if($message && $message['ret'] == 0 && $request->getStatusCode() == 200){
return urldecode($message['nick']);
}

Expand Down
6 changes: 2 additions & 4 deletions app/Console/Commands/AutoJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ private function expireCode(): void {
// 封禁访问异常的订阅链接
private function blockSubscribe(): void {
if(self::$systemConfig['is_subscribe_ban']){
$userList = User::query()->where('status', '>=', 0)->whereEnable(1)->get();
foreach($userList as $user){
foreach(User::query()->activeUser()->get() as $user){
$subscribe = UserSubscribe::query()->whereUserId($user->id)->first();
if($subscribe){
// 24小时内不同IP的请求次数
Expand Down Expand Up @@ -318,8 +317,7 @@ private function dispatchPort(): void {
// 检测节点是否离线
private function checkNodeStatus(): void {
if(self::$systemConfig['is_node_offline']){
$nodeList = SsNode::whereIsRelay(0)->whereStatus(1)->get();
foreach($nodeList as $node){
foreach(SsNode::whereIsRelay(0)->whereStatus(1)->get() as $node){
// 10分钟内无节点负载信息则认为是后端炸了
$nodeTTL = SsNodeInfo::query()
->whereNodeId($node->id)
Expand Down
11 changes: 5 additions & 6 deletions app/Console/Commands/AutoPingNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class AutoPingNode extends Command {
public function handle(): void {
$jobStartTime = microtime(true);

$nodeList = SsNode::query()->whereIsRelay(0)->whereStatus(1)->get();
foreach($nodeList as $node){
foreach(SsNode::query()->whereIsRelay(0)->whereStatus(1)->get() as $node){
$this->pingNode($node->id, $node->is_ddns? $node->server : $node->ip);
}

Expand All @@ -33,10 +32,10 @@ private function pingNode($nodeId, $ip): void {
if($result){
$obj = new SsNodePing();
$obj->node_id = $nodeId;
$obj->ct = intval($result['telecom']['time']);//电信
$obj->cu = intval($result['Unicom']['time']);// 联通
$obj->cm = intval($result['move']['time']);// 移动
$obj->hk = intval($result['HongKong']['time']);// 香港
$obj->ct = (int) $result['telecom']['time'];//电信
$obj->cu = (int) $result['Unicom']['time'];// 联通
$obj->cm = (int) $result['move']['time'];// 移动
$obj->hk = (int) $result['HongKong']['time'];// 香港
$obj->save();
}else{
Log::info("".$ip."】Ping测速获取失败");
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/AutoStatisticsNodeDailyTraffic.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class AutoStatisticsNodeDailyTraffic extends Command {
public function handle(): void {
$jobStartTime = microtime(true);

$nodeList = SsNode::query()->whereStatus(1)->orderBy('id')->get();
foreach($nodeList as $node){
foreach(SsNode::query()->whereStatus(1)->orderBy('id')->get() as $node){
$this->statisticsByNode($node->id);
}

Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/AutoStatisticsNodeHourlyTraffic.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class AutoStatisticsNodeHourlyTraffic extends Command {
public function handle(): void {
$jobStartTime = microtime(true);

$nodeList = SsNode::query()->whereStatus(1)->orderBy('id')->get();
foreach($nodeList as $node){
foreach(SsNode::query()->whereStatus(1)->orderBy('id')->get() as $node){
$this->statisticsByNode($node->id);
}

Expand Down
6 changes: 2 additions & 4 deletions app/Console/Commands/AutoStatisticsUserDailyTraffic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ class AutoStatisticsUserDailyTraffic extends Command {
public function handle(): void {
$jobStartTime = microtime(true);

$userList = User::query()->where('status', '>=', 0)->whereEnable(1)->get();
foreach($userList as $user){
foreach(User::query()->activeUser()->get() as $user){
// 统计一次所有节点的总和
$this->statisticsByNode($user->id);

// 统计每个节点产生的流量
$nodeList = SsNode::query()->whereStatus(1)->orderBy('id')->get();
foreach($nodeList as $node){
foreach(SsNode::query()->whereStatus(1)->orderBy('id')->get() as $node){
$this->statisticsByNode($user->id, $node->id);
}
}
Expand Down
6 changes: 2 additions & 4 deletions app/Console/Commands/AutoStatisticsUserHourlyTraffic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ class AutoStatisticsUserHourlyTraffic extends Command {
public function handle(): void {
$jobStartTime = microtime(true);

$userList = User::query()->where('status', '>=', 0)->whereEnable(1)->get();
foreach($userList as $user){
foreach(User::query()->activeUser()->get() as $user){
// 统计一次所有节点的总和
$this->statisticsByNode($user->id);

// 统计每个节点产生的流量
$nodeList = SsNode::query()->whereStatus(1)->orderBy('id')->get();
foreach($nodeList as $node){
foreach(SsNode::query()->whereStatus(1)->orderBy('id')->get() as $node){
$this->statisticsByNode($user->id, $node->id);
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/UserExpireAutoWarning.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function handle(): void {

private function userExpireWarning(): void {
// 只取SSR没被禁用的用户,其他不用管
$userList = User::query()->whereEnable(1)->get();
foreach($userList as $user){
foreach(User::query()->whereEnable(1)->get() as $user){
// 用户名不是邮箱的跳过
if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
continue;
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/UserTrafficAutoWarning.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function handle(): void {

// 用户流量超过警告阈值自动发邮件提醒
private function userTrafficWarning(): void {
$userList = User::query()->where('status', '>=', 0)->whereEnable(1)->where('transfer_enable', '>', 0)->get();
foreach($userList as $user){
foreach(User::query()->activeUser()->where('transfer_enable', '>', 0)->get() as $user){
// 用户名不是邮箱的跳过
if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
continue;
Expand Down
9 changes: 3 additions & 6 deletions app/Console/Commands/updateTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ class updateTicket extends Command {
public function handle(): void {
Log::info('----------------------------【更新工单】开始----------------------------');
// 获取管理员
$adminList = User::query()->whereIsAdmin(1)->get();
foreach($adminList as $admin){
foreach(User::query()->whereIsAdmin(1)->get() as $admin){
Log::info('----------------------------【更新管理员'.$admin->id.'回复工单】开始----------------------------');
// 获取该管理回复过的工单
$replyList = TicketReply::query()->whereUserId($admin->id)->get();
// 更新工单
foreach($replyList as $reply){
// 获取该管理回复过的工单, 更新工单
foreach(TicketReply::query()->whereUserId($admin->id)->get() as $reply){
$ret = TicketReply::query()->whereId($reply->id)->update(['user_id' => 0, 'admin_id' => $admin->id]);
if($ret){
Log::info('--- 管理员:'.$admin->email.'回复子单ID:'.$reply->id.' ---');
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/updateUserLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class updateUserLevel extends Command {
public function handle(): void {
Log::info('----------------------------【用户等级升级】开始----------------------------');
// 预设level 0
$users = User::query()->where('level', '<>', 0)->get();
foreach($users as $user){
foreach(User::query()->where('level', '<>', 0)->get() as $user){
User::query()->whereId($user->id)->update(['level' => 0]);
}
// 获取商品列表,取新等级
Expand Down
10 changes: 5 additions & 5 deletions app/Console/Commands/updateUserName.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class updateUserName extends Command {
public function handle(): void {
Log::info('----------------------------【升级用户昵称】开始----------------------------');

$userList = User::query()->get();
$userList = User::all();
foreach($userList as $user){
$name = process($user->id);
User::query()->whereId($user->id)->update(['username' => $name]);
Expand Down Expand Up @@ -44,17 +44,17 @@ function process($id) {
if($user->qq){
$name = QQInfo::getName3($user->qq);
// 检测用户注册是否为QQ邮箱
}elseif(stripos($user->email, '@qq') != false){
}elseif(stripos($user->email, '@qq') !== false){
// 分离QQ邮箱后缀
$email = explode('@', $user->email);
$email = explode('@', $user->email, 2);
if(is_numeric($email[0])){
$name = QQInfo::getName3($email[0]);
}elseif(strpos($email[0], '.') !== false){
}elseif(str_contains($email[0], '.')){
$temp = explode('.', $email[0]);
if(is_numeric($temp[1])){
$name = QQInfo::getName3($temp[1]);
}else{
print_r($user->email.PHP_EOL);
echo $user->email.PHP_EOL;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/Console/Commands/upgradeUserResetTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class upgradeUserResetTime extends Command {
public function handle(): void {
Log::info('----------------------------【升级用户重置日期】开始----------------------------');

$userList = User::query()->get();
foreach($userList as $user){
foreach(User::all() as $user){
$reset_time = null;
if($user->traffic_reset_day){
$today = date('d');// 今天 日期
Expand Down
Loading

0 comments on commit 54ee807

Please sign in to comment.