Skip to content

Commit

Permalink
Vnet订阅适配+Trojan订阅
Browse files Browse the repository at this point in the history
  • Loading branch information
ZBrettonYe committed Jul 18, 2020
1 parent 335a717 commit 692d466
Show file tree
Hide file tree
Showing 8 changed files with 574 additions and 457 deletions.
99 changes: 62 additions & 37 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,69 +139,94 @@ public function uploadFile(UploadedFile $file): string {
* @return string
*/
public function getUserNodeInfo($uid, $nodeId, $infoType): string {
$user = User::whereId($uid)->first();
$node = SsNode::whereId($nodeId)->first();
$user = User::whereId($uid)->firstOrFail();
$node = SsNode::whereId($nodeId)->firstOrFail();
$scheme = null;
// 获取分组名称
$group = $node->getLevel->name;
$host = $node->server?: $node->ip;
$data = null;
switch($node->type){
case 2:
// 生成v2ray scheme
if($infoType !== 1){
// 生成v2ray scheme
$data = $this->v2raySubUrl($node->name, $host, $node->v2_port, $user->vmess_id, $node->v2_alter_id,
$node->v2_net, $node->v2_type, $node->v2_host, $node->v2_path, $node->v2_tls? "tls" : "");
}else{
$data = "服务器:".$host.PHP_EOL."IPv6:".($node->ipv6?: "").PHP_EOL."端口:".$node->v2_port.PHP_EOL."加密方式:".$node->v2_method.PHP_EOL."用户ID:".$user->vmess_id.PHP_EOL."额外ID:".$node->v2_alter_id.PHP_EOL."传输协议:".$node->v2_net.PHP_EOL."伪装类型:".$node->v2_type.PHP_EOL."伪装域名:".($node->v2_host?: "").PHP_EOL."路径:".($node->v2_path?: "").PHP_EOL."TLS:".($node->v2_tls? "tls" : "").PHP_EOL;
}
break;
case 3:
if($infoType !== 1){
$data = $this->trojanSubUrl($user->passwd, $host, $node->port, $node->name);
}else{
$data = "备注:".$node->name.PHP_EOL."服务器:".$host.PHP_EOL."密码:".$user->passwd.PHP_EOL."端口:".$node->port.PHP_EOL;
}
break;
case 1:
case 4:
$protocol = $node->protocol;
$method = $node->method;
$obfs = $node->obfs;
if($node->single){
$port = $node->port;
$protocol = $node->protocol;
$method = $node->method;
$obfs = $node->obfs;
$passwd = $node->passwd;
$protocol_param = $user->port.':'.$user->passwd;
}else{
$port = $user->port;
$protocol = $user->protocol;
$method = $user->method;
$obfs = $user->obfs;
$passwd = $user->passwd;
$protocol_param = $node->protocol_param;
if($node->type === 1){
$protocol = $user->protocol;
$method = $user->method;
$obfs = $user->obfs;
}
}

if($infoType != 1){
if($infoType !== 1){
// 生成ss/ssr scheme
if($node->compatible){
$data = 'ss://'.base64url_encode($method.':'.$passwd.'@'.$host.':'.$port).'#'.$group;
}else{
$data = 'ssr://'.base64url_encode($host.':'.$port.':'.$protocol.':'.$method.':'.$obfs.':'.base64url_encode($passwd).'/?obfsparam='.base64url_encode($node->obfs_param).'&protoparam='.base64url_encode($protocol_param).'&remarks='.base64url_encode($node->name).'&group='.base64url_encode($group).'&udpport=0&uot=0');
}
$data = $node->compatible? $this->ssSubUrl($host, $port, $method, $passwd,
$group) : $this->ssrSubUrl($host, $port, $protocol, $method, $obfs, $passwd, $node->obfs_param,
$protocol_param, $node->name, $group, $node->is_udp);
}else{
// 生成文本配置信息
$data = "服务器:".$host.PHP_EOL."IPv6:".$node->ipv6.PHP_EOL."服务器端口:".$port.PHP_EOL."密码:".$passwd.PHP_EOL."加密:".$method.PHP_EOL.($node->compatible? '' : "协议:".$protocol.PHP_EOL."协议参数:".$protocol_param.PHP_EOL."混淆:".$obfs.PHP_EOL."混淆参数:".$node->obfs_param.PHP_EOL);
}
break;
case 2:
// 生成v2ray scheme
if($infoType != 1){
// 生成v2ray scheme
$data = 'vmess://'.base64url_encode(json_encode([
"v" => "2",
"ps" => $node->name,
"add" => $host,
"port" => $node->v2_port,
"id" => $user->vmess_id,
"aid" => $node->v2_alter_id,
"net" => $node->v2_net,
"type" => $node->v2_type,
"host" => $node->v2_host,
"path" => $node->v2_path,
"tls" => $node->v2_tls? "tls" : ""
], JSON_PRETTY_PRINT));
}else{
$data = "服务器:".$host.PHP_EOL."IPv6:".($node->ipv6?: "").PHP_EOL."端口:".$node->v2_port.PHP_EOL."加密方式:".$node->v2_method.PHP_EOL."用户ID:".$user->vmess_id.PHP_EOL."额外ID:".$node->v2_alter_id.PHP_EOL."传输协议:".$node->v2_net.PHP_EOL."伪装类型:".$node->v2_type.PHP_EOL."伪装域名:".($node->v2_host?: "").PHP_EOL."路径:".($node->v2_path?: "").PHP_EOL."TLS:".($node->v2_tls? "tls" : "").PHP_EOL;
}
break;
case 3:
break;
default:
}

return $data;
}

public function v2raySubUrl($name, $host, $port, $uuid, $alter_id, $net, $type, $domain, $path, $tls): string {
return 'vmess://'.base64url_encode(json_encode([
"v" => "2",
"ps" => $name,
"add" => $host,
"port" => $port,
"id" => $uuid,
"aid" => $alter_id,
"net" => $net,
"type" => $type,
"host" => $domain,
"path" => $path,
"tls" => $tls? "tls" : ""
], JSON_PRETTY_PRINT));
}

public function trojanSubUrl($password, $domain, $port, $remark): string {
return 'trojan://'.urlencode($password).'@'.$domain.':'.$port.'#'.urlencode($remark);
}

public function ssSubUrl($host, $port, $method, $passwd, $group): string {
return 'ss://'.base64url_encode($method.':'.$passwd.'@'.$host.':'.$port).'#'.$group;
}

public function ssrSubUrl(
$host, $port, $protocol, $method, $obfs, $passwd, $obfs_param, $protocol_param, $name, $group, $is_udp
): string {
return 'ssr://'.base64url_encode($host.':'.$port.':'.$protocol.':'.$method.':'.$obfs.':'.base64url_encode($passwd).'/?obfsparam='.base64url_encode($obfs_param).'&protoparam='.base64url_encode($protocol_param).'&remarks='.base64url_encode($name).'&group='.base64url_encode($group).'&udpport='.$is_udp.'&uot=0');
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/User/SubscribeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function getSubscribeByCode(Request $request, $code) {
// 获取这个账号可用节点
$query = SsNode::query()->whereStatus(1)->whereIsSubscribe(1)->where('level', '<=', $user->level);

if($this->subType){
if($this->subType === 1){
$query = $query->whereIn('type', [1, 4]);
}elseif($this->subType){
$query = $query->whereType($this->subType);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public function buy($goods_id) {
public function help(): \Illuminate\Http\Response {
//$view['articleList'] = Article::type(1)->orderByDesc('sort')->orderByDesc('id')->limit(10)->paginate(5);
$data = [];
if(SsNode::query()->whereType(1)->whereStatus(1)->exists()){
if(SsNode::query()->whereIn('type',[1,4])->whereStatus(1)->exists()){
$data[] = 'ss';
//array_push
}
Expand Down
73 changes: 73 additions & 0 deletions database/migrations/2020_07_18_032504_create_node_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNodeTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('node', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedTinyInteger('type')->default(1)->comment('服务类型:1-Shadowsocks(R)、2-V2ray、3-Trojan、4-VNet');
$table->string('name', 128)->comment('名称');
$table->char('country_code', 5)->default('un')->comment('国家代码');
$table->string('server')->nullable()->comment('服务器域名地址');
$table->char('ip', 15)->nullable()->comment('服务器IPV4地址');
$table->ipAddress('ipv6')->nullable()->comment('服务器IPV6地址');
$table->string('relay_server')->nullable()->comment('中转地址');
$table->unsignedSmallInteger('relay_port')->nullable()->default(0)->comment('中转端口');
$table->unsignedTinyInteger('level')->default(0)->comment('等级:0-无等级,全部可见');
$table->unsignedBigInteger('speed_limit')->default(0)->comment('节点限速,为0表示不限速,单位Byte');
$table->unsignedSmallInteger('client_limit')->default(0)->comment('设备数限制');
$table->string('description')->nullable()->comment('节点简单描述');
$table->string('method', 32)->default('aes-256-cfb')->comment('加密方式');
$table->string('protocol', 64)->default('origin')->comment('协议');
$table->string('protocol_param', 128)->nullable()->comment('协议参数');
$table->string('obfs', 64)->default('plain')->comment('混淆');
$table->string('obfs_param')->nullable()->comment('混淆参数');
$table->unsignedDecimal('traffic_rate', 6)->default(1.00)->comment('流量比率');
$table->boolean('is_subscribe')->default(1)->comment('是否允许用户订阅该节点:0-否、1-是');
$table->boolean('is_ddns')->default(0)->comment('是否使用DDNS:0-否、1-是');
$table->boolean('is_relay')->default(0)->comment('是否中转节点:0-否、1-是');
$table->boolean('is_udp')->default(1)->comment('是否启用UDP:0-不启用、1-启用');
$table->unsignedSmallInteger('push_port')->default(0)->comment('消息推送端口');
$table->boolean('detection_type')->default(1)->comment('节点检测: 0-关闭、1-只检测TCP、2-只检测ICMP、3-检测全部');
$table->boolean('compatible')->default(0)->comment('兼容SS');
$table->boolean('single')->default(0)->comment('启用单端口功能:0-否、1-是');
$table->unsignedSmallInteger('port')->nullable()->comment('单端口的端口号或连接端口号');
$table->string('passwd')->nullable()->comment('单端口的连接密码');
$table->unsignedTinyInteger('sort')->default(0)->comment('排序值,值越大越靠前显示');
$table->boolean('status')->default(1)->comment('状态:0-维护、1-正常');
$table->unsignedSmallInteger('v2_alter_id')->default(16)->comment('V2Ray额外ID');
$table->unsignedSmallInteger('v2_port')->default(0)->comment('V2Ray服务端口');
$table->string('v2_method', 32)->default('aes-128-gcm')->comment('V2Ray加密方式');
$table->string('v2_net', 16)->default('tcp')->comment('V2Ray传输协议');
$table->string('v2_type', 32)->default('none')->comment('V2Ray伪装类型');
$table->string('v2_host')->comment('V2Ray伪装的域名');
$table->string('v2_path')->comment('V2Ray的WS/H2路径');
$table->boolean('v2_tls')->default(0)->comment('V2Ray后端TLS:0-未开启、1-开启');
$table->text('tls_provider')->nullable()->comment('V2Ray节点的TLS提供商授权信息');
$table->timestamps();
$table->index('is_subscribe', 'idx_sub');
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});

DB::statement("ALTER TABLE `node` comment '节点信息表'");
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('node');
}
}
Loading

0 comments on commit 692d466

Please sign in to comment.