Skip to content

Commit

Permalink
Support Trojan
Browse files Browse the repository at this point in the history
  • Loading branch information
孔雀 committed Apr 8, 2020
1 parent 493d023 commit af2a7d2
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 18 deletions.
6 changes: 6 additions & 0 deletions app/Command/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use App\Models\UnblockIp;
use Exception;
use RuntimeException;
use Ramsey\Uuid\Uuid;

class Job
{
Expand Down Expand Up @@ -610,6 +611,11 @@ public static function CheckJob()

$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
Expand Down
5 changes: 4 additions & 1 deletion app/Controllers/Admin/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function ajax($request, $response, $args)
});

$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) {
Expand Down Expand Up @@ -322,6 +322,9 @@ public function ajax($request, $response, $args)
case 13:
$sort = 'Shadowsocks - V2Ray-Plugin';
break;
case 14:
$sort = 'Trojan';
break;
default:
$sort = '系统保留';
}
Expand Down
11 changes: 6 additions & 5 deletions app/Controllers/Mod_Mu/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,20 @@ static function ($query1) use ($node) {

$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;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function node($request, $response, $args)
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;
Expand All @@ -504,7 +504,7 @@ public function node($request, $response, $args)
// 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;
Expand Down
10 changes: 5 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Utils\QQWry;
use App\Utils\Radius;
use App\Utils\URL;
use Ramsey\Uuid\Uuid;
use App\Models\DetectLog;
use App\Models\DetectBanLog;

Expand Down Expand Up @@ -140,10 +139,11 @@ public function addInviteCode()

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'];
}

/*
Expand Down
35 changes: 35 additions & 0 deletions app/Utils/AppURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static function getSurgeURI($item, $version)
: '');
$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;
}
Expand Down Expand Up @@ -145,6 +148,12 @@ public static function getQuantumultXURI($item)
}
$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;
}
Expand Down Expand Up @@ -267,6 +276,16 @@ public static function getClashURI($item, $ssr_support = false)
}
}
break;
case 'trojan':
$return = [
'name' => $item['remark'],
'type' => 'trojan',
'server' => $item['address'],
'port' => $item['port'],
'password' => $item['passwd'],
'sni' => $item['host']
];
break;
}
return $return;
}
Expand Down Expand Up @@ -344,6 +363,10 @@ public static function getShadowrocketURI($item)
}
$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;
}
Expand Down Expand Up @@ -481,4 +504,16 @@ public static function getSSJSON($item)
}
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;
}
}
71 changes: 69 additions & 2 deletions app/Utils/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,12 @@ public static function getNew_AllItems($user, $Rule)
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) {
Expand Down Expand Up @@ -296,7 +299,7 @@ static function ($query) use ($user) {
$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) {
Expand Down Expand Up @@ -359,6 +362,17 @@ static function ($query) use ($user) {
}
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) {
Expand Down Expand Up @@ -442,6 +456,59 @@ static function ($query) use ($user) {
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 = '';
Expand Down
1 change: 1 addition & 0 deletions resources/views/malio/admin/node/create.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<option value="11">V2Ray</option>
<option value="12">V2Ray 中转</option>
<option value="13">Shadowsocks V2Ray-Plugin</option>
<option value="14">Trojan</option>
</select>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions resources/views/malio/admin/node/edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<option value="13" {if $node->sort==13}selected{/if}>Shadowsocks
V2Ray-Plugin
</option>
<option value="14" {if $node->sort==14}selected{/if}>Trojan</option>
</select>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/malio/user/node.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
{$region = substr($node['name'],0,6)}
<img alt="image" class="mr-3 rounded-circle" width="50" src="/theme/malio/assets/modules/flag-icon-css/flags/1x1/{if $malio_config['flag_mode']=='node-name'}{if $flags[$region] != ''}{$flags[$region]}{else}un{/if}{else}{$node['status']}{/if}.svg">
<div class="media-body">
<div class="media-title node-status {if $node['online']=='1'}node-is-online{else}node-is-offline{/if}">{current(explode(" - ", $node['name']))}</div>
<div class="media-title node-status {if $node['online']=='1' or $node['sort'] == 14}node-is-online{else}node-is-offline{/if}">{current(explode(" - ", $node['name']))}</div>
<div class=" text-job text-muted">{$node['info']}</div>
</div>
<div class="media-items">
Expand Down
35 changes: 33 additions & 2 deletions resources/views/malio/user/nodeinfo.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@
<div id="app">
<div class="main-wrapper">
<ul class="nav nav-tabs" id="myTab" role="tablist">
{if (in_array("ssr",$malio_config['support_sub_type']))}
{if $node->sort == 14}
<li class="nav-item">
<a class="nav-link" id="trojan-tab" data-toggle="tab" href="#trojan" role="tab" aria-controls="trojan" aria-selected="true">Trojan</a>
</li>
{elseif (in_array("ssr",$malio_config['support_sub_type']))}
{if $node->mu_only != 1}
<li class="nav-item">
<a class="nav-link active" id="ssr-tab" data-toggle="tab" href="#ssr" role="tab" aria-controls="ssr" aria-selected="true">ShadowsocksR</a>
Expand All @@ -91,6 +95,32 @@
{/if}
</ul>
<div class="tab-content" id="myTabContent">
{if $node->sort == 14}
<div class="tab-pane fade active show" id="trojan" role="tabpanel" aria-labelledby="trojan-tab">
<div class="row mt-2">
<div class="col-12 col-sm-3 col-md-3">
<ul class="nav nav-pills flex-column" id="myTab4" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="trojan-info-tab" data-toggle="tab" href="#trojan-info" role="tab" aria-controls="trojan-info" aria-selected="true">信息</a>
</li>
</ul>
</div>
<div class="col-12 col-sm-9 col-md-9">
<div class="tab-content no-padding" id="myTab2Content">
<div class="tab-pane fade active show" id="trojan-info" role="tabpanel" aria-labelledby="trojan-info-tab">
{$sort14Node = URL::getTrojanItem($user, $node, false)}
<p>服务器地址:<code class="card-tag tag-blue">{$sort14Node['address']}</code></p>
<p>服务器端口:<code class="card-tag tag-volcano">{$sort14Node['port']}</code></p>
<p>密码:<code class="card-tag tag-geekblue">{$sort14Node['passwd']}</code></p>
{if $sort14Node['host'] != $sort14Node['address']}
<p>HOST&PEER:<code class="card-tag tag-green">{$sort14Node['host']}</code></p>
{/if}
</div>
</div>
</div>
</div>
</div>
{elseif in_array("ssr",$malio_config['support_sub_type']) }
<div class="tab-pane fade {if $node->mu_only != 1}active show{/if}" id="ssr" role="tabpanel" aria-labelledby="ssr-tab">
<div class="row mt-2">
{if URL::SSRCanConnect($user, $mu)}
Expand Down Expand Up @@ -364,6 +394,7 @@
</div>
{/foreach}
</div>
{/if}
</div>
</div>
Expand Down Expand Up @@ -422,4 +453,4 @@
</body>
</html>
</html>

0 comments on commit af2a7d2

Please sign in to comment.