Skip to content

Commit

Permalink
add upload/download buffer size setting
Browse files Browse the repository at this point in the history
  • Loading branch information
nobody committed Jan 12, 2025
1 parent 3f793eb commit 83b6d65
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/obfus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ jobs:
这个发布使用 [attestations](https://docs.github.com/zh/actions/security-for-github-actions/using-artifact-attestations/using-artifact-attestations-to-establish-provenance-for-builds) 来保证,混淆后的代码没有夹带私货。
当然,自己使用明文源代码做混淆更安全。
`${{ env.projName }}.zip` is the obfuscated code of `${{ env.TAG }}`.
`Source code (zip)` contains the plain text source code.
draft: false
prerelease: false
files: "${{ env.projName }}.zip"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* `IP_QUERY_PATH` 查询客户 IP 信息功能的访问路径,例如: `/ip-query/?key=123456`,留空表示关闭这个功能,后面那个 key 相当于密码
* `LOG_LEVEL` 日志级别,可选值:`debug`, `info`, `error`, `none`
* `TIME_ZONE` 日志时间戳的时区,中国填 `8`
* `BUFFER_SIZE` 上传、下载缓存大小,单位 KiB,我也不知道应该设为多大

#### 注意事项
* src/index.js 是开发中的代码,会有 bug,请到 [releases](https://github.com/vrnobody/cfxhttp/releases) 里面下载 Source code (zip)
Expand Down
1 change: 1 addition & 0 deletions docs/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Visit `https://sub-domain.your-website.com/(XHTTP_PATH)/?fragment=true&uuid=(UUI
* `IP_QUERY_PATH` URL path for querying client IP information feature. e.g. `/ip-query/?key=123456`. Leave it empty to disable this feature. The `key` parameter is used for authentication.
* `LOG_LEVEL` debug, info, error, none
* `TIME_ZONE` Timestamp time zone of logs. e.g. Argentina is `-3`
* `BUFFER_SIZE` Upload/Download buffer size in KiB. I don't know what the optimal size is. XD

#### Notice
* `src/index.js` is under developing, could have bugs, please download `Source code (zip)` from [releases](https://github.com/vrnobody/cfxhttp/releases).
Expand Down
6 changes: 5 additions & 1 deletion docs/update-log.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
### 发布记录
### 发布记录 update log

#### v1.0.5 (2025-01-12)
* 添加上传、下载缓存大小设置项
* add upload/download buffer size setting

#### v1.0.4 (2025-01-10)
* 重构代码
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ const SETTINGS = {
['UPSTREAM_DOH']: 'https://dns.google/dns-query', // upstream DNS over HTTP(S) server

['IP_QUERY_PATH']: '', // URL path for querying client IP information, empty means disabled

['BUFFER_SIZE']: '32', // Upload/Download buffer size in KiB
}

// source code
const BUFFER_SIZE = 128 * 1024 // download/upload buffer-size in bytes, must smaller than 1 MiB

const BAD_REQUEST = new Response(null, {
status: 404,
Expand Down Expand Up @@ -321,7 +322,7 @@ async function read_atleast(reader, n) {
}
}

function create_xhttp_client(cfg, client_readable) {
function create_xhttp_client(cfg, buff_size, client_readable) {
const abort_ctrl = new AbortController()

const buff_stream = new TransformStream(
Expand All @@ -334,7 +335,7 @@ function create_xhttp_client(cfg, client_readable) {
}
},
},
new ByteLengthQueuingStrategy({ highWaterMark: BUFFER_SIZE }),
new ByteLengthQueuingStrategy({ highWaterMark: buff_size }),
)

const headers = {
Expand All @@ -360,7 +361,7 @@ function create_xhttp_client(cfg, client_readable) {
}
}

function create_ws_client(ws_client, ws_server) {
function create_ws_client(buff_size, ws_client, ws_server) {
const abort_ctrl = new AbortController()

const readable = new ReadableStream(
Expand All @@ -384,7 +385,7 @@ function create_ws_client(ws_client, ws_server) {
})
},
},
new ByteLengthQueuingStrategy({ highWaterMark: BUFFER_SIZE }),
new ByteLengthQueuingStrategy({ highWaterMark: buff_size }),
)

const writable = new WritableStream(
Expand All @@ -397,7 +398,7 @@ function create_ws_client(ws_client, ws_server) {
}
},
},
new ByteLengthQueuingStrategy({ highWaterMark: BUFFER_SIZE }),
new ByteLengthQueuingStrategy({ highWaterMark: buff_size }),
)

const resp = new Response(null, {
Expand Down Expand Up @@ -682,6 +683,7 @@ async function handle_request(cfg, log, request) {
}

const path = url.pathname
const buff_size = parseInt(cfg.BUFFER_SIZE) * 1024

if (
cfg.WS_PATH &&
Expand All @@ -690,7 +692,7 @@ async function handle_request(cfg, log, request) {
) {
log.info('handle ws client')
const [ws_client, ws_server] = new WebSocketPair()
const client = create_ws_client(ws_client, ws_server)
const client = create_ws_client(buff_size, ws_client, ws_server)
// Do not block here. Client is waiting for upgrade-response.
setTimeout(() => {
try {
Expand All @@ -709,7 +711,7 @@ async function handle_request(cfg, log, request) {
path.endsWith(cfg.XHTTP_PATH)
) {
log.info('handle xhttp client')
const client = create_xhttp_client(cfg, request.body)
const client = create_xhttp_client(cfg, buff_size, request.body)
const ok = await handle_client(cfg, log, client)
return ok ? client.resp : BAD_REQUEST
}
Expand Down

0 comments on commit 83b6d65

Please sign in to comment.