update: server submit

This commit is contained in:
Tokumeikoi 2020-11-09 00:12:19 +08:00
parent 59a8429456
commit 9917e21a61
7 changed files with 20 additions and 49 deletions

View File

@ -64,7 +64,7 @@ class DeepbworkController extends Controller
// 后端提交数据 // 后端提交数据
public function submit(Request $request) public function submit(Request $request)
{ {
// Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input')); Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input'));
$server = Server::find($request->input('node_id')); $server = Server::find($request->input('node_id'));
if (!$server) { if (!$server) {
return response([ return response([
@ -75,25 +75,15 @@ class DeepbworkController extends Controller
$data = file_get_contents('php://input'); $data = file_get_contents('php://input');
$data = json_decode($data, true); $data = json_decode($data, true);
Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600);
$serverService = new ServerService();
$userService = new UserService(); $userService = new UserService();
DB::beginTransaction(); DB::beginTransaction();
try { try {
foreach ($data as $item) { foreach ($data as $item) {
$u = $item['u'] * $server->rate; $u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate; $d = $item['d'] * $server->rate;
if (!$userService->trafficFetch($u, $d, $item['user_id'])) { if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'vmess')) {
continue; continue;
} }
$serverService->log(
$item['user_id'],
$request->input('node_id'),
$item['u'],
$item['d'],
$server->rate,
'vmess'
);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();

View File

@ -68,23 +68,13 @@ class PoseidonController extends Controller
$data = file_get_contents('php://input'); $data = file_get_contents('php://input');
$data = json_decode($data, true); $data = json_decode($data, true);
Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600);
$serverService = new ServerService();
$userService = new UserService(); $userService = new UserService();
foreach ($data as $item) { foreach ($data as $item) {
$u = $item['u'] * $server->rate; $u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate; $d = $item['d'] * $server->rate;
if (!$userService->trafficFetch($u, $d, $item['user_id'])) { if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'vmess')) {
return $this->error("user fetch fail", 500); return $this->error("user fetch fail", 500);
} }
$serverService->log(
$item['user_id'],
$request->input('node_id'),
$item['u'],
$item['d'],
$server->rate,
'vmess'
);
} }
return $this->success(''); return $this->success('');

View File

@ -70,25 +70,15 @@ class ShadowsocksTidalabController extends Controller
$data = file_get_contents('php://input'); $data = file_get_contents('php://input');
$data = json_decode($data, true); $data = json_decode($data, true);
Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_SHADOWSOCKS_ONLINE_USER', $server->id), count($data), 3600);
$serverService = new ServerService();
$userService = new UserService(); $userService = new UserService();
DB::beginTransaction(); DB::beginTransaction();
try { try {
foreach ($data as $item) { foreach ($data as $item) {
$u = $item['u'] * $server->rate; $u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate; $d = $item['d'] * $server->rate;
if (!$userService->trafficFetch((float)$u, (float)$d, (int)$item['user_id'])) { if (!$userService->trafficFetch((float)$u, (float)$d, (int)$item['user_id'], $server, 'shadowsocks')) {
continue; continue;
} }
$serverService->log(
$item['user_id'],
$request->input('node_id'),
$item['u'],
$item['d'],
$server->rate,
'shadowsocks'
);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();

View File

@ -72,25 +72,15 @@ class TrojanTidalabController extends Controller
$data = file_get_contents('php://input'); $data = file_get_contents('php://input');
$data = json_decode($data, true); $data = json_decode($data, true);
Cache::put(CacheKey::get('SERVER_TROJAN_ONLINE_USER', $server->id), count($data), 3600); Cache::put(CacheKey::get('SERVER_TROJAN_ONLINE_USER', $server->id), count($data), 3600);
$serverService = new ServerService();
$userService = new UserService(); $userService = new UserService();
DB::beginTransaction(); DB::beginTransaction();
try { try {
foreach ($data as $item) { foreach ($data as $item) {
$u = $item['u'] * $server->rate; $u = $item['u'] * $server->rate;
$d = $item['d'] * $server->rate; $d = $item['d'] * $server->rate;
if (!$userService->trafficFetch($u, $d, $item['user_id'])) { if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'trojan')) {
continue; continue;
} }
$serverService->log(
$item['user_id'],
$request->input('node_id'),
$item['u'],
$item['d'],
$server->rate,
'trojan'
);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();

View File

@ -43,7 +43,6 @@ class Kernel extends HttpKernel
\Illuminate\Session\Middleware\StartSession::class, \Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\ForceJson::class, \App\Http\Middleware\ForceJson::class,
\App\Http\Middleware\CORS::class, \App\Http\Middleware\CORS::class,
'throttle:120,1',
'bindings', 'bindings',
], ],
]; ];

View File

@ -270,6 +270,7 @@ class ServerService
->where('user_id', $userId) ->where('user_id', $userId)
->where('rate', $rate) ->where('rate', $rate)
->where('method', $method) ->where('method', $method)
->lockForUpdate()
->first(); ->first();
if ($serverLog) { if ($serverLog) {
$serverLog->u = $serverLog->u + $u; $serverLog->u = $serverLog->u + $u;

View File

@ -3,6 +3,7 @@
namespace App\Services; namespace App\Services;
use App\Models\Order; use App\Models\Order;
use App\Models\Server;
use App\Models\User; use App\Models\User;
class UserService class UserService
@ -76,9 +77,10 @@ class UserService
return true; return true;
} }
public function trafficFetch(int $u, int $d, int $userId):bool public function trafficFetch(int $u, int $d, int $userId, Server $server, string $protocol):bool
{ {
$user = User::find($userId); $user = User::lockForUpdate()
->find($userId);
if (!$user) { if (!$user) {
return true; return true;
} }
@ -88,9 +90,18 @@ class UserService
if (!$user->save()) { if (!$user->save()) {
return false; return false;
} }
try {
$mailService = new MailService(); $mailService = new MailService();
$serverService = new ServerService();
try {
$mailService->remindTraffic($user); $mailService->remindTraffic($user);
$serverService->log(
$userId,
$server->id,
$u,
$d,
$server->rate,
$protocol
);
} catch (\Exception $e) { } catch (\Exception $e) {
} }
return true; return true;