v2board/app/Http/Controllers/Server/ShadowsocksTidalabController.php

84 lines
2.6 KiB
PHP
Raw Normal View History

2020-10-04 14:21:09 +08:00
<?php
namespace App\Http\Controllers\Server;
use App\Models\ServerShadowsocks;
use App\Services\ServerService;
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
]);
}
return response([
'data' => $result
]);
}
// 后端提交数据
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();
2021-08-31 23:55:07 +08:00
foreach ($data as $item) {
$u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate;
2021-09-06 00:19:43 +08:00
$userService->trafficFetch($u, $d, $item['user_id'], $server, 'shadowsocks');
2020-10-04 14:21:09 +08:00
}
return response([
'ret' => 1,
'msg' => 'ok'
]);
}
}