Skip to content

Commit

Permalink
feat: add custom rule selection
Browse files Browse the repository at this point in the history
  • Loading branch information
7Sageer committed Aug 20, 2024
1 parent b2ec0d4 commit 6e01c4e
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 109 deletions.
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Sublink Worker

Sublink Worker 是一个轻量级的订阅转换工具,可部署在 Cloudflare Worker 上。它可以将各种代理协议的分享 URL 转换为不同客户端可用的订阅链接。
Sublink Worker 是一个轻量级的订阅转换工具,可部署在 Cloudflare Worker 上。它可以将各种代理协议的分享 URL 转换为不同客户端可用的订阅链接。同时,它还提供了灵活的自定义规则与API支持。

![image](/doc/main.png)
![image](/doc/img/main.png)

## 功能特点

Expand All @@ -15,10 +15,10 @@ Sublink Worker 是一个轻量级的订阅转换工具,可部署在 Cloudflare
- Xray/V2Ray
- 支持短链接生成(基于 R2)
- 浅色/深色主题切换
- 用户友好的 Web 界面,提供主流自定义路由规则
- 灵活的 API,支持脚本化操作
- 用户友好的 Web 界面,灵活的自定义规则:

![image](/doc/rules.png)
![image](/doc/img/custom.png)

## 快速开始

Expand All @@ -41,27 +41,24 @@ Sublink Worker 是一个轻量级的订阅转换工具,可部署在 Cloudflare

## 最近更新

### 2024-08-20

- 新增:
- 自定义规则
- 自定义规则的 API 支持,详见 [API-doc.md](/doc/API-doc.md)

### 2024-08-19

- 更新规则集:
- `💬 AI 服务` 规则集增加 `Perplexity`
- `💰 金融服务` 规则集增加 `Stripe`, `Wise`


### 2024-08-13

- 优化 API 架构
- 更新了文档,增加了详细的 API 说明
- 修复 hy2 协议解析问题

### 2024-08-10

- 添加快速选择规则集
- 细化规则集
- 增添更多分类
-`💬 OpenAI` 改为 `💬 AI 服务`,包含 Claude,Jetbrains-AI 等规则
- 优化显示

## 项目结构

- `index.js`: 主要的服务器逻辑,处理请求路由
Expand Down
67 changes: 41 additions & 26 deletions doc/API-doc-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Overview

Sublink Worker is a lightweight subscription conversion tool deployed on Cloudflare Workers. It converts various proxy protocol share URLs into subscription links usable by different clients. This document outlines the API endpoints and their usage.
Sublink Worker is a lightweight subscription conversion tool deployed on Cloudflare Workers. It can convert sharing URLs of various proxy protocols into subscription links usable by different clients. This document outlines the API endpoints and their usage.

## Base URL

All API requests should be made to:
All API requests should be sent to:

```
https://your-worker-domain.workers.dev
Expand All @@ -24,15 +24,12 @@ Replace `your-worker-domain` with your actual Cloudflare Workers domain.
- **Method**: GET
- **Parameters**:
- `config` (required): URL-encoded string containing one or more proxy configurations
- `selectedRules` (optional): Either a predefined rule set name or a JSON array of custom rules
- `selectedRules` (optional): Name of predefined rule set or JSON array of custom rules
- `customRules` (optional): JSON array of custom rules

**Example**:
```
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=balanced
```
or
```
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=%5B%22Ad%20Block%22%2C%22Private%22%5D
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=balanced&customRules=%5B%7B%22sites%22%3A%5B%22example.com%22%5D%2C%22ips%22%3A%5B%22192.168.1.1%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

#### Clash Configuration
Expand All @@ -53,7 +50,7 @@ or
- **URL**: `/shorten`
- **Method**: GET
- **Parameters**:
- `url` (required): The original URL to be shortened
- `url` (required): Original URL to be shortened

