Skip to content

Commit a2e7e18

Browse files
authored
Merge pull request #146 from 4x99/develop
Develop
2 parents a459450 + 8d0de01 commit a2e7e18

19 files changed

Lines changed: 376 additions & 85 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
- 扫描结果信息丰富,支持批量操作
2323
- 支持代理配置,扫描 GitHub 更通畅
2424
- 支持白名单模式,可按仓库和文件名过滤
25-
- 支持邮件、钉钉、WebHook、Telegram、企业微信通知
25+
- 支持邮件、钉钉、飞书、WebHook、Telegram、企业微信通知
2626

2727
---
2828

app/Console/Commands/JobRunCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function handle()
8080
$this->log->info('Start running scan job');
8181

8282
$this->createGitHubService();
83-
$this->whitelist = ConfigWhitelist::all()->keyBy('value');
83+
$this->whitelist = ConfigWhitelist::pluck('value');
8484
$this->whitelistFile = json_decode(ConfigCommon::getValue(ConfigCommon::KEY_WHITELIST_FILE));
8585
$this->log->info('Get whitelist success');
8686

@@ -194,8 +194,10 @@ private function storeLeak($item, $storeType)
194194
$repoName = $item['repository']['name'];
195195

196196
// 扫描白名单
197-
if ($this->whitelist->has("$repoOwner/$repoName")) {
198-
return false;
197+
foreach ($this->whitelist as $pattern) {
198+
if (fnmatch($pattern, "$repoOwner/$repoName")) {
199+
return false;
200+
}
199201
}
200202

201203
// 按文件名忽略

app/Console/Commands/NotifyCommand.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct()
4949
* @uses NotifyService::email()
5050
* @uses NotifyService::webhook()
5151
* @uses NotifyService::telegram()
52+
* @uses NotifyService::feishu()
5253
* @uses NotifyService::dingTalk()
5354
* @uses NotifyService::workWechat()
5455
*/
@@ -81,10 +82,9 @@ public function handle()
8182
continue;
8283
}
8384

84-
$content = $this->getContent($data);
85-
$content = implode($type === ConfigNotify::TYPE_EMAIL ? '<br/><br/>' : "\n\n", $content);
86-
$config = $config = json_decode($config->value, true);
87-
$result = $service->$type($content, $config);
85+
$config = json_decode($config->value, true);
86+
$tpl = $service->getTemplate($type, $data['stime'], $data['etime'], $data['count']);
87+
$result = $service->$type($tpl['title'], $tpl['content'], $config);
8888
$this->log->info('Send complete', array_merge(['type' => $type], $result));
8989
}
9090

@@ -106,18 +106,4 @@ private function count($timestamp, $interval)
106106
$data['count'] = $query->count();
107107
return $data;
108108
}
109-
110-
/**
111-
* @param $data
112-
* @return array
113-
*/
114-
private function getContent($data)
115-
{
116-
$content = [];
117-
$content[] = "码小六消息通知";
118-
$content[] = "开始时间:{$data['stime']}";
119-
$content[] = "结束时间:{$data['etime']}";
120-
$content[] = "本时段共有 {$data['count']} 条未审记录";
121-
return $content;
122-
}
123109
}

app/Http/Controllers/ConfigNotifyController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\ConfigNotify;
66
use App\Services\NotifyService;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Arr;
89
use Illuminate\Validation\Rule;
910

1011
class ConfigNotifyController extends Controller
@@ -51,8 +52,14 @@ public function test(Request $request)
5152
{
5253
$config = $request->all();
5354
$type = $config['type'];
55+
56+
$count = 'xxx';
57+
$etime = now()->toDateTimeString();
58+
$stiem = now()->subHour()->toDateTimeString();
59+
5460
$service = new NotifyService();
55-
return $service->$type('码小六消息通知测试', $config);
61+
$tpl = $service->getTemplate($type, $stiem, $etime, $count);
62+
return $service->$type($tpl['title'], $tpl['content'], $config);
5663
}
5764

5865
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\ConfigCommon;
6+
use App\Services\NotifyService;
7+
use Exception;
8+
use Illuminate\Http\Request;
9+
10+
class ConfigNotifyTemplateController extends Controller
11+
{
12+
/**
13+
* 通知模板
14+
*
15+
* @return array
16+
*/
17+
public function index()
18+
{
19+
try {
20+
$config = ConfigCommon::getValue(ConfigCommon::KEY_NOTIFY_TEMPLATE);
21+
$config = json_decode($config, true);
22+
$data['title'] = $config['title'] ?? NotifyService::TEMPLATE_DEFAULT_TITLE;
23+
$data['content'] = $config['content'] ?? NotifyService::TEMPLATE_DEFAULT_CONTENT;
24+
return ['success' => true, 'data' => $data];
25+
} catch (Exception $e) {
26+
return ['success' => false, 'message' => $e->getMessage()];
27+
}
28+
}
29+
30+
/**
31+
* 更新通知模板
32+
*
33+
* @param Request $request
34+
* @return array
35+
*/
36+
public function store(Request $request)
37+
{
38+
try {
39+
$title = $request->input('title');
40+
$content = $request->input('content');
41+
$value = json_encode(compact('title', 'content'));
42+
ConfigCommon::updateOrCreate(['key' => ConfigCommon::KEY_NOTIFY_TEMPLATE], ['value' => $value]);
43+
return ['success' => true];
44+
} catch (Exception $e) {
45+
return ['success' => false, 'message' => $e->getMessage()];
46+
}
47+
}
48+
}

