Skip to content

Commit

Permalink
move config to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed Oct 26, 2017
1 parent 7ddf41e commit ff5f8f9
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 59 deletions.
41 changes: 27 additions & 14 deletions app/Http/Controllers/ChallengeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,33 @@ public function list(Request $request)
$categories = Category::with(["levels", 'challenges'])->get();
$validLevels = collect([]);
$result = collect([]);
$categories->each(function ($category) use ($validLevels, $team) {
collect($category->levels)->each(function ($level) use ($validLevels, $team) {
if ((new RuleValidator($team->team_id, $level->rules))->check($team->logs) && Carbon::now()->gt(Carbon::parse($level->release_time))) {
$validLevels->push($level->level_id);
}
try{
$categories->each(function ($category) use ($validLevels, $team) {
collect($category->levels)->each(function ($level) use ($validLevels, $team) {
if ((new RuleValidator($team->team_id, $level->rules))->check($team->logs) && Carbon::now()->gt(Carbon::parse($level->release_time))) {
$validLevels->push($level->level_id);
}
});
});
});

$categories->each(function ($category) use ($validLevels, $result) {
$result[$category->category_name] = $category->challenges->filter(function ($challenge) use ($validLevels) {
return $validLevels->contains($challenge->level_id) && Carbon::now()->gt(Carbon::parse($challenge->release_time));
})->groupBy('level_id');
});
$categories->each(function ($category) use ($validLevels, $result) {
$result[$category->category_name] = $category->challenges->filter(function ($challenge) use ($validLevels) {
return $validLevels->contains($challenge->level_id) && Carbon::now()->gt(Carbon::parse($challenge->release_time));
})->groupBy('level_id');
});

return APIReturn::success($result);
$placeholders = [
'teamId' => hash('sha256', ((string)$team->team_id) . 'Nanjolno')
];
}
catch (\Exception $e){
dump($e);
}

return APIReturn::success([
'placeholders' => $placeholders,
'challenges' => $result
]);
}

/**
Expand Down Expand Up @@ -368,8 +380,9 @@ public function submitFlag(Request $request)
}
try {
$flag = Flag::where('flag', $request->input('flag'))->first();
$flagPrefix = \Config::get("ctf.flagPrefix");
$flagSuffix = \Config::get("ctf.flagSuffix");
$config = collect(\DB::table("config")->get())->pluck('value', 'key');
$flagPrefix = $config["flag_prefix"];
$flagSuffix = $config["flag_suffix"];

if (!$flag) {
// Flag 不正确
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class IndexController extends Controller
{
public function index(Request $request)
{
Carbon::now()->toDateTimeString();
$title = 'hctf';
return APIReturn::success([
'hello' => "hctf"
]);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/LevelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function create(Request $request)
$newLevel = new Level();
$newLevel->level_name = $request->input('levelName');
$newLevel->release_time = Carbon::parse($request->input('releaseTime'))->setTimezone('UTC')->toDateTimeString();
$newLevel->rules = '[]';
$newLevel->rules = [];

$category->levels()->save($newLevel);

Expand Down
39 changes: 31 additions & 8 deletions app/Http/Controllers/SystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use APIReturn;
use Carbon\Carbon;
use Config;
use Illuminate\Http\Request;

class SystemController extends Controller
Expand All @@ -17,14 +16,21 @@ class SystemController extends Controller
*/
public function getMetaInfo(Request $request)
{
return \APIReturn::success([
"startTime" => Carbon::parse(Config::get("ctf.startTime"))->toIso8601String(),
"endTime" => Carbon::parse(Config::get("ctf.endTime"))->toIso8601String(),
"flagPrefix" => Config::get("ctf.flagPrefix"),
"flagSuffix" => Config::get("ctf.flagSuffix")
$config = collect(\DB::table("config")->get())->pluck('value', 'key');
return APIReturn::success([
'startTime' => Carbon::parse($config['start_time'], 'UTC')->toIso8601String(),
'endTime' => Carbon::parse($config['end_time'], 'UTC')->toIso8601String(),
'flagPrefix' => $config['flag_prefix'],
'flagSuffix' => $config['flag_suffix']
]);
}

/**
* 编辑系统设置
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @author Eridanus Sora <[email protected]>
*/
public function editMetaInfo(Request $request){
$validator = \Validator::make($request->only(['startTime', 'endTime', 'flagPrefix', 'flagSuffix']), [
'startTime' => 'required|date',
Expand All @@ -37,7 +43,24 @@ public function editMetaInfo(Request $request){
return APIReturn::error('invalid_parameters', $validator->errors()->all(), 400);
}

Config::set('ctf.startTime', $request->input('startTime'));
Config::set('ctf.endTime', $request->input('endTime'));
try{
\DB::table("config")->where('key', '=', 'start_time')->update([
'value' => Carbon::parse($request->input('startTime'))->setTimezone('UTC')->toDateTimeString()
]);
\DB::table("config")->where('key', '=', 'end_time')->update([
'value' => Carbon::parse($request->input('endTime'))->setTimezone('UTC')->toDateTimeString()
]);
\DB::table("config")->where('key', '=', 'flag_prefix')->update([
'value' => $request->input('flagPrefix')
]);
\DB::table("config")->where('key', '=', 'flag_suffix')->update([
'value' => $request->input('flagSuffix')
]);
}
catch (\Exception $e){
return \APIReturn::error("database_error", "数据库读写错误", 500);
}

return APIReturn::success();
}
}
5 changes: 3 additions & 2 deletions app/Http/Middleware/TimeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*/
class TimeCheck {
public function handle($request, Closure $next){
$start = Carbon::parse(\Config::get("ctf.startTime"));
$end = Carbon::parse(\Config::get("ctf.endTime"));
$config = collect(\DB::table("config")->get())->pluck('value', 'key');
$start = Carbon::parse($config["start_time"], 'UTC');
$end = Carbon::parse($config["end_time"], 'UTC');
$now = Carbon::now("Asia/Shanghai");
if ($now->lt($start) || $now->gt($end)){
return \APIReturn::error("under_maintenance", [
Expand Down
4 changes: 2 additions & 2 deletions app/Services/RuleValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function check($logs)
// 记录开放时间
if ($result === true) {
if (!Cache::has($this->teamId . '-' . $this->rulesHash)) {
Cache::forever($this->teamId . '-' . $this->rulesHash, Carbon::now('Asia/Shanghai')->toDateTimeString());
Cache::forever($this->teamId . '-' . $this->rulesHash, Carbon::now()->toIso8601String());
}
}
return $result;
Expand All @@ -76,7 +76,7 @@ public function secondsAfterOpen()
if (!Cache::has($this->teamId . '-' . $this->rulesHash)) {
return 0;
} else {
return Carbon::parse(Cache::get($this->teamId . '-' . $this->rulesHash), 'Asia/Shanghai')->diffInSeconds(Carbon::now('Asia/Shanghai'));
return Carbon::parse(Cache::get($this->teamId . '-' . $this->rulesHash))->diffInSeconds(Carbon::now());
}
}
}
30 changes: 0 additions & 30 deletions config/ctf.php

This file was deleted.

31 changes: 31 additions & 0 deletions database/migrations/2017_10_26_154016_add_config_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddConfigTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create("config", function(Blueprint $table){
$table->string('key')->unique();
$table->string('value');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
37 changes: 37 additions & 0 deletions database/seeds/ConfigTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Seeder;

class ConfigTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('config')->delete();

$config = [
[
'key' => 'start_time',
'value' => '2017-11-11T08:00:00+08:00'
],
[
'key' => 'end_time',
'value' => '2017-11-13T08:00:00+08:00'
],
[
'key' => 'flag_prefix',
'value' => 'hctf{'
],
[
'key' => 'flag_suffix',
'value' => '}'
]
];

DB::table('config')->insert($config);
}
}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@

Route::group(['prefix' => 'System'], function(){
Route::get('meta', 'SystemController@getMetaInfo');
Route::group(['middleware' => ['jwt.auth.mod', 'AdminCheck']], function(){
Route::post('edit', 'SystemController@editMetaInfo');
});
});

0 comments on commit ff5f8f9

Please sign in to comment.