You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Malio/app/Utils/URL.php

833 lines
32 KiB
PHTML

9 years ago
<?php
9 years ago
namespace App\Utils;
9 years ago
use App\Models\User;
use App\Models\Node;
use App\Models\Relay;
use App\Services\Config;
use App\Controllers\LinkController;
use App\Controllers\ConfController;
9 years ago
class URL
{
/*
* 1 SSR can
* 2 SS can
* 3 Both can
*/
public static function CanMethodConnect($method)
{
9 years ago
$ss_aead_method_list = Config::getSupportParam('ss_aead_method');
if (in_array($method, $ss_aead_method_list)) {
9 years ago
return 2;
}
return 3;
}
8 years ago
9 years ago
/*
* 1 SSR can
* 2 SS can
* 3 Both can
*/
public static function CanProtocolConnect($protocol)
{
if ($protocol != 'origin') {
8 years ago
if (strpos($protocol, '_compatible') === false) {
9 years ago
return 1;
}
8 years ago
return 3;
9 years ago
}
return 3;
}
8 years ago
9 years ago
/*
* 1 SSR can
* 2 SS can
* 3 Both can
* 4 Both can, But ssr need set obfs to plain
* 5 Both can, But ss need set obfs to plain
*/
public static function CanObfsConnect($obfs)
{
if ($obfs != 'plain') {
9 years ago
//SS obfs only
$ss_obfs = Config::getSupportParam('ss_obfs');
if (in_array($obfs, $ss_obfs)) {
8 years ago
if (strpos($obfs, '_compatible') === false) {
9 years ago
return 2;
}
8 years ago
7 years ago
return 4; //SSR need origin plain
9 years ago
}
8 years ago
if (strpos($obfs, '_compatible') === false) {
return 1;
}
7 years ago
return 5; //SS need plain
9 years ago
}
8 years ago
return 3;
9 years ago
}
8 years ago
public static function parse_args($origin)
{
// parse xxx=xxx|xxx=xxx to array(xxx => xxx, xxx => xxx)
$args_explode = explode('|', $origin);
$return_array = [];
foreach ($args_explode as $arg) {
$split_point = strpos($arg, '=');
$return_array[substr($arg, 0, $split_point)] = substr($arg, $split_point + 1);
}
return $return_array;
}
public static function SSCanConnect($user, $mu_port = 0)
{
if ($mu_port != 0) {
8 years ago
$mu_user = User::where('port', '=', $mu_port)->where('is_multi_user', '<>', 0)->first();
9 years ago
if ($mu_user == null) {
return;
}
8 years ago
return self::SSCanConnect($mu_user);
9 years ago
}
8 years ago
return self::CanMethodConnect($user->method) >= 2 && self::CanProtocolConnect($user->protocol) >= 2 && self::CanObfsConnect($user->obfs) >= 2;
9 years ago
}
8 years ago
public static function SSRCanConnect($user, $mu_port = 0)
{
if ($mu_port != 0) {
8 years ago
$mu_user = User::where('port', '=', $mu_port)->where('is_multi_user', '<>', 0)->first();
9 years ago
if ($mu_user == null) {
return;
}
8 years ago
return self::SSRCanConnect($mu_user);
9 years ago
}
8 years ago
return self::CanMethodConnect($user->method) != 2 && self::CanProtocolConnect($user->protocol) != 2 && self::CanObfsConnect($user->obfs) != 2;
9 years ago
}
8 years ago
public static function getSSConnectInfo($user)
{
9 years ago
$new_user = clone $user;
8 years ago
if (self::CanObfsConnect($new_user->obfs) == 5) {
9 years ago
$new_user->obfs = 'plain';
$new_user->obfs_param = '';
}
8 years ago
if (self::CanProtocolConnect($new_user->protocol) == 3) {
9 years ago
$new_user->protocol = 'origin';
$new_user->protocol_param = '';
}
8 years ago
$new_user->obfs = str_replace('_compatible', '', $new_user->obfs);
$new_user->protocol = str_replace('_compatible', '', $new_user->protocol);
9 years ago
return $new_user;
}
8 years ago
public static function getSSRConnectInfo($user)
{
9 years ago
$new_user = clone $user;
8 years ago
if (self::CanObfsConnect($new_user->obfs) == 4) {
9 years ago
$new_user->obfs = 'plain';
$new_user->obfs_param = '';
}
8 years ago
$new_user->obfs = str_replace('_compatible', '', $new_user->obfs);
$new_user->protocol = str_replace('_compatible', '', $new_user->protocol);
9 years ago
return $new_user;
}
7 years ago
public static function getAllItems($user, $is_mu = 0, $is_ss = 0, $emoji = 0)
{
9 years ago
$return_array = array();
if ($user->is_admin) {
8 years ago
$nodes = Node::where(
static function ($query) {
$query->where('sort', 0)->orwhere('sort', 10);
9 years ago
}
)
7 years ago
->where('type', '1')
->orderBy('name')
->get();
9 years ago
} else {
8 years ago
$nodes = Node::where(
static function ($query) {
$query->where('sort', 0)->orwhere('sort', 10);
9 years ago
}
)
7 years ago
->where(
static function ($query) use ($user) {
$query->where('node_group', '=', $user->node_group)
->orWhere('node_group', '=', 0);
}
)
->where('type', '1')
->where('node_class', '<=', $user->class)
->orderBy('name')
->get();
9 years ago
}
if ($is_mu) {
9 years ago
if ($user->is_admin) {
8 years ago
if ($is_mu != 1) {
8 years ago
$mu_nodes = Node::where('sort', 9)->where('server', '=', $is_mu)->where('type', '1')->get();
} else {
8 years ago
$mu_nodes = Node::where('sort', 9)->where('type', '1')->get();
}
8 years ago
} elseif ($is_mu != 1) {
$mu_nodes = Node::where('sort', 9)->where('server', '=', $is_mu)->where('node_class', '<=', $user->class)->where('type', '1')->where(
static function ($query) use ($user) {
$query->where('node_group', '=', $user->node_group)
->orWhere('node_group', '=', 0);
}
)->get();
9 years ago
} else {
8 years ago
$mu_nodes = Node::where('sort', 9)->where('node_class', '<=', $user->class)->where('type', '1')->where(
static function ($query) use ($user) {
$query->where('node_group', '=', $user->node_group)
->orWhere('node_group', '=', 0);
}
)->get();
9 years ago
}
}
$relay_rules = Relay::where('user_id', $user->id)->orwhere('user_id', 0)->orderBy('id', 'asc')->get();
if (!Tools::is_protocol_relay($user)) {
$relay_rules = array();
}
foreach ($nodes as $node) {
if ($node->mu_only != 1 && $is_mu == 0) {
if ($node->sort == 10) {
$relay_rule_id = 0;
$relay_rule = Tools::pick_out_relay_rule($node->id, $user->port, $relay_rules);
8 years ago
if (($relay_rule != null) && $relay_rule->dist_node() != null) {
$relay_rule_id = $relay_rule->id;
9 years ago
}
7 years ago
$item = self::getItem($user, $node, 0, $relay_rule_id, $is_ss, $emoji);
if ($item != null) {
8 years ago
$return_array[] = $item;
9 years ago
}
} else {
7 years ago
$item = self::getItem($user, $node, 0, 0, $is_ss, $emoji);
if ($item != null) {
8 years ago
$return_array[] = $item;
9 years ago
}
}
}
if ($node->custom_rss == 1 && $node->mu_only != -1 && $is_mu != 0) {
9 years ago
foreach ($mu_nodes as $mu_node) {
7 years ago
if ($node->sort == 10) { // ss 中转
9 years ago
$relay_rule_id = 0;
$relay_rule = Tools::pick_out_relay_rule($node->id, $mu_node->server, $relay_rules);
8 years ago
if (($relay_rule != null) && $relay_rule->dist_node() != null) {
$relay_rule_id = $relay_rule->id;
9 years ago
}
7 years ago
$item = self::getItem($user, $node, $mu_node->server, $relay_rule_id, $is_ss, $emoji);
if ($item != null) {
8 years ago
$return_array[] = $item;
9 years ago
}
} else {
7 years ago
$item = self::getItem($user, $node, $mu_node->server, 0, $is_ss, $emoji);
if ($item != null) {
8 years ago
$return_array[] = $item;
9 years ago
}
}
}
}
}
9 years ago
return $return_array;
}
public static function getAllUrl($user, $is_mu, $is_ss = 0, $getV2rayPlugin = 1)
{
9 years ago
$return_url = '';
8 years ago
if (strtotime($user->expire_in) < time()) {
return $return_url;
}
$items = self::getAllItems($user, $is_mu, $is_ss);
foreach ($items as $item) {
8 years ago
$return_url .= self::getItemUrl($item, $is_ss) . PHP_EOL;
9 years ago
}
$is_mu = $is_mu == 0 ? 1 : 0;
$items = self::getAllItems($user, $is_mu, $is_ss);
foreach ($items as $item) {
8 years ago
$return_url .= self::getItemUrl($item, $is_ss) . PHP_EOL;
}
9 years ago
return $return_url;
}
7 years ago
/**
* 获取全部节点 Url
*
* @param object $user 用户
* @param int $is_ss 是否 ss
7 years ago
* @param int $getV2rayPlugin 是否获取 V2rayPlugin 节点
* @param array $Rule 节点筛选规则
* @param bool $find 是否筛选节点
*
* @return string
*/
7 years ago
public static function get_NewAllUrl($user, $is_ss, $getV2rayPlugin, $Rule, $find, $emoji = 0)
{
$return_url = '';
if (strtotime($user->expire_in) < time()) {
return $return_url;
}
if ($getV2rayPlugin === 0) {
$items = array_merge(
7 years ago
self::getAllItems($user, 0, $is_ss, $emoji),
self::getAllItems($user, 1, $is_ss, $emoji)
);
} else {
$items = array_merge(
7 years ago
self::getAllItems($user, 0, $is_ss, $emoji),
self::getAllItems($user, 1, $is_ss, $emoji),
self::getAllV2RayPluginItems($user, $emoji)
);
}
if ($find) {
foreach ($items as $item) {
7 years ago
$item = ConfController::getMatchProxy($item, $Rule);
if ($item !== null) {
7 years ago
$return_url .= self::getItemUrl($item, $is_ss) . PHP_EOL;
}
}
} else {
foreach ($items as $item) {
7 years ago
$return_url .= self::getItemUrl($item, $is_ss) . PHP_EOL;
}
}
return $return_url;
}
public static function getItemUrl($item, $is_ss)
{
9 years ago
$ss_obfs_list = Config::getSupportParam('ss_obfs');
if (!$is_ss) {
$ssurl = $item['address'] . ':' . $item['port'] . ':' . $item['protocol'] .
':' . $item['method'] . ':' . $item['obfs'] . ':' .
Tools::base64_url_encode(
$item['passwd']
) . '/?obfsparam=' . Tools::base64_url_encode(
$item['obfs_param']
) . '&protoparam=' . Tools::base64_url_encode(
$item['protocol_param']
) . '&remarks=' . Tools::base64_url_encode(
$item['remark']
) . '&group=' . Tools::base64_url_encode(
$item['group']
);
8 years ago
return 'ssr://' . Tools::base64_url_encode($ssurl);
}
if ($is_ss == 2) {
$personal_info = $item['method'] . ':' . $item['passwd'] . '@' . $item['address'] . ':' . $item['port'];
$ssurl = 'ss://' . Tools::base64_url_encode($personal_info);
7 years ago
$ssurl .= (Config::get('add_appName_to_ss_uri') === true
? '#' . rawurlencode(Config::get('appName') . ' - ' . $item['remark'])
: '#' . rawurlencode($item['remark']));
9 years ago
} else {
8 years ago
$personal_info = $item['method'] . ':' . $item['passwd'];
$ssurl = 'ss://' . Tools::base64_url_encode($personal_info) . '@' . $item['address'] . ':' . $item['port'];
$plugin = '';
if ($item['obfs'] == 'v2ray' || in_array($item['obfs'], $ss_obfs_list)) {
8 years ago
if (strpos($item['obfs'], 'http') !== false) {
$plugin .= 'obfs-local;obfs=http';
} elseif (strpos($item['obfs'], 'tls') !== false) {
$plugin .= 'obfs-local;obfs=tls';
} else {
$plugin .= 'v2ray;' . $item['obfs_param'];
9 years ago
}
8 years ago
if ($item['obfs_param'] != '' && $item['obfs'] != 'v2ray') {
$plugin .= ';obfs-host=' . $item['obfs_param'];
9 years ago
}
$ssurl .= '/?plugin=' . rawurlencode($plugin) . '&group=' . Tools::base64_url_encode(Config::get('appName'));
} else {
$ssurl .= '/?group=' . Tools::base64_url_encode(Config::get('appName'));
9 years ago
}
7 years ago
$ssurl .= (Config::get('add_appName_to_ss_uri') === true
? '#' . rawurlencode(Config::get('appName') . ' - ' . $item['remark'])
: '#' . rawurlencode($item['remark']));
9 years ago
}
8 years ago
return $ssurl;
9 years ago
}
/**
* 获取 V2RayPlugin 全部节点
*
* @param object $user 用户
*
* @return array
*/
7 years ago
public static function getAllV2RayPluginItems($user, $emoji = 0)
{
$return_array = array();
if ($user->is_admin) {
$nodes = Node::where('sort', 13)
->where('type', '1')
->orderBy('name')
->get();
} else {
$nodes = Node::where('sort', 13)
->where(
static function ($query) use ($user) {
$query->where('node_group', '=', $user->node_group)
->orWhere('node_group', '=', 0);
}
)
->where('type', '1')
->where('node_class', '<=', $user->class)
->orderBy('name')
->get();
}
foreach ($nodes as $node) {
7 years ago
$item = self::getV2RayPluginItem($user, $node, $emoji);
if ($item != null) {
$return_array[] = $item;
}
}
return $return_array;
}
/**
* 获取 V2RayPlugin 节点
*
* @param object $user 用户
* @param object $node 节点
*
* @return array
*/
7 years ago
public static function getV2RayPluginItem($user, $node, $emoji = 0)
{
// 非 AEAD 加密无法使用
if (!in_array($user->method, Config::getSupportParam('ss_aead_method'))) {
return null;
}
$return_array = Tools::ssv2Array($node->server);
7 years ago
$return_array['remark'] = ($emoji == 1
? Tools::addEmoji($node->name)
: $node->name);
$return_array['address'] = $return_array['add'];
$return_array['method'] = $user->method;
$return_array['passwd'] = $user->passwd;
$return_array['protocol'] = 'origin';
$return_array['protocol_param'] = '';
$return_array['obfs'] = 'v2ray';
if ($return_array['tls'] == 'tls' && $return_array['net'] == 'ws') {
$return_array['obfs_param'] = ('mode=ws;security=tls;path=' . $return_array['path'] .
';host=' . $return_array['host']);
} else {
$return_array['obfs_param'] = ('mode=ws;security=none;path=' . $return_array['path'] .
';host=' . $return_array['host']);
}
$return_array['path'] = ($return_array['path'] . '?redirect=' . $user->getMuMd5());
$return_array['class'] = $node->node_class;
$return_array['group'] = Config::get('appName');
return $return_array;
}
7 years ago
public static function getV2Url($user, $node, $arrout = 0, $emoji = 0)
{
$item = Tools::v2Array($node->server);
8 years ago
$item['v'] = '2';
7 years ago
$item['ps'] = ($emoji == 1
? Tools::addEmoji($node->name)
: $node->name);
$item['id'] = $user->getUuid();
$item['class'] = $node->node_class;
if ($arrout == 0) {
return 'vmess://' . base64_encode(
json_encode($item, 320)
);
}
8 years ago
return $item;
}
7 years ago
public static function getAllVMessUrl($user, $arrout = 0, $emoji = 0)
{
if ($user->is_admin) {
$nodes = Node::where(
static function ($query) {
$query->where('sort', 11)
->orwhere('sort', 12);
}
)
7 years ago
->where('type', '1')
->orderBy('name')
->get();
} else {
$nodes = Node::where(
static function ($query) {
$query->where('sort', 11)
->orwhere('sort', 12);
}
)->where(
static function ($query) use ($user) {
$query->where('node_group', '=', $user->node_group)
->orWhere('node_group', '=', 0);
}
)
7 years ago
->where('type', '1')
->where('node_class', '<=', $user->class)
->orderBy('name')
->get();
}
# 增加中转配置,后台目前配置user=0的话是自由门直接中转
$tmp_nodes = array();
foreach($nodes as $node){
$tmp_nodes[]=$node;
if ($node->sort==12){
$relay_rule = Relay::where('source_node_id', $node->id)->where(
static function ($query) use ($user) {
$query->Where('user_id', '=',0);
}
)->orderBy('priority', 'DESC')->orderBy('id')->first();
if ($relay_rule != null) {
//是中转起源节点
$tmp_node = $relay_rule->dist_node();
$server = explode(';', $tmp_node->server);
$source_server = Tools::v2Array($node->server);
if (count($server)<6){
$tmp_node->server.=str_repeat(";",6-count($server));
}
$tmp_node->server.="relayserver=".$source_server['add']."|"."outside_port=".$source_server['port'];
$tmp_node->name = $node->name."=>".$tmp_node->name;
$tmp_nodes[]=$tmp_node;
}
}
}
$nodes=$tmp_nodes;
if ($arrout == 0) {
8 years ago
$result = '';
foreach ($nodes as $node) {
7 years ago
$result .= (self::getV2Url($user, $node, $arrout, $emoji) . "\n");
}
} else {
$result = [];
foreach ($nodes as $node) {
7 years ago
$result[] = self::getV2Url($user, $node, $arrout, $emoji);
8 years ago
}
}
return $result;
}
public static function getAllSSDUrl($user)
{
8 years ago
if (!self::SSCanConnect($user)) {
return null;
}
8 years ago
$array_all = array();
8 years ago
$array_all['airport'] = Config::get('appName');
8 years ago
$array_all['port'] = $user->port;
$array_all['encryption'] = $user->method;
$array_all['password'] = $user->passwd;
$array_all['traffic_used'] = Tools::flowToGB($user->u + $user->d);
$array_all['traffic_total'] = Tools::flowToGB($user->transfer_enable);
$array_all['expiry'] = $user->class_expire;
$array_all['url'] = Config::get('subUrl') . LinkController::GenerateSSRSubCode($user->id, 0) . '?ssd=1';
8 years ago
$plugin_options = '';
8 years ago
if (strpos($user->obfs, 'http') != false) {
8 years ago
$plugin_options = 'obfs=http';
}
8 years ago
if (strpos($user->obfs, 'tls') != false) {
8 years ago
$plugin_options = 'obfs=tls';
}
if ($plugin_options != '') {
7 years ago
$array_all['plugin'] = 'simple-obfs'; //目前只支持这个
8 years ago
$array_all['plugin_options'] = $plugin_options;
if ($user->obfs_param != '') {
$array_all['plugin_options'] .= ';obfs-host=' . $user->obfs_param;
}
}
$nodes_muport = Node::where('type', 1)
->where('sort', '=', 9)
->orderBy('name')
->get();
8 years ago
$array_server = array();
$nodes = Node::where('type', 1)
->where('node_class', '<=', $user->class)
->where(
static function ($func) {
$func->where('sort', '=', 0)
->orwhere('sort', '=', 10)
->orwhere('sort', '=', 13);
}
)
->where(
static function ($func) use ($user) {
$func->where('node_group', '=', $user->node_group)
->orwhere('node_group', '=', 0);
}
)
->orderBy('name')
->get();
8 years ago
$server_index = 1;
foreach ($nodes as $node) {
8 years ago
$server = array();
if ($node->sort == 13) {
8 years ago
if (self::CanMethodConnect($user->method) != 2) {
continue;
}
$server = Tools::ssv2Array($node->server);
$server['server'] = $server['add'];
8 years ago
$server['id'] = $server_index;
$server['remarks'] = $node->name . ' - 单多' . $server['port'] . '端口';
8 years ago
$server['encryption'] = $user->method;
$server['password'] = $user->passwd;
$server['plugin'] = 'v2ray';
7 years ago
$server['path'] = ($server['path'] . '?redirect=' . $user->getMuMd5());
if ($server['tls'] == 'tls' && $server['net'] == 'ws') {
7 years ago
$server['obfs_param'] = ('mode=ws;security=tls;path=' . $server['path'] .
7 years ago
';host=' . $server['host']);
} else {
7 years ago
$server['obfs_param'] = ('mode=ws;security=none;path=' . $server['path'] .
7 years ago
';host=' . $server['host']);
}
8 years ago
$array_server[] = $server;
$server_index++;
continue;
} else {
$node_server = explode(';', $node->server);
$server['server'] = $node_server[0];
}
8 years ago
$server['id'] = $server_index;
//判断是否是中转起源节点
$relay_rule = Relay::where('source_node_id', $node->id)->where(
8 years ago
static function ($query) use ($user) {
$query->Where('user_id', '=', $user->id)
->orWhere('user_id', '=', 0);
}
)->orderBy('priority', 'DESC')->orderBy('id')->first();
if ($relay_rule != null) {
//是中转起源节点
8 years ago
$server['remarks'] = $node->name . ' => ' . $relay_rule->dist_node()->name;
$server['ratio'] = $node->traffic_rate + $relay_rule->dist_node()->traffic_rate;
8 years ago
$array_server[] = $server;
$server_index++;
continue;
}
//不是中转起源节点
8 years ago
$server['ratio'] = $node->traffic_rate;
//包含普通
if (($node->mu_only == 0 || $node->mu_only == -1) && $node->sort != 13) {
8 years ago
$server['remarks'] = $node->name;
8 years ago
$array_server[] = $server;
$server_index++;
}
//包含单多
if (($node->mu_only == 0 || $node->mu_only == 1) && $node->sort != 13) {
8 years ago
$nodes_muport = Node::where('type', '1')->where('sort', '=', 9)
8 years ago
->where(static function ($query) use ($user) {
$query->Where('node_group', '=', $user->group)
->orWhere('node_group', '=', 0);
})
->where('node_class', '<=', $user->class)
->orderBy('server')->get();
foreach ($nodes_muport as $node_muport) {
8 years ago
$muport_user = User::where('port', '=', $node_muport->server)->first();
8 years ago
if (!self::SSCanConnect($muport_user)) {
continue;
}
8 years ago
$server['id'] = $server_index;
$server['remarks'] = $node->name . ' - 单多' . $node_muport->server . '端口';
$server['port'] = $node_muport->server;
// 端口偏移
if (strpos($node->server, ';') !== false) {
$node_tmp = Tools::OutPort($node->server, $node->name, $node_muport->server);
$server['port'] = $node_tmp['port'];
$server['remarks'] = $node->name . ' - 单多' . $node_tmp['port'] . '端口';
}
8 years ago
$server['encryption'] = $muport_user->method;
$server['password'] = $muport_user->passwd;
7 years ago
$server['plugin'] = 'simple-obfs'; //目前只支持这个
8 years ago
$plugin_options = '';
8 years ago
if (strpos($muport_user->obfs, 'http') != false) {
8 years ago
$plugin_options = 'obfs=http';
}
8 years ago
if (strpos($muport_user->obfs, 'tls') != false) {
8 years ago
$plugin_options = 'obfs=tls';
}
8 years ago
$server['plugin_options'] = $plugin_options . ';obfs-host=' . $user->getMuMd5();
8 years ago
$array_server[] = $server;
$server_index++;
}
}
}
8 years ago
$array_all['servers'] = $array_server;
$json_all = json_encode($array_all);
8 years ago
return 'ssd://' . Tools::base64_url_encode($json_all);
}
8 years ago
public static function getJsonObfs($item)
{
9 years ago
$ss_obfs_list = Config::getSupportParam('ss_obfs');
8 years ago
$plugin = '';
if (in_array($item['obfs'], $ss_obfs_list)) {
8 years ago
if (strpos($item['obfs'], 'http') !== false) {
$plugin .= 'obfs-local --obfs http';
9 years ago
} else {
8 years ago
$plugin .= 'obfs-local --obfs tls';
9 years ago
}
if ($item['obfs_param'] != '') {
8 years ago
$plugin .= '--obfs-host ' . $item['obfs_param'];
9 years ago
}
}
return $plugin;
}
public static function getSurgeObfs($item)
{
9 years ago
$ss_obfs_list = Config::getSupportParam('ss_obfs');
8 years ago
$plugin = '';
if (in_array($item['obfs'], $ss_obfs_list)) {
8 years ago
if (strpos($item['obfs'], 'http') !== false) {
$plugin .= ', obfs=http';
} else {
$plugin .= ', obfs=tls';
9 years ago
}
if ($item['obfs_param'] != '') {
$plugin .= ', obfs-host=' . $item['obfs_param'];
} else {
$plugin .= ', obfs-host=wns.windows.com';
9 years ago
}
}
return $plugin;
}
8 years ago
9 years ago
/*
* Conn info
* address
* port
* passwd
* method
* remark
* protocol
* protocol_param
* obfs
* obfs_param
*/
7 years ago
public static function getItem($user, $node, $mu_port = 0, $relay_rule_id = 0, $is_ss = 0, $emoji = 0)
{
9 years ago
$relay_rule = Relay::where('id', $relay_rule_id)->where(
8 years ago
static function ($query) use ($user) {
$query->Where('user_id', '=', $user->id)
->orWhere('user_id', '=', 0);
9 years ago
}
)->orderBy('priority', 'DESC')->orderBy('id')->first();
9 years ago
$node_name = $node->name;
if ($relay_rule != null) {
8 years ago
$node_name .= ' - ' . $relay_rule->dist_node()->name;
9 years ago
}
if ($mu_port != 0) {
8 years ago
$mu_user = User::where('port', '=', $mu_port)->where('is_multi_user', '<>', 0)->first();
9 years ago
if ($mu_user == null) {
return;
}
$mu_user->obfs_param = $user->getMuMd5();
8 years ago
$mu_user->protocol_param = $user->id . ':' . $user->passwd;
9 years ago
$user = $mu_user;
$node_name .= ' - ' . $mu_port . ' 单端口';
9 years ago
}
if ($is_ss) {
8 years ago
if (!self::SSCanConnect($user)) {
9 years ago
return;
}
8 years ago
$user = self::getSSConnectInfo($user);
} else {
8 years ago
if (!self::SSRCanConnect($user)) {
9 years ago
return;
}
8 years ago
$user = self::getSSRConnectInfo($user);
9 years ago
}
$return_array['address'] = $node->server;
$return_array['port'] = $user->port;
$return_array['protocol'] = $user->protocol;
$return_array['protocol_param'] = $user->protocol_param;
$return_array['obfs'] = $user->obfs;
$return_array['obfs_param'] = $user->obfs_param;
if (strpos($node->server, ';') !== false) {
$node_tmp = Tools::OutPort($node->server, $node->name, $mu_port);
if ($mu_port != 0) {
$return_array['port'] = $node_tmp['port'];
$node_name = $node_tmp['name'];
7 years ago
}
$return_array['address'] = $node_tmp['address'];
}
9 years ago
$return_array['passwd'] = $user->passwd;
$return_array['method'] = $user->method;
7 years ago
$return_array['remark'] = ($emoji == 1
? Tools::addEmoji($node_name)
: $node_name);
$return_array['class'] = $node->node_class;
$return_array['group'] = Config::get('appName');
7 years ago
$return_array['ratio'] = ($relay_rule != null
? $node->traffic_rate + $relay_rule->dist_node()->traffic_rate
: $node->traffic_rate);
9 years ago
return $return_array;
}
public static function cloneUser($user)
{
8 years ago
return clone $user;
9 years ago
}
public static function getUserInfo($user, $type, $traffic_class_expire)
{
$return = '';
// 订阅信息
$info_array = (count(Config::get('sub_message')) != 0
? (array) Config::get('sub_message')
: []);
if ($traffic_class_expire !== 0) {
// 用户账户及流量信息
if (strtotime($user->expire_in) > time()) {
if ($user->transfer_enable == 0) {
$info_array[] = '剩余流量:0.00%';
} else {
$info_array[] = '剩余流量:' . number_format(($user->transfer_enable - $user->u - $user->d) / $user->transfer_enable * 100, 2) . '% ' . $user->unusedTraffic();
}
$info_array[] = '过期时间:' . $user->class_expire;
} else {
$info_array[] = '账户已过期,请续费后使用';
}
}
$group_name = Config::get('appName');
foreach ($info_array as $item) {
switch ($type) {
case 'ss':
$return .= 'ss://' . Tools::base64_url_encode('chacha20:breakwall') . '@www.google.com:10086' . '/?group=' . Tools::base64_url_encode($group_name) . '#' . rawurlencode($item) . PHP_EOL;
break;
case 'ssr':
$return .= 'ssr://' . Tools::base64_url_encode('www.google.com:10086:auth_chain_a:chacha20:tls1.2_ticket_auth:YnJlYWt3YWxs/?obfsparam=&protoparam=&remarks=' . Tools::base64_url_encode($item) . '&group=' . Tools::base64_url_encode($group_name)) . PHP_EOL;
break;
case 'v2ray':
$v2ray = ['v' => '2', 'ps' => $item, 'add' => 'www.google.com', 'port' => '10086', 'id' => '2661b5f8-8062-34a5-9371-a44313a75b6b', 'aid' => '2', 'net' => 'tcp', 'type' => 'none', 'host' => '', 'tls' => ''];
$return .= 'vmess://' . base64_encode(json_encode($v2ray, JSON_UNESCAPED_UNICODE)) . PHP_EOL;
break;
case 'quantumult_v2':
$quantumult_v2 = base64_encode($item . ' = vmess, ' . 'www.google.com' . ', ' . '10086' . ', chacha20-ietf-poly1305, "2661b5f8-8062-34a5-9371-a44313a75b6b", group=' . $group_name . '_v2') . PHP_EOL;
$return .= 'vmess://' . base64_encode($quantumult_v2) . PHP_EOL;
break;
}
}
return $return;
}
9 years ago
}