From af2a7d2a41c487a2f985fec8bb8357558a9fa82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=94=E9=9B=80?= <4700630-gtary@users.noreply.gitlab.com> Date: Wed, 8 Apr 2020 10:13:09 +0800 Subject: [PATCH] Support Trojan --- app/Command/Job.php | 6 ++ app/Controllers/Admin/NodeController.php | 5 +- app/Controllers/Mod_Mu/UserController.php | 11 ++-- app/Controllers/UserController.php | 4 +- app/Models/User.php | 10 +-- app/Utils/AppURI.php | 35 ++++++++++ app/Utils/URL.php | 71 ++++++++++++++++++++- resources/views/malio/admin/node/create.tpl | 1 + resources/views/malio/admin/node/edit.tpl | 1 + resources/views/malio/user/node.tpl | 2 +- resources/views/malio/user/nodeinfo.tpl | 35 +++++++++- 11 files changed, 163 insertions(+), 18 deletions(-) diff --git a/app/Command/Job.php b/app/Command/Job.php index bd16eb92..76385d58 100644 --- a/app/Command/Job.php +++ b/app/Command/Job.php @@ -36,6 +36,7 @@ use App\Models\Token; use App\Models\UnblockIp; use Exception; use RuntimeException; +use Ramsey\Uuid\Uuid; class Job { @@ -610,6 +611,11 @@ class Job $users = User::all(); foreach ($users as $user) { + $user->uuid = Uuid::uuid3( + Uuid::NAMESPACE_DNS, + strval($user->id) . '|' . $user->passwd + )->toString(); + $user->save(); if (($user->transfer_enable <= $user->u + $user->d || $user->enable == 0 || (strtotime($user->expire_in) < time() && strtotime($user->expire_in) > 644447105)) && RadiusBan::where( 'userid', $user->id diff --git a/app/Controllers/Admin/NodeController.php b/app/Controllers/Admin/NodeController.php index e184f075..431323aa 100644 --- a/app/Controllers/Admin/NodeController.php +++ b/app/Controllers/Admin/NodeController.php @@ -281,7 +281,7 @@ class NodeController extends AdminController }); $datatables->edit('outaddress', static function ($data) { - return (in_array($data['sort'], [0, 10, 11, 12, 13]) ? explode(';', $data['server'])[0] : ''); + return (in_array($data['sort'], [0, 10, 11, 12, 13, 14]) ? explode(';', $data['server'])[0] : ''); }); $datatables->edit('node_bandwidth', static function ($data) { @@ -322,6 +322,9 @@ class NodeController extends AdminController case 13: $sort = 'Shadowsocks - V2Ray-Plugin'; break; + case 14: + $sort = 'Trojan'; + break; default: $sort = '系统保留'; } diff --git a/app/Controllers/Mod_Mu/UserController.php b/app/Controllers/Mod_Mu/UserController.php index 49cb84a1..1ac16f67 100644 --- a/app/Controllers/Mod_Mu/UserController.php +++ b/app/Controllers/Mod_Mu/UserController.php @@ -131,19 +131,20 @@ class UserController extends BaseController $key_list = array('email', 'method', 'obfs', 'obfs_param', 'protocol', 'protocol_param', 'forbidden_ip', 'forbidden_port', 'node_speedlimit', 'disconnect_ip', - 'is_multi_user', 'id', 'port', 'passwd', 'u', 'd', 'node_connector'); + 'is_multi_user', 'id', 'port', 'passwd', 'u', 'd', 'node_connector', + 'sort', 'uuid'); $users = array(); foreach ($users_raw as $user_raw) { + $user_raw = Tools::keyFilter($user_raw, $key_list); + if ($node->sort == 14) { + $user_raw->sha224uuid = hash('sha224', $user_raw->uuid); + } if ($user_raw->transfer_enable > $user_raw->u + $user_raw->d) { - $user_raw = Tools::keyFilter($user_raw, $key_list); - $user_raw->uuid = $user_raw->getUuid(); $users[] = $user_raw; } else if (Config::get('keep_connect') === true) { // 流量耗尽用户限速至 1Mbps - $user_raw = Tools::keyFilter($user_raw, $key_list); - $user_raw->uuid = $user_raw->getUuid(); $user_raw->node_speedlimit = 1; $users[] = $user_raw; } diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 986ad03c..94c4d046 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -492,7 +492,7 @@ class UserController extends BaseController if ($log['node_id'] != $node->id) { continue; } - if (in_array($sort, array(0, 7, 8, 10, 11, 12, 13))) { + if (in_array($sort, array(0, 7, 8, 10, 11, 12, 13, 14))) { $array_node['online_user'] = $log['online_user']; } else { $array_node['online_user'] = -1; @@ -504,7 +504,7 @@ class UserController extends BaseController // 0: new node; -1: offline; 1: online $node_heartbeat = $node->node_heartbeat + 300; $array_node['online'] = -1; - if (!in_array($sort, array(0, 7, 8, 10, 11, 12, 13)) || $node_heartbeat == 300) { + if (!in_array($sort, array(0, 7, 8, 10, 11, 12, 13, 14)) || $node_heartbeat == 300) { $array_node['online'] = 0; } elseif ($node_heartbeat > time()) { $array_node['online'] = 1; diff --git a/app/Models/User.php b/app/Models/User.php index 0d9a0772..1ca2ea47 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -13,7 +13,6 @@ use App\Utils\GA; use App\Utils\QQWry; use App\Utils\Radius; use App\Utils\URL; -use Ramsey\Uuid\Uuid; use App\Models\DetectLog; use App\Models\DetectBanLog; @@ -140,10 +139,11 @@ class User extends Model public function getUuid() { - return Uuid::uuid3( - Uuid::NAMESPACE_DNS, - $this->attributes['id'] . '|' . $this->attributes['passwd'] - )->toString(); + // return Uuid::uuid3( + // Uuid::NAMESPACE_DNS, + // $this->attributes['id'] . '|' . $this->attributes['passwd'] + // )->toString(); + return $this->attributes['uuid']; } /* diff --git a/app/Utils/AppURI.php b/app/Utils/AppURI.php index 716f43b3..5ee5b535 100644 --- a/app/Utils/AppURI.php +++ b/app/Utils/AppURI.php @@ -38,6 +38,9 @@ class AppURI : ''); $return = $item['remark'] . ' = vmess, ' . $item['add'] . ', ' . $item['port'] . ', username = ' . $item['id'] . $ws . $tls; break; + case 'trojan': + $return = ($item['remark'] . ' = trojan, ' . $item['address'] . ', ' . $item['port'] . ', password=' . $item['passwd']) . ", sni=" . $item['host']; + break; } break; } @@ -145,6 +148,12 @@ class AppURI } $return .= (', tag=' . $item['remark']); break; + case 'trojan': + // ;trojan=example.com:443, password=pwd, over-tls=true, tls-verification=true, fast-open=false, udp-relay=false, tag=trojan-tls-01 + $return = ('trojan=' . $item['address'] . ':' . $item['port'] . ', password=' . $item['passwd'] . ', tls-host=' . $item['host']); + $return .= ', over-tls=true, tls-verification=true'; + $return .= (', tag=' . $item['remark']); + break; } return $return; } @@ -267,6 +276,16 @@ class AppURI } } break; + case 'trojan': + $return = [ + 'name' => $item['remark'], + 'type' => 'trojan', + 'server' => $item['address'], + 'port' => $item['port'], + 'password' => $item['passwd'], + 'sni' => $item['host'] + ]; + break; } return $return; } @@ -344,6 +363,10 @@ class AppURI } $return = ('vmess://' . Tools::base64_url_encode('chacha20-poly1305:' . $item['id'] . '@' . $item['add'] . ':' . $item['port']) . '?remarks=' . rawurlencode($item['remark']) . $obfs . $tls); break; + case 'trojan': + $return = ('trojan://' . $item['passwd'] . '@' . $item['address'] . ':' . $item['port']); + $return .= ('?peer=' . $item['host'] . '#' . rawurlencode($item['remark'])); + break; } return $return; } @@ -481,4 +504,16 @@ class AppURI } return $return; } + + public static function getTrojanURI(array $item) + { + $return = null; + switch ($item['type']) { + case 'trojan': + $return = ('trojan://' . $item['passwd'] . '@' . $item['address'] . ':' . $item['port']); + $return .= ('?peer=' . $item['host'] . '#' . rawurlencode($item['remark'])); + break; + } + return $return; + } } diff --git a/app/Utils/URL.php b/app/Utils/URL.php index 1a446a23..4533ce8f 100644 --- a/app/Utils/URL.php +++ b/app/Utils/URL.php @@ -265,9 +265,12 @@ class URL case 'vmess': $sort = [11, 12]; break; + case 'trojan': + $sort = [14]; + break; default: $Rule['type'] = 'all'; - $sort = [0, 10, 11, 12, 13]; + $sort = [0, 10, 11, 12, 13, 14]; break; } if ($user->is_admin) { @@ -296,7 +299,7 @@ class URL $nodes = $node_query->orderBy('name')->get(); } $return_array = array(); - if ($is_mu != 0 && $Rule['type'] != 'vmess') { + if ($is_mu != 0 && $Rule['type'] != 'vmess' && $Rule['type'] != 'trojan') { $mu_node_query = Node::query(); $mu_node_query->where('sort', 9)->where('type', '1'); if ($user->is_admin) { @@ -359,6 +362,17 @@ class URL } continue; } + if (in_array($node->sort, [14]) && (($Rule['type'] == 'all' && $x == 0) || ($Rule['type'] == 'trojan'))) { + // Trojan + $item = self::getTrojanItem($user, $node, $emoji); + if ($item != null) { + $find = (isset($Rule['content']['regex']) && $Rule['content']['regex'] != '' ? ConfController::getMatchProxy($item, ['content' => ['regex' => $Rule['content']['regex']]]) : true); + if ($find) { + $return_array[] = $item; + } + } + continue; + } if (in_array($node->sort, [0, 10]) && $node->mu_only != 1 && ($is_mu == 0 || ($is_mu != 0 && Config::get('mergeSub') === true))) { // 节点非只启用单端口 && 只获取普通端口 if ($node->sort == 10) { @@ -442,6 +456,59 @@ class URL return $return_array; } + + /** + * 获取 Trojan 全部节点 + * + * @param User $user 用户 + * @param bool $emoji + * + * @return array + */ + public static function getAllTrojan($user, $emoji = false) + { + $return_array = array(); + $nodes = Node::where('sort', 14) + ->where('type', '1') + ->orderBy('name') + ->get(); + foreach ($nodes as $node) { + $item = self::getTrojanItem($user, $node, $emoji); + if ($item != null) { + $return_array[] = $item; + } + } + return $return_array; + } + /** + * Trojan 节点 + * + * @param User $user 用户 + * @param Node $node + * @param bool $emoji + * + * @return array + */ + public static function getTrojanItem($user, $node, $emoji = false) + { + $server = explode(';', $node->server); + $opt = []; + if (isset($server[1])) { + $opt = self::parse_args($server[1]); + } + $item['remark'] = ($emoji == true ? Tools::addEmoji($node->name) : $node->name); + $item['type'] = 'trojan'; + $item['address'] = $server[0]; + $item['port'] = (isset($opt['port']) ? (int) $opt['port'] : 443); + $item['passwd'] = $user->uuid; + $item['host'] = $item['address']; + if (isset($opt['host'])) { + $item['host'] = $opt['host']; + } + return $item; + } + + public static function getAllUrl($user, $is_mu, $is_ss = 0, $getV2rayPlugin = 1) { $return_url = ''; diff --git a/resources/views/malio/admin/node/create.tpl b/resources/views/malio/admin/node/create.tpl index 54026882..1c2ae343 100644 --- a/resources/views/malio/admin/node/create.tpl +++ b/resources/views/malio/admin/node/create.tpl @@ -112,6 +112,7 @@ + diff --git a/resources/views/malio/admin/node/edit.tpl b/resources/views/malio/admin/node/edit.tpl index c1368ebd..9c04fc88 100644 --- a/resources/views/malio/admin/node/edit.tpl +++ b/resources/views/malio/admin/node/edit.tpl @@ -119,6 +119,7 @@ + diff --git a/resources/views/malio/user/node.tpl b/resources/views/malio/user/node.tpl index 28f09b04..7b05e68f 100644 --- a/resources/views/malio/user/node.tpl +++ b/resources/views/malio/user/node.tpl @@ -79,7 +79,7 @@ {$region = substr($node['name'],0,6)} image
-
{current(explode(" - ", $node['name']))}
+
{current(explode(" - ", $node['name']))}
{$node['info']}
diff --git a/resources/views/malio/user/nodeinfo.tpl b/resources/views/malio/user/nodeinfo.tpl index 353bd486..d47f5cd3 100755 --- a/resources/views/malio/user/nodeinfo.tpl +++ b/resources/views/malio/user/nodeinfo.tpl @@ -64,7 +64,11 @@
+ {if $node->sort == 14} +
+
+
+ +
+
+
+
+ {$sort14Node = URL::getTrojanItem($user, $node, false)} +

服务器地址:{$sort14Node['address']}

+

服务器端口:{$sort14Node['port']}

+

密码:{$sort14Node['passwd']}

+ {if $sort14Node['host'] != $sort14Node['address']} +

HOST&PEER:{$sort14Node['host']}

+ {/if} +
+
+
+
+
+ {elseif in_array("ssr",$malio_config['support_sub_type']) }
{if URL::SSRCanConnect($user, $mu)} @@ -364,6 +394,7 @@
{/foreach}
+ {/if}
@@ -422,4 +453,4 @@ - \ No newline at end of file +