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/Controllers/LinkController.php

682 lines
26 KiB
PHTML

9 years ago
<?php
//Thanks to http://blog.csdn.net/jollyjumper/article/details/9823047
namespace App\Controllers;
use App\Models\Link;
use App\Models\User;
use App\Models\Node;
use App\Models\Relay;
use App\Models\Smartline;
use App\Utils\ConfRender;
9 years ago
use App\Utils\Tools;
use App\Utils\URL;
use App\Services\Config;
/**
* HomeController
*/
class LinkController extends BaseController
{
public function __construct()
{
}
public static function GenerateRandomLink()
{
$i =0;
for ($i = 0; $i < 10; $i++) {
$token = Tools::genRandomChar(16);
$Elink = Link::where("token", "=", $token)->first();
if ($Elink == null) {
return $token;
}
}
return "couldn't alloc token";
}
public static function GenerateAclCode($address, $port, $userid, $geo, $method)
{
$Elink = Link::where("type", "=", 9)->where("address", "=", $address)->where("port", "=", $port)->where("userid", "=", $userid)->where("geo", "=", $geo)->where("method", "=", $method)->first();
if ($Elink != null) {
return $Elink->token;
}
$NLink = new Link();
$NLink->type = 9;
$NLink->address = $address;
$NLink->port = $port;
$NLink->ios = 0;
$NLink->geo = $geo;
$NLink->method = $method;
$NLink->userid = $userid;
$NLink->token = LinkController::GenerateRandomLink();
$NLink->save();
return $NLink->token;
}
public static function GenerateRouterCode($userid, $without_mu)
{
$Elink = Link::where("type", "=", 10)->where("userid", "=", $userid)->where("geo", $without_mu)->first();
if ($Elink != null) {
return $Elink->token;
}
$NLink = new Link();
$NLink->type = 10;
$NLink->address = "";
$NLink->port = 0;
$NLink->ios = 0;
$NLink->geo = $without_mu;
$NLink->method = "";
$NLink->userid = $userid;
$NLink->token = LinkController::GenerateRandomLink();
$NLink->save();
return $NLink->token;
}
public static function GenerateSSRSubCode($userid, $without_mu)
{
$Elink = Link::where("type", "=", 11)->where("userid", "=", $userid)->where("geo", $without_mu)->first();
if ($Elink != null) {
return $Elink->token;
}
$NLink = new Link();
$NLink->type = 11;
$NLink->address = "";
$NLink->port = 0;
$NLink->ios = 0;
$NLink->geo = $without_mu;
$NLink->method = "";
$NLink->userid = $userid;
$NLink->token = LinkController::GenerateRandomLink();
$NLink->save();
return $NLink->token;
}
public static function GetContent($request, $response, $args)
{
$token = $args['token'];
//$builder->getPhrase();
$Elink = Link::where("token", "=", $token)->first();
if ($Elink == null) {
return null;
}
switch ($Elink->type) {
case 9:
$newResponse = $response->withHeader('Content-type', ' application/octet-stream; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')->withHeader('Content-Disposition', ' attachment; filename='.$token.'.acl');//->getBody()->write($builder->output());
$newResponse->getBody()->write(LinkController::GetAcl(User::where("id", "=", $Elink->userid)->first()));
return $newResponse;
case 10:
$user=User::where("id", $Elink->userid)->first();
if ($user == null) {
return null;
}
$is_ss = 0;
if (isset($request->getQueryParams()["is_ss"])) {
$is_ss = $request->getQueryParams()["is_ss"];
}
$newResponse = $response->withHeader('Content-type', ' application/octet-stream; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')->withHeader('Content-Disposition', ' attachment; filename='.$token.'.sh');//->getBody()->write($builder->output());
$newResponse->getBody()->write(LinkController::GetRouter(User::where("id", "=", $Elink->userid)->first(), $Elink->geo, $is_ss));
return $newResponse;
case 11:
$user=User::where("id", $Elink->userid)->first();
if ($user == null) {
return null;
}
$max = 0;
if (isset($request->getQueryParams()["max"])) {
$max = (int)$request->getQueryParams()["max"];
}
$mu = 0;
if (isset($request->getQueryParams()["mu"])) {
$mu = (int)$request->getQueryParams()["mu"];
}
$newResponse = $response->withHeader('Content-type', ' application/octet-stream; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')->withHeader('Content-Disposition', ' attachment; filename='.$token.'.txt');
$newResponse->getBody()->write(LinkController::GetSSRSub(User::where("id", "=", $Elink->userid)->first(), $mu, $max));
9 years ago
return $newResponse;
default:
break;
}
$newResponse = $response->withHeader('Content-type', ' application/x-ns-proxy-autoconfig; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate');//->getBody()->write($builder->output());
$newResponse->getBody()->write(LinkController::GetPac($type, $Elink->address, $Elink->port, User::where("id", "=", $Elink->userid)->first()->pac));
return $newResponse;
}
public static function GetGfwlistJs($request, $response, $args)
{
$newResponse = $response->withHeader('Content-type', ' application/x-ns-proxy-autoconfig; charset=utf-8')->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate')->withHeader('Content-Disposition', ' attachment; filename=gfwlist.js');
;//->getBody()->write($builder->output());
$newResponse->getBody()->write(LinkController::GetMacPac());
return $newResponse;
}
public static function GetPcConf($user, $is_mu = 0, $is_ss = 0)
{
if ($is_ss==0) {
9 years ago
$string='
{
"index" : 0,
"random" : false,
"sysProxyMode" : 0,
"shareOverLan" : false,
"bypassWhiteList" : false,
"localPort" : 1080,
"localAuthPassword" : "'.Tools::genRandomChar(26).'",
"dns_server" : "",
"reconnectTimes" : 4,
"randomAlgorithm" : 0,
"TTL" : 60,
"connect_timeout" : 5,
"proxyRuleMode" : 1,
"proxyEnable" : false,
"pacDirectGoProxy" : false,
"proxyType" : 0,
"proxyHost" : "",
"proxyPort" : 0,
"proxyAuthUser" : "",
"proxyAuthPass" : "",
"proxyUserAgent" : "",
"authUser" : "",
"authPass" : "",
"autoBan" : false,
"sameHostForSameTarget" : true,
"keepVisitTime" : 180,
"isHideTips" : true,
"token" : {
},
"portMap" : {
}
}
';
} else {
$string='
{
"strategy": null,
"index": 6,
"global": false,
"enabled": false,
"shareOverLan": false,
"isDefault": false,
"localPort": 1080,
"pacUrl": null,
"useOnlinePac": false,
"secureLocalPac": true,
"availabilityStatistics": false,
"autoCheckUpdate": false,
"checkPreRelease": false,
"isVerboseLogging": true,
"logViewer": {
"topMost": false,
"wrapText": false,
"toolbarShown": false,
"Font": "Consolas, 8pt",
"BackgroundColor": "Black",
"TextColor": "White"
},
"proxy": {
"useProxy": false,
"proxyType": 0,
"proxyServer": "",
"proxyPort": 0,
"proxyTimeout": 3
},
"hotkey": {
"SwitchSystemProxy": "",
"SwitchSystemProxyMode": "",
"SwitchAllowLan": "",
"ShowLogs": "",
"ServerMoveUp": "",
"ServerMoveDown": ""
}
}
';
}
9 years ago
$json=json_decode($string, true);
$temparray=array();
$items = URL::getAllItems($user, $is_mu, $is_ss);
foreach($items as $item) {
if ($is_ss==0) {
array_push($temparray, array("remarks"=>$item['remark'],
"server"=>$item['address'],
"server_port"=>$item['port'],
"method"=>$item['method'],
"obfs"=>$item['obfs'],
"obfsparam"=>$item['obfs_param'],
"remarks_base64"=>base64_encode($item['remark']),
"password"=>$item['passwd'],
"tcp_over_udp"=>false,
"udp_over_tcp"=>false,
"group"=>$item['group'],
"protocol"=>$item['protocol'],
"protoparam"=>$item['protocol_param'],
"protocolparam"=>$item['protocol_param'],
"obfs_udp"=>false,
"enable"=>true));
} else {
array_push($temparray, array("server"=>$item['address'],
"server_port"=>$item['port'],
"password"=>$item['passwd'],
"method"=>$item['method'],
"plugin"=>"obfs-local",
"plugin_opts"=>str_replace(',',';',URL::getSurgeObfs($item)),
"remarks"=>$item['remark'],
"timeout"=>5));
}
9 years ago
}
$json["configs"]=$temparray;
return json_encode($json, JSON_PRETTY_PRINT);
}
private static function GetPac($type, $address, $port, $defined)
{
header('Content-type: application/x-ns-proxy-autoconfig; charset=utf-8');
return LinkController::get_pac($type, $address, $port, true, $defined);
}
private static function GetMacPac()
{
header('Content-type: application/x-ns-proxy-autoconfig; charset=utf-8');
return LinkController::get_mac_pac();
}
private static function GetAcl($user)
{
$rulelist = base64_decode(file_get_contents("https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"))."\n".$user->pac;
$gfwlist = explode("\n", $rulelist);
$count = 0;
$acl_content = '';
$find_function_content = '
#Generated by sspanel-glzjin-mod v3
#Time:'.date('Y-m-d H:i:s').'
[bypass_all]
';
$proxy_list = '[proxy_list]
';
$bypass_list = '[bypass_list]
';
$outbound_block_list = '[outbound_block_list]
';
$isget=array();
foreach ($gfwlist as $index=>$rule) {
if (empty($rule)) {
continue;
} elseif (substr($rule, 0, 1) == '!' || substr($rule, 0, 1) == '[') {
continue;
}
if (substr($rule, 0, 2) == '@@') {
// ||开头表示前面还有路径
if (substr($rule, 2, 2) =='||') {
//$rule_reg = preg_match("/^((http|https):\/\/)?([^\/]+)/i",substr($rule, 2), $matches);
$host = substr($rule, 4);
//preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN,".$host.",DIRECT,force-remote-dns\n";
$bypass_list .= $host."\n";
continue;
// !开头相当于正则表达式^
} elseif (substr($rule, 2, 1) == '|') {
preg_match("/(\d{1,3}\.){3}\d{1,3}/", substr($rule, 3), $matches);
if (!isset($matches[0])) {
continue;
}
$host = $matches[0];
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="IP-CIDR,".$host."/32,DIRECT,no-resolve \n";
$bypass_list .= $host."/32\n";
continue;
} else {
preg_match_all("~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i", substr($rule, 3), $matches);
if (!isset($matches[4][0])) {
continue;
}
$host = $matches[4][0];
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-SUFFIX,".$host.",DIRECT,force-remote-dns\n";
$bypass_list .= $host."\n";
continue;
}
}
} elseif (substr($rule, 2, 1) == '.') {
$host = substr($rule, 3);
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-SUFFIX,".$host.",DIRECT,force-remote-dns \n";
$bypass_list .= $host."\n";
continue;
}
}
}
// ||开头表示前面还有路径
if (substr($rule, 0, 2) =='||') {
//$rule_reg = preg_match("/^((http|https):\/\/)?([^\/]+)/i",substr($rule, 2), $matches);
$host = substr($rule, 2);
//preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
if (strpos($host, "*")!==false) {
$host = substr($host, strpos($host, "*")+1);
if (strpos($host, ".")!==false) {
$host = substr($host, strpos($host, ".")+1);
}
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-KEYWORD,".$host.",Proxy,force-remote-dns\n";
$proxy_list .= $host."\n";
continue;
}
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN,".$host.",Proxy,force-remote-dns\n";
$proxy_list .= $host."\n";
// !开头相当于正则表达式^
} elseif (substr($rule, 0, 1) == '|') {
preg_match("/(\d{1,3}\.){3}\d{1,3}/", substr($rule, 1), $matches);
if (!isset($matches[0])) {
continue;
}
$host = $matches[0];
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
preg_match("/(\d{1,3}\.){3}\d{1,3}\/\d{1,2}/", substr($rule, 1), $matches_ips);
if (!isset($matches_ips[0])) {
$proxy_list .= $host."/32\n";
} else {
$host = $matches_ips[0];
$proxy_list .= $host."\n";
}
//$find_function_content.="IP-CIDR,".$host."/32,Proxy,no-resolve \n";
continue;
} else {
preg_match_all("~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i", substr($rule, 1), $matches);
if (!isset($matches[4][0])) {
continue;
}
$host = $matches[4][0];
if (strpos($host, "*")!==false) {
$host = substr($host, strpos($host, "*")+1);
if (strpos($host, ".")!==false) {
$host = substr($host, strpos($host, ".")+1);
}
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-KEYWORD,".$host.",Proxy,force-remote-dns\n";
$proxy_list .= $host."\n";
continue;
}
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-SUFFIX,".$host.",Proxy,force-remote-dns\n";
$proxy_list .= $host."\n";
continue;
}
}
} else {
$host = substr($rule, 0);
if (strpos($host, "/")!==false) {
$host = substr($host, 0, strpos($host, "/"));
}
if ($host != "") {
if (isset($isget[$host])) {
continue;
}
$isget[$host]=1;
//$find_function_content.="DOMAIN-KEYWORD,".$host.",PROXY,force-remote-dns \n";
$proxy_list .= $host."\n";
continue;
}
}
$count = $count + 1;
}
$acl_content .= $find_function_content."\n".$proxy_list."\n".$bypass_list."\n".$outbound_block_list;
return $acl_content;
}
/**
* This is a php implementation of autoproxy2pac
*/
private static function reg_encode($str)
{
$tmp_str = $str;
$tmp_str = str_replace('/', "\\/", $tmp_str);
$tmp_str = str_replace('.', "\\.", $tmp_str);
$tmp_str = str_replace(':', "\\:", $tmp_str);
$tmp_str = str_replace('%', "\\%", $tmp_str);
$tmp_str = str_replace('*', ".*", $tmp_str);
$tmp_str = str_replace('-', "\\-", $tmp_str);
$tmp_str = str_replace('&', "\\&", $tmp_str);
$tmp_str = str_replace('?', "\\?", $tmp_str);
$tmp_str = str_replace('+', "\\+", $tmp_str);
return $tmp_str;
}
private static function get_pac($proxy_type, $proxy_host, $proxy_port, $proxy_google, $defined)
{
$rulelist = base64_decode(file_get_contents("https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"))."\n".$defined;
$gfwlist = explode("\n", $rulelist);
if ($proxy_google == "true") {
$gfwlist[] = ".google.com";
}
$count = 0;
$pac_content = '';
$find_function_content = 'function FindProxyForURL(url, host) { var PROXY = "'.$proxy_type.' '.$proxy_host.':'.$proxy_port.'; DIRECT"; var DEFAULT = "DIRECT";'."\n";
foreach ($gfwlist as $index=>$rule) {
if (empty($rule)) {
continue;
} elseif (substr($rule, 0, 1) == '!' || substr($rule, 0, 1) == '[') {
continue;
}
$return_proxy = 'PROXY';
// @@开头表示默认是直接访问
if (substr($rule, 0, 2) == '@@') {
$rule = substr($rule, 2);
$return_proxy = "DEFAULT";
}
// ||开头表示前面还有路径
if (substr($rule, 0, 2) =='||') {
$rule_reg = "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?".LinkController::reg_encode(substr($rule, 2));
// !开头相当于正则表达式^
} elseif (substr($rule, 0, 1) == '|') {
$rule_reg = "^" . LinkController::reg_encode(substr($rule, 1));
// 前后匹配的/表示精确匹配
} elseif (substr($rule, 0, 1) == '/' && substr($rule, -1) == '/') {
$rule_reg = substr($rule, 1, strlen($rule) - 2);
} else {
$rule_reg = LinkController::reg_encode($rule);
}
// 以|结尾,替换为$结尾
if (preg_match("/\|$/i", $rule_reg)) {
$rule_reg = substr($rule_reg, 0, strlen($rule_reg) - 1)."$";
}
$find_function_content.='if (/' . $rule_reg . '/i.test(url)) return '.$return_proxy.';'."\n";
$count = $count + 1;
}
$find_function_content.='return DEFAULT;'."}";
$pac_content.=$find_function_content;
return $pac_content;
}
private static function get_mac_pac()
{
$rulelist = base64_decode(file_get_contents("https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"));
$gfwlist = explode("\n", $rulelist);
$gfwlist[] = ".google.com";
$count = 0;
$pac_content = '';
$find_function_content = 'function FindProxyForURL(url, host) { var PROXY = "SOCKS5 127.0.0.1:1080; SOCKS 127.0.0.1:1080; DIRECT;"; var DEFAULT = "DIRECT";'."\n";
foreach ($gfwlist as $index=>$rule) {
if (empty($rule)) {
continue;
} elseif (substr($rule, 0, 1) == '!' || substr($rule, 0, 1) == '[') {
continue;
}
$return_proxy = 'PROXY';
// @@开头表示默认是直接访问
if (substr($rule, 0, 2) == '@@') {
$rule = substr($rule, 2);
$return_proxy = "DEFAULT";
}
// ||开头表示前面还有路径
if (substr($rule, 0, 2) =='||') {
$rule_reg = "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?".LinkController::reg_encode(substr($rule, 2));
// !开头相当于正则表达式^
} elseif (substr($rule, 0, 1) == '|') {
$rule_reg = "^" . LinkController::reg_encode(substr($rule, 1));
// 前后匹配的/表示精确匹配
} elseif (substr($rule, 0, 1) == '/' && substr($rule, -1) == '/') {
$rule_reg = substr($rule, 1, strlen($rule) - 2);
} else {
$rule_reg = LinkController::reg_encode($rule);
}
// 以|结尾,替换为$结尾
if (preg_match("/\|$/i", $rule_reg)) {
$rule_reg = substr($rule_reg, 0, strlen($rule_reg) - 1)."$";
}
$find_function_content.='if (/' . $rule_reg . '/i.test(url)) return '.$return_proxy.';'."\n";
$count = $count + 1;
}
$find_function_content.='return DEFAULT;'."}";
$pac_content.=$find_function_content;
return $pac_content;
}
public static function GetRouter($user, $is_mu = 0, $is_ss = 0)
{
$bash = '#!/bin/sh'."\n";
$bash .= 'export PATH=\'/opt/usr/sbin:/opt/usr/bin:/opt/sbin:/opt/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin\''."\n";
$bash .= 'export LD_LIBRARY_PATH=/lib:/opt/lib'."\n";
$bash .= 'nvram set ss_type='.($is_ss == 1 ? '0' : '1')."\n";
$count = 0;
$items = URL::getAllItems($user, $is_mu, $is_ss);
foreach($items as $item) {
if($is_ss == 0) {
$bash .= 'nvram set rt_ss_name_x'.$count.'="'.$item['remark']."\"\n";
$bash .= 'nvram set rt_ss_port_x'.$count.'='.$item['port']."\n";
$bash .= 'nvram set rt_ss_password_x'.$count.'="'.$item['passwd']."\"\n";
$bash .= 'nvram set rt_ss_server_x'.$count.'='.$item['address']."\n";
$bash .= 'nvram set rt_ss_usage_x'.$count.'="'."-o ".$item['obfs']." -g ".$item['obfs_param']." -O ".$item['protocol']." -G ".$item['protocol_param']."\"\n";
$bash .= 'nvram set rt_ss_method_x'.$count.'='.$item['method']."\n";
$count += 1;
}else{
$bash .= 'nvram set rt_ss_name_x'.$count.'="'.$item['remark']."\"\n";
$bash .= 'nvram set rt_ss_port_x'.$count.'='.$item['port']."\n";
$bash .= 'nvram set rt_ss_password_x'.$count.'="'.$item['passwd']."\"\n";
$bash .= 'nvram set rt_ss_server_x'.$count.'='.$item['address']."\n";
$bash .= 'nvram set rt_ss_usage_x'.$count.'=""'."\n";
$bash .= 'nvram set rt_ss_method_x'.$count.'='.$item['method']."\n";
$count += 1;
}
}
$bash .= "nvram set rt_ssnum_x=".$count."\n";
return $bash;
}
const V2RYA_MU = 2;
const SSD_MU = 3;
const CLASH_MU = 4;
public static function GetSSRSub($user, $mu = 0, $max = 0)
9 years ago
{
if ($mu==0||$mu==1) {
return Tools::base64_url_encode(URL::getAllUrl($user, $mu, 0, 1));
8 years ago
}
elseif ($mu == LinkController::V2RYA_MU){
return Tools::base64_url_encode(URL::getAllVMessUrl($user));
}
elseif ($mu==LinkController::SSD_MU) {
return URL::getAllSSDUrl($user);
} elseif ($mu==LinkController::CLASH_MU) {
// Clash
$render = ConfRender::getTemplateRender();
$confs = URL::getClashInfo($user);
$render->assign('user', $user)->assign('confs', $confs)->assign('proxies', array_map(function ($conf) {
return $conf['name'];
}, $confs));
return $render->fetch('clash.tpl');
}
9 years ago
}
}