Skip to content

Commit

Permalink
Merge pull request #15 from 7Sageer/test
Browse files Browse the repository at this point in the history
feat:update cide and domain suffix in custom rules
  • Loading branch information
7Sageer authored Sep 1, 2024
2 parents e5fc14a + 2f90011 commit 2697c36
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 42 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 功能特点

Expand All @@ -18,7 +18,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转
- 灵活的 API,支持脚本化操作
- 用户友好的 Web 界面,灵活的自定义规则:

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

## 快速开始

Expand All @@ -41,7 +41,15 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转

## 最近更新

### 2024-08-28
### 2024-09-01

- 自定义规则现在支持以下规则:
- domain_suffix
- ip_cidr
- geoip
- geosite

### 2024-08-25

- 修复 ClashMeta For Android 高于[v2.10.1]版本不显示规则集的问题

Expand All @@ -55,12 +63,6 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转
- 自定义规则
- 自定义规则的 API 支持,详见 [API-doc.md](/doc/API-doc.md)

### 2024-08-19

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

## 项目结构

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

## Overview

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.
Sublink Worker is a lightweight subscription conversion tool deployed on Cloudflare Workers. It can convert shared URLs of various proxy protocols into subscription links usable by different clients. This document outlines the API endpoints and their usage.

## Base URL

Expand All @@ -24,12 +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): Name of predefined rule set or JSON array of custom rules
- `selectedRules` (optional): Name of a 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&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
/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%22domain_suffix%22%3A%5B%22.com%22%5D%2C%22ip_cidr%22%3A%5B%2210.0.0.0%2F8%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

#### Clash Configuration
Expand All @@ -50,7 +50,7 @@ Replace `your-worker-domain` with your actual Cloudflare Workers domain.
- **URL**: `/shorten`
- **Method**: GET
- **Parameters**:
- `url` (required): Original URL to be shortened
- `url` (required): The original URL to be shortened

**Example**:
```
Expand Down Expand Up @@ -80,7 +80,7 @@ The API supports the following predefined rule sets:

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

Below are the currently supported predefined rule sets:
Here are the currently supported predefined rule sets:

| Rule Name | Used Site Rules | Used IP Rules |
|---|---|---|
Expand All @@ -103,14 +103,16 @@ Below are the currently supported predefined rule sets:
| Financial | paypal, visa, mastercard, stripe, wise | |
| Cloud Services | aws, azure, digitalocean, heroku, dropbox | |

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!
The rule sets for Singbox are from [https://github.com/lyc8503/sing-box-rules](https://github.com/lyc8503/sing-box-rules), thanks to lyc8503 for the 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:
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 contain the following fields:

- `sites`: Array of domain rules
- `ips`: Array of IP rules
- `domain_suffix`: Array of domain suffix rules
- `ip_cidr`: Array of IP CIDR rules
- `outbound`: Outbound name

Example:
Expand All @@ -120,17 +122,19 @@ Example:
{
"sites": ["google", "anthropic"],
"ips": ["private", "cn"],
"domain_suffix": [".com", ".org"],
"ip_cidr": ["192.168.0.0/16", "10.0.0.0/8"],
"outbound": "🤪 MyCustomRule"
}
]
```

## Error Handling

The API will return appropriate HTTP status codes and error messages when issues occur:
The API will return appropriate HTTP status codes and error messages when problems occur:

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

## Usage Notes
Expand All @@ -149,7 +153,7 @@ The API will return appropriate HTTP status codes and error messages when issues

2. Generate a Clash configuration with custom rules:
```
/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
/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%22domain_suffix%22%3A%5B%22.com%22%5D%2C%22ip_cidr%22%3A%5B%2210.0.0.0%2F8%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

3. Shorten a URL:
Expand All @@ -159,6 +163,6 @@ The API will return appropriate HTTP status codes and error messages when issues

## Conclusion

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.
The Sublink Worker 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 questions or feature requests, please contact the repository maintainer.
For any questions or feature requests, please contact [@7Sageer](https://github.com/7Sageer).
12 changes: 8 additions & 4 deletions doc/API-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ https://your-worker-domain.workers.dev

**示例**:
```
/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
/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%22domain_suffix%22%3A%5B%22.com%22%5D%2C%22ip_cidr%22%3A%5B%2210.0.0.0%2F8%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