**Example**:
```
Expand All @@ -77,15 +74,13 @@ or

The API supports the following predefined rule sets:

- `minimal`: Basic set of rules
- `balanced`: Moderate set of rules
- `comprehensive`: Full set of rules
- `minimal`: Basic rule set
- `balanced`: Moderate rule set
- `comprehensive`: Complete rule set

These can be used in the `selectedRules` parameter for Sing-Box and Clash configurations.

## Custom Rules

Instead of using predefined rule sets, you can provide a custom list of rules as a JSON array in the `selectedRules` parameter. Available rules include:
Below are the currently supported predefined rule sets:

| Rule Name | Used Site Rules | Used IP Rules |
|---|---|---|
Expand All @@ -108,33 +103,53 @@ Instead of using predefined rule sets, you can provide a custom list of rules as
| Financial | paypal, visa, mastercard, stripe, wise | |
| Cloud Services | aws, azure, digitalocean, heroku, dropbox | |

The rule set of SingBox comes from [https://github.com/lyc8503/sing-box-rules](https://github.com/lyc8503/sing-box-rules), thanks to lyc8503's contribution!
Singbox rule sets are sourced from [https://github.com/lyc8503/sing-box-rules](https://github.com/lyc8503/sing-box-rules), thanks to lyc8503's contribution!

## Custom Rules

In addition to using predefined rule sets, you can provide a list of custom rules in the `customRules` parameter as a JSON array. Each custom rule should include the following fields:

- `sites`: Array of domain rules
- `ips`: Array of IP rules
- `outbound`: Outbound name

Example:

```json
[
{
"sites": ["google", "anthropic"],
"ips": ["private", "cn"],
"outbound": "🤪 MyCustomRule"
}
]
```

## Error Handling

The API will return appropriate HTTP status codes along with error messages in case of issues:
The API will return appropriate HTTP status codes and error messages when issues occur:

- 400 Bad Request: When required parameters are missing or invalid
- 404 Not Found: When a requested resource (e.g., short URL) doesn't exist
- 500 Internal Server Error: For server-side errors
- 404 Not Found: When the requested resource (like a short URL) doesn't exist
- 500 Internal Server Error: Server-side errors

## Usage Notes

1. All proxy configurations in the `config` parameter should be URL-encoded.
2. Multiple proxy configurations can be included in a single request by separating them with newline characters (`%0A`) in the URL-encoded `config` parameter.
3. When using custom rules, ensure the rule names exactly match those listed in the Custom Rules section.
4. The shortened URLs are meant to be temporary and may expire after a certain period.
2. Multiple proxy configurations can be included in a single request by separating them with newlines (`%0A`) in the URL-encoded `config` parameter.
3. When using custom rules, ensure that the rule names match exactly with those listed in the custom rules section.
4. Shortened URLs are intended for temporary use and may expire after a certain period.

## Examples

1. Generate a Sing-Box configuration with a balanced rule set:
1. Generate a Sing-Box configuration with balanced rule set:
```
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=balanced
```

2. Generate a Clash configuration with custom rules:
```
/clash?config=vless%3A%2F%2Fexample&selectedRules=%5B%22Ad%20Block%22%2C%22Google%22%2C%22Streaming%22%5D
/clash?config=vless%3A%2F%2Fexample&customRules=%5B%7B%22sites%22%3A%5B%22example.com%22%5D%2C%22ips%22%3A%5B%22192.168.1.1%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

3. Shorten a URL:
Expand All @@ -144,6 +159,6 @@ The API will return appropriate HTTP status codes along with error messages in c

## Conclusion

This API provides a flexible and powerful way to generate and manage proxy configurations. It supports multiple proxy protocols, various client types, and customizable routing rules. The URL shortening feature allows for easy sharing and management of complex configurations.
Our API provides a flexible and powerful way to generate and manage proxy configurations. It supports multiple proxy protocols, various client types, and customizable routing rules. The URL shortening feature allows for easy sharing and management of complex configurations.

For any issues or feature requests, please contact the repository maintainer.
For any questions or feature requests, please contact the repository maintainer.
37 changes: 26 additions & 11 deletions doc/API-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ https://your-worker-domain.workers.dev
- **参数**:
- `config` (必需): URL 编码的字符串,包含一个或多个代理配置
- `selectedRules` (可选): 预定义规则集名称或自定义规则的 JSON 数组
- `customRules` (可选): 自定义规则的 JSON 数组

**示例**:
```
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=balanced
```
```
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=%5B%22Ad%20Block%22%2C%22Private%22%5D
/singbox?config=vmess%3A%2F%2Fexample&selectedRules=balanced&customRules=%5B%7B%22sites%22%3A%5B%22example.com%22%5D%2C%22ips%22%3A%5B%22192.168.1.1%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

#### Clash 配置
Expand Down Expand Up @@ -83,9 +80,7 @@ API 支持以下预定义规则集:

这些可以在 Sing-Box 和 Clash 配置的 `selectedRules` 参数中使用。

## 自定义规则

除了使用预定义规则集,您还可以在 `selectedRules` 参数中提供自定义规则列表作为 JSON 数组。可用规则包括:
下面是目前支持的预定义规则集:

| Rule Name | Used Site Rules | Used IP Rules |
|---|---|---|
Expand All @@ -108,7 +103,27 @@ API 支持以下预定义规则集:
| Financial | paypal, visa, mastercard, stripe, wise | |
| Cloud Services | aws, azure, digitalocean, heroku, dropbox | |

> SingBox的规则集来自于[https://github.com/lyc8503/sing-box-rules](https://github.com/lyc8503/sing-box-rules), 感谢lyc8503的贡献!
Singbox 的规则集来自 [https://github.com/lyc8503/sing-box-rules](https://github.com/lyc8503/sing-box-rules), 感谢 lyc8503 的贡献!

## 自定义规则

除了使用预定义规则集,您还可以在 `customRules` 参数中提供自定义规则列表作为 JSON 数组。每个自定义规则应包含以下字段:

- `sites`: 域名规则数组
- `ips`: IP 规则数组
- `outbound`: 出站名称

示例:

```json
[
{
"sites": ["google", "anthropic"],
"ips": ["private", "cn"],
"outbound": "🤪 MyCustomRule"
}
]
```

## 错误处理

Expand All @@ -134,7 +149,7 @@ API 在出现问题时将返回适当的 HTTP 状态码和错误消息:

2. 生成带有自定义规则的 Clash 配置:
```
/clash?config=vless%3A%2F%2Fexample&selectedRules=%5B%22Ad%20Block%22%2C%22Google%22%2C%22Streaming%22%5D
/clash?config=vless%3A%2F%2Fexample&customRules=%5B%7B%22sites%22%3A%5B%22example.com%22%5D%2C%22ips%22%3A%5B%22192.168.1.1%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

