URL.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\User;
  4. use App\Models\Node;
  5. use App\Models\Relay;
  6. use App\Services\Config;
  7. class URL
  8. {
  9. /*
  10. * 1 SSR can
  11. * 2 SS can
  12. * 3 Both can
  13. */
  14. public static function CanMethodConnect($method) {
  15. $ss_aead_method_list = Config::getSupportParam('ss_aead_method');
  16. if(in_array($method, $ss_aead_method_list)) {
  17. return 2;
  18. }
  19. return 3;
  20. }
  21. /*
  22. * 1 SSR can
  23. * 2 SS can
  24. * 3 Both can
  25. */
  26. public static function CanProtocolConnect($protocol) {
  27. if($protocol != 'origin') {
  28. if(strpos($protocol, '_compatible') === FALSE) {
  29. return 1;
  30. }else{
  31. return 3;
  32. }
  33. }
  34. return 3;
  35. }
  36. /*
  37. * 1 SSR can
  38. * 2 SS can
  39. * 3 Both can
  40. * 4 Both can, But ssr need set obfs to plain
  41. * 5 Both can, But ss need set obfs to plain
  42. */
  43. public static function CanObfsConnect($obfs) {
  44. if($obfs != 'plain') {
  45. //SS obfs only
  46. $ss_obfs = Config::getSupportParam('ss_obfs');
  47. if(in_array($obfs, $ss_obfs)) {
  48. if(strpos($obfs, '_compatible') === FALSE) {
  49. return 2;
  50. }else{
  51. return 4;//SSR need origin plain
  52. }
  53. }else{
  54. //SSR obfs only
  55. if(strpos($obfs, '_compatible') === FALSE) {
  56. return 1;
  57. }else{
  58. return 5;//SS need plain
  59. }
  60. }
  61. }else{
  62. return 3;
  63. }
  64. }
  65. public static function SSCanConnect($user, $mu_port = 0) {
  66. if($mu_port != 0) {
  67. $mu_user = User::where('port', '=', $mu_port)->where("is_multi_user", "<>", 0)->first();
  68. if ($mu_user == null) {
  69. return;
  70. }
  71. return URL::SSCanConnect($mu_user);
  72. }
  73. if(URL::CanMethodConnect($user->method) >= 2 && URL::CanProtocolConnect($user->protocol) >= 2 && URL::CanObfsConnect($user->obfs) >= 2) {
  74. return true;
  75. }else{
  76. return false;
  77. }
  78. }
  79. public static function SSRCanConnect($user, $mu_port = 0) {
  80. if($mu_port != 0) {
  81. $mu_user = User::where('port', '=', $mu_port)->where("is_multi_user", "<>", 0)->first();
  82. if ($mu_user == null) {
  83. return;
  84. }
  85. return URL::SSRCanConnect($mu_user);
  86. }
  87. if(URL::CanMethodConnect($user->method) != 2 && URL::CanProtocolConnect($user->protocol) != 2 && URL::CanObfsConnect($user->obfs) != 2) {
  88. return true;
  89. }else{
  90. return false;
  91. }
  92. }
  93. public static function getSSConnectInfo($user) {
  94. $new_user = clone $user;
  95. if(URL::CanObfsConnect($new_user->obfs) == 5) {
  96. $new_user->obfs = 'plain';
  97. $new_user->obfs_param = '';
  98. }
  99. if(URL::CanProtocolConnect($new_user->protocol) == 3) {
  100. $new_user->protocol = 'origin';
  101. $new_user->protocol_param = '';
  102. }
  103. $new_user->obfs = str_replace("_compatible", "", $new_user->obfs);
  104. $new_user->protocol = str_replace("_compatible", "", $new_user->protocol);
  105. return $new_user;
  106. }
  107. public static function getSSRConnectInfo($user) {
  108. $new_user = clone $user;
  109. if(URL::CanObfsConnect($new_user->obfs) == 4) {
  110. $new_user->obfs = 'plain';
  111. $new_user->obfs_param = '';
  112. }
  113. $new_user->obfs = str_replace("_compatible", "", $new_user->obfs);
  114. $new_user->protocol = str_replace("_compatible", "", $new_user->protocol);
  115. return $new_user;
  116. }
  117. public static function getAllItems($user, $is_mu = 0, $is_ss = 0) {
  118. $return_array = array();
  119. if ($user->is_admin) {
  120. $nodes=Node::where(
  121. function ($query) {
  122. $query->where('sort', 0)
  123. ->orwhere('sort', 10);
  124. }
  125. )->where("type", "1")->orderBy("name")->get();
  126. } else {
  127. $nodes=Node::where(
  128. function ($query) {
  129. $query->where('sort', 0)
  130. ->orwhere('sort', 10);
  131. }
  132. )->where(
  133. function ($query) use ($user){
  134. $query->where("node_group", "=", $user->node_group)
  135. ->orWhere("node_group", "=", 0);
  136. }
  137. )->where("type", "1")->where("node_class", "<=", $user->class)->orderBy("name")->get();
  138. }
  139. if($is_mu) {
  140. if ($user->is_admin) {
  141. $mu_nodes = Node::where('sort', 9)->where("type", "1")->get();
  142. } else {
  143. $mu_nodes = Node::where('sort', 9)->where('node_class', '<=', $user->class)->where("type", "1")->where(
  144. function ($query) use ($user) {
  145. $query->where("node_group", "=", $user->node_group)
  146. ->orWhere("node_group", "=", 0);
  147. }
  148. )->get();
  149. }
  150. }
  151. $relay_rules = Relay::where('user_id', $user->id)->orwhere('user_id', 0)->orderBy('id', 'asc')->get();
  152. if (!Tools::is_protocol_relay($user)) {
  153. $relay_rules = array();
  154. }
  155. foreach ($nodes as $node) {
  156. if ($node->mu_only != 1 && $is_mu == 0) {
  157. if ($node->sort == 10) {
  158. $relay_rule_id = 0;
  159. $relay_rule = Tools::pick_out_relay_rule($node->id, $user->port, $relay_rules);
  160. if ($relay_rule != null) {
  161. if ($relay_rule->dist_node() != null) {
  162. $relay_rule_id = $relay_rule->id;
  163. }
  164. }
  165. $item = URL::getItem($user, $node, 0, $relay_rule_id, $is_ss);
  166. if($item != null) {
  167. array_push($return_array, $item);
  168. }
  169. }else{
  170. $item = URL::getItem($user, $node, 0, 0, $is_ss);
  171. if($item != null) {
  172. array_push($return_array, $item);
  173. }
  174. }
  175. }
  176. if ($node->custom_rss == 1 && $node->mu_only != -1 && $is_mu == 1) {
  177. foreach ($mu_nodes as $mu_node) {
  178. if ($node->sort == 10) {
  179. $relay_rule_id = 0;
  180. $relay_rule = Tools::pick_out_relay_rule($node->id, $mu_node->server, $relay_rules);
  181. if ($relay_rule != null) {
  182. if ($relay_rule->dist_node() != null) {
  183. $relay_rule_id = $relay_rule->id;
  184. }
  185. }
  186. $item = URL::getItem($user, $node, $mu_node->server, $relay_rule_id, $is_ss);
  187. if($item != null) {
  188. array_push($return_array, $item);
  189. }
  190. }else{
  191. $item = URL::getItem($user, $node, $mu_node->server, 0, $is_ss);
  192. if($item != null) {
  193. array_push($return_array, $item);
  194. }
  195. }
  196. }
  197. }
  198. }
  199. return $return_array;
  200. }
  201. public static function getAllUrl($user, $is_mu, $is_ss = 0, $enter = 0) {
  202. $items = URL::getAllItems($user, $is_mu, $is_ss);
  203. $return_url = '';
  204. foreach($items as $item) {
  205. $return_url .= URL::getItemUrl($item, $is_ss).($enter == 0 ? ' ' : "\n");
  206. }
  207. return $return_url;
  208. }
  209. public static function getItemUrl($item, $is_ss) {
  210. $ss_obfs_list = Config::getSupportParam('ss_obfs');
  211. if(!$is_ss) {
  212. $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(Config::get('appName'));
  213. return "ssr://".Tools::base64_url_encode($ssurl);
  214. } else {
  215. if($is_ss == 2) {
  216. $personal_info = $item['method'].':'.$item['passwd']."@".$item['address'].":".$item['port'];
  217. $ssurl = "ss://".Tools::base64_url_encode($personal_info);
  218. $ssurl .= "#".rawurlencode(Config::get('appName')." - ".$item['remark']);
  219. }else{
  220. $personal_info = $item['method'].':'.$item['passwd'];
  221. $ssurl = "ss://".Tools::base64_url_encode($personal_info)."@".$item['address'].":".$item['port'];
  222. $plugin = '';
  223. if(in_array($item['obfs'], $ss_obfs_list)) {
  224. if(strpos($item['obfs'], 'http') !== FALSE) {
  225. $plugin .= "obfs-local;obfs=http";
  226. } else {
  227. $plugin .= "obfs-local;obfs=tls";
  228. }
  229. if($item['obfs_param'] != '') {
  230. $plugin .= ";obfs-host=".$item['obfs_param'];
  231. }
  232. $ssurl .= "?plugin=".rawurlencode($plugin);
  233. }
  234. $ssurl .= "#".rawurlencode(Config::get('appName')." - ".$item['remark']);
  235. }
  236. return $ssurl;
  237. }
  238. }
  239. public static function getJsonObfs($item) {
  240. $ss_obfs_list = Config::getSupportParam('ss_obfs');
  241. $plugin = "";
  242. if(in_array($item['obfs'], $ss_obfs_list)) {
  243. if(strpos($item['obfs'], 'http') !== FALSE) {
  244. $plugin .= "obfs-local --obfs http";
  245. } else {
  246. $plugin .= "obfs-local --obfs tls";
  247. }
  248. if($item['obfs_param'] != '') {
  249. $plugin .= "--obfs-host ".$item['obfs_param'];
  250. }
  251. }
  252. return $plugin;
  253. }
  254. public static function getSurgeObfs($item) {
  255. $ss_obfs_list = Config::getSupportParam('ss_obfs');
  256. $plugin = "";
  257. if(in_array($item['obfs'], $ss_obfs_list)) {
  258. if(strpos($item['obfs'], 'http') !== FALSE) {
  259. $plugin .= ",obfs=http";
  260. } else {
  261. $plugin .= ",obfs=tls";
  262. }
  263. if($item['obfs_param'] != '') {
  264. $plugin .= ",obfs-host=".$item['obfs_param'];
  265. }
  266. }
  267. return $plugin;
  268. }
  269. /*
  270. * Conn info
  271. * address
  272. * port
  273. * passwd
  274. * method
  275. * remark
  276. * protocol
  277. * protocol_param
  278. * obfs
  279. * obfs_param
  280. */
  281. public static function getItem($user, $node, $mu_port = 0, $relay_rule_id = 0, $is_ss = 0) {
  282. $relay_rule = Relay::where('id', $relay_rule_id)->where(
  283. function ($query) use ($user) {
  284. $query->Where("user_id", "=", $user->id)
  285. ->orWhere("user_id", "=", 0);
  286. }
  287. )->first();
  288. $node_name = $node->name;
  289. if ($relay_rule != null) {
  290. $node_name .= " - ".$relay_rule->dist_node()->name;
  291. }
  292. if($mu_port != 0) {
  293. $mu_user = User::where('port', '=', $mu_port)->where("is_multi_user", "<>", 0)->first();
  294. if ($mu_user == null) {
  295. return;
  296. }
  297. $mu_user->obfs_param = $user->getMuMd5();
  298. $mu_user->protocol_param = $user->id.":".$user->passwd;
  299. $user = $mu_user;
  300. $node_name .= " - ".$mu_port." 端口单端口多用户";
  301. }
  302. if($is_ss) {
  303. if(!URL::SSCanConnect($user)) {
  304. return;
  305. }
  306. $user = URL::getSSConnectInfo($user);
  307. }else{
  308. if(!URL::SSRCanConnect($user)) {
  309. return;
  310. }
  311. $user = URL::getSSRConnectInfo($user);
  312. }
  313. $return_array['address'] = $node->server;
  314. $return_array['port'] = $user->port;
  315. $return_array['passwd'] = $user->passwd;
  316. $return_array['method'] = $user->method;
  317. $return_array['remark'] = $node_name;
  318. $return_array['protocol'] = $user->protocol;
  319. $return_array['protocol_param'] = $user->protocol_param;
  320. $return_array['obfs'] = $user->obfs;
  321. $return_array['obfs_param'] = $user->obfs_param;
  322. return $return_array;
  323. }
  324. public static function cloneUser($user) {
  325. $new_user = clone $user;
  326. return $new_user;
  327. }
  328. }