2020-10-04 14:21:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Server;
|
|
|
|
|
|
|
|
use App\Models\ServerShadowsocks;
|
|
|
|
use App\Services\ServerService;
|
2023-04-15 19:34:54 +08:00
|
|
|
use App\Services\StatisticalService;
|
2020-10-04 14:21:09 +08:00
|
|
|
use App\Services\UserService;
|
|
|
|
use App\Utils\CacheKey;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tidal Lab Shadowsocks
|
|
|
|
* Github: https://github.com/tokumeikoi/tidalab-ss
|
|
|
|
*/
|
|
|
|
class ShadowsocksTidalabController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct(Request $request)
|
|
|
|
{
|
|
|
|
$token = $request->input('token');
|
|
|
|
if (empty($token)) {
|
|
|
|
abort(500, 'token is null');
|
|
|
|
}
|
|
|
|
if ($token !== config('v2board.server_token')) {
|
|
|
|
abort(500, 'token is error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 后端获取用户
|
|
|
|
public function user(Request $request)
|
|
|
|
{
|
2022-03-06 23:07:55 +08:00
|
|
|
ini_set('memory_limit', -1);
|
2020-10-04 14:21:09 +08:00
|
|
|
$nodeId = $request->input('node_id');
|
|
|
|
$server = ServerShadowsocks::find($nodeId);
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
|
|
|
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_CHECK_AT', $server->id), time(), 3600);
|
|
|
|
$serverService = new ServerService();
|
2021-08-06 00:43:01 +08:00
|
|
|
$users = $serverService->getAvailableUsers($server->group_id);
|
2020-10-04 14:21:09 +08:00
|
|
|
$result = [];
|
|
|
|
foreach ($users as $user) {
|
|
|
|
array_push($result, [
|
|
|
|
'id' => $user->id,
|
2020-10-04 16:26:28 +08:00
|
|
|
'port' => $server->server_port,
|
2020-10-04 14:21:09 +08:00
|
|
|
'cipher' => $server->cipher,
|
|
|
|
'secret' => $user->uuid
|
|
|
|
]);
|
|
|
|
}
|
2022-03-15 21:45:15 +08:00
|
|
|
$eTag = sha1(json_encode($result));
|
2022-03-15 22:40:30 +08:00
|
|
|
if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
|
2022-03-15 21:45:15 +08:00
|
|
|
abort(304);
|
|
|
|
}
|
2020-10-04 14:21:09 +08:00
|
|
|
return response([
|
|
|
|
'data' => $result
|
2022-03-15 22:40:30 +08:00
|
|
|
])->header('ETag', "\"{$eTag}\"");
|
2020-10-04 14:21:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 后端提交数据
|
|
|
|
public function submit(Request $request)
|
|
|
|
{
|
2020-10-04 14:52:33 +08:00
|
|
|
// Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input'));
|
2020-10-04 14:21:09 +08:00
|
|
|
$server = ServerShadowsocks::find($request->input('node_id'));
|
|
|
|
if (!$server) {
|
|
|
|
return response([
|
|
|
|
'ret' => 0,
|
|
|
|
'msg' => 'server is not found'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
$data = file_get_contents('php://input');
|
|
|
|
$data = json_decode($data, true);
|
|
|
|
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_ONLINE_USER', $server->id), count($data), 3600);
|
2021-03-24 19:57:06 +08:00
|
|
|
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_LAST_PUSH_AT', $server->id), time(), 3600);
|
2020-10-04 14:21:09 +08:00
|
|
|
$userService = new UserService();
|
2023-04-15 19:34:54 +08:00
|
|
|
$statData = [];
|
2021-08-31 23:55:07 +08:00
|
|
|
foreach ($data as $item) {
|
2022-07-17 00:59:35 +08:00
|
|
|
$u = $item['u'];
|
|
|
|
$d = $item['d'];
|
|
|
|
$userService->trafficFetch($u, $d, $item['user_id'], $server->toArray(), 'shadowsocks');
|
2023-04-15 19:34:54 +08:00
|
|
|
$statData[$item['user_id']] = [$u, $d];
|
2020-10-04 14:21:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-15 19:34:54 +08:00
|
|
|
$statService = new StatisticalService();
|
2023-05-03 15:37:19 +08:00
|
|
|
$statService->setStartAt(strtotime(date('Y-m-d')));
|
|
|
|
$statService->setUserStats();
|
2023-04-15 19:34:54 +08:00
|
|
|
$statService->statUser($server['rate'], $statData);
|
|
|
|
|
2020-10-04 14:21:09 +08:00
|
|
|
return response([
|
|
|
|
'ret' => 1,
|
|
|
|
'msg' => 'ok'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|