3. 缩短 URL:
Expand All @@ -146,4 +161,4 @@ API 在出现问题时将返回适当的 HTTP 状态码和错误消息:

我们的 API 提供了一种灵活而强大的方式来生成和管理代理配置。它支持多种代理协议、各种客户端类型和可自定义的路由规则。URL 缩短功能允许轻松共享和管理复杂的配置。

如有任何问题或功能请求,欢迎联系仓库维护者。
如有任何问题或功能请求,欢迎联系仓库维护者。
Binary file added doc/img/custom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
12 changes: 12 additions & 0 deletions doc/newbie-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 快速入门

这部分内容是用来帮助不了解的人快速了解这个项目,如果你知道订阅链接,分享url,自建节点,那么可以跳过这部分内容。

## 分享url

如果你自建过节点,应该

## 什么是订阅链接转换?

如果你使用过机场,那么你应该知道大部分机场都使用订阅链接来提供节点信息。仅需复制一个链接,就能在对应客户端(如sing-box, clash, v2rayN)中使用。
然而,这些客户端的分享链接并不互通
Binary file removed doc/rules.png
Binary file not shown.
22 changes: 16 additions & 6 deletions src/ClashConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { BaseConfigBuilder } from './BaseConfigBuilder.js';
import { DeepCopy } from './utils.js';

export class ClashConfigBuilder extends BaseConfigBuilder {
constructor(inputString, selectedRules) {
constructor(inputString, selectedRules, customRules) {
super(inputString, CLASH_CONFIG);
this.selectedRules = selectedRules;
this.customRules = customRules;
}

addCustomItems(customItems) {
Expand Down Expand Up @@ -57,19 +58,28 @@ export class ClashConfigBuilder extends BaseConfigBuilder {
}
});

this.customRules.forEach(rule => {
this.config['proxy-groups'].push({
type: "select",
name: rule.outbound,
proxies: ['🚀 节点选择', ...proxyList]
});
});

this.config['proxy-groups'].push({
type: "select",
name: "🐟 漏网之鱼",
proxies: ['🚀 节点选择', ...proxyList]
});
}
formatConfig() {
const rules = generateRules(this.selectedRules);
const rules = generateRules(this.selectedRules, this.customRules);

this.config.rules = rules.flatMap(rule => [
...rule.site_rules.map(site => `GEOSITE,${site},${rule.outbound}`),
...rule.ip_rules.map(ip => `GEOIP,${ip},${rule.outbound}`)
]);
this.config.rules = rules.flatMap(rule => {
const siteRules = rule.site_rules ? rule.site_rules.map(site => `GEOSITE,${site},${rule.outbound}`) : [];
const ipRules = rule.ip_rules ? rule.ip_rules.map(ip => `GEOIP,${ip},${rule.outbound}`) : [];
return [...siteRules, ...ipRules];
});

// Add the final catch-all rule
this.config.rules.push('MATCH,🐟 漏网之鱼');
Expand Down
17 changes: 13 additions & 4 deletions src/SingboxConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { BaseConfigBuilder } from './BaseConfigBuilder.js';
import { DeepCopy } from './utils.js';

export class ConfigBuilder extends BaseConfigBuilder {
constructor(inputString, selectedRules) {
super(inputString, SING_BOX_CONFIG);
constructor(inputString, selectedRules, customRules) {
super(inputString, SING_BOX_CONFIG);
this.selectedRules = selectedRules;
this.customRules = customRules;
}

addCustomItems(customItems) {
Expand Down Expand Up @@ -50,6 +51,14 @@ export class ConfigBuilder extends BaseConfigBuilder {
}
});

this.customRules.forEach(rule => {
this.config.outbounds.push({
type: "selector",
tag: rule.outbound,
outbounds: ['🚀 节点选择', ...proxyList]
});
});

this.config.outbounds.push({
type: "selector",
tag: "🐟 漏网之鱼",
Expand All @@ -58,8 +67,8 @@ export class ConfigBuilder extends BaseConfigBuilder {
}

formatConfig() {
const rules = generateRules(this.selectedRules);
const { site_rule_sets, ip_rule_sets } = generateRuleSets(this.selectedRules);
const rules = generateRules(this.selectedRules, this.customRules);
const { site_rule_sets, ip_rule_sets } = generateRuleSets(this.selectedRules,this.customRules);

this.config.route.rule_set = [...site_rule_sets, ...ip_rule_sets];

Expand Down
Loading

0 comments on commit 6e01c4e

Please sign in to comment.