app/Http/Controllers/ConfigProxyController.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\ConfigCommon;
6+
use App\Services\GitHubService;
67
use Illuminate\Http\Request;
7-
use Github\Client;
8-
use Github\HttpClient\Builder;
9-
use Http\Adapter\Guzzle6\Client as GuzzleClient;
108

119
class ConfigProxyController extends Controller
1210
{
@@ -48,16 +46,11 @@ public function store(Request $request)
4846
*/
4947
public function test(Request $request)
5048
{
51-
try {
52-
$builder = new Builder(GuzzleClient::createWithConfig([
53-
'timeout' => 5,
54-
'proxy' => $request->input('value'),
55-
]));
56-
$client = new Client($builder, 'v3.text-match');
57-
$client->api('repo')->releases()->latest('4x99', 'code6');
49+
$service = new GitHubService();
50+
if ($service->testProxy($request->input('value'))) {
5851
return ['success' => true];
59-
} catch (\Exception $e) {
60-
return ['success' => false, 'message' => $e->getMessage()];
52+
} else {
53+
return ['success' => false];
6154
}
6255
}
6356
}

app/Models/ConfigCommon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ConfigCommon extends ModelBase
66
{
77
const KEY_PROXY = 'proxy';
88
const KEY_WHITELIST_FILE = 'whitelist_file';
9+
const KEY_NOTIFY_TEMPLATE = 'notify_template';
910
const CREATED_AT = null;
1011
const UPDATED_AT = null;
1112
protected $table = 'config_common';

app/Models/ConfigNotify.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace App\Models;
44

5-
use Illuminate\Database\Eloquent\Model;
6-
7-
class ConfigNotify extends Model
5+
class ConfigNotify extends ModelBase
86
{
97
const TYPE = [
108
self::TYPE_EMAIL,
119
self::TYPE_DING_TALK,
1210
self::TYPE_WORK_WECHAT,
1311
self::TYPE_TELEGRAM,
12+
self::TYPE_FEISHU,
1413
self::TYPE_WEBHOOK,
1514
];
1615
const TYPE_EMAIL = 'email';
1716
const TYPE_DING_TALK = 'dingTalk';
1817
const TYPE_WORK_WECHAT = 'workWechat';
1918
const TYPE_TELEGRAM = 'telegram';
19+
const TYPE_FEISHU = 'feishu';
2020
const TYPE_WEBHOOK = 'webhook';
2121

2222
protected $table = 'config_notify';

app/Models/ModelBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
namespace App\Models;
44

55
use DateTimeInterface;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Database\Eloquent\Model;
78

9+
/**
10+
* @mixin Builder
11+
*/
812
class ModelBase extends Model
913
{
1014
protected function serializeDate(DateTimeInterface $date)

app/Services/GitHubService.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class GitHubService
2222
{
2323
const HTTP_TIMEOUT = 30;
24+
const HTTP_TIMEOUT_TEST = 5; // 代理测试超时时间
2425
const HTTP_DELAY = 2000;
2526
const HTTP_MAX_RETRIES = 5;
2627
const GET_CLIENT_TIMEOUT = 1800;
@@ -43,6 +44,7 @@ public function __construct()
4344
*/
4445
public function init()
4546
{
47+
$this->proxy = $this->testProxy($this->proxy) ? $this->proxy : null;
4648
$tokens = ConfigToken::inRandomOrder()->get()->pluck('token');
4749
foreach ($tokens as $token) {
4850
$client = ['token' => $token];
@@ -143,6 +145,28 @@ public function updateClient(&$client)
143145
return $client['status'] == ConfigToken::STATUS_NORMAL;
144146
}
145147

148+
/**
149+
* 代理测试
150+
*
151+
* @param $proxy
152+
* @return bool
153+
*/
154+
public function testProxy($proxy)
155+
{
156+
try {
157+
$builder = new Builder(GuzzleClient::createWithConfig([
158+
'proxy' => $proxy,
159+
'timeout' => self::HTTP_TIMEOUT_TEST,
160+
]));
161+
$client = new Client($builder, 'v3.text-match');
162+
$client->api('repo')->releases()->latest('4x99', 'code6');
163+
return true;
164+
} catch (Exception $e) {
165+
Log::error("Proxy $proxy not available", [$e->getMessage()]);
166+
return false;
167+
}
168+
}
169+
146170
/**
147171
* 更新数据库
148172
*

0 commit comments

Comments
 (0)