Skip to content

Commit

Permalink
feat: change how sing-box config is generated
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed May 1, 2024
1 parent d6e798d commit 46d5faa
Show file tree
Hide file tree
Showing 25 changed files with 2,098 additions and 741 deletions.
20 changes: 11 additions & 9 deletions docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { defineUserConfig, HeadConfig, PluginConfig } from 'vuepress'
import { defineUserConfig, type HeadConfig, type PluginConfig, type UserConfig } from 'vuepress'
import { path } from '@vuepress/utils'
import docsearchPlugin from '@vuepress/plugin-docsearch'
import registerComponentsPlugin from '@vuepress/plugin-register-components'
import { docsearchPlugin } from '@vuepress/plugin-docsearch'
import { registerComponentsPlugin } from '@vuepress/plugin-register-components'
import { sitemapPlugin } from 'vuepress-plugin-sitemap2'
import { umamiAnalyticsPlugin } from 'vuepress-plugin-umami-analytics'
import { viteBundler } from '@vuepress/bundler-vite'

import customTheme from './theme'

Expand Down Expand Up @@ -60,14 +61,16 @@ if (process.env.NODE_ENV === 'production') {
sitemapPlugin({
hostname: 'https://surgio.js.org',
}),
umamiAnalyticsPlugin({
id: '444a5a25-af75-4c30-b7a4-6aaba520daf6',
src: 'https://sashimi.royli.dev/sashimi.js',
}),
// umamiAnalyticsPlugin({
// id: '444a5a25-af75-4c30-b7a4-6aaba520daf6',
// src: 'https://sashimi.royli.dev/sashimi.js',
// }) as any,
)
}

export default defineUserConfig({
bundler: viteBundler(),
theme: customTheme,
locales: {
'/': {
lang: 'zh-CN',
Expand All @@ -78,6 +81,5 @@ export default defineUserConfig({
title: meta.title,
description: meta.description,
head,
theme: customTheme,
plugins,
})
}) as UserConfig
2 changes: 1 addition & 1 deletion docs/.vuepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
},
{
text: '客户端规则维护指南',
children: ['/guide/client/clash', '/guide/client/examples'],
children: ['/guide/client/sing-box', '/guide/client/clash', '/guide/client/examples'],
},
'/guide/api',
'/guide/cli',
Expand Down
115 changes: 115 additions & 0 deletions docs/guide/client/sing-box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
sidebarDepth: 1
---

# sing-box

> <Badge text="v3.7.0" vertical="middle" />
因为 sing-box 的配置文件为 JSON 格式,所以我们引入了一种新的方式来维护 sing-box 规则,或是其它 JSON 格式的规则。

## 准备

首先我们找到一份基础的规则文件,它可能是这样的:

```json
{
"inbounds": [],
"outbounds": [
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns"
}
],
"route": {},
"experimental": {
"cache_file": {
"enabled": true
},
"clash_api": {
"external_controller": "127.0.0.1:9090"
}
}
}
```

我们看到此时 `outbounds` 已经包含了一些内容,我们要做的就是把节点信息填充到 `outbounds` 中。

把这个文件保存在 `tempalte` 目录下,命名为 `singbox.json`

## 编写 Artifact

```js{9-26}
const { extendOutbounds } = require('surgio');
module.exports = {
artifacts: [
{
name: 'singbox.json',
template: 'singbox',
templateType: 'json',
extendTemplate: extendOutbounds(
({ getSingboxNodes, getSingboxNodeNames, nodeList }) => [
{
type: 'direct',
tag: 'direct',
tcp_fast_open: false,
tcp_multi_path: true,
},
{
type: 'selector',
tag: 'proxy',
outbounds: ['auto', ...getSingboxNodeNames(nodeList)],
// outbounds: getSingboxNodeNames(nodeList), // 如果你不需要 auto 节点
interrupt_exist_connections: false,
},
...getSingboxNodes(nodeList),
],
),
provider: 'ss',
},
]
}
```

这个配置的含义是:

- `template``singbox`,即我们刚刚创建的模板文件
- `extendTemplate``extendOutbounds`,这个函数会把节点信息填充到 `outbounds`

第 10 行的 `getSingboxNodes`, `getSingboxNodeNames` 属于「模板方法」,具体有哪些可用的模板方法可以看 [这里](/guide/custom-template.md#模板方法)

## `extendOutbounds` 函数

`extendOutbounds` 支持两种写法,一种是直接输入一个不可变的变量,另一种是输入一个函数。变量即确定的不会变化的内容,函数则是相对动态的内容。上面的例子中我们使用了函数的写法。

### 直接输入变量

```js
const { extendOutbounds } = require('surgio');

module.exports = {
artifacts: [
{
name: 'singbox.json',
template: 'singbox',
templateType: 'json',
extendTemplate: extendOutbounds([
{
type: 'direct',
tag: 'direct',
tcp_fast_open: false,
tcp_multi_path: true,
},
]),
provider: 'ss',
},
]
}
```

你可以在 [这里](/guide/custom-template.md#模板方法) 查看这篇文章中提到的所有模板方法的文档。
21 changes: 19 additions & 2 deletions docs/guide/custom-artifact.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sidebarDepth: 2

Surgio 会根据 Artifact 的值来生成配置文件。你可以一次性配置多个 Artifact,一次性生成所有需要的配置文件。

```js
```json5
{
name: 'SurgeV3.conf',
template: 'surge_v3',
Expand All @@ -33,6 +33,23 @@ Surgio 会根据 Artifact 的值来生成配置文件。你可以一次性配置

模板名。会在 `./template` 目录内寻找同名文件(`.tpl` 后缀可省略)。

### templateType

- 类型: `string`
- 默认值: `default`
- 有效值: `default`, `json`
- <Badge text="可选" vertical="middle" />

模板类型。默认为 `default`,即以传统方式解析模板文件。

### extendTemplate

- 类型: `function`
- 默认值: `undefined`
- <Badge text="可选" vertical="middle" />

拓展 JSON 类型的模板,在编写 sing-box 规则时会用到。

### provider

- 类型: `string`
Expand Down Expand Up @@ -81,7 +98,7 @@ Surgio 会根据 Artifact 的值来生成配置文件。你可以一次性配置

例如:

```js
```json5
{
customParams: {
beta: true,
Expand Down
Loading

0 comments on commit 46d5faa

Please sign in to comment.