2020-06-11 20:47:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Server;
|
|
|
|
|
|
|
|
use App\Services\ServerService;
|
|
|
|
use App\Services\UserService;
|
|
|
|
use App\Utils\CacheKey;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\ServerTrojan;
|
|
|
|
use App\Models\ServerLog;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
2020-06-18 02:25:05 +08:00
|
|
|
/*
|
|
|
|
* Tidal Lab Trojan
|
|
|
|
* Github: https://github.com/tokumeikoi/tidalab-trojan
|
|
|
|
*/
|
2020-06-11 20:47:02 +08:00
|
|
|
class TrojanTidalabController 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)
|
|
|
|
{
|
|
|
|
$nodeId = $request->input('node_id');
|
|
|
|
$server = ServerTrojan::find($nodeId);
|
|
|
|
if (!$server) {
|
|
|
|
abort(500, 'fail');
|
|
|
|
}
|
2020-06-11 21:50:18 +08:00
|
|
|
Cache::put(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $server->id), time(), 3600);
|
2020-06-11 20:47:02 +08:00
|
|
|
$serverService = new ServerService();
|
|
|
|
$users = $serverService->getAvailableUsers(json_decode($server->group_id));
|
|
|
|
$result = [];
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$user->trojan_user = [
|
|
|
|
"password" => $user->uuid,
|
|
|
|
];
|
|
|
|
unset($user['uuid']);
|
2020-11-17 21:23:16 +08:00
|
|
|
unset($user['email']);
|
2020-06-11 20:47:02 +08:00
|
|
|
array_push($result, $user);
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'msg' => 'ok',
|
|
|
|
'data' => $result,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 后端提交数据
|
|
|
|
public function submit(Request $request)
|
|
|
|
{
|
|
|
|
// Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input'));
|
|
|
|
$server = ServerTrojan::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);
|
2020-06-11 21:50:18 +08:00
|
|
|
Cache::put(CacheKey::get('SERVER_TROJAN_ONLINE_USER', $server->id), count($data), 3600);
|
2021-03-24 19:57:06 +08:00
|
|
|
Cache::put(CacheKey::get('SERVER_TROJAN_LAST_PUSH_AT', $server->id), time(), 3600);
|
2020-06-11 20:47:02 +08:00
|
|
|
$userService = new UserService();
|
2020-10-13 23:55:17 +08:00
|
|
|
DB::beginTransaction();
|
2020-11-07 16:38:57 +08:00
|
|
|
try {
|
|
|
|
foreach ($data as $item) {
|
|
|
|
$u = $item['u'] * $server->rate;
|
|
|
|
$d = $item['d'] * $server->rate;
|
2020-11-09 00:12:19 +08:00
|
|
|
if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'trojan')) {
|
2020-11-07 16:38:57 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
return response([
|
|
|
|
'ret' => 0,
|
|
|
|
'msg' => 'user fetch fail'
|
|
|
|
]);
|
2020-06-11 20:47:02 +08:00
|
|
|
}
|
2020-10-13 23:55:17 +08:00
|
|
|
DB::commit();
|
2020-06-11 20:47:02 +08:00
|
|
|
|
|
|
|
return response([
|
|
|
|
'ret' => 1,
|
|
|
|
'msg' => 'ok'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 后端获取配置
|
|
|
|
public function config(Request $request)
|
|
|
|
{
|
|
|
|
$nodeId = $request->input('node_id');
|
|
|
|
$localPort = $request->input('local_port');
|
|
|
|
if (empty($nodeId) || empty($localPort)) {
|
|
|
|
abort(500, '参数错误');
|
|
|
|
}
|
|
|
|
$serverService = new ServerService();
|
|
|
|
try {
|
2020-06-12 01:31:00 +08:00
|
|
|
$json = $serverService->getTrojanConfig($nodeId, $localPort);
|
2020-06-11 20:47:02 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
abort(500, $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
die(json_encode($json, JSON_UNESCAPED_UNICODE));
|
|
|
|
}
|
|
|
|
}
|