mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 09:21:46 +08:00 
			
		
		
		
	support trojan and more optimization
This commit is contained in:
		
							
								
								
									
										128
									
								
								app/Http/Controllers/Admin/Server/TrojanController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								app/Http/Controllers/Admin/Server/TrojanController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,128 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Http\Controllers\Admin\Server; | ||||
|  | ||||
| use App\Http\Requests\Admin\ServerTrojanSave; | ||||
| use App\Http\Requests\Admin\ServerTrojanSort; | ||||
| use App\Http\Requests\Admin\ServerTrojanUpdate; | ||||
| use App\Utils\CacheKey; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Models\ServerTrojan; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class TrojanController extends Controller | ||||
| { | ||||
|     public function fetch(Request $request) | ||||
|     { | ||||
|         $server = ServerTrojan::orderBy('sort', 'ASC')->get(); | ||||
|         for ($i = 0; $i < count($server); $i++) { | ||||
|             if (!empty($server[$i]['tags'])) { | ||||
|                 $server[$i]['tags'] = json_decode($server[$i]['tags']); | ||||
|             } | ||||
|             $server[$i]['group_id'] = json_decode($server[$i]['group_id']); | ||||
|             $server[$i]['online'] = Cache::get(CacheKey::get('SERVER_ONLINE_USER', $server[$i]['id'])); | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => $server | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function save(ServerTrojanSave $request) | ||||
|     { | ||||
|         $params = $request->only(array_keys(ServerTrojanSave::RULES)); | ||||
|         $params['group_id'] = json_encode($params['group_id']); | ||||
|         if (isset($params['tags'])) { | ||||
|             $params['tags'] = json_encode($params['tags']); | ||||
|         } | ||||
|  | ||||
|         if ($request->input('id')) { | ||||
|             $server = ServerTrojan::find($request->input('id')); | ||||
|             if (!$server) { | ||||
|                 abort(500, '服务器不存在'); | ||||
|             } | ||||
|             try { | ||||
|                 $server->update($params); | ||||
|             } catch (\Exception $e) { | ||||
|                 abort(500, '保存失败'); | ||||
|             } | ||||
|             return response([ | ||||
|                 'data' => true | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         if (!ServerTrojan::create($params)) { | ||||
|             abort(500, '创建失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function drop(Request $request) | ||||
|     { | ||||
|         if ($request->input('id')) { | ||||
|             $server = ServerTrojan::find($request->input('id')); | ||||
|             if (!$server) { | ||||
|                 abort(500, '节点ID不存在'); | ||||
|             } | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => $server->delete() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(ServerTrojanUpdate $request) | ||||
|     { | ||||
|         $params = $request->only([ | ||||
|             'show', | ||||
|         ]); | ||||
|  | ||||
|         $server = ServerTrojan::find($request->input('id')); | ||||
|  | ||||
|         if (!$server) { | ||||
|             abort(500, '该服务器不存在'); | ||||
|         } | ||||
|         try { | ||||
|             $server->update($params); | ||||
|         } catch (\Exception $e) { | ||||
|             abort(500, '保存失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function copy(Request $request) | ||||
|     { | ||||
|         $server = ServerTrojan::find($request->input('id')); | ||||
|         if (!$server) { | ||||
|             abort(500, '服务器不存在'); | ||||
|         } | ||||
|         if (!ServerTrojan::create($server->toArray())) { | ||||
|             abort(500, '复制失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function sort(ServerTrojanSort $request) | ||||
|     { | ||||
|         DB::beginTransaction(); | ||||
|         foreach ($request->input('server_ids') as $k => $v) { | ||||
|             if (!ServerTrojan::find($v)->update(['sort' => $k + 1])) { | ||||
|                 DB::rollBack(); | ||||
|                 abort(500, '保存失败'); | ||||
|             } | ||||
|         } | ||||
|         DB::commit(); | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										167
									
								
								app/Http/Controllers/Admin/Server/V2rayController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								app/Http/Controllers/Admin/Server/V2rayController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,167 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Http\Controllers\Admin\Server; | ||||
|  | ||||
| use App\Http\Requests\Admin\ServerV2raySave; | ||||
| use App\Http\Requests\Admin\ServerV2raySort; | ||||
| use App\Http\Requests\Admin\ServerV2rayUpdate; | ||||
| use App\Services\ServerService; | ||||
| use App\Utils\CacheKey; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Models\Server; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
| use Illuminate\Support\Facades\DB; | ||||
|  | ||||
| class V2rayController extends Controller | ||||
| { | ||||
|     public function fetch(Request $request) | ||||
|     { | ||||
|         $server = Server::orderBy('sort', 'ASC')->get(); | ||||
|         for ($i = 0; $i < count($server); $i++) { | ||||
|             if (!empty($server[$i]['tags'])) { | ||||
|                 $server[$i]['tags'] = json_decode($server[$i]['tags']); | ||||
|             } | ||||
|             $server[$i]['group_id'] = json_decode($server[$i]['group_id']); | ||||
|             $server[$i]['online'] = Cache::get(CacheKey::get('SERVER_ONLINE_USER', $server[$i]['parent_id'] ? $server[$i]['parent_id'] : $server[$i]['id'])); | ||||
|             if ($server[$i]['parent_id']) { | ||||
|                 $server[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $server[$i]['parent_id'])); | ||||
|             } else { | ||||
|                 $server[$i]['last_check_at'] = Cache::get(CacheKey::get('SERVER_LAST_CHECK_AT', $server[$i]['id'])); | ||||
|             } | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => $server | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function save(ServerV2raySave $request) | ||||
|     { | ||||
|         $params = $request->only(array_keys(ServerV2raySave::RULES)); | ||||
|         $params['group_id'] = json_encode($params['group_id']); | ||||
|         if (isset($params['tags'])) { | ||||
|             $params['tags'] = json_encode($params['tags']); | ||||
|         } | ||||
|  | ||||
|         if (isset($params['dnsSettings'])) { | ||||
|             if (!is_object(json_decode($params['dnsSettings']))) { | ||||
|                 abort(500, 'DNS规则配置格式不正确'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (isset($params['ruleSettings'])) { | ||||
|             if (!is_object(json_decode($params['ruleSettings']))) { | ||||
|                 abort(500, '审计规则配置格式不正确'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (isset($params['networkSettings'])) { | ||||
|             if (!is_object(json_decode($params['networkSettings']))) { | ||||
|                 abort(500, '传输协议配置格式不正确'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (isset($params['tlsSettings'])) { | ||||
|             if (!is_object(json_decode($params['tlsSettings']))) { | ||||
|                 abort(500, 'TLS配置格式不正确'); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if ($request->input('id')) { | ||||
|             $server = Server::find($request->input('id')); | ||||
|             if (!$server) { | ||||
|                 abort(500, '服务器不存在'); | ||||
|             } | ||||
|             try { | ||||
|                 $server->update($params); | ||||
|             } catch (\Exception $e) { | ||||
|                 abort(500, '保存失败'); | ||||
|             } | ||||
|             return response([ | ||||
|                 'data' => true | ||||
|             ]); | ||||
|         } | ||||
|  | ||||
|         if (!Server::create($params)) { | ||||
|             abort(500, '创建失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function drop(Request $request) | ||||
|     { | ||||
|         if ($request->input('id')) { | ||||
|             $server = Server::find($request->input('id')); | ||||
|             if (!$server) { | ||||
|                 abort(500, '节点ID不存在'); | ||||
|             } | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => $server->delete() | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(ServerV2rayUpdate $request) | ||||
|     { | ||||
|         $params = $request->only([ | ||||
|             'show', | ||||
|         ]); | ||||
|  | ||||
|         $server = Server::find($request->input('id')); | ||||
|  | ||||
|         if (!$server) { | ||||
|             abort(500, '该服务器不存在'); | ||||
|         } | ||||
|         try { | ||||
|             $server->update($params); | ||||
|         } catch (\Exception $e) { | ||||
|             abort(500, '保存失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function copy(Request $request) | ||||
|     { | ||||
|         $server = Server::find($request->input('id')); | ||||
|         if (!$server) { | ||||
|             abort(500, '服务器不存在'); | ||||
|         } | ||||
|         if (!Server::create($server->toArray())) { | ||||
|             abort(500, '复制失败'); | ||||
|         } | ||||
|  | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function viewConfig(Request $request) | ||||
|     { | ||||
|         $serverService = new ServerService(); | ||||
|         $config = $serverService->getConfig($request->input('node_id'), 23333); | ||||
|         return response([ | ||||
|             'data' => $config | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function sort(ServerV2raySort $request) | ||||
|     { | ||||
|         DB::beginTransaction(); | ||||
|         foreach ($request->input('server_ids') as $k => $v) { | ||||
|             if (!Server::find($v)->update(['sort' => $k + 1])) { | ||||
|                 DB::rollBack(); | ||||
|                 abort(500, '保存失败'); | ||||
|             } | ||||
|         } | ||||
|         DB::commit(); | ||||
|         return response([ | ||||
|             'data' => true | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
| @@ -2,9 +2,9 @@ | ||||
|  | ||||
| namespace App\Http\Controllers\Admin; | ||||
|  | ||||
| use App\Http\Requests\Admin\ServerSave; | ||||
| use App\Http\Requests\Admin\ServerSort; | ||||
| use App\Http\Requests\Admin\ServerUpdate; | ||||
| use App\Http\Requests\Admin\ServerV2raySave; | ||||
| use App\Http\Requests\Admin\ServerV2raySort; | ||||
| use App\Http\Requests\Admin\ServerV2rayUpdate; | ||||
| use App\Services\ServerService; | ||||
| use App\Utils\CacheKey; | ||||
| use Illuminate\Http\Request; | ||||
| @@ -38,9 +38,9 @@ class ServerController extends Controller | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function save(ServerSave $request) | ||||
|     public function save(ServerV2raySave $request) | ||||
|     { | ||||
|         $params = $request->only(array_keys(ServerSave::RULES)); | ||||
|         $params = $request->only(array_keys(ServerV2raySave::RULES)); | ||||
|         $params['group_id'] = json_encode($params['group_id']); | ||||
|         if (isset($params['tags'])) { | ||||
|             $params['tags'] = json_encode($params['tags']); | ||||
| @@ -165,7 +165,7 @@ class ServerController extends Controller | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function update(ServerUpdate $request) | ||||
|     public function update(ServerV2rayUpdate $request) | ||||
|     { | ||||
|         $params = $request->only([ | ||||
|             'show', | ||||
| @@ -211,7 +211,7 @@ class ServerController extends Controller | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     public function sort(ServerSort $request) | ||||
|     public function sort(ServerV2raySort $request) | ||||
|     { | ||||
|         DB::beginTransaction(); | ||||
|         foreach ($request->input('server_ids') as $k => $v) { | ||||
|   | ||||
| @@ -3,52 +3,65 @@ | ||||
| namespace App\Http\Controllers\Client; | ||||
|  | ||||
| use App\Http\Controllers\Controller; | ||||
| use App\Services\UserService; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Models\User; | ||||
| use App\Models\Plan; | ||||
| use App\Models\Server; | ||||
| use App\Models\Notice; | ||||
| use App\Utils\Helper; | ||||
| use Symfony\Component\Yaml\Yaml; | ||||
|  | ||||
| class AppController extends Controller | ||||
| { | ||||
|     CONST CLIENT_CONFIG = '{"policy":{"levels":{"0":{"uplinkOnly":0}}},"dns":{"servers":["8.8.8.8","localhost"]},"outboundDetour":[{"protocol":"freedom","tag":"direct","settings":{}}],"inbound":{"listen":"0.0.0.0","port":31211,"protocol":"socks","settings":{"auth":"noauth","udp":true,"ip":"127.0.0.1"}},"inboundDetour":[{"listen":"0.0.0.0","allocate":{"strategy":"always","refresh":5,"concurrency":3},"port":31210,"protocol":"http","tag":"httpDetour","domainOverride":["http","tls"],"streamSettings":{},"settings":{"timeout":0}}],"routing":{"strategy":"rules","settings":{"domainStrategy":"IPIfNonMatch","rules":[{"type":"field","ip":["geoip:cn"],"outboundTag":"direct"},{"type":"field","ip":["0.0.0.0/8","10.0.0.0/8","100.64.0.0/10","127.0.0.0/8","169.254.0.0/16","172.16.0.0/12","192.0.0.0/24","192.0.2.0/24","192.168.0.0/16","198.18.0.0/15","198.51.100.0/24","203.0.113.0/24","::1/128","fc00::/7","fe80::/10"],"outboundTag":"direct"}]}},"outbound":{"tag":"proxy","sendThrough":"0.0.0.0","mux":{"enabled":false,"concurrency":8},"protocol":"vmess","settings":{"vnext":[{"address":"server","port":443,"users":[{"id":"uuid","alterId":2,"security":"auto","level":0}],"remark":"remark"}]},"streamSettings":{"network":"tcp","tcpSettings":{"header":{"type":"none"}},"security":"none","tlsSettings":{"allowInsecure":true,"allowInsecureCiphers":true},"kcpSettings":{"header":{"type":"none"},"mtu":1350,"congestion":false,"tti":20,"uplinkCapacity":5,"writeBufferSize":1,"readBufferSize":1,"downlinkCapacity":20},"wsSettings":{"path":"","headers":{"Host":"server.cc"}}}}}'; | ||||
|     CONST SOCKS_PORT = 10010; | ||||
|     CONST HTTP_PORT = 10011; | ||||
|  | ||||
|     // TODO: 1.1.1 abolish | ||||
|     public function data(Request $request) | ||||
|     { | ||||
|         $server = []; | ||||
|         $user = $request->user; | ||||
|         $nodes = []; | ||||
|         if ($user->plan_id) { | ||||
|             $user['plan'] = Plan::find($user->plan_id); | ||||
|             if (!$user['plan']) { | ||||
|                 abort(500, '订阅计划不存在'); | ||||
|             } | ||||
|             if ($user->expired_at > time()) { | ||||
|                 $servers = Server::where('show', 1) | ||||
|                     ->orderBy('name') | ||||
|                     ->get(); | ||||
|                 foreach ($servers as $item) { | ||||
|                     $groupId = json_decode($item['group_id']); | ||||
|                     if (in_array($user->group_id, $groupId)) { | ||||
|                         array_push($nodes, $item); | ||||
|                     } | ||||
|         $userService = new UserService(); | ||||
|         if ($userService->isAvailable($user)) { | ||||
|             $servers = Server::where('show', 1) | ||||
|                 ->orderBy('sort', 'ASC') | ||||
|                 ->get(); | ||||
|             foreach ($servers as $item) { | ||||
|                 $groupId = json_decode($item['group_id']); | ||||
|                 if (in_array($user->group_id, $groupId)) { | ||||
|                     array_push($server, $item); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         return response([ | ||||
|             'data' => [ | ||||
|                 'nodes' => $nodes, | ||||
|                 'u' => $user->u, | ||||
|                 'd' => $user->d, | ||||
|                 'transfer_enable' => $user->transfer_enable, | ||||
|                 'expired_at' => $user->expired_at, | ||||
|                 'plan' => isset($user['plan']) ? $user['plan'] : false, | ||||
|                 'notice' => Notice::orderBy('created_at', 'DESC')->first() | ||||
|             ] | ||||
|         ]); | ||||
|         $config = Yaml::parseFile(base_path() . '/resources/rules/app.clash.yaml'); | ||||
|         $proxy = []; | ||||
|         $proxies = []; | ||||
|         foreach ($server as $item) { | ||||
|             $array = []; | ||||
|             $array['name'] = $item->name; | ||||
|             $array['type'] = 'vmess'; | ||||
|             $array['server'] = $item->host; | ||||
|             $array['port'] = $item->port; | ||||
|             $array['uuid'] = $user->uuid; | ||||
|             $array['alterId'] = $user->v2ray_alter_id; | ||||
|             $array['cipher'] = 'auto'; | ||||
|             if ($item->tls) { | ||||
|                 $tlsSettings = json_decode($item->tlsSettings); | ||||
|                 $array['tls'] = true; | ||||
|                 if (isset($tlsSettings->allowInsecure)) $array['skip-cert-verify'] = ($tlsSettings->allowInsecure ? true : false ); | ||||
|             } | ||||
|             if ($item->network == 'ws') { | ||||
|                 $array['network'] = $item->network; | ||||
|                 if ($item->networkSettings) { | ||||
|                     $wsSettings = json_decode($item->networkSettings); | ||||
|                     if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path; | ||||
|                     if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [ | ||||
|                         'Host' => $wsSettings->headers->Host | ||||
|                     ]; | ||||
|                 } | ||||
|             } | ||||
|             array_push($proxy, $array); | ||||
|             array_push($proxies, $item->name); | ||||
|         } | ||||
|  | ||||
|         $config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy); | ||||
|         foreach ($config['Proxy Group'] as $k => $v) { | ||||
|             $config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies); | ||||
|         } | ||||
|         die(Yaml::dump($config)); | ||||
|     } | ||||
|  | ||||
|     public function config(Request $request) | ||||
|   | ||||
| @@ -206,9 +206,9 @@ class ClientController extends Controller | ||||
|             $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | ||||
|         } | ||||
|  | ||||
|         $config = str_replace('$subs_link',$subsURL,$config); | ||||
|         $config = str_replace('$proxies',$proxies,$config); | ||||
|         $config = str_replace('$proxy_group',rtrim($proxyGroup, ', '),$config); | ||||
|         $config = str_replace('$subs_link', $subsURL, $config); | ||||
|         $config = str_replace('$proxies', $proxies, $config); | ||||
|         $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config); | ||||
|         return $config; | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -90,7 +90,8 @@ class DeepbworkController extends Controller | ||||
|                 $request->input('node_id'), | ||||
|                 $item['u'], | ||||
|                 $item['d'], | ||||
|                 $server->rate | ||||
|                 $server->rate, | ||||
|                 'vmess' | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -80,7 +80,8 @@ class PoseidonController extends Controller | ||||
|                 $request->input('node_id'), | ||||
|                 $item['u'], | ||||
|                 $item['d'], | ||||
|                 $server->rate | ||||
|                 $server->rate, | ||||
|                 'vmess' | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|   | ||||
							
								
								
									
										118
									
								
								app/Http/Controllers/Server/TrojanTidalabController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								app/Http/Controllers/Server/TrojanTidalabController.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,118 @@ | ||||
| <?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; | ||||
|  | ||||
| class TrojanTidalabController extends Controller | ||||
| { | ||||
|     CONST SERVER_CONFIG = '{"api":{"services":["HandlerService","StatsService"],"tag":"api"},"stats":{},"inbound":{"port":443,"protocol":"vmess","settings":{"clients":[]},"sniffing":{"enabled": true,"destOverride": ["http","tls"]},"streamSettings":{"network":"tcp"},"tag":"proxy"},"inboundDetour":[{"listen":"0.0.0.0","port":23333,"protocol":"dokodemo-door","settings":{"address":"0.0.0.0"},"tag":"api"}],"log":{"loglevel":"debug","access":"access.log","error":"error.log"},"outbound":{"protocol":"freedom","settings":{}},"outboundDetour":[{"protocol":"blackhole","settings":{},"tag":"block"}],"routing":{"rules":[{"inboundTag":"api","outboundTag":"api","type":"field"}]},"policy":{"levels":{"0":{"handshake":4,"connIdle":300,"uplinkOnly":5,"downlinkOnly":30,"statsUserUplink":true,"statsUserDownlink":true}}}}'; | ||||
|  | ||||
|     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'); | ||||
|         } | ||||
|         Cache::put(CacheKey::get('SERVER_LAST_CHECK_AT', $server->id), time(), 3600); | ||||
|         $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']); | ||||
|             unset($user['v2ray_alter_id']); | ||||
|             unset($user['v2ray_level']); | ||||
|             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); | ||||
|         Cache::put(CacheKey::get('SERVER_ONLINE_USER', $server->id), count($data), 3600); | ||||
|         $serverService = new ServerService(); | ||||
|         $userService = new UserService(); | ||||
|         foreach ($data as $item) { | ||||
|             $u = $item['u'] * $server->rate; | ||||
|             $d = $item['d'] * $server->rate; | ||||
|             if (!$userService->trafficFetch($u, $d, $item['user_id'])) { | ||||
|                 return response([ | ||||
|                     'ret' => 0, | ||||
|                     'msg' => 'user fetch fail' | ||||
|                 ]); | ||||
|             } | ||||
|  | ||||
|             $serverService->log( | ||||
|                 $item['user_id'], | ||||
|                 $request->input('node_id'), | ||||
|                 $item['u'], | ||||
|                 $item['d'], | ||||
|                 $server->rate, | ||||
|                 'trojan' | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|         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 { | ||||
|             $json = $serverService->getConfig($nodeId, $localPort); | ||||
|         } catch (\Exception $e) { | ||||
|             abort(500, $e->getMessage()); | ||||
|         } | ||||
|  | ||||
|         die(json_encode($json, JSON_UNESCAPED_UNICODE)); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user