123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace App\Models;
- use App\Components\IP;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- /**
- * 节点配置信息.
- *
- * @property int $id
- * @property int $type 服务类型:1-Shadowsocks(R)、2-V2ray、3-Trojan、4-VNet
- * @property string $name 名称
- * @property string $country_code 国家代码
- * @property string|null $server 服务器域名地址
- * @property string|null $ip 服务器IPV4地址
- * @property string|null $ipv6 服务器IPV6地址
- * @property int $level 等级:0-无等级,全部可见
- * @property int|null $rule_group_id 从属规则分组ID
- * @property int $speed_limit 节点限速,为0表示不限速,单位Byte
- * @property int $client_limit 设备数限制
- * @property string|null $relay_server 中转地址
- * @property int|null $relay_port 中转端口
- * @property string|null $description 节点简单描述
- * @property string|null $geo 节点地理位置
- * @property string $method 加密方式
- * @property string $protocol 协议
- * @property string|null $protocol_param 协议参数
- * @property string $obfs 混淆
- * @property string|null $obfs_param 混淆参数
- * @property float $traffic_rate 流量比率
- * @property int $is_subscribe 是否允许用户订阅该节点:0-否、1-是
- * @property int $is_ddns 是否使用DDNS:0-否、1-是
- * @property int $is_relay 是否中转节点:0-否、1-是
- * @property int $is_udp 是否启用UDP:0-不启用、1-启用
- * @property int $push_port 消息推送端口
- * @property int $detection_type 节点检测: 0-关闭、1-只检测TCP、2-只检测ICMP、3-检测全部
- * @property int $compatible 兼容SS
- * @property int $single 启用单端口功能:0-否、1-是
- * @property int|null $port 单端口的端口号或连接端口号
- * @property string|null $passwd 单端口的连接密码
- * @property int $sort 排序值,值越大越靠前显示
- * @property int $status 状态:0-维护、1-正常
- * @property int $v2_alter_id V2Ray额外ID
- * @property int $v2_port V2Ray服务端口
- * @property string $v2_method V2Ray加密方式
- * @property string $v2_net V2Ray传输协议
- * @property string $v2_type V2Ray伪装类型
- * @property string|null $v2_host V2Ray伪装的域名
- * @property string|null $v2_path V2Ray的WS/H2路径
- * @property int $v2_tls V2Ray连接TLS:0-未开启、1-开启
- * @property string|null $tls_provider V2Ray节点的TLS提供商授权信息
- * @property \Illuminate\Support\Carbon $created_at 创建时间
- * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
- * @property string|null $v2_sni
- * @property int|null $vless
- * @property string|null $vless_privateKey 密钥key
- * @property string|null $vless_pulkey 公钥
- * @property string|null $shortIds
- * @property-read \App\Models\NodeAuth|null $auth
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeDailyDataFlow[] $dailyDataFlows
- * @property-read int|null $daily_data_flows_count
- * @property-read string $type_label
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeHeartbeat[] $heartbeats
- * @property-read int|null $heartbeats_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeHourlyDataFlow[] $hourlyDataFlows
- * @property-read int|null $hourly_data_flows_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Label[] $labels
- * @property-read int|null $labels_count
- * @property-read \App\Models\Level|null $level_table
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineIp[] $onlineIps
- * @property-read int|null $online_ips_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineLog[] $onlineLogs
- * @property-read int|null $online_logs_count
- * @property-read \App\Models\RuleGroup|null $ruleGroup
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RuleLog[] $ruleLogs
- * @property-read int|null $rule_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataFlowLog[] $userDataFlowLogs
- * @property-read int|null $user_data_flow_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserGroup[] $userGroups
- * @property-read int|null $user_groups_count
- * @method static \Illuminate\Database\Eloquent\Builder|Node newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|Node newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|Node query()
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereClientLimit($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereCompatible($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereCountryCode($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereDescription($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereDetectionType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereGeo($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIp($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIpv6($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsDdns($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsRelay($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsSubscribe($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereIsUdp($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereLevel($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereMethod($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereName($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereObfs($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereObfsParam($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node wherePasswd($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node wherePort($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereProtocol($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereProtocolParam($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node wherePushPort($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereRelayPort($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereRelayServer($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereRuleGroupId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereServer($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereShortIds($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereSingle($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereSort($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereSpeedLimit($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereStatus($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereTlsProvider($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereTrafficRate($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereUpdatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2AlterId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Host($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Method($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Net($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Path($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Port($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Sni($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Tls($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereV2Type($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereVless($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereVlessPrivateKey($value)
- * @method static \Illuminate\Database\Eloquent\Builder|Node whereVlessPulkey($value)
- * @mixin \Eloquent
- */
- class Node extends Model
- {
- protected $table = 'node';
- protected $guarded = [];
- public function labels()
- {
- return $this->belongsToMany(Label::class);
- }
- public function heartbeats(): HasMany
- {
- return $this->hasMany(NodeHeartbeat::class);
- }
- public function onlineIps(): HasMany
- {
- return $this->hasMany(NodeOnlineIp::class);
- }
- public function onlineLogs(): HasMany
- {
- return $this->hasMany(NodeOnlineLog::class);
- }
- public function userDataFlowLogs(): HasMany
- {
- return $this->hasMany(UserDataFlowLog::class);
- }
- public function ruleLogs(): HasMany
- {
- return $this->hasMany(RuleLog::class);
- }
- public function dailyDataFlows(): HasMany
- {
- return $this->hasMany(NodeDailyDataFlow::class);
- }
- public function hourlyDataFlows(): HasMany
- {
- return $this->hasMany(NodeHourlyDataFlow::class);
- }
- public function ruleGroup(): BelongsTo
- {
- return $this->belongsTo(RuleGroup::class);
- }
- public function userGroups(): BelongsToMany
- {
- return $this->belongsToMany(UserGroup::class);
- }
- public function auth(): HasOne
- {
- return $this->hasOne(NodeAuth::class);
- }
- public function level_table(): HasOne
- {
- return $this->hasOne(Level::class, 'level', 'level');
- }
- public function users()
- {
- return User::activeUser()
- ->where('level', '>=', $this->attributes['level'])
- ->whereNull('user_group_id')
- ->orwhereIn('user_group_id', $this->userGroups->pluck('id')->toArray())
- ->get();
- }
- public function refresh_geo()
- {
- $data = IP::IPSB($this->is_ddns ? gethostbyname($this->server) : $this->ip);
- if ($data) {
- self::withoutEvents(function () use ($data) {
- $this->update(['geo' => $data['latitude'].','.$data['longitude']]);
- });
- return 1;
- }
- return 0;
- }
- public function config(User $user)
- {
- $config = [
- 'id' => $this->id,
- 'name' => $this->name,
- 'host' => $this->is_relay ? $this->relay_server : ($this->server ?: $this->ip),
- 'group' => sysConfig('website_name'),
- ];
- switch ($this->type) {
- case 0:
- $config = array_merge($config, [
- 'type' => 'shadowsocks',
- 'method' => $this->method,
- 'udp' => $this->is_udp,
- 'port' => $this->is_relay ? $this->relay_port : $this->port,
- 'passwd' => $user->passwd
- ]);
- break;
- case 2:
- $config = array_merge($config, [
- 'type' => 'v2ray',
- 'port' => $this->is_relay ? $this->relay_port : $this->port,
- 'uuid' => $user->vmess_id,
- 'method' => $this->v2_method,
- 'v2_alter_id' => $this->v2_alter_id,
- 'v2_net' => $this->v2_net,
- 'v2_type' => $this->vless === 1 ? 'vless' : $this->v2_type,
- 'v2_host' => $this->v2_host == null ? '':$this->v2_host,
- 'v2_path' => $this->v2_path == null ? '':$this->v2_path,
- 'v2_tls' => $this->v2_tls ? 'tls' : '',
- 'v2_sni' => $this->v2_sni == null ? '':$this->v2_sni,
- 'udp' => $this->is_udp,
- 'vless' => $this->vless,
- 'vless_pulkey'=> $this->vless_pulkey,
- ]);
- break;
- case 3:
- $config = array_merge($config, [
- 'type' => 'trojan',
- 'port' => $this->is_relay ? $this->relay_port : 443,
- 'passwd' => $user->passwd,
- 'sni' => $this->is_relay ? $this->server : '',
- 'udp' => $this->is_udp,
- ]);
- break;
- case 1:
- case 4:
- $config = array_merge($config, [
- 'type' => $this->compatible ? 'shadowsocks' : 'shadowsocksr',
- 'method' => $this->method,
- 'protocol' => $this->protocol,
- 'obfs' => $this->obfs,
- 'obfs_param' => $this->obfs_param,
- 'udp' => $this->is_udp,
- ]);
- if ($this->single) {
- //单端口使用中转的端口
- $config['port'] = $this->is_relay ? $this->relay_port : $this->port;
- $config['passwd'] = $this->passwd;
- $config['protocol_param'] = $user->port.':'.$user->passwd;
- } else {
- $config['port'] = $user->port;
- $config['passwd'] = $user->passwd;
- $config['protocol_param'] = $this->protocol_param;
- if ($this->type === 1) {
- $config['method'] = $user->method;
- $config['protocol'] = $user->protocol;
- $config['obfs'] = $user->obfs;
- }
- }
- break;
- }
- return $config;
- }
- public function getSpeedLimitAttribute($value)
- {
- return $value / Mbps;
- }
- public function setSpeedLimitAttribute($value)
- {
- return $this->attributes['speed_limit'] = $value * Mbps;
- }
- public function getTypeLabelAttribute(): string
- {
- switch ($this->attributes['type']) {
- case 0:
- $type_label = 'shadowsocks';
- break;
- case 1:
- $type_label = 'ShadowsocksR';
- break;
- case 2:
- $type_label = 'V2Ray';
- break;
- case 3:
- $type_label = 'Trojan';
- break;
- case 4:
- $type_label = 'VNet';
- break;
- default:
- $type_label = 'UnKnown';
- }
- return $type_label;
- }
- }
|