#### Clash 配置
Expand Down Expand Up @@ -111,6 +111,8 @@ Singbox 的规则集来自 [https://github.com/lyc8503/sing-box-rules](https://g

- `sites`: 域名规则数组
- `ips`: IP 规则数组
- `domain_suffix`: 域名后缀规则数组
- `ip_cidr`: IP CIDR 规则数组
- `outbound`: 出站名称

示例:
Expand All @@ -120,6 +122,8 @@ Singbox 的规则集来自 [https://github.com/lyc8503/sing-box-rules](https://g
{
"sites": ["google", "anthropic"],
"ips": ["private", "cn"],
"domain_suffix": [".com", ".org"],
"ip_cidr": ["192.168.0.0/16", "10.0.0.0/8"],
"outbound": "🤪 MyCustomRule"
}
]
Expand Down Expand Up @@ -149,7 +153,7 @@ API 在出现问题时将返回适当的 HTTP 状态码和错误消息:

2. 生成带有自定义规则的 Clash 配置:
```
/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
/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%22domain_suffix%22%3A%5B%22.com%22%5D%2C%22ip_cidr%22%3A%5B%2210.0.0.0%2F8%22%5D%2C%22outbound%22%3A%22MyCustomRule%22%7D%5D
```

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

## 结论

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

如有任何问题或功能请求,欢迎联系仓库维护者
如有任何问题或功能请求,欢迎联系[@7Sageer](https://github.com/7Sageer)
Binary file modified 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.
Binary file modified doc/img/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/ClashConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class ClashConfigBuilder extends BaseConfigBuilder {
this.config.rules = rules.flatMap(rule => {
const siteRules = rule.site_rules[0] !== '' ? rule.site_rules.map(site => `GEOSITE,${site},${rule.outbound}`) : [];
const ipRules = rule.ip_rules[0] !== '' ? rule.ip_rules.map(ip => `GEOIP,${ip},${rule.outbound}`) : [];
return [...siteRules, ...ipRules];
const domainSuffixRules = rule.domain_suffix ? rule.domain_suffix.map(suffix => `DOMAIN-SUFFIX,${suffix},${rule.outbound}`) : [];
const ipCidrRules = rule.ip_cidr ? rule.ip_cidr.map(cidr => `IP-CIDR,${cidr},${rule.outbound}`) : [];
return [...siteRules, ...ipRules, ...domainSuffixRules, ...ipCidrRules];
});

// Add the final catch-all rule
Expand Down
9 changes: 6 additions & 3 deletions src/SingboxConfigBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export class ConfigBuilder extends BaseConfigBuilder {
this.config.route.rule_set = [...site_rule_sets, ...ip_rule_sets];

this.config.route.rules = rules.map(rule => ({
rule_set: rule.site_rules.length > 0 && rule.site_rules[0] !== '' ?
[...rule.site_rules, ...rule.ip_rules.filter(ip => ip.trim() !== '').map(ip => `${ip}-ip`)] :
rule.ip_rules.filter(ip => ip.trim() !== '').map(ip => `${ip}-ip`),
rule_set: [
...(rule.site_rules.length > 0 && rule.site_rules[0] !== '' ? rule.site_rules : []),
...(rule.ip_rules.filter(ip => ip.trim() !== '').map(ip => `${ip}-ip`))
],
domain_suffix: rule.domain_suffix,
ip_cidr: rule.ip_cidr,
outbound: rule.outbound
}));
// Add any default rules that should always be present
Expand Down
20 changes: 11 additions & 9 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ export function generateRules(selectedRules = [], customRules = []) {
}
});

if (customRules.length > 0) {
customRules.forEach((rule) => {
rules.push({
site_rules: rule.site.split(','),
ip_rules: rule.ip.split(','),
outbound: rule.name
});
});
}
if (customRules.length > 0) {
customRules.forEach((rule) => {
rules.push({
site_rules: rule.site.split(','),
ip_rules: rule.ip.split(','),
domain_suffix: rule.domain_suffix ? rule.domain_suffix.split(',') : [],
ip_cidr: rule.ip_cidr ? rule.ip_cidr.split(',') : [],
outbound: rule.name
});
});
}

return rules;
}
Expand Down
18 changes: 14 additions & 4 deletions src/htmlBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,9 @@ const submitFormFunction = () => `
const customRules = Array.from(document.querySelectorAll('.custom-rule')).map(rule => ({
site: rule.querySelector('input[name="customRuleSite[]"]').value,
ip: rule.querySelector('input[name="customRuleIP[]"]').value,
name: rule.querySelector('input[name="customRuleName[]"]').value
name: rule.querySelector('input[name="customRuleName[]"]').value,
domain_suffix: rule.querySelector('input[name="customRuleDomainSuffix[]"]').value,
ip_cidr: rule.querySelector('input[name="customRuleIPCIDR[]"]').value
}));
const xrayUrl = \`\${window.location.origin}/xray?config=\${encodeURIComponent(inputString)}\`;
Expand Down Expand Up @@ -717,6 +719,10 @@ const customRuleFunctions = `
newRuleDiv.className = 'custom-rule mb-3 p-3 border rounded';
newRuleDiv.dataset.ruleId = customRuleCount++;
newRuleDiv.innerHTML = \`
<div class="mb-2">
<label class="form-label">Outbound Name*</label>
<input type="text" class="form-control mb-2" name="customRuleName[]" placeholder="Rule Name" required>
</div>
<div class="mb-2">
<label class="form-label">Geo-Site Rule Sets</label>
<span class="tooltip-icon">
Expand All @@ -738,10 +744,14 @@ const customRuleFunctions = `
<input type="text" class="form-control" name="customRuleIP[]" placeholder="e.g., private,cn">
</div>
<div class="mb-2">
<label class="form-label">Outbound Name</label>
<input type="text" class="form-control" name="customRuleName[]" placeholder="e.g., 🤪 Custom Rule" required>
<label class="form-label">Domain Suffix</label>
<input type="text" class="form-control mb-2" name="customRuleDomainSuffix[]" placeholder="Domain Suffix (comma separated)">
</div>
<div class="mb-2">
<label class="form-label">IP CIDR</label>
<input type="text" class="form-control mb-2" name="customRuleIPCIDR[]" placeholder="IP CIDR (comma separated)">
<button type="button" class="btn btn-danger btn-sm" onclick="removeCustomRule(this)">Remove</button>
</div>
<button type="button" class="btn btn-danger btn-sm mt-2" onclick="removeCustomRule(this)">Remove Rule</button>
\`;
customRulesDiv.appendChild(newRuleDiv);
}
Expand Down

0 comments on commit 2697c36

Please sign in to comment.