Added database recording

This commit is contained in:
2023-03-03 10:29:39 +11:00
parent a389264ec4
commit 32a75cb140
5 changed files with 101 additions and 41 deletions

View File

@@ -7,6 +7,9 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use App\Models\Site;
use App\Models\SiteVersion;
class VersionController extends Controller
{
const CACHE_TIME = 10; // Time to cache version
@@ -21,6 +24,14 @@ class VersionController extends Controller
// v1.2.3-xxx-abcdef01
Log::info(sprintf('Connection from [%s] reporting version [%s]',$this->getUserIpAddr(),$version));
$so = Site::firstOrCreate([
'ip_address'=>$this->getUserIpAddr()
]);
$vo = new SiteVersion;
$vo->version = $version;
$so->versions()->save($vo);
$matches = [];
if (preg_match(self::VERSION_REGEX,$version,$matches)) {
// If xxx is "dev" we are a development version
@@ -45,10 +56,12 @@ class VersionController extends Controller
$repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id);
// Find the tag associated with version $matches[1] and see if it is more recent than $matches[4]
return ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
$response = ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
} else
return ['unknown'=>'vn.n.n-dev-hhhhhhhh'];
$response = ['unknown'=>'vn.n.n-dev-hhhhhhhh'];
break;
case 'rel':
$current = Cache::remember('dev',self::CACHE_TIME,function() {
@@ -72,19 +85,26 @@ class VersionController extends Controller
// If $matches[1] is smaller, "upgrade available"
if ($matches[1] < $current->name)
return ['upgrade'=>$repository];
$response = ['upgrade'=>$repository];
// If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error
elseif ($matches[1] === $current->name)
return ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository];
$response = ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository];
// if $matches[1] is higher, abort
else
return ['unknown'=>$repository];
$response = ['unknown'=>$repository];
} else
return ['unknown'=>'vn.n.n-rel-hhhhhhhh'];
$response = ['unknown'=>'vn.n.n-rel-hhhhhhhh'];
break;
}
$vo->response = $response;
$vo->save();
return $response;
}
// Return the current version
@@ -95,8 +115,10 @@ class VersionController extends Controller
{
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = preg_split('/,\s*/',$_SERVER['HTTP_X_FORWARDED_FOR']);
$ipaddress = $ips[0];
}
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))