refactor announce

This commit is contained in:
xiaomlove
2022-03-17 18:46:49 +08:00
parent 3ad8fabe7b
commit 6665c98169
16 changed files with 1486 additions and 27 deletions
+29
View File
@@ -4,9 +4,15 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
class Peer extends NexusModel
{
protected $fillable = [
'torrent', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'to_go', 'seeder', 'started', 'last_action',
'prev_action', 'connectable', 'userid', 'agent', 'finishedat', 'downloadoffset', 'uploadedoffset', 'passkey',
];
const CONNECTABLE_YES = 'yes';
const CONNECTABLE_NO = 'no';
@@ -71,4 +77,27 @@ class Peer extends NexusModel
{
return $this->belongsTo(Torrent::class, 'torrent');
}
/**
*
*/
public function updateConnectableStateIfNeeded()
{
$tmp_ip = $this->ip;
// IPv6 Check
if (filter_var($tmp_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$tmp_ip = '['.$tmp_ip.']';
}
$cacheKey = 'peers:connectable:'.$tmp_ip.'-'.$this->port.'-'.$this->agent;
if (!Cache::has($cacheKey)) {
$con = @fsockopen($tmp_ip, $this->port, $error_code, $error_message, 1);
if (is_resource($con)) {
$this->connectable = self::CONNECTABLE_YES;
fclose($con);
} else {
$this->connectable = self::CONNECTABLE_NO;
}
Cache::put($cacheKey, $this->connectable, 600);
}
}
}