Compare commits

..
Author SHA1 Message Date
patternihaandClaude Opus 5.5 23f37b571c TUN inbound: IPv6 entries in dns keep the TUN from starting on Windows when IPv6 cannot be set up, as in gateway and autoSystemRoutingTable
Follow-up to #7325: an IPv6 server in dns now asks for IPv6 as well.
When the adapter has no IPv6 interface, or refuses its IPv6 DNS servers,
the TUN fails to start instead of starting without them. Without IPv6 in
any of the three, both are still skipped: the IPv6 DNS servers that Xray
sets on every start are none then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 21:13:11 +03:30
97 changed files with 1974 additions and 4780 deletions
+2 -1
View File
@@ -1,6 +1,5 @@
{
"android-arm64": { "friendlyName": "android-arm64-v8a" },
"android-amd64": { "friendlyName": "android-amd64" },
"darwin-amd64": { "friendlyName": "macos-64" },
"darwin-arm64": { "friendlyName": "macos-arm64-v8a" },
"freebsd-386": { "friendlyName": "freebsd-32" },
@@ -15,7 +14,9 @@
"linux-arm7": { "friendlyName": "linux-arm32-v7a" },
"linux-mips64le": { "friendlyName": "linux-mips64le" },
"linux-mips64": { "friendlyName": "linux-mips64" },
"linux-mipslesoftfloat": { "friendlyName": "linux-mips32le-softfloat" },
"linux-mipsle": { "friendlyName": "linux-mips32le" },
"linux-mipssoftfloat": { "friendlyName": "linux-mips32-softfloat" },
"linux-mips": { "friendlyName": "linux-mips32" },
"linux-ppc64le": { "friendlyName": "linux-ppc64le" },
"linux-ppc64": { "friendlyName": "linux-ppc64" },
+2 -2
View File
@@ -9,8 +9,8 @@ ARG TARGETARCH
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags "-s -w -buildid=" ./main
# Download geodat into a staging directory
ADD https://github.com/XTLS/Xray-rules-dat/releases/latest/download/geoip-full.dat /tmp/geodat/geoip.dat
ADD https://github.com/XTLS/Xray-rules-dat/releases/latest/download/geosite-expand.dat /tmp/geodat/geosite.dat
ADD https://raw.githubusercontent.com/XTLS/Xray-rules-dat/release/geoip/geoip-full.dat /tmp/geodat/geoip.dat
ADD https://raw.githubusercontent.com/XTLS/Xray-rules-dat/release/geosite/geosite-expand.dat /tmp/geodat/geosite.dat
RUN mkdir -p /tmp/empty
+2 -2
View File
@@ -9,8 +9,8 @@ ARG TARGETARCH
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags "-s -w -buildid=" ./main
# Download geodat into a staging directory
ADD https://github.com/XTLS/Xray-rules-dat/releases/latest/download/geoip-full.dat /tmp/geodat/geoip.dat
ADD https://github.com/XTLS/Xray-rules-dat/releases/latest/download/geosite-expand.dat /tmp/geodat/geosite.dat
ADD https://raw.githubusercontent.com/XTLS/Xray-rules-dat/release/geoip/geoip-full.dat /tmp/geodat/geoip.dat
ADD https://raw.githubusercontent.com/XTLS/Xray-rules-dat/release/geosite/geosite-expand.dat /tmp/geodat/geosite.dat
RUN mkdir -p /tmp/empty
+1 -1
View File
@@ -82,7 +82,7 @@ jobs:
uses: docker/setup-buildx-action@v4
- name: Login to GitHub Container Registry
uses: docker/login-action@v4.8.0
uses: docker/login-action@v4.7.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
+13 -8
View File
@@ -1,14 +1,17 @@
name: Build and Release for Windows 7
# Pre-compiling support removal since 1.29
on:
workflow_dispatch:
release:
types: [published]
push:
pull_request:
types: [opened, synchronize, reopened]
jobs:
check-assets:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
steps:
- name: Restore Geodat Cache
uses: actions/cache/restore@v6
@@ -112,6 +115,8 @@ jobs:
COMMID=$(git describe --always --dirty)
echo 'Building Xray for Windows 7...'
go build -o build_assets/xray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
# The line below is for without running conhost.exe version. Commented for not being used. Provided for reference.
# go build -o build_assets/wxray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-H windowsgui -X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
- name: Restore Geodat Cache
uses: actions/cache/restore@v6
@@ -147,13 +152,6 @@ jobs:
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
- name: Upload files to Artifacts
uses: actions/upload-artifact@v7
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
./build_assets/*
- name: Create ZIP archive
if: github.event_name == 'release'
shell: bash
@@ -177,3 +175,10 @@ jobs:
file: ./Xray-${{ env.ASSET_NAME }}.zip*
tag: ${{ github.ref }}
file_glob: true
- name: Upload files to Artifacts
uses: actions/upload-artifact@v7
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
./build_assets/*
+90 -183
View File
@@ -79,23 +79,86 @@ jobs:
contents: write
strategy:
matrix:
# Include amd64 and arm64 on all platforms.
goos: [windows, freebsd, openbsd, linux, darwin, android]
goarch: [amd64, arm64]
# Include amd64 on all platforms.
goos: [windows, freebsd, openbsd, linux, darwin]
goarch: [amd64, 386]
patch-assetname: [""]
exclude:
# Exclude i386 on darwin
- goarch: 386
goos: darwin
include:
# BEGIN Linux specified
# BEGIN MacOS ARM64
- goos: darwin
goarch: arm64
# END MacOS ARM64
# BEGIN Linux ARM 5 6 7
- goos: linux
goarch: ppc64
goarch: arm
goarm: 7
- goos: linux
goarch: ppc64le
goarch: arm
goarm: 6
- goos: linux
goarch: s390x
goarch: arm
goarm: 5
# END Linux ARM 5 6 7
# BEGIN Android ARM 8
- goos: android
goarch: arm64
# END Android ARM 8
# BEGIN Android AMD64
- goos: android
goarch: amd64
patch-assetname: android-amd64
# END Android AMD64
# Windows ARM
- goos: windows
goarch: arm64
# BEGIN Other architectures
# BEGIN riscv64 & ARM64 & LOONG64
- goos: linux
goarch: arm64
- goos: linux
goarch: riscv64
- goos: linux
goarch: loong64
# END Linux specified
# END riscv64 & ARM64 & LOONG64
# BEGIN MIPS
- goos: linux
goarch: mips64
- goos: linux
goarch: mips64le
- goos: linux
goarch: mipsle
- goos: linux
goarch: mips
# END MIPS
# BEGIN PPC
- goos: linux
goarch: ppc64
- goos: linux
goarch: ppc64le
# END PPC
# BEGIN FreeBSD ARM
- goos: freebsd
goarch: arm64
- goos: freebsd
goarch: arm
goarm: 7
# END FreeBSD ARM
# BEGIN S390X
- goos: linux
goarch: s390x
# END S390X
# END Other architectures
# BEGIN OPENBSD ARM
- goos: openbsd
goarch: arm64
- goos: openbsd
goarch: arm
goarm: 7
# END OPENBSD ARM
fail-fast: false
runs-on: ubuntu-latest
@@ -103,6 +166,7 @@ jobs:
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOARM: ${{ matrix.goarm }}
CGO_ENABLED: 0
steps:
- name: Checkout codebase
@@ -124,8 +188,8 @@ jobs:
- name: Show workflow information
run: |
_NAME=${{ matrix.patch-assetname }}
[ -n "$_NAME" ] || _NAME=$(jq ".[\"$GOOS-$GOARCH\"].friendlyName" -r < .github/build/friendly-filenames.json)
echo "GOOS: $GOOS, GOARCH: $GOARCH, RELEASE_NAME: $_NAME"
[ -n "$_NAME" ] || _NAME=$(jq ".[\"$GOOS-$GOARCH$GOARM$GOMIPS\"].friendlyName" -r < .github/build/friendly-filenames.json)
echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, GOMIPS: $GOMIPS, RELEASE_NAME: $_NAME"
echo "ASSET_NAME=$_NAME" >> $GITHUB_ENV
- name: Set up Go
@@ -139,18 +203,6 @@ jobs:
- name: Build Xray
run: |
case "${GOARCH}:${GOOS}" in
# Special processing for x86-64 and ARM64
amd64:windows|amd64:darwin)
GOAMD64="v2"
;;
arm64:windows)
GOARM64="v8.2"
;;
arm64:darwin)
GOARM64="v8.4"
;;
esac
mkdir -p build_assets
COMMID=$(git describe --always --dirty)
if [[ ${GOOS} == 'windows' ]]; then
@@ -160,164 +212,13 @@ jobs:
# go build -o build_assets/wxray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-H windowsgui -X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
elif [[ ${GOOS} == 'android' ]]; then
echo 'Building Xray for Android...'
go build -o build_assets/xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid= -checklinkname=0 -extldflags=-Wl,--pack-dyn-relocs=android" -v ./main
else
echo 'Building Xray...'
go build -o build_assets/xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
fi
- name: Restore Geodat Cache
uses: actions/cache/restore@v6
with:
path: resources
key: xray-geodat-
- name: Restore Wintun Cache
if: matrix.goos == 'windows'
uses: actions/cache/restore@v6
with:
path: resources
key: xray-wintun-
- name: Add additional assets into package
run: |
mv -f resources/geo* build_assets/
if [[ ${GOOS} == 'windows' ]]; then
cp .github/build/windows/* build_assets/
fi
if [[ ${GOOS} == 'windows' ]]; then
echo 'Adding Wintun into packages'
if [[ ${GOARCH} == 'amd64' ]]; then
mv resources/wintun/bin/amd64/wintun.dll build_assets/
fi
if [[ ${GOARCH} == 'arm64' ]]; then
mv resources/wintun/bin/arm64/wintun.dll build_assets/
fi
mv resources/wintun/LICENSE.txt build_assets/LICENSE-Wintun
fi
- name: Copy README.md & LICENSE
run: |
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
- name: Upload files to Artifacts
uses: actions/upload-artifact@v7
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
./build_assets/*
- name: Create ZIP archive
if: github.event_name == 'release'
shell: bash
run: |
pushd build_assets || exit 1
touch -mt $(date +%Y01010000) *
zip -9vr ../Xray-${{ env.ASSET_NAME }}.zip .
popd || exit 1
FILE=./Xray-${{ env.ASSET_NAME }}.zip
DGST=$FILE.dgst
for METHOD in {"md5","sha1","sha256","sha512"}
do
openssl dgst -$METHOD $FILE | sed 's/([^)]*)//g' >>$DGST
done
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
if: github.event_name == 'release'
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ./Xray-${{ env.ASSET_NAME }}.zip*
tag: ${{ github.ref }}
file_glob: true
build-release-only:
needs: check-assets
permissions:
contents: write
strategy:
matrix:
# First batch (since Go 1.29) of removal:
# x86: windows, linux, freebsd, openbsd
# arm: linux(v5), linux(v6)
# other: linux(mips), linux(mips64), linux(mips64le)
# Second batch (needs discussion) of removal
# arm(v7): freebsd, openbsd, linux
# other: linux(mipsle)
goos: [windows, freebsd, openbsd, linux]
goarch: [arm, 386]
goarm: [7]
exclude:
- goos: windows
goarch: arm
include:
# Second batch of removal [In the future]
- goos: linux
goarch: mipsle
# END of second batch
# First batch of removal [Go 1.29]
- goos: linux
goarch: arm
goarm: 5
- goos: linux
goarch: arm
goarm: 6
- goos: linux
goarch: mips
- goos: linux
goarch: mips64
- goos: linux
goarch: mips64le
# END of first batch
fail-fast: false
runs-on: ubuntu-latest
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
GOARM: ${{ matrix.goarm }}
CGO_ENABLED: 0
steps:
- name: Checkout codebase
uses: actions/checkout@v7
- name: Show workflow information
run: |
_NAME=${{ matrix.patch-assetname }}
if [[ ${GOARCH} == 'arm' ]]; then
[ -n "$_NAME" ] || _NAME=$(jq ".[\"$GOOS-$GOARCH$GOARM\"].friendlyName" -r < .github/build/friendly-filenames.json)
echo "GOOS: $GOOS, GOARCH: $GOARCH, GOARM: $GOARM, RELEASE_NAME: $_NAME"
else
echo "GOARM=''" >> $GITHUB_ENV
[ -n "$_NAME" ] || _NAME=$(jq ".[\"$GOOS-$GOARCH\"].friendlyName" -r < .github/build/friendly-filenames.json)
echo "GOOS: $GOOS, GOARCH: $GOARCH, RELEASE_NAME: $_NAME"
fi
echo "ASSET_NAME=$_NAME" >> $GITHUB_ENV
- name: Set up Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
check-latest: true
- name: Get project dependencies
run: go mod download
- name: Build Xray
run: |
mkdir -p build_assets
COMMID=$(git describe --always --dirty)
if [[ ${GOOS} == 'windows' ]]; then
echo 'Building Xray for Windows...'
go build -o build_assets/xray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
go build -o build_assets/xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid= -checklinkname=0" -v ./main
else
echo 'Building Xray...'
if [[ ${GOARCH} == 'mips' || ${GOARCH} == 'mipsle' ]]; then
go build -o build_assets/xray -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
go build -o build_assets/xray -trimpath -buildvcs=false -gcflags="-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
echo 'Building soft-float Xray for MIPS/MIPSLE 32-bit...'
GOMIPS=softfloat go build -o build_assets/xray_softfloat -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
GOMIPS=softfloat go build -o build_assets/xray_softfloat -trimpath -buildvcs=false -gcflags="-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
else
go build -o build_assets/xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=${COMMID} -s -w -buildid=" -v ./main
fi
@@ -344,9 +245,15 @@ jobs:
fi
if [[ ${GOOS} == 'windows' ]]; then
echo 'Adding Wintun into packages'
if [[ ${GOARCH} == 'amd64' ]]; then
mv resources/wintun/bin/amd64/wintun.dll build_assets/
fi
if [[ ${GOARCH} == '386' ]]; then
mv resources/wintun/bin/x86/wintun.dll build_assets/
fi
if [[ ${GOARCH} == 'arm64' ]]; then
mv resources/wintun/bin/arm64/wintun.dll build_assets/
fi
mv resources/wintun/LICENSE.txt build_assets/LICENSE-Wintun
fi
@@ -355,13 +262,6 @@ jobs:
cp ${GITHUB_WORKSPACE}/README.md ./build_assets/README.md
cp ${GITHUB_WORKSPACE}/LICENSE ./build_assets/LICENSE
- name: Upload files to Artifacts
uses: actions/upload-artifact@v7
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
./build_assets/*
- name: Create ZIP archive
if: github.event_name == 'release'
shell: bash
@@ -385,3 +285,10 @@ jobs:
file: ./Xray-${{ env.ASSET_NAME }}.zip*
tag: ${{ github.ref }}
file_glob: true
- name: Upload files to Artifacts
uses: actions/upload-artifact@v7
with:
name: Xray-${{ env.ASSET_NAME }}
path: |
./build_assets/*
+5 -26
View File
@@ -231,40 +231,19 @@ Make sure that you are using the same Go version, and remember to set the git co
CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
```
For Windows (x86-64 and AArch64, not win7):
```bash
GOOS=windows GOARCH=amd64 GOAMD64=v2 CGO_ENABLED=0 go build -o xray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
GOOS=windows GOARCH=arm64 GOARM64=v8.2 CGO_ENABLED=0 go build -o xray.exe -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
```
For macOS:
```bash
GOOS=darwin GOARCH=amd64 GOAMD64=v2 CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
GOOS=darwin GOARCH=arm64 GOARM64=v8.4 CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
```
For Android:
```bash
GOOS=android GOARCH=arm64 CGO_ENABLED=1 CC=/path/to/aarch64-linux-android24-clang go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid= -checklinkname=0 -extldflags=-Wl,--pack-dyn-relocs=android" -v ./main
GOOS=android GOARCH=amd64 CGO_ENABLED=1 CC=/path/to/x86_64-linux-android24-clang go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid= -checklinkname=0 -extldflags=-Wl,--pack-dyn-relocs=android" -v ./main
GOOS=android GOARCH=arm64 CGO_ENABLED=1 CC=/path/to/aarch64-linux-android24-clang go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid= -checklinkname=0" -v ./main
GOOS=android GOARCH=amd64 CGO_ENABLED=1 CC=/path/to/x86_64-linux-android24-clang go build -o xray -trimpath -buildvcs=false -gcflags="all=-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid= -checklinkname=0" -v ./main
```
For Linux (MIPS32 and MIPS32LE):
If you are compiling a 32-bit MIPS/MIPSLE target, use this command instead:
```bash
GOOS=linux GOARCH=mips GOMIPS=hardfloat CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
GOOS=linux GOARCH=mips GOMIPS=softfloat CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
GOOS=linux GOARCH=mipsle GOMIPS=hardfloat CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
GOOS=linux GOARCH=mipsle GOMIPS=softfloat CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
CGO_ENABLED=0 go build -o xray -trimpath -buildvcs=false -gcflags="-l=4" -ldflags="-X github.com/xtls/xray-core/core.build=REPLACE -s -w -buildid=" -v ./main
```
## Stargazers over time
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=xtls/xray-core&type=date&theme=dark&legend=top-left" />
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=xtls/xray-core&type=date&legend=top-left" />
<img alt="Star History Chart" src="https://api.star-history.com/chart?repos=xtls/xray-core&type=date&legend=top-left" />
</picture>
[![Stargazers over time](https://starchart.cc/XTLS/Xray-core.svg)](https://starchart.cc/XTLS/Xray-core)
+5 -13
View File
@@ -29,7 +29,6 @@ type MetricsHandler struct {
listen string
tcpListener xnet.Listener
listener *OutboundListener
server *http.Server
}
// NewMetricsHandler creates a new MetricsHandler based on the given config.
@@ -51,8 +50,7 @@ func (p *MetricsHandler) Type() interface{} {
}
func (p *MetricsHandler) Start() error {
server := &http.Server{Handler: p.httpHandler()}
p.server = server
handler := p.httpHandler()
// direct listen a port if listen is set
if p.listen != "" {
@@ -63,7 +61,7 @@ func (p *MetricsHandler) Start() error {
p.tcpListener = TCPlistener
errors.LogInfo(context.Background(), "Metrics server listening on ", p.listen)
go p.serve(TCPlistener, server)
go p.serve(TCPlistener, handler)
}
if p.tag == "" {
@@ -79,7 +77,7 @@ func (p *MetricsHandler) Start() error {
}
p.listener = listener
go p.serve(listener, server)
go p.serve(listener, handler)
if err := p.ohm.RemoveHandler(context.Background(), p.tag); err != nil {
errors.LogInfo(context.Background(), "failed to remove existing handler")
@@ -108,12 +106,6 @@ func (p *MetricsHandler) Close() error {
errs = append(errs, p.listener.Close())
p.listener = nil
}
if p.server != nil {
if err := p.server.Close(); !isClosedListenerError(err) {
errs = append(errs, err)
}
p.server = nil
}
if p.ohm != nil && p.tag != "" {
if err := p.ohm.RemoveHandler(context.Background(), p.tag); err != nil {
errors.LogInfo(context.Background(), "failed to remove metrics handler")
@@ -122,8 +114,8 @@ func (p *MetricsHandler) Close() error {
return errors.Combine(errs...)
}
func (p *MetricsHandler) serve(listener xnet.Listener, server *http.Server) {
if err := server.Serve(listener); err != nil && !isClosedListenerError(err) {
func (p *MetricsHandler) serve(listener xnet.Listener, handler http.Handler) {
if err := http.Serve(listener, handler); err != nil && !isClosedListenerError(err) {
errors.LogErrorInner(context.Background(), err, "failed to start metrics server")
}
}
+1 -11
View File
@@ -51,17 +51,7 @@ func (m *Manager) AddHandler(ctx context.Context, handler inbound.Handler) error
}
if m.running {
if err := handler.Start(); err != nil {
if len(tag) > 0 {
delete(m.taggedHandlers, tag)
} else {
m.untaggedHandlers = m.untaggedHandlers[:len(m.untaggedHandlers)-1]
}
if err := handler.Close(); err != nil {
errors.LogWarningInner(ctx, err, "failed to close handler ", tag)
}
return err
}
return handler.Start()
}
return nil
+2 -5
View File
@@ -125,9 +125,6 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
if config.Concurrency == 0 {
config.Concurrency = 8 // same as before
}
if config.Concurrency > 0 && config.Concurrency < 128 && h.streamSettings != nil && h.streamSettings.ProtocolName == "xdrive" {
config.Concurrency = 128
}
if config.Concurrency > 0 {
h.mux = &mux.ClientManager{
Enabled: true,
@@ -137,7 +134,7 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
Dialer: h,
Strategy: mux.ClientStrategy{
MaxConcurrency: uint32(config.Concurrency),
MaxConnection: 1024,
MaxConnection: 128,
},
},
},
@@ -158,7 +155,7 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (outbou
Dialer: h,
Strategy: mux.ClientStrategy{
MaxConcurrency: uint32(config.XudpConcurrency),
MaxConnection: 1024,
MaxConnection: 128,
},
},
},
+1 -1
View File
@@ -73,7 +73,7 @@ func (r *TimeoutWrapperReader) ReadMultiBufferTimeout(duration time.Duration) (M
}
return r.mb, r.err
case <-timeout:
return nil, ErrReadTimeout
return nil, nil
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
//go:build !windows && !openbsd
//go:build !windows && !wasm && !illumos && !openbsd
// +build !windows,!wasm,!illumos,!openbsd
package buf
+2 -1
View File
@@ -1,4 +1,5 @@
//go:build !openbsd
//go:build !wasm && !openbsd
// +build !wasm,!openbsd
package buf
+2 -1
View File
@@ -1,4 +1,5 @@
//go:build openbsd
//go:build wasm || openbsd
// +build wasm openbsd
package buf
+2 -1
View File
@@ -1,4 +1,5 @@
//go:build !openbsd
//go:build !wasm && !openbsd
// +build !wasm,!openbsd
package buf_test
+37
View File
@@ -0,0 +1,37 @@
//go:build illumos
// +build illumos
package buf
import "golang.org/x/sys/unix"
type unixReader struct {
iovs [][]byte
}
func (r *unixReader) Init(bs []*Buffer) {
iovs := r.iovs
if iovs == nil {
iovs = make([][]byte, 0, len(bs))
}
for _, b := range bs {
iovs = append(iovs, b.v)
}
r.iovs = iovs
}
func (r *unixReader) Read(fd uintptr) int32 {
n, e := unix.Readv(int(fd), r.iovs)
if e != nil {
return -1
}
return int32(n)
}
func (r *unixReader) Clear() {
r.iovs = r.iovs[:0]
}
func newMultiReader() multiReader {
return &unixReader{}
}
-4
View File
@@ -236,10 +236,6 @@ func (w *BufferedWriter) Close() error {
return common.Close(w.writer)
}
func (w *BufferedWriter) Interrupt() {
common.Interrupt(w.writer)
}
// SequentialWriter is a Writer that writes MultiBuffer sequentially into the underlying io.Writer.
type SequentialWriter struct {
io.Writer
@@ -1,4 +1,5 @@
//go:build generate
// +build generate
package main
-3
View File
@@ -188,9 +188,6 @@ var (
// NewClientWorker creates a new mux.Client.
func NewClientWorker(stream transport.Link, s ClientStrategy) (*ClientWorker, error) {
writer := buf.NewBufferedWriter(stream.Writer)
writer.SetBuffered(false)
stream.Writer = writer
c := &ClientWorker{
sessionManager: NewSessionManager(),
link: stream,
+1 -3
View File
@@ -93,11 +93,9 @@ type ServerWorker struct {
}
func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.Link) (*ServerWorker, error) {
writer := buf.NewBufferedWriter(link.Writer)
writer.SetBuffered(false)
worker := &ServerWorker{
dispatcher: d,
link: &transport.Link{Reader: link.Reader, Writer: writer},
link: link,
sessionManager: NewSessionManager(),
done: done.New(),
timer: time.NewTicker(60 * time.Second),
+1
View File
@@ -1,4 +1,5 @@
//go:build !windows
// +build !windows
package platform
+1
View File
@@ -1,4 +1,5 @@
//go:build windows
// +build windows
package platform
+2 -2
View File
@@ -19,8 +19,8 @@ import (
var (
Version_x byte = 26
Version_y byte = 11
Version_z byte = 9
Version_y byte = 10
Version_z byte = 31
)
var (
+5 -5
View File
@@ -3,7 +3,7 @@ module github.com/xtls/xray-core
go 1.27
require (
github.com/apernet/quic-go v0.63.1-0.20261004180939-a10df75c260c
github.com/apernet/quic-go v0.61.1-0.20260806010916-184d081eef3e
github.com/bufbuild/protocompile v0.14.1
github.com/ghodss/yaml v1.0.1-0.20220118164431-d8423dcdf344
github.com/golang/mock v1.7.0-rc.1
@@ -16,11 +16,11 @@ require (
github.com/pelletier/go-toml v1.9.5
github.com/pion/stun/v3 v3.1.7
github.com/pires/go-proxyproto v0.16.0
github.com/refraction-networking/utls v1.8.3-0.20261108164504-a13c59ffe98b
github.com/refraction-networking/utls v1.8.3-0.20260301010127-aa6edf4b11af
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.12.1
github.com/vishvananda/netlink v1.3.1
github.com/xtls/reality v0.0.0-20261108101329-4bb46b2afd8b
github.com/xtls/reality v0.0.0-20261014101318-83f621788ca8
github.com/yuin/gopher-lua v1.1.2
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/crypto v0.58.0
@@ -41,14 +41,14 @@ require (
)
require (
github.com/andybalholm/brotli v1.2.3 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/huin/goupnp v1.2.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/juju/ratelimit v1.0.2 // indirect
github.com/klauspost/compress v1.20.1 // indirect
github.com/klauspost/compress v1.20.0 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/molecule-man/go-brrr v1.2.0 // indirect
github.com/pion/dtls/v3 v3.1.5 // indirect
github.com/pion/logging v0.2.4 // indirect
github.com/pion/transport/v4 v4.1.0 // indirect
+12 -10
View File
@@ -1,5 +1,7 @@
github.com/apernet/quic-go v0.63.1-0.20261004180939-a10df75c260c h1:cxK8qTA0YCsj68A7zLYDCfPgMTOyt3ePkHh+oXEmYU0=
github.com/apernet/quic-go v0.63.1-0.20261004180939-a10df75c260c/go.mod h1:J7UZVOMF2M1LiE3iiUZWPuy8YCPAcLJOwVURrfJCvc0=
github.com/andybalholm/brotli v1.2.3 h1:8H1qwOkl2LPfjf3YezB90JnCliZb6SInJ/OJkEbA5NQ=
github.com/andybalholm/brotli v1.2.3/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/apernet/quic-go v0.61.1-0.20260806010916-184d081eef3e h1:5mgtR5gwIgBKMiGI1QdXldZZ+SNor06Nbu1wCBulQBg=
github.com/apernet/quic-go v0.61.1-0.20260806010916-184d081eef3e/go.mod h1:x7qxEvX6MCVtDuBKHj3E+88+BtrbEMuAL5qGUKItjW8=
github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@@ -31,8 +33,8 @@ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7Bd
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ=
github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
github.com/klauspost/compress v1.20.0/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/cpuid/v2 v2.4.2 h1:xRw7IZEsAgVCp6trS/DgWbTAqpixhmywgVgBjYGg1bQ=
github.com/klauspost/cpuid/v2 v2.4.2/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/koron/go-ssdp v0.0.4 h1:1IDwrghSKYM7yLf7XCzbByg2sJ/JcNOZRXS2jczTwz0=
@@ -47,8 +49,6 @@ github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9t
github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ=
github.com/miekg/dns v1.1.73 h1:uhT8nJxmTrPJYClxVxTCX+CVn6qnzSiybRk72Z6DgrE=
github.com/miekg/dns v1.1.73/go.mod h1:RW2Obtfd5NZHvOFe3zYG0W8koWOQtAzyHaLo8vASBuQ=
github.com/molecule-man/go-brrr v1.2.0 h1:dOJU45BC3Gc2WXoxAW0rgCAgVuu2gruAOGncfieA0Jw=
github.com/molecule-man/go-brrr v1.2.0/go.mod h1:7ybW6/7gA3oKY45jOfVNjSJDtrr6ea4tzbsTkjmQDC4=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
@@ -67,8 +67,8 @@ github.com/quic-go/go-ossfuzz-seeds v0.1.0 h1:APacT+iIaNF6fd8AGEiN3bT/Jtkd2jz4v4
github.com/quic-go/go-ossfuzz-seeds v0.1.0/go.mod h1:3IOHRbJIc+L6YKMwfDtJAM9Vj9k0YY4muhuyUYk5tbk=
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
github.com/refraction-networking/utls v1.8.3-0.20261108164504-a13c59ffe98b h1:Mi/DuaTxHeNIQ9g1ylZicAqa6Cb9wkHRiNtQH9uZ7Ts=
github.com/refraction-networking/utls v1.8.3-0.20261108164504-a13c59ffe98b/go.mod h1:rc/ctWeKlh9LSk5k90KlSPcwF7P6D31mn/9GI2NAfBI=
github.com/refraction-networking/utls v1.8.3-0.20260301010127-aa6edf4b11af h1:er2acxbi3N1nvEq6HXHUAR1nTWEJmQfqiGR8EVT9rfs=
github.com/refraction-networking/utls v1.8.3-0.20260301010127-aa6edf4b11af/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.16.0 h1:O9DK+vNMDVGLr2BeZqmpLeMjiMNkuXfcqntWbZV6S5g=
@@ -81,8 +81,10 @@ github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zd
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
github.com/xtls/reality v0.0.0-20261108101329-4bb46b2afd8b h1:fAn11r21QiUY2oAGwENWb5ummjhWwMcs2WzkbjDMh+k=
github.com/xtls/reality v0.0.0-20261108101329-4bb46b2afd8b/go.mod h1:ptlSkJ8k/7IDauNH0SA96JPOo+nYGLTv2Y3UMpwvAsY=
github.com/xtls/reality v0.0.0-20261014101318-83f621788ca8 h1:RjLnVvlzK4q6zWaSBPNRGsrgx2fbJDeUWffjw574uzs=
github.com/xtls/reality v0.0.0-20261014101318-83f621788ca8/go.mod h1:ptlSkJ8k/7IDauNH0SA96JPOo+nYGLTv2Y3UMpwvAsY=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
github.com/yuin/gopher-lua v1.1.2 h1:yF/FjE3hD65tBbt0VXLE13HWS9h34fdzJmrWRXwobGA=
+1 -11
View File
@@ -2,7 +2,6 @@ package conf
import (
"net/url"
"strings"
"github.com/robfig/cron/v3"
"github.com/xtls/xray-core/app/geodata"
@@ -20,20 +19,11 @@ func (c *GeodataAssetConfig) Build() (*geodata.Asset, error) {
return nil, errors.New("invalid geodata asset url: ", c.URL).Base(err)
}
return &geodata.Asset{
Url: migrateGeodataURL(c.URL),
Url: c.URL,
File: c.File,
}, nil
}
func migrateGeodataURL(assetURL string) string {
if strings.HasSuffix(strings.ToLower(assetURL), ".dat") && (strings.HasPrefix(strings.ToLower(assetURL), "https://github.com/xtls/xray-rules-dat/") ||
strings.HasPrefix(strings.ToLower(assetURL), "https://raw.githubusercontent.com/xtls/xray-rules-dat/")) {
errors.PrintDeprecatedFeatureWarning(assetURL, assetURL+".gz")
return assetURL + ".gz"
}
return assetURL
}
func validateHTTPS(s string) error {
u, err := url.ParseRequestURI(s)
if err != nil {
+10 -50
View File
@@ -932,17 +932,9 @@ func (c *XDNS) Build() (proto.Message, error) {
}
type XMC struct {
Hostname string `json:"hostname"`
Profiles []XMCProfile `json:"profiles"`
Password string `json:"password"`
Paddings []XMCPaddingTurn `json:"paddings,omitempty"`
}
type XMCPaddingTurn struct {
Length string `json:"length"`
Direction string `json:"direction"`
// Delay is an optional millisecond value or inclusive min-max range.
Delay string `json:"delay,omitempty"`
Hostname string `json:"hostname"`
Profiles []XMCProfile `json:"profiles"`
Password string `json:"password"`
}
type XMCProfile struct {
@@ -985,41 +977,6 @@ func (c *XMC) Build() (proto.Message, error) {
if c.Password == "" {
return nil, fmt.Errorf("empty password")
}
if len(c.Password) > xmc.MaxLegacyPasswordLength {
return nil, fmt.Errorf("minecraft password is longer than %d bytes", xmc.MaxLegacyPasswordLength)
}
config := &xmc.Config{Password: c.Password, Hostname: c.Hostname}
for i, turn := range c.Paddings {
minimum, maximum, err := ParseRangeString(turn.Length)
if err != nil {
return nil, fmt.Errorf("minecraft padding turn %d length: %w", i, err)
}
delayMinimum, delayMaximum, err := ParseRangeString(turn.Delay)
if err != nil {
return nil, fmt.Errorf("minecraft padding turn %d delay: %w", i, err)
}
var direction int32
switch turn.Direction {
case "c2s", "client-to-server":
direction = 1
case "s2c", "server-to-client":
direction = 2
default:
return nil, fmt.Errorf("minecraft padding turn %d: invalid direction %q (must be \"c2s\" or \"s2c\")", i, turn.Direction)
}
config.Paddings = append(config.Paddings, &xmc.Padding{
LengthMin: int64(minimum),
LengthMax: int64(maximum),
Direction: direction,
DelayMin: int64(delayMinimum),
DelayMax: int64(delayMaximum),
})
}
if err := config.ValidatePadding(); err != nil {
return nil, fmt.Errorf("minecraft padding: %w", err)
}
rsaPrivateKey, err := xmc.DeriveRSAKey(c.Password)
if err != nil {
@@ -1040,10 +997,13 @@ func (c *XMC) Build() (proto.Message, error) {
profiles = append(profiles, profile)
}
config.RsaPrivateKey = x509.MarshalPKCS1PrivateKey(rsaPrivateKey)
config.RsaPublicKey = rsaPublicKey
config.Profiles = profiles
return config, nil
return &xmc.Config{
Password: c.Password,
Hostname: c.Hostname,
RsaPrivateKey: x509.MarshalPKCS1PrivateKey(rsaPrivateKey),
RsaPublicKey: rsaPublicKey,
Profiles: profiles,
}, nil
}
type Xicmp struct {
@@ -1,7 +1,6 @@
package conf
import (
"encoding/json"
"strings"
"testing"
@@ -11,11 +10,6 @@ import (
func TestXMCBuildProfile(t *testing.T) {
built, err := (&XMC{
Password: "test-password",
Paddings: []XMCPaddingTurn{
{Length: "3", Direction: "c2s"},
{Length: "127-129", Direction: "s2c", Delay: "100"},
{Length: "8388608", Direction: "c2s", Delay: "50-150"},
},
Profiles: []XMCProfile{
{
Username: "TestUser",
@@ -32,19 +26,6 @@ func TestXMCBuildProfile(t *testing.T) {
if len(config.Profiles) != 1 || len(config.Profiles[0].Uuid) != 16 {
t.Fatalf("unexpected profiles: %+v", config.Profiles)
}
if len(config.Paddings) != 3 || config.Paddings[0].LengthMin != 3 || config.Paddings[0].LengthMax != 3 ||
config.Paddings[1].LengthMin != 127 || config.Paddings[1].LengthMax != 129 ||
config.Paddings[2].LengthMin != 8388608 || config.Paddings[2].LengthMax != 8388608 {
t.Fatalf("unexpected padding: %v", config.Paddings)
}
if config.Paddings[0].Direction != 1 || config.Paddings[1].Direction != 2 || config.Paddings[2].Direction != 1 {
t.Fatalf("unexpected padding directions: %v, %v, %v", config.Paddings[0].Direction, config.Paddings[1].Direction, config.Paddings[2].Direction)
}
if config.Paddings[0].DelayMin != 0 || config.Paddings[0].DelayMax != 0 ||
config.Paddings[1].DelayMin != 100 || config.Paddings[1].DelayMax != 100 ||
config.Paddings[2].DelayMin != 50 || config.Paddings[2].DelayMax != 150 {
t.Fatalf("unexpected padding delays: %v, %v, %v", config.Paddings[0].DelayMin, config.Paddings[1].DelayMin, config.Paddings[2].DelayMin)
}
}
func TestXMCBuildRequiresProfile(t *testing.T) {
@@ -53,62 +34,3 @@ func TestXMCBuildRequiresProfile(t *testing.T) {
t.Fatalf("expected required profiles error, got %v", err)
}
}
func TestXMCBuildRejectsPadding(t *testing.T) {
for name, padding := range map[string]string{
"empty_length": `[{"length": "", "direction": "c2s"}]`,
"zero_length": `[{"length": "0", "direction": "c2s"}]`,
"one_length": `[{"length": "1", "direction": "c2s"}]`,
"two_length": `[{"length": "2", "direction": "c2s"}]`,
"negative_length": `[{"length": "-1", "direction": "c2s"}]`,
"reversed_range": `[{"length": "64-32", "direction": "c2s"}]`,
"oversized_length": `[{"length": "8388609", "direction": "c2s"}]`,
"huge_length": `[{"length": "4294967299", "direction": "c2s"}]`,
"overflow_length": `[{"length": "9223372036854775808", "direction": "c2s"}]`,
"invalid_range_format": `[{"length": "1-2-3", "direction": "c2s"}]`,
"invalid_direction": `[{"length": "3", "direction": "invalid"}]`,
"missing_direction": `[{"length": "3"}]`,
"numeric_direction": `[{"length": "3", "direction": "1"}]`,
"negative_delay": `[{"length": "3", "direction": "c2s", "delay": "-1"}]`,
"reversed_delay": `[{"length": "3", "direction": "c2s", "delay": "10-5"}]`,
"invalid_delay_format": `[{"length": "3", "direction": "c2s", "delay": "abc"}]`,
"huge_delay": `[{"length": "3", "direction": "c2s", "delay": "9223372036854775807"}]`,
} {
t.Run(name, func(t *testing.T) {
var config XMC
if err := json.Unmarshal([]byte(`{"paddings":`+padding+`}`), &config); err != nil {
t.Fatal(err)
}
config.Password = "test-password"
config.Profiles = []XMCProfile{{
Username: "TestUser", UUID: "00112233-4455-6677-8899-aabbccddeeff",
TexturesValue: "textures-value", TexturesSignature: "textures-signature",
}}
if _, err := config.Build(); err == nil {
t.Fatalf("expected error for padding %s, got success", padding)
} else {
t.Logf("correctly rejected: %v", err)
}
})
}
}
func TestXMCBuildRejectsLongPassword(t *testing.T) {
profile := XMCProfile{
Username: "TestUser", UUID: "00112233-4455-6677-8899-aabbccddeeff",
TexturesValue: "textures-value", TexturesSignature: "textures-signature",
}
// The verify token and the password share one RSA-1024 PKCS#1 v1.5 block,
// so the password cannot exceed xmc.MaxLegacyPasswordLength bytes.
valid := strings.Repeat("p", xmc.MaxLegacyPasswordLength)
if _, err := (&XMC{Password: valid, Profiles: []XMCProfile{profile}}).Build(); err != nil {
t.Fatalf("password of %d bytes was rejected: %v", len(valid), err)
}
tooLong := valid + "p"
_, err := (&XMC{Password: tooLong, Profiles: []XMCProfile{profile}}).Build()
if err == nil || !strings.Contains(err.Error(), "longer than") {
t.Fatalf("expected a password length error, got %v", err)
}
}
+4 -4
View File
@@ -697,10 +697,10 @@ func (c *HttpUpgradeConfig) Build() (proto.Message, error) {
const (
Byte = 1
Kilobyte = 1000 * Byte
Megabyte = 1000 * Kilobyte
Gigabyte = 1000 * Megabyte
Terabyte = 1000 * Gigabyte
Kilobyte = 1024 * Byte
Megabyte = 1024 * Kilobyte
Gigabyte = 1024 * Megabyte
Terabyte = 1024 * Gigabyte
)
type Bandwidth string
+1
View File
@@ -1,4 +1,5 @@
//go:build coveragemain
// +build coveragemain
package main
+1
View File
@@ -1,4 +1,5 @@
//go:build linux
// +build linux
package dokodemo
+1
View File
@@ -1,4 +1,5 @@
//go:build openbsd
// +build openbsd
package dokodemo
+1
View File
@@ -1,4 +1,5 @@
//go:build !linux && !openbsd
// +build !linux,!openbsd
package dokodemo
+3 -3
View File
@@ -223,16 +223,16 @@ To make it start, wintun.dll specific for your Windows/arch must be present next
After the start network adapter with the name you chose in the config will be created in the system, and exist while Xray is running. A Wintun adapter that already has that name is reused.
With IPv6 turned off by the `DisabledComponents` registry value (0x10 set, as in 0xFF), errors setting up IPv6 for `gateway`, `dns` and `autoSystemRoutingTable`, clearing the IPv6 DNS servers when `dns` has none included, are only logged. Otherwise, they keep the TUN from starting.
With IPv6 disabled system-wide (the `DisabledComponents` registry value), the adapter has no IPv6, and the TUN starts with IPv4 only, unless `gateway`, `dns` or `autoSystemRoutingTable` has IPv6 entries, which then keep it from starting.
When `dns` is set, those servers are applied to the adapter. Windows is kept from registering the TUN's addresses in DNS, and its DNS cache is flushed when the TUN starts and stops. Xray's own lookups through the system's DNS servers, like those of the `localhost` DNS server with `autoOutboundsInterface`, skip them, unless another interface uses them as well, as they would lead back into the TUN.
Setting `autoSystemWfpBlockLeak` to at least `["dns", "misconfigtun"]` is recommended, to block DNS leaks and the traffic of an IP version without routes. It needs `autoSystemRoutingTable` (the config is rejected otherwise), and adds Windows Filtering Platform filters that let traffic of every program but Xray itself leave only through the TUN, each kind chosen by a value:
Setting `autoSystemWfpBlockLeak` to at least `["dns"]` is recommended, to block DNS leaks. It needs `autoSystemRoutingTable` (the config is rejected otherwise), and adds Windows Filtering Platform filters that let traffic of every program but Xray itself leave only through the TUN, each kind chosen by a value:
- `"dns"`: stands for `"53"`, `"853"` (DNS over TLS and QUIC) and `"windnsclient"`. Windows also queries the DNS servers of the other interfaces, through those interfaces whatever the routes say, and other programs reach a resolver on the local network (e.g. `192.168.1.1` from DHCP) through its more specific route.
- `"windnsclient"`: Windows' DNS Client service, except for name resolution on the local network (LLMNR, mDNS). Since Windows 11 and Server 2022, it may query over HTTPS or TLS, to any port; earlier versions only query port 53, so there it does nothing.
- `"tcp"`, `"udp"`: that protocol, to all ports, or with `/<port>` or `/<low>-<high>` (1 to 65535) to some, e.g. `"tcp/853"` or `"udp/10-20"`. The ports alone stand for both, e.g. `"53"` or `"10-20"`.
- `"icmp"`: ICMP and ICMPv6, e.g. `ping`.
- `"misconfigtun"`: an IP version without routes in `autoSystemRoutingTable`, which would bypass the TUN. With IPv6 turned off by `DisabledComponents`, it covers IPv6 whatever the routes, as IPv6 setup errors are then only logged, and IPv6 still works until the reboot that applies the value.
- `"misconfigtun"`: an IP version without routes in `autoSystemRoutingTable`, which would bypass the TUN.
- `"all"`: everything, in both directions. Access to the local network then breaks unless it is routed into the TUN as well, and so do incoming connections to other programs, other VPNs, and virtual machines whose NAT runs on the host.
- `"allexceptlan"`: like `"all"`, but traffic to and from the local network stays allowed, e.g. for printers or casting to a TV: private, link-local, multicast and broadcast addresses (`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, `169.254.0.0/16`, `224.0.0.0/4`, `255.255.255.255`, `fc00::/7`, `fe80::/10`, `ff00::/8`), not global IPv6 ones. It includes `"dns"`, and the other values apply there as well. `"all"` overrides it.
+10 -41
View File
@@ -23,7 +23,6 @@ import (
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/transport/internet"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"golang.zx2c4.com/wintun"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
)
@@ -99,19 +98,6 @@ func open(name, desc string) (*wintun.Adapter, error) {
return nil, err
}
// ipv6Off tells whether DisabledComponents turns IPv6 off on the non-tunnel
// interfaces (0x10, as 0xFF does), the TUN among them. Windows applies it at
// boot only.
func ipv6Off() bool {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters`, registry.QUERY_VALUE)
if err != nil {
return false
}
defer key.Close()
value, _, err := key.GetIntegerValue("DisabledComponents")
return err == nil && value&0x10 != 0
}
func (t *WindowsTun) Start() (err error) {
var address4, address6 bool
addresses := make([]netip.Prefix, 0, len(t.options.Gateway))
@@ -129,6 +115,7 @@ func (t *WindowsTun) Start() (err error) {
for _, ip := range t.options.DNS {
dns = append(dns, netip.MustParseAddr(ip))
}
dns6 := slices.ContainsFunc(dns, netip.Addr.Is6)
var route4, route6 bool
routesMap := make(map[winipcfg.RouteData]struct{})
@@ -153,17 +140,6 @@ func (t *WindowsTun) Start() (err error) {
routesData = append(routesData, &r)
}
// With IPv6 off by DisabledComponents, setting it up may fail, clearing
// its DNS servers too, which is then only logged.
off6 := ipv6Off()
ignored := func(family winipcfg.AddressFamily, err error) bool {
if family != windows.AF_INET6 || !off6 {
return false
}
errors.LogErrorInner(context.Background(), err, "[tun] ignored, as DisabledComponents turns IPv6 off")
return true
}
var retryTimes int
var firstErr error
startOver:
@@ -180,9 +156,6 @@ startOver:
err = t.luid.SetRoutesForFamily(family, routesData)
if err != nil {
firstErr = errors.New("unable to set routes").Base(err)
if ignored(family, firstErr) {
continue
}
if err == windows.ERROR_NOT_FOUND {
goto startOver
}
@@ -193,9 +166,6 @@ startOver:
err = t.luid.SetIPAddressesForFamily(family, addresses)
if err != nil {
firstErr = errors.New("unable to set ips").Base(err)
if ignored(family, firstErr) {
continue
}
if err == windows.ERROR_NOT_FOUND {
goto startOver
}
@@ -204,7 +174,9 @@ startOver:
}
ipif, err := t.luid.IPInterface(family)
if err != nil {
if ignored(family, errors.New("unable to get the interface").Base(err)) {
// With IPv6 disabled system-wide (DisabledComponents), the adapter has no
// IPv6 interface at all. Skip the family unless the config asks for it.
if err == windows.ERROR_NOT_FOUND && family == windows.AF_INET6 && !address6 && !route6 && !dns6 {
continue
}
return err
@@ -223,9 +195,6 @@ startOver:
err = ipif.Set()
if err != nil {
firstErr = errors.New("unable to set metric and MTU").Base(err)
if ignored(family, firstErr) {
continue
}
if err == windows.ERROR_NOT_FOUND {
goto startOver
}
@@ -233,10 +202,13 @@ startOver:
}
err = t.luid.SetDNS(family, dns, nil)
if err != nil {
firstErr = errors.New("unable to set DNS").Base(err)
if ignored(family, firstErr) {
// Skip IPv6 the config does not ask for also when setting its DNS
// servers fails, as reported on Windows 11 24H2 with IPv6 disabled
// (Access is denied).
if family == windows.AF_INET6 && !address6 && !route6 && !dns6 {
continue
}
firstErr = errors.New("unable to set DNS").Base(err)
if err == windows.ERROR_NOT_FOUND {
goto startOver
}
@@ -293,10 +265,7 @@ startOver:
if err != nil {
return err
}
// With IPv6 off by DisabledComponents, IPv6 setup failures are only logged,
// and IPv6 still works until the reboot that applies it. So "misconfigtun"
// then blocks IPv6 outside the TUN whatever the routes.
block := newLeakFilters(leaks, route4, route6 && !off6)
block := newLeakFilters(leaks, route4, route6)
if route4 || route6 {
dnsClient := func(l leak) bool { return l.name == "windnsclient" }
if slices.ContainsFunc(block, dnsClient) && !dnsClientUsesAnyPort() {
+1
View File
@@ -1,4 +1,5 @@
//go:build coverage
// +build coverage
package scenarios
+1
View File
@@ -1,4 +1,5 @@
//go:build !coverage
// +build !coverage
package scenarios
+1
View File
@@ -1,4 +1,5 @@
//go:build !windows
// +build !windows
package internet
@@ -1,4 +1,5 @@
//go:build !amd64
// +build !amd64
package original
+2 -9
View File
@@ -108,15 +108,8 @@ func NewDomain(domain string, lenLimit int, labelLimit int, types []uint16, edns
return nil, err
}
ascii = strings.Trim(ascii, ".")
if ascii == "" {
return nil, errors.New("empty domain")
}
for _, label := range strings.Split(ascii, ".") {
if len(label) == 0 || len(label) > 63 {
return nil, errors.New("invalid domain")
}
}
name, err := dnsmessage.NewName(ascii + ".")
name, err := dnsmessage.NewName(domain + ".")
if err != nil {
return nil, err
}
+57 -104
View File
@@ -1,11 +1,11 @@
package xdns
import (
"context"
"encoding/binary"
"errors"
"io"
"sync"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet/finalmask"
)
@@ -14,9 +14,11 @@ type TCPResolver struct {
dest net.Destination
dialer *finalmask.Dialer
conn net.Conn
tcpAddr *net.TCPAddr
udpAddr *net.UDPAddr
readCh chan []byte
sendCh chan []byte
closeCh chan struct{}
wg sync.WaitGroup
mu sync.Mutex
@@ -31,10 +33,12 @@ func NewTCPResolver(config *ResolverProto, dialer *finalmask.Dialer) (Resolver,
dest: dest,
dialer: dialer,
readCh: make(chan []byte),
sendCh: make(chan []byte),
closeCh: make(chan struct{}),
}
go r.run(config.Worker)
if err := r.dial(); err != nil {
r.Close()
return nil, err
}
return r, nil
}
@@ -47,113 +51,60 @@ func (r *TCPResolver) closed() bool {
}
}
func (r *TCPResolver) setaddr(addr net.Addr) {
r.mu.Lock()
defer r.mu.Unlock()
switch v := addr.(type) {
case *net.TCPAddr:
r.udpAddr = &net.UDPAddr{IP: v.IP, Port: v.Port}
case *net.UDPAddr:
r.udpAddr = v
default:
panic(v)
func (r *TCPResolver) dial() error {
if r.closed() {
return errors.New("closed")
}
}
func (r *TCPResolver) read(reader io.Reader, p []byte) (l int, err error) {
if _, err := io.ReadFull(reader, p[:2]); err != nil {
return 0, err
if r.conn != nil {
return nil
}
l = int(p[0])<<8 | int(p[1])
if l == 0 || l > 4096 {
return 0, errors.New("err resp len ", l)
}
if _, err = io.ReadFull(reader, p[:l]); err != nil {
return 0, err
}
return l, nil
}
func (r *TCPResolver) do(conn *net.Conn, buf *[4096 + 2]byte, p []byte) error {
var err error
if *conn == nil {
*conn, err = r.dialer.DialTCP(r.dest)
if err != nil {
*conn = nil
return err
}
r.setaddr((*conn).RemoteAddr())
}
c := *conn
l := len(p)
buf[0] = byte(l >> 8)
buf[1] = byte(l)
copy(buf[2:], p)
_, err = c.Write(buf[:2+len(p)])
conn, err := r.dialer.DialTCP(r.dest)
if err != nil {
c.Close()
*conn = nil
return err
}
b := pool4K.Get().([]byte)
l, err = r.read(c, b)
if err != nil {
c.Close()
*conn = nil
pool4K.Put(b[:cap(b)])
return err
}
select {
case <-r.closeCh:
pool4K.Put(b[:cap(b)])
case r.readCh <- b[:l]:
}
r.conn = conn
r.tcpAddr = conn.RemoteAddr().(*net.TCPAddr)
r.udpAddr = &net.UDPAddr{IP: r.tcpAddr.IP, Port: r.tcpAddr.Port}
r.wg.Add(1)
go r.recv(conn)
return nil
}
func (r *TCPResolver) worker() {
func (r *TCPResolver) recv(conn net.Conn) {
defer r.wg.Done()
var conn net.Conn
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
var buf [4096 + 2]byte
var p []byte
var buf [4096]byte
for {
_, err := io.ReadFull(conn, buf[:2])
if err != nil {
break
}
n := binary.BigEndian.Uint16(buf[:2])
if n == 0 || n > 4096 {
io.CopyN(io.Discard, conn, int64(n))
continue
}
_, err = io.ReadFull(conn, buf[:n])
if err != nil {
break
}
p := pool4K.Get().([]byte)
copy(p, buf[:n])
select {
case <-r.closeCh:
return
default:
select {
case <-r.closeCh:
return
case p = <-r.sendCh:
}
}
if err := r.do(&conn, &buf, p); err != nil {
errors.LogErrorInner(context.Background(), err, "do err")
pool4K.Put(p[:cap(p)])
case r.readCh <- p[:n]:
}
}
}
func (r *TCPResolver) run(worker int32) {
for range worker {
r.wg.Add(1)
go r.worker()
}
r.wg.Wait()
close(r.readCh)
// close(r.sendCh)
r.mu.Lock()
defer r.mu.Unlock()
_ = conn.Close()
r.conn = nil
}
func (r *TCPResolver) Addr() *net.UDPAddr {
r.mu.Lock()
defer r.mu.Unlock()
return r.udpAddr
}
@@ -168,25 +119,27 @@ func (r *TCPResolver) Read(p []byte) (n int, err error) {
}
func (r *TCPResolver) Send(p []byte) {
select {
case <-r.closeCh:
r.mu.Lock()
defer r.mu.Unlock()
if r.dial() != nil {
return
default:
b := make([]byte, len(p))
copy(b, p)
select {
case <-r.closeCh:
return
case r.sendCh <- b:
}
}
_ = binary.Write(r.conn, binary.BigEndian, len(p))
_, _ = r.conn.Write(p)
}
func (r *TCPResolver) Close() {
r.mu.Lock()
defer r.mu.Unlock()
if r.closed() {
r.mu.Unlock()
return
}
close(r.closeCh)
conn := r.conn
r.mu.Unlock()
if conn != nil {
_ = conn.Close()
}
r.wg.Wait()
close(r.readCh)
}
+3 -22
View File
@@ -41,17 +41,14 @@ var (
clientStateProxy clientState = 2
)
func newClientConn(c net.Conn, profiles []loginProfile, password string, rsaPublicKey []byte, hostname string, padding []*Padding) (*clientConn, error) {
func newClientConn(c net.Conn, profiles []loginProfile, password string, rsaPublicKey []byte, hostname string) (*clientConn, error) {
if len(rsaPublicKey) == 0 {
return nil, fmt.Errorf("empty rsa public key")
}
if len(profiles) == 0 {
return nil, fmt.Errorf("empty profiles")
}
if len(password) > MaxLegacyPasswordLength {
return nil, fmt.Errorf("password is longer than %d bytes", MaxLegacyPasswordLength)
}
paddingSchedule, err := newPaddingSchedule(padding, true)
paddingSchedule, err := newClientPaddingSchedule2612()
if err != nil {
return nil, fmt.Errorf("select padding profile: %w", err)
}
@@ -198,22 +195,6 @@ func (c *clientConn) handshake() error {
if err != nil {
return fmt.Errorf("read login finished: %w", err)
}
// Set Compression is not compressed itself and must be handled before the
// packet that follows it, which already uses the compressed framing.
if pkt.packetID == 0x03 {
var threshold Varint
if thresholdErr := pkt.readFields(&threshold); thresholdErr != nil {
return fmt.Errorf("read login compression threshold: %w", thresholdErr)
}
if threshold >= 0 {
c.reader = newCompressionReader(c.reader)
c.writer = newCompressionWriter(c.writer, int32(threshold))
}
pkt, err = readPacket(c.reader)
if err != nil {
return fmt.Errorf("read login finished: %w", err)
}
}
if pkt.packetID == 0x00 {
var reason String
if readErr := pkt.readFields(&reason); readErr != nil {
@@ -236,7 +217,7 @@ func (c *clientConn) handshake() error {
if err != nil {
return fmt.Errorf("write login acknowledged: %w", err)
}
if err = runPaddingSchedule(c.writer, true, loginAcknowledgedLength, c.paddingSchedule); err != nil {
if err = runPaddingSchedule(c.reader, c.writer, true, loginAcknowledgedLength, c.paddingSchedule); err != nil {
return fmt.Errorf("run startup padding: %w", err)
}
@@ -1,194 +0,0 @@
// Minecraft compression framing, negotiated by the login Set Compression packet.
package xmc
import (
"bytes"
"compress/zlib"
"fmt"
"io"
)
const (
// Packet Length is a VarInt, so one frame cannot exceed 2^21-1 bytes.
maxCompressedFrameLength = (1 << 21) - 1
// Vanilla caps serverbound uncompressed packet bodies at 2^23 bytes.
maxUncompressedFrameBody = 1 << 23
)
// compressionReader converts the post-Set Compression framing
// (Packet Length, Data Length, optional zlib body) back into the plain
// (VarInt body length, body) frame that readPacket expects.
//
// A zero Data Length means the body was sent uncompressed, which vanilla
// servers accept even when the body exceeds the negotiated threshold.
type compressionReader struct {
r io.Reader
frame bytes.Buffer
}
func newCompressionReader(r io.Reader) io.Reader {
return &compressionReader{r: r}
}
func (c *compressionReader) Read(p []byte) (int, error) {
if c.frame.Len() == 0 {
if err := c.readFrame(); err != nil {
return 0, err
}
}
return c.frame.Read(p)
}
func (c *compressionReader) readFrame() error {
packetLength, _, err := readVarintWithLength(c.r)
if err != nil {
return fmt.Errorf("read compressed packet length: %w", err)
}
if packetLength < 1 || int(packetLength) > maxCompressedFrameLength {
return fmt.Errorf("read compressed packet: bad length: %d", packetLength)
}
dataLength, dataLengthSize, err := readVarintWithLength(c.r)
if err != nil {
return fmt.Errorf("read compressed data length: %w", err)
}
if dataLength < 0 || int(dataLength) > maxUncompressedFrameBody {
return fmt.Errorf("read compressed packet: bad uncompressed length: %d", dataLength)
}
payloadLength := int(packetLength) - dataLengthSize
if payloadLength < 0 {
return fmt.Errorf("read compressed packet: bad length: %d", packetLength)
}
payload := make([]byte, payloadLength)
if _, err := io.ReadFull(c.r, payload); err != nil {
return fmt.Errorf("read compressed packet data: %w", err)
}
body := payload
if dataLength > 0 {
zr, err := zlib.NewReader(bytes.NewReader(payload))
if err != nil {
return fmt.Errorf("open compressed packet: %w", err)
}
body = make([]byte, int(dataLength))
_, readErr := io.ReadFull(zr, body)
closeErr := zr.Close()
if readErr != nil {
return fmt.Errorf("decompress packet: %w", readErr)
}
if closeErr != nil {
return fmt.Errorf("close decompressor: %w", closeErr)
}
}
if len(body) < 1 {
return fmt.Errorf("read compressed packet: empty body")
}
var frame bytes.Buffer
frame.Grow(varintSize(Varint(len(body))) + len(body))
bodyLength := Varint(len(body))
if err := bodyLength.writeTo(&frame); err != nil {
return err
}
frame.Write(body)
c.frame = frame
return nil
}
// compressionWriter converts a plain frame (VarInt body length, body) into the
// post-Set Compression framing. Frames are buffered until complete, because a
// caller may split one frame across several Write calls.
type compressionWriter struct {
w io.Writer
threshold int32
pending []byte
}
func newCompressionWriter(w io.Writer, threshold int32) io.Writer {
return &compressionWriter{w: w, threshold: threshold}
}
func (c *compressionWriter) Write(p []byte) (int, error) {
c.pending = append(c.pending, p...)
for {
body, ok, err := c.nextFrame()
if err != nil {
return 0, err
}
if !ok {
return len(p), nil
}
if err := c.writeFrame(body); err != nil {
return 0, err
}
}
}
// nextFrame returns one complete frame body from the pending bytes. It only
// waits for more bytes while the length prefix or the body is still
// incomplete; a complete but invalid prefix is an error.
func (c *compressionWriter) nextFrame() ([]byte, bool, error) {
limit := len(c.pending)
if limit > 5 {
limit = 5
}
bodyLength, prefixLength, err := readVarintWithLength(bytes.NewReader(c.pending[:limit]))
if err != nil {
if len(c.pending) < 5 {
return nil, false, nil
}
return nil, false, fmt.Errorf("read frame length: %w", err)
}
if bodyLength < 1 {
return nil, false, fmt.Errorf("read frame: bad length: %d", bodyLength)
}
end := prefixLength + int(bodyLength)
if end > len(c.pending) {
return nil, false, nil
}
// The body stays valid after the reslice: appends only write past the
// remaining pending length and writeFrame consumes it immediately.
body := c.pending[prefixLength:end]
c.pending = c.pending[end:]
return body, true, nil
}
func (c *compressionWriter) writeFrame(body []byte) error {
if len(body) >= int(c.threshold) {
var compressed bytes.Buffer
zw := zlib.NewWriter(&compressed)
if _, err := zw.Write(body); err != nil {
_ = zw.Close()
return fmt.Errorf("compress packet: %w", err)
}
if err := zw.Close(); err != nil {
return fmt.Errorf("close compressor: %w", err)
}
var frame bytes.Buffer
packetLength := varintSize(Varint(len(body))) + compressed.Len()
frame.Grow(varintSize(Varint(packetLength)) + packetLength)
packetLengthVarint := Varint(packetLength)
if err := packetLengthVarint.writeTo(&frame); err != nil {
return err
}
bodyLength := Varint(len(body))
if err := bodyLength.writeTo(&frame); err != nil {
return err
}
frame.Write(compressed.Bytes())
return writeFull(c.w, frame.Bytes())
}
var frame bytes.Buffer
frame.Grow(varintSize(Varint(1+len(body))) + 1 + len(body))
frameLength := Varint(1 + len(body))
if err := frameLength.writeTo(&frame); err != nil {
return err
}
frame.WriteByte(0)
frame.Write(body)
return writeFull(c.w, frame.Bytes())
}
@@ -1,220 +0,0 @@
package xmc
import (
"bytes"
"crypto/rand"
"errors"
"io"
"testing"
)
func plainFrame(body []byte) []byte {
frame := make([]byte, 0, 5+len(body))
length := Varint(len(body))
var buf bytes.Buffer
_ = length.writeTo(&buf)
frame = append(frame, buf.Bytes()...)
return append(frame, body...)
}
type wireFrame struct {
packetLength int
dataLength int
payload []byte
}
func readWireFrame(t *testing.T, r *bytes.Reader) wireFrame {
t.Helper()
packetLength, _, err := readVarintWithLength(r)
if err != nil {
t.Fatalf("read packet length: %v", err)
}
dataLength, dataLengthSize, err := readVarintWithLength(r)
if err != nil {
t.Fatalf("read data length: %v", err)
}
payload := make([]byte, int(packetLength)-dataLengthSize)
if _, err := io.ReadFull(r, payload); err != nil {
t.Fatalf("read payload: %v", err)
}
return wireFrame{packetLength: int(packetLength), dataLength: int(dataLength), payload: payload}
}
func randomBody(t *testing.T, size int) []byte {
t.Helper()
body := make([]byte, size)
if _, err := rand.Read(body); err != nil {
t.Fatalf("random body: %v", err)
}
return body
}
func TestCompressionWriterThreshold(t *testing.T) {
for _, threshold := range []int32{0, 1, 256, 65536} {
for _, size := range []int{1, 10, 255, 256, 257, 65535, 65536} {
body := bytes.Repeat([]byte{0}, size)
var wire bytes.Buffer
w := newCompressionWriter(&wire, threshold)
if _, err := w.Write(plainFrame(body)); err != nil {
t.Fatalf("threshold %d size %d: write: %v", threshold, size, err)
}
frame := readWireFrame(t, bytes.NewReader(wire.Bytes()))
wantCompressed := int32(size) >= threshold
if wantCompressed && frame.dataLength != size {
t.Fatalf("threshold %d size %d: want compressed data length %d, got %d", threshold, size, size, frame.dataLength)
}
if !wantCompressed {
if frame.dataLength != 0 {
t.Fatalf("threshold %d size %d: want uncompressed frame, got data length %d", threshold, size, frame.dataLength)
}
if !bytes.Equal(frame.payload, body) {
t.Fatalf("threshold %d size %d: uncompressed payload mismatch", threshold, size)
}
if frame.packetLength != 1+size {
t.Fatalf("threshold %d size %d: packet length %d", threshold, size, frame.packetLength)
}
}
decoded, _, err := readFrame(newCompressionReader(bytes.NewReader(wire.Bytes())), maxUncompressedFrameBody)
if err != nil {
t.Fatalf("threshold %d size %d: decode: %v", threshold, size, err)
}
if !bytes.Equal(decoded, body) {
t.Fatalf("threshold %d size %d: round trip mismatch", threshold, size)
}
}
}
}
func TestCompressionRoundTripChunked(t *testing.T) {
cases := [][]byte{
[]byte("hello"),
randomBody(t, 255),
randomBody(t, 24*1024),
randomBody(t, 64*1024),
bytes.Repeat([]byte("xmc"), 8*1024),
}
for _, threshold := range []int32{0, 256, 65536} {
var wire bytes.Buffer
w := newCompressionWriter(&wire, threshold)
// One Write per frame, split into awkward chunk sizes.
for _, body := range cases {
frame := plainFrame(body)
for len(frame) > 0 {
n := 7
if n > len(frame) {
n = len(frame)
}
if _, err := w.Write(frame[:n]); err != nil {
t.Fatalf("threshold %d: chunked write: %v", threshold, err)
}
frame = frame[n:]
}
}
r := newCompressionReader(bytes.NewReader(wire.Bytes()))
for i, want := range cases {
got, _, err := readFrame(r, maxUncompressedFrameBody)
if err != nil {
t.Fatalf("threshold %d frame %d: decode: %v", threshold, i, err)
}
if !bytes.Equal(got, want) {
t.Fatalf("threshold %d frame %d: mismatch (%d vs %d bytes)", threshold, i, len(got), len(want))
}
}
if _, _, err := readFrame(r, maxUncompressedFrameBody); !errors.Is(err, io.EOF) {
t.Fatalf("threshold %d: expected EOF after the last frame, got %v", threshold, err)
}
}
}
func TestCompressionMultipleFramesOneWrite(t *testing.T) {
var plain []byte
bodies := [][]byte{[]byte("a"), randomBody(t, 1024), randomBody(t, 300)}
for _, body := range bodies {
plain = append(plain, plainFrame(body)...)
}
var wire bytes.Buffer
w := newCompressionWriter(&wire, 256)
if _, err := w.Write(plain); err != nil {
t.Fatalf("write: %v", err)
}
r := newCompressionReader(bytes.NewReader(wire.Bytes()))
for i, want := range bodies {
got, _, err := readFrame(r, maxUncompressedFrameBody)
if err != nil {
t.Fatalf("frame %d: decode: %v", i, err)
}
if !bytes.Equal(got, want) {
t.Fatalf("frame %d: mismatch", i)
}
}
}
// Vanilla servers accept uncompressed packets even when the body exceeds the
// negotiated threshold, which is what lets padding frames keep their wire size.
func TestCompressionReaderUncompressedOverThreshold(t *testing.T) {
body := randomBody(t, 64*1024)
var wire bytes.Buffer
length := Varint(1 + len(body))
if err := length.writeTo(&wire); err != nil {
t.Fatal(err)
}
wire.WriteByte(0)
wire.Write(body)
got, _, err := readFrame(newCompressionReader(bytes.NewReader(wire.Bytes())), maxUncompressedFrameBody)
if err != nil {
t.Fatalf("decode: %v", err)
}
if !bytes.Equal(got, body) {
t.Fatal("payload mismatch")
}
}
func TestCompressionReaderErrors(t *testing.T) {
// Compressed packet with a bogus body.
var badZlib bytes.Buffer
length := Varint(1 + 10)
_ = length.writeTo(&badZlib)
dataLength := Varint(64)
_ = dataLength.writeTo(&badZlib)
badZlib.Write(bytes.Repeat([]byte{0xff}, 10))
if _, _, err := readFrame(newCompressionReader(bytes.NewReader(badZlib.Bytes())), maxUncompressedFrameBody); err == nil {
t.Fatal("expected an error for an invalid zlib body")
}
// Uncompressed length beyond the vanilla limit.
var tooLong bytes.Buffer
length = Varint(1 + 4)
_ = length.writeTo(&tooLong)
dataLength = Varint(maxUncompressedFrameBody + 1)
_ = dataLength.writeTo(&tooLong)
tooLong.Write([]byte{1, 2, 3, 4})
if _, _, err := readFrame(newCompressionReader(bytes.NewReader(tooLong.Bytes())), maxUncompressedFrameBody); err == nil {
t.Fatal("expected an error for an oversized uncompressed length")
}
// Truncated payload.
var truncated bytes.Buffer
length = Varint(1 + 100)
_ = length.writeTo(&truncated)
var zero Varint
_ = zero.writeTo(&truncated)
truncated.Write([]byte{1, 2, 3})
if _, _, err := readFrame(newCompressionReader(bytes.NewReader(truncated.Bytes())), maxUncompressedFrameBody); err == nil {
t.Fatal("expected an error for a truncated payload")
}
}
func TestCompressionWriterInvalidFrameLength(t *testing.T) {
var wire bytes.Buffer
w := newCompressionWriter(&wire, 256)
if _, err := w.Write([]byte{0xff, 0xff, 0xff, 0xff, 0xff}); err == nil {
t.Fatal("expected an error for an invalid frame length prefix")
}
}
+2 -71
View File
@@ -2,28 +2,17 @@ package xmc
import (
"fmt"
"math"
"time"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet/finalmask"
)
// maxPaddingDelayMilliseconds keeps the millisecond value convertible to a
// time.Duration on every platform.
const maxPaddingDelayMilliseconds = int64(math.MaxInt64 / int64(time.Millisecond))
// MaxLegacyPasswordLength is the longest pre-shared password the login can
// carry. The verify token (4 random bytes) and the password travel together in
// one RSA-1024 PKCS#1 v1.5 block, which holds at most 128-11 bytes.
const MaxLegacyPasswordLength = 128 - 11 - 4
func (c *Config) WrapConnClient(conn net.Conn, dest *net.Destination, dialer *finalmask.Dialer) (net.Conn, error) {
profiles, err := profilesFromConfig(c.Profiles)
if err != nil {
return nil, fmt.Errorf("minecraft finalmask: %w", err)
}
cc, err := newClientConn(conn, profiles, c.Password, c.RsaPublicKey, c.Hostname, c.Paddings)
cc, err := newClientConn(conn, profiles, c.Password, c.RsaPublicKey, c.Hostname)
if err != nil {
return nil, fmt.Errorf("minecraft finalmask: %w", err)
}
@@ -36,68 +25,10 @@ func (c *Config) WrapConnServer(conn net.Conn) (net.Conn, error) {
if err != nil {
return nil, fmt.Errorf("minecraft finalmask: %w", err)
}
cc, err := wrapConnServer(conn, profiles, c.Password, c.RsaPrivateKey, c.RsaPublicKey, c.Paddings)
cc, err := wrapConnServer(conn, profiles, c.Password, c.RsaPrivateKey, c.RsaPublicKey)
if err != nil {
return nil, fmt.Errorf("minecraft finalmask: %w", err)
}
return cc, nil
}
// ValidatePadding checks custom startup turns without selecting a built-in preset.
func (c *Config) ValidatePadding() error {
_, err := paddingScheduleFromConfig(c.Paddings)
return err
}
func paddingScheduleFromConfig(paddings []*Padding) ([]paddingTurn, error) {
if len(paddings) == 0 {
return nil, nil
}
schedule := make([]paddingTurn, len(paddings))
for i, turn := range paddings {
if turn == nil || turn.LengthMin < 1 || turn.LengthMax < turn.LengthMin || turn.LengthMax > maxPaddingTurnLength {
return nil, fmt.Errorf("invalid padding length range at turn %d", i)
}
if turn.DelayMin < 0 || turn.DelayMax < turn.DelayMin {
return nil, fmt.Errorf("invalid padding delay range %d-%d at turn %d (must be non-negative and ordered)", turn.DelayMin, turn.DelayMax, i)
}
if turn.DelayMax > maxPaddingDelayMilliseconds {
return nil, fmt.Errorf("padding delay range %d-%d at turn %d is too large", turn.DelayMin, turn.DelayMax, i)
}
var direction paddingDirection
switch turn.Direction {
case 1:
direction = paddingClientToServer
case 2:
direction = paddingServerToClient
default:
return nil, fmt.Errorf("invalid padding direction %d at turn %d (1=client-to-server, 2=server-to-client)", turn.Direction, i)
}
schedule[i] = paddingTurn{
direction: direction,
minLength: int(turn.LengthMin),
maxLength: int(turn.LengthMax),
delay: paddingDelayRange{
min: time.Duration(turn.DelayMin) * time.Millisecond,
max: time.Duration(turn.DelayMax) * time.Millisecond,
},
}
}
// The first turn includes the two-byte Login Acknowledged packet.
if err := validatePaddingSchedule(schedule, 2); err != nil {
return nil, err
}
return schedule, nil
}
func newPaddingSchedule(paddings []*Padding, isClient bool) ([]paddingTurn, error) {
schedule, err := paddingScheduleFromConfig(paddings)
if err != nil || schedule != nil {
return schedule, err
}
if isClient {
return newClientPaddingSchedule2612()
}
return newServerPaddingSchedule2612()
}
+9 -109
View File
@@ -99,8 +99,6 @@ type Config struct {
RsaPublicKey []byte `protobuf:"bytes,9,opt,name=rsa_public_key,json=rsaPublicKey,proto3" json:"rsa_public_key,omitempty"`
Hostname string `protobuf:"bytes,10,opt,name=hostname,proto3" json:"hostname,omitempty"`
Profiles []*Profile `protobuf:"bytes,11,rep,name=profiles,proto3" json:"profiles,omitempty"`
// Startup padding turns. Empty uses the built-in preset.
Paddings []*Padding `protobuf:"bytes,12,rep,name=paddings,proto3" json:"paddings,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -170,93 +168,6 @@ func (x *Config) GetProfiles() []*Profile {
return nil
}
func (x *Config) GetPaddings() []*Padding {
if x != nil {
return x.Paddings
}
return nil
}
type Padding struct {
state protoimpl.MessageState `protogen:"open.v1"`
LengthMin int64 `protobuf:"varint,1,opt,name=length_min,json=lengthMin,proto3" json:"length_min,omitempty"`
LengthMax int64 `protobuf:"varint,2,opt,name=length_max,json=lengthMax,proto3" json:"length_max,omitempty"`
// 1 = client to server, 2 = server to client
Direction int32 `protobuf:"varint,3,opt,name=direction,proto3" json:"direction,omitempty"`
// Millisecond pause after this turn, including the last one. The sender
// waits for a uniformly selected value in [delay_min, delay_max]; the
// receiver waits at least delay_min. Zero means no delay.
DelayMin int64 `protobuf:"varint,4,opt,name=delay_min,json=delayMin,proto3" json:"delay_min,omitempty"`
DelayMax int64 `protobuf:"varint,5,opt,name=delay_max,json=delayMax,proto3" json:"delay_max,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *Padding) Reset() {
*x = Padding{}
mi := &file_transport_internet_finalmask_xmc_config_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *Padding) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Padding) ProtoMessage() {}
func (x *Padding) ProtoReflect() protoreflect.Message {
mi := &file_transport_internet_finalmask_xmc_config_proto_msgTypes[2]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Padding.ProtoReflect.Descriptor instead.
func (*Padding) Descriptor() ([]byte, []int) {
return file_transport_internet_finalmask_xmc_config_proto_rawDescGZIP(), []int{2}
}
func (x *Padding) GetLengthMin() int64 {
if x != nil {
return x.LengthMin
}
return 0
}
func (x *Padding) GetLengthMax() int64 {
if x != nil {
return x.LengthMax
}
return 0
}
func (x *Padding) GetDirection() int32 {
if x != nil {
return x.Direction
}
return 0
}
func (x *Padding) GetDelayMin() int64 {
if x != nil {
return x.DelayMin
}
return 0
}
func (x *Padding) GetDelayMax() int64 {
if x != nil {
return x.DelayMax
}
return 0
}
var File_transport_internet_finalmask_xmc_config_proto protoreflect.FileDescriptor
const file_transport_internet_finalmask_xmc_config_proto_rawDesc = "" +
@@ -266,23 +177,14 @@ const file_transport_internet_finalmask_xmc_config_proto_rawDesc = "" +
"\busername\x18\x01 \x01(\tR\busername\x12\x12\n" +
"\x04uuid\x18\x02 \x01(\fR\x04uuid\x12%\n" +
"\x0etextures_value\x18\x03 \x01(\tR\rtexturesValue\x12-\n" +
"\x12textures_signature\x18\x04 \x01(\tR\x11texturesSignature\"\xac\x02\n" +
"\x12textures_signature\x18\x04 \x01(\tR\x11texturesSignature\"\xe0\x01\n" +
"\x06Config\x12\x1a\n" +
"\bpassword\x18\x01 \x01(\tR\bpassword\x12&\n" +
"\x0frsa_private_key\x18\b \x01(\fR\rrsaPrivateKey\x12$\n" +
"\x0ersa_public_key\x18\t \x01(\fR\frsaPublicKey\x12\x1a\n" +
"\bhostname\x18\n" +
" \x01(\tR\bhostname\x12J\n" +
"\bprofiles\x18\v \x03(\v2..xray.transport.internet.finalmask.xmc.ProfileR\bprofiles\x12J\n" +
"\bpaddings\x18\f \x03(\v2..xray.transport.internet.finalmask.xmc.PaddingR\bpaddingsJ\x04\b\x02\x10\x03\"\x9f\x01\n" +
"\aPadding\x12\x1d\n" +
"\n" +
"length_min\x18\x01 \x01(\x03R\tlengthMin\x12\x1d\n" +
"\n" +
"length_max\x18\x02 \x01(\x03R\tlengthMax\x12\x1c\n" +
"\tdirection\x18\x03 \x01(\x05R\tdirection\x12\x1b\n" +
"\tdelay_min\x18\x04 \x01(\x03R\bdelayMin\x12\x1b\n" +
"\tdelay_max\x18\x05 \x01(\x03R\bdelayMaxB\x91\x01\n" +
"\bprofiles\x18\v \x03(\v2..xray.transport.internet.finalmask.xmc.ProfileR\bprofilesJ\x04\b\x02\x10\x03B\x91\x01\n" +
")com.xray.transport.internet.finalmask.xmcP\x01Z:github.com/xtls/xray-core/transport/internet/finalmask/xmc\xaa\x02%Xray.Transport.Internet.Finalmask.XMCb\x06proto3"
var (
@@ -297,20 +199,18 @@ func file_transport_internet_finalmask_xmc_config_proto_rawDescGZIP() []byte {
return file_transport_internet_finalmask_xmc_config_proto_rawDescData
}
var file_transport_internet_finalmask_xmc_config_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_transport_internet_finalmask_xmc_config_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_transport_internet_finalmask_xmc_config_proto_goTypes = []any{
(*Profile)(nil), // 0: xray.transport.internet.finalmask.xmc.Profile
(*Config)(nil), // 1: xray.transport.internet.finalmask.xmc.Config
(*Padding)(nil), // 2: xray.transport.internet.finalmask.xmc.Padding
}
var file_transport_internet_finalmask_xmc_config_proto_depIdxs = []int32{
0, // 0: xray.transport.internet.finalmask.xmc.Config.profiles:type_name -> xray.transport.internet.finalmask.xmc.Profile
2, // 1: xray.transport.internet.finalmask.xmc.Config.paddings:type_name -> xray.transport.internet.finalmask.xmc.Padding
2, // [2:2] is the sub-list for method output_type
2, // [2:2] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_transport_internet_finalmask_xmc_config_proto_init() }
@@ -324,7 +224,7 @@ func file_transport_internet_finalmask_xmc_config_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_transport_internet_finalmask_xmc_config_proto_rawDesc), len(file_transport_internet_finalmask_xmc_config_proto_rawDesc)),
NumEnums: 0,
NumMessages: 3,
NumMessages: 2,
NumExtensions: 0,
NumServices: 0,
},
@@ -23,18 +23,4 @@ message Config {
bytes rsa_public_key = 9;
string hostname = 10;
repeated Profile profiles = 11;
// Startup padding turns. Empty uses the built-in preset.
repeated Padding paddings = 12;
}
message Padding {
int64 length_min = 1;
int64 length_max = 2;
// 1 = client to server, 2 = server to client
int32 direction = 3;
// Millisecond pause after this turn, including the last one. The sender
// waits for a uniformly selected value in [delay_min, delay_max]; the
// receiver waits at least delay_min. Zero means no delay.
int64 delay_min = 4;
int64 delay_max = 5;
}
@@ -1,23 +0,0 @@
package xmc
import (
"net"
"strings"
"testing"
)
func TestClientRejectsLongLegacyPassword(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
profiles := []loginProfile{{Username: "TestUser"}}
publicKey := []byte{0x01}
if _, err := newClientConn(client, profiles, strings.Repeat("p", MaxLegacyPasswordLength), publicKey, "localhost", nil); err != nil {
t.Fatalf("password of %d bytes was rejected: %v", MaxLegacyPasswordLength, err)
}
if _, err := newClientConn(client, profiles, strings.Repeat("p", MaxLegacyPasswordLength+1), publicKey, "localhost", nil); err == nil {
t.Fatal("expected the long password to be rejected")
}
}
@@ -0,0 +1,81 @@
package xmc
import (
"net"
"sync"
"testing"
"time"
)
func TestConnectionDeadlinesRestoreCallerValues(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
recording := &deadlineRecordingConn{Conn: client}
deadlines := newConnectionDeadlines(recording)
callerDeadline := time.Now().Add(10 * time.Minute)
if err := deadlines.setDeadline(callerDeadline); err != nil {
t.Fatal(err)
}
if err := deadlines.beginHandshake(); err != nil {
t.Fatal(err)
}
read, write := recording.currentDeadlines()
if !read.Before(callerDeadline) || !write.Before(callerDeadline) {
t.Fatalf("handshake deadlines = %s/%s, caller = %s", read, write, callerDeadline)
}
shortReadDeadline := time.Now().Add(time.Second)
if err := deadlines.setReadDeadline(shortReadDeadline); err != nil {
t.Fatal(err)
}
read, _ = recording.currentDeadlines()
if !read.Equal(shortReadDeadline) {
t.Fatalf("read deadline = %s, want %s", read, shortReadDeadline)
}
if err := deadlines.endHandshake(); err != nil {
t.Fatal(err)
}
read, write = recording.currentDeadlines()
if !read.Equal(shortReadDeadline) || !write.Equal(callerDeadline) {
t.Fatalf("restored deadlines = %s/%s, want %s/%s", read, write, shortReadDeadline, callerDeadline)
}
}
type deadlineRecordingConn struct {
net.Conn
mu sync.Mutex
read time.Time
write time.Time
}
func (c *deadlineRecordingConn) SetDeadline(t time.Time) error {
c.mu.Lock()
c.read = t
c.write = t
c.mu.Unlock()
return c.Conn.SetDeadline(t)
}
func (c *deadlineRecordingConn) SetReadDeadline(t time.Time) error {
c.mu.Lock()
c.read = t
c.mu.Unlock()
return c.Conn.SetReadDeadline(t)
}
func (c *deadlineRecordingConn) SetWriteDeadline(t time.Time) error {
c.mu.Lock()
c.write = t
c.mu.Unlock()
return c.Conn.SetWriteDeadline(t)
}
func (c *deadlineRecordingConn) currentDeadlines() (time.Time, time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
return c.read, c.write
}
@@ -0,0 +1,58 @@
package xmc
import (
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"testing"
)
func TestDeriveRSAKey(t *testing.T) {
password := "my-very-secret-password-12345"
key1, err := DeriveRSAKey(password)
if err != nil {
t.Fatalf("failed to derive key: %v", err)
}
err = key1.Validate()
if err != nil {
t.Fatalf("key is not valid RSA key: %v", err)
}
key2, err := DeriveRSAKey(password)
if err != nil {
t.Fatalf("failed to derive key second time: %v", err)
}
// Verify determinism
if key1.D.Cmp(key2.D) != 0 || key1.N.Cmp(key2.N) != 0 {
t.Errorf("derived keys are not identical for the same password")
}
// Verify different passwords yield different keys
keyDifferent, err := DeriveRSAKey(password + "-different")
if err != nil {
t.Fatalf("failed to derive different key: %v", err)
}
if key1.D.Cmp(keyDifferent.D) == 0 || key1.N.Cmp(keyDifferent.N) == 0 {
t.Errorf("derived keys are identical for different passwords")
}
}
func TestDeriveRSAKeyGoldenPrivateKey(t *testing.T) {
const password = "deterministic-rsa-key-golden"
const wantPKCS1DERHash = "3a8c4ad56a6fb42dab73c4d5fc3af754460a2db1441edc0970cbc7f4e0798d2f"
key, err := DeriveRSAKey(password)
if err != nil {
t.Fatalf("failed to derive key: %v", err)
}
gotHash := sha256.Sum256(x509.MarshalPKCS1PrivateKey(key))
got := hex.EncodeToString(gotHash[:])
if got != wantPKCS1DERHash {
t.Fatalf("derived private key changed\nwant sha256: %s\n got sha256: %s", wantPKCS1DERHash, got)
}
}
@@ -0,0 +1,390 @@
package xmc
import (
"bytes"
"crypto/sha256"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"testing"
"time"
)
func deriveTestRSAKey(t *testing.T, password string) ([]byte, []byte) {
t.Helper()
key, err := DeriveRSAKey(password)
if err != nil {
t.Fatalf("failed to derive rsa key: %v", err)
}
publicKey, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
t.Fatalf("failed to marshal public key: %v", err)
}
return x509.MarshalPKCS1PrivateKey(key), publicKey
}
func testLoginProfile(username string) loginProfile {
profile := loginProfile{
Username: username,
TexturesValue: strings.Repeat("texture-value-", 40),
TexturesSignature: strings.Repeat("texture-signature-", 24),
}
digest := sha256.Sum256([]byte(username))
copy(profile.UUID[:], digest[:16])
profile.UUID[6] = (profile.UUID[6] & 0x0f) | 0x40
profile.UUID[8] = (profile.UUID[8] & 0x3f) | 0x80
return profile
}
func TestHandshakeSuccess(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
defer ln.Close()
password := "super-secure-shared-key-12345"
profiles := []loginProfile{testLoginProfile("test_user")}
privateKey, publicKey := deriveTestRSAKey(t, password)
go func() {
rawConn, err := ln.Accept()
if err != nil {
return
}
defer rawConn.Close()
server, err := wrapConnServer(rawConn, profiles, password, privateKey, publicKey)
if err != nil {
t.Errorf("failed to wrap server: %v", err)
return
}
buf := make([]byte, 1024)
n, err := server.Read(buf)
if err != nil {
t.Errorf("server read error: %v", err)
return
}
if !bytes.Equal(buf[:n], []byte("hello server")) {
t.Errorf("unexpected payload from client: %s", string(buf[:n]))
return
}
_, err = server.Write([]byte("hello client"))
if err != nil {
t.Errorf("server write error: %v", err)
return
}
}()
clientRaw, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer clientRaw.Close()
client, err := newClientConn(clientRaw, profiles, password, publicKey, "localhost")
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
_, err = client.Write([]byte("hello server"))
if err != nil {
t.Fatalf("client write error: %v", err)
}
buf := make([]byte, 1024)
n, err := client.Read(buf)
if err != nil {
t.Fatalf("client read error: %v", err)
}
if !bytes.Equal(buf[:n], []byte("hello client")) {
t.Errorf("unexpected payload from server: %s", string(buf[:n]))
}
}
func TestHandshakePasswordMismatch(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
defer ln.Close()
clientPassword := "client-secret-123"
serverPassword := "server-secret-456"
profiles := []loginProfile{testLoginProfile("test_user")}
serverPrivateKey, serverPublicKey := deriveTestRSAKey(t, serverPassword)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
rawConn, err := ln.Accept()
if err != nil {
return
}
defer rawConn.Close()
server, err := wrapConnServer(rawConn, profiles, serverPassword, serverPrivateKey, serverPublicKey)
if err != nil {
// Wrapping is synchronous and shouldn't fail initially simply because key derivation works with any string
t.Logf("wrapped server: %v", err)
}
// When client sends data, handshake happens and should fail
buf := make([]byte, 1024)
_, err = server.Read(buf)
if err == nil {
t.Errorf("expected handshake to fail due to password mismatch, but it succeeded")
} else {
t.Logf("server read failed as expected: %v", err)
}
}()
clientRaw, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer clientRaw.Close()
client, err := newClientConn(clientRaw, profiles, clientPassword, serverPublicKey, "localhost")
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
err = client.handshake()
if err == nil {
t.Fatal("expected client handshake to fail due to password mismatch")
}
wg.Wait()
// Check if we lost connection or received error
t.Log("Handshake mismatch tested")
}
func TestHandshakeNetPipeWithKeepAlive(t *testing.T) {
clientRaw, serverRaw := net.Pipe()
defer clientRaw.Close()
defer serverRaw.Close()
const password = "net-pipe-shared-key"
profiles := []loginProfile{testLoginProfile("pipe_user")}
privateKey, publicKey := deriveTestRSAKey(t, password)
serverDone := make(chan error, 1)
go func() {
server, err := wrapConnServer(serverRaw, profiles, password, privateKey, publicKey)
if err != nil {
serverDone <- err
return
}
request := make([]byte, len("hello server"))
if _, err = io.ReadFull(server, request); err != nil {
serverDone <- fmt.Errorf("read request: %w", err)
return
}
if string(request) != "hello server" {
serverDone <- fmt.Errorf("unexpected request: %q", request)
return
}
followupDone := make(chan error, 1)
go func() {
followup := make([]byte, len("after keepalive"))
_, readErr := io.ReadFull(server, followup)
if readErr == nil && string(followup) != "after keepalive" {
readErr = fmt.Errorf("unexpected followup: %q", followup)
}
followupDone <- readErr
}()
if err = server.packet.writeKeepAlive(Long(42)); err != nil {
serverDone <- fmt.Errorf("write keep-alive: %w", err)
return
}
if _, err = server.Write([]byte("hello client")); err != nil {
serverDone <- fmt.Errorf("write response: %w", err)
return
}
serverDone <- <-followupDone
}()
client, err := newClientConn(clientRaw, profiles, password, publicKey, "localhost")
if err != nil {
t.Fatal(err)
}
if _, err = client.Write([]byte("hello server")); err != nil {
t.Fatalf("write request: %v", err)
}
response := make([]byte, len("hello client"))
if _, err = io.ReadFull(client, response); err != nil {
t.Fatalf("read response: %v", err)
}
if string(response) != "hello client" {
t.Fatalf("unexpected response: %q", response)
}
if _, err = client.Write([]byte("after keepalive")); err != nil {
t.Fatalf("write followup: %v", err)
}
select {
case err = <-serverDone:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("net.Pipe handshake timed out")
}
}
func TestStatusQueryUnaffected(t *testing.T) {
clientRaw, serverRaw := net.Pipe()
defer clientRaw.Close()
defer serverRaw.Close()
const password = "status-shared-key"
profiles := []loginProfile{testLoginProfile("status_user")}
privateKey, publicKey := deriveTestRSAKey(t, password)
serverDone := make(chan error, 1)
go func() {
server, err := wrapConnServer(serverRaw, profiles, password, privateKey, publicKey)
if err == nil {
err = server.handshake()
}
serverDone <- err
}()
protocolVersion := Varint(775)
serverAddress := String("localhost")
serverPort := UnsignedShort(25565)
nextState := Varint(1)
if err := writePacket(clientRaw, 0x00, &protocolVersion, &serverAddress, &serverPort, &nextState); err != nil {
t.Fatal(err)
}
if err := writePacket(clientRaw, 0x00); err != nil {
t.Fatal(err)
}
response, err := readPacket(clientRaw)
if err != nil {
t.Fatal(err)
}
if response.packetID != 0x00 {
t.Fatalf("status packet id = %d", response.packetID)
}
var responseJSON String
if err = response.readFields(&responseJSON); err != nil {
t.Fatal(err)
}
if string(responseJSON) != statusResponse {
t.Fatalf("status response = %q", responseJSON)
}
payload := Long(0x0102030405060708)
if err = writePacket(clientRaw, 0x01, &payload); err != nil {
t.Fatal(err)
}
pong, err := readPacket(clientRaw)
if err != nil {
t.Fatal(err)
}
var receivedPayload Long
if pong.packetID != 0x01 {
t.Fatalf("pong packet id = %d", pong.packetID)
}
if err = pong.readFields(&receivedPayload); err != nil {
t.Fatal(err)
}
if receivedPayload != payload {
t.Fatalf("pong payload = %x", receivedPayload)
}
select {
case err = <-serverDone:
if err == nil || !strings.Contains(err.Error(), "ping") {
t.Fatalf("server error = %v", err)
}
case <-time.After(time.Second):
t.Fatal("status handshake timed out")
}
}
func TestClientHandshakeHonorsCallerDeadline(t *testing.T) {
clientRaw, serverRaw := net.Pipe()
defer clientRaw.Close()
defer serverRaw.Close()
const password = "deadline-shared-key"
profiles := []loginProfile{testLoginProfile("deadline_user")}
_, publicKey := deriveTestRSAKey(t, password)
client, err := newClientConn(clientRaw, profiles, password, publicKey, "localhost")
if err != nil {
t.Fatal(err)
}
if err = client.SetDeadline(time.Now().Add(30 * time.Millisecond)); err != nil {
t.Fatal(err)
}
started := time.Now()
_, err = client.Write([]byte("blocked"))
var netErr net.Error
if !errors.As(err, &netErr) || !netErr.Timeout() {
t.Fatalf("error = %v, want network timeout", err)
}
if elapsed := time.Since(started); elapsed > 500*time.Millisecond {
t.Fatalf("caller deadline took %s", elapsed)
}
}
func TestClientCloseInterruptsHandshake(t *testing.T) {
clientRaw, serverRaw := net.Pipe()
defer serverRaw.Close()
const password = "close-shared-key"
profiles := []loginProfile{testLoginProfile("close_user")}
_, publicKey := deriveTestRSAKey(t, password)
client, err := newClientConn(clientRaw, profiles, password, publicKey, "localhost")
if err != nil {
t.Fatal(err)
}
done := make(chan error, 1)
go func() {
_, writeErr := client.Write([]byte("blocked"))
done <- writeErr
}()
time.Sleep(20 * time.Millisecond)
if err = client.Close(); err != nil {
t.Fatal(err)
}
select {
case err = <-done:
if err == nil {
t.Fatal("handshake unexpectedly succeeded after close")
}
case <-time.After(time.Second):
t.Fatal("close did not interrupt handshake")
}
}
func TestValidateLoginAcknowledgedPacketRejectsData(t *testing.T) {
if err := validateLoginAcknowledgedPacket(&mcPacket{packetID: 0x03}); err != nil {
t.Fatalf("valid login acknowledged packet: %v", err)
}
if err := validateLoginAcknowledgedPacket(&mcPacket{packetID: 0x03, data: []byte{0x00}}); err == nil {
t.Fatal("login acknowledged packet with trailing data was accepted")
}
}
@@ -0,0 +1,102 @@
package xmc
import (
"bytes"
"io"
"net"
"testing"
)
func TestPacketStreamUsesPlainFraming(t *testing.T) {
payload := []byte("hello")
var wire bytes.Buffer
stream := newPacketStream(bytes.NewReader(nil), &wire, true)
written, err := stream.Write(payload)
if err != nil {
t.Fatalf("write payload: %v", err)
}
if written != len(payload) {
t.Fatalf("written = %d, want %d", written, len(payload))
}
wantOutbound := []byte{0x0f, 0x02, 0x08, 'x', 'm', 'c', ':', 'd', 'a', 't', 'a', 'h', 'e', 'l', 'l', 'o'}
if !bytes.Equal(wire.Bytes(), wantOutbound) {
t.Fatalf("wire frame = %x, want %x", wire.Bytes(), wantOutbound)
}
wantInbound := append([]byte(nil), wantOutbound...)
wantInbound[1] = configurationClientboundCustomPayload
reader := newPacketStream(bytes.NewReader(wantInbound), io.Discard, true)
got := make([]byte, len(payload))
if _, err = io.ReadFull(reader, got); err != nil {
t.Fatalf("read payload: %v", err)
}
if !bytes.Equal(got, payload) {
t.Fatalf("payload = %q, want %q", got, payload)
}
}
func TestPacketStreamRoundTrip(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
const password = "packet-stream-shared-key"
privateKey, publicKey := deriveTestRSAKey(t, password)
profiles := []loginProfile{testLoginProfile("packet_user")}
clientPayload := bytes.Repeat([]byte("client-payload-"), 5000)
serverPayload := bytes.Repeat([]byte("server-payload-"), 5000)
serverDone := make(chan error, 1)
go func() {
rawConn, acceptErr := ln.Accept()
if acceptErr != nil {
serverDone <- acceptErr
return
}
defer rawConn.Close()
server, wrapErr := wrapConnServer(rawConn, profiles, password, privateKey, publicKey)
if wrapErr != nil {
serverDone <- wrapErr
return
}
got := make([]byte, len(clientPayload))
if _, readErr := io.ReadFull(server, got); readErr != nil {
serverDone <- readErr
return
}
if !bytes.Equal(got, clientPayload) {
serverDone <- io.ErrUnexpectedEOF
return
}
_, writeErr := server.Write(serverPayload)
serverDone <- writeErr
}()
rawClient, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
defer rawClient.Close()
client, err := newClientConn(rawClient, profiles, password, publicKey, "localhost")
if err != nil {
t.Fatal(err)
}
if _, err = client.Write(clientPayload); err != nil {
t.Fatalf("write payload: %v", err)
}
got := make([]byte, len(serverPayload))
if _, err = io.ReadFull(client, got); err != nil {
t.Fatalf("read payload: %v", err)
}
if !bytes.Equal(got, serverPayload) {
t.Fatal("server payload mismatch")
}
if err = <-serverDone; err != nil {
t.Fatalf("server: %v", err)
}
}
+87 -126
View File
@@ -1,6 +1,7 @@
package xmc
import (
"bytes"
"crypto/rand"
"fmt"
"io"
@@ -17,21 +18,8 @@ const (
paddingBufferLength = 16 * 1024
maxPaddingChunkLength = 48 * 1024
maxPaddingTurnLength = 8 * 1024 * 1024
// Frame padding carries the startup padding in ordinary Minecraft custom
// payload packets so that a vanilla server can parse the whole connection
// without taking the pipeline over. The channel is deliberately different
// from the packet stream channel: both ends drop unknown payloads.
paddingFrameChannel = "xmc:pad"
// Serverbound custom payload data is capped at 32767 bytes by
// ServerboundCustomPayloadPacket; keep the same budget in both directions.
maxPaddingFrameDataLength = 32767
// Length varint, packet id, channel length varint and the channel itself.
paddingFrameOverhead = 1 + 1 + 1 + len(paddingFrameChannel)
)
var maxPaddingFrameLength = paddingFrameLength(maxPaddingFrameDataLength)
type paddingVariant struct {
chunks []int
delays []paddingDelayRange
@@ -46,7 +34,6 @@ type paddingTurn struct {
direction paddingDirection
minLength int
maxLength int
delay paddingDelayRange // pause after this turn, including the last one
variants []paddingVariant
startDelay paddingDelayRange
chunkDelay paddingDelayRange
@@ -57,40 +44,27 @@ type paddingTurn struct {
sendVariants []int
}
func runPaddingSchedule(writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn) error {
return runPaddingScheduleWithSleep(writer, isClient, firstTurnPrefixLength, schedule, time.Sleep)
}
func runPaddingScheduleWithSleep(writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn, sleep func(time.Duration)) error {
func runPaddingSchedule(reader io.Reader, writer io.Writer, isClient bool, firstTurnPrefixLength int, schedule []paddingTurn) error {
if err := validatePaddingSchedule(schedule, firstTurnPrefixLength); err != nil {
return err
}
var writeBuffer []byte
for i, turn := range schedule {
prefixLength := 0
if i == 0 {
prefixLength = firstTurnPrefixLength
}
if isClient != (turn.direction == paddingClientToServer) {
// Padding is ordinary Minecraft traffic, so the peer's turns are
// never consumed here: they stay in the stream and are dropped as
// unknown packets by whoever reads it next. Both endpoints still
// apply the same minimum pause, including after the last turn.
if turn.delay.min > 0 {
sleep(turn.delay.min)
localSends := isClient == (turn.direction == paddingClientToServer)
if localSends {
if err := writePaddingTurnWithBuffer(writer, turn, prefixLength, time.Sleep, &writeBuffer); err != nil {
return fmt.Errorf("write padding turn %d: %w", i, err)
}
continue
}
if _, err := writePaddingTurnFrames(writer, turn, prefixLength, sleep, paddingFramePacketID(isClient)); err != nil {
return fmt.Errorf("write padding turn %d: %w", i, err)
}
delay, err := randomPaddingDelay(turn.delay)
if err != nil {
return fmt.Errorf("select padding turn %d delay: %w", i, err)
}
if delay > 0 {
sleep(delay)
if err := readPaddingTurn(reader, turn, prefixLength); err != nil {
return fmt.Errorf("read padding turn %d: %w", i, err)
}
}
return nil
@@ -117,9 +91,6 @@ func validatePaddingSchedule(schedule []paddingTurn, firstTurnPrefixLength int)
if err := validatePaddingDelayRange(turn.chunkDelay); err != nil {
return fmt.Errorf("padding turn %d has an invalid chunk delay: %w", i, err)
}
if err := validatePaddingDelayRange(turn.delay); err != nil {
return fmt.Errorf("padding turn %d has an invalid inter-turn delay: %w", i, err)
}
if turn.writeChunkMinLength < 0 || turn.writeChunkLength < turn.writeChunkMinLength || turn.writeChunkLength > maxPaddingChunkLength {
return fmt.Errorf("padding turn %d has an invalid write chunk range: %d-%d", i, turn.writeChunkMinLength, turn.writeChunkLength)
}
@@ -150,27 +121,25 @@ func validatePaddingSchedule(schedule []paddingTurn, firstTurnPrefixLength int)
}
}
}
if i > 0 && turn.direction == schedule[i-1].direction {
return fmt.Errorf("padding turns %d and %d have the same direction", i-1, i)
}
}
return nil
}
// paddingFramePacketID returns the configuration-state custom payload packet id
// used by the side that sends the padding bytes; both values come from the
// vanilla configuration protocol, so no proxy-specific id ever reaches the wire.
func paddingFramePacketID(isClient bool) int {
if isClient {
return configurationServerboundCustomPayload
}
return configurationClientboundCustomPayload
func writePaddingTurn(w io.Writer, turn paddingTurn, prefixLength int) error {
return writePaddingTurnWithSleep(w, turn, prefixLength, time.Sleep)
}
// writePaddingTurnFrames writes one padding turn as a sequence of valid
// Minecraft custom payload frames. Only the framing is native: payload bodies
// stay zero-filled, so the captured traffic shape is kept.
func writePaddingTurnFrames(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration), packetID int) (int, error) {
func writePaddingTurnWithSleep(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration)) error {
return writePaddingTurnWithBuffer(w, turn, prefixLength, sleep, nil)
}
func writePaddingTurnWithBuffer(w io.Writer, turn paddingTurn, prefixLength int, sleep func(time.Duration), reusableBuffer *[]byte) error {
startDelay, err := randomPaddingDelay(turn.startDelay)
if err != nil {
return 0, fmt.Errorf("select padding start delay: %w", err)
return fmt.Errorf("select padding start delay: %w", err)
}
if startDelay > 0 {
sleep(startDelay)
@@ -178,11 +147,17 @@ func writePaddingTurnFrames(w io.Writer, turn paddingTurn, prefixLength int, sle
targetLength, chunks, delays, err := selectPaddingVariant(turn, prefixLength)
if err != nil {
return 0, err
return err
}
payloadLength := targetLength - prefixLength
if payloadLength < 1 {
return 0, fmt.Errorf("target length %d leaves an invalid padding payload of %d bytes", targetLength, payloadLength)
recordLength := targetLength - prefixLength
if recordLength < 1 {
return fmt.Errorf("target length %d leaves an invalid record length %d", targetLength, recordLength)
}
encodedLength := Varint(recordLength)
var header bytes.Buffer
if err = encodedLength.writeTo(&header); err != nil {
return fmt.Errorf("write padding header: %w", err)
}
if len(chunks) == 0 {
writeChunkLength := turn.writeChunkLength
@@ -191,103 +166,89 @@ func writePaddingTurnFrames(w io.Writer, turn paddingTurn, prefixLength int, sle
} else if turn.writeChunkMinLength > 0 {
writeChunkLength, err = randomPaddingTarget(turn.writeChunkMinLength, writeChunkLength)
if err != nil {
return 0, fmt.Errorf("select padding write chunk length: %w", err)
return fmt.Errorf("select padding write chunk length: %w", err)
}
}
chunks = defaultPaddingChunks(payloadLength, writeChunkLength)
chunks = defaultPaddingChunks(recordLength, writeChunkLength)
delays = make([]paddingDelayRange, len(chunks))
for i := 1; i < len(delays); i++ {
delays[i] = turn.chunkDelay
}
}
if chunks[0] < header.Len() {
return fmt.Errorf("first padding chunk %d is shorter than header %d", chunks[0], header.Len())
}
zeros := make([]byte, maxPaddingFrameDataLength)
channel := String(paddingFrameChannel)
maxChunkLength := 0
for _, chunkLength := range chunks {
if chunkLength < 1 || chunkLength > maxPaddingChunkLength {
return fmt.Errorf("invalid padding chunk length: %d", chunkLength)
}
maxChunkLength = max(maxChunkLength, chunkLength)
}
var buffer []byte
if reusableBuffer == nil {
buffer = make([]byte, maxChunkLength)
} else {
if cap(*reusableBuffer) < maxChunkLength {
*reusableBuffer = make([]byte, maxChunkLength)
}
buffer = (*reusableBuffer)[:maxChunkLength]
clear(buffer)
}
copy(buffer, header.Bytes())
written := 0
for i, chunkLength := range chunks {
if i < len(delays) {
delay, delayErr := randomPaddingDelay(delays[i])
if delayErr != nil {
return written, fmt.Errorf("select padding chunk %d delay: %w", i, delayErr)
return fmt.Errorf("select padding chunk %d delay: %w", i, delayErr)
}
if delay > 0 {
sleep(delay)
}
}
frameLengths, planErr := paddingFramePlan(chunkLength)
if planErr != nil {
return written, planErr
if err = writeFull(w, buffer[:chunkLength]); err != nil {
return fmt.Errorf("write padding chunk %d: %w", i, err)
}
for _, frameLength := range frameLengths {
dataLength, ok := paddingFrameDataLength(frameLength)
if !ok {
return written, fmt.Errorf("unframed padding length %d", frameLength)
}
payload := RestBytes(zeros[:dataLength])
if err = writePacket(w, packetID, &channel, &payload); err != nil {
return written, fmt.Errorf("write padding frame: %w", err)
}
written += frameLength
written += chunkLength
if i == 0 {
clear(buffer[:header.Len()])
}
}
return written, nil
if written != recordLength {
return fmt.Errorf("padding chunks total %d, want %d", written, recordLength)
}
return nil
}
// paddingFrameLength is the wire size of a custom payload frame whose payload
// body is dataLength bytes long.
func paddingFrameLength(dataLength int) int {
bodyLength := 1 + 1 + len(paddingFrameChannel) + dataLength
return varintSize(Varint(bodyLength)) + bodyLength
}
func readPaddingTurn(r io.Reader, turn paddingTurn, prefixLength int) error {
encodedLength, headerLength, err := readVarintWithLength(r)
if err != nil {
return fmt.Errorf("read padding header: %w", err)
}
recordLength := int(encodedLength)
if recordLength < headerLength || recordLength > maxPaddingTurnLength {
return fmt.Errorf("invalid padding record length: %d", recordLength)
}
totalLength := prefixLength + recordLength
if !paddingTurnAcceptsLength(turn, totalLength) {
if len(turn.variants) > 0 {
return fmt.Errorf("padding turn length %d is not an allowed variant", totalLength)
}
return fmt.Errorf("padding turn length %d is outside %d-%d", totalLength, turn.minLength, turn.maxLength)
}
// paddingFrameDataLength is the payload body that makes a frame exactly
// frameLength bytes long. Some lengths are unreachable because the length
// varint grows in steps.
func paddingFrameDataLength(frameLength int) (int, bool) {
for overhead := paddingFrameOverhead; overhead <= paddingFrameOverhead+2; overhead++ {
dataLength := frameLength - overhead
if dataLength < 0 || dataLength > maxPaddingFrameDataLength {
continue
}
if paddingFrameLength(dataLength) == frameLength {
return dataLength, true
var buffer [paddingBufferLength]byte
remaining := recordLength - headerLength
for remaining > 0 {
chunkLength := min(remaining, len(buffer))
if _, err := io.ReadFull(r, buffer[:chunkLength]); err != nil {
return fmt.Errorf("read padding body: %w", err)
}
remaining -= chunkLength
}
return 0, false
}
// paddingFramePlan splits length bytes into reachable frame sizes. The result is
// exact whenever a partition exists; a length below the smallest frame (the
// captured 2-byte turns) is rounded up to that frame.
func paddingFramePlan(length int) ([]int, error) {
if length <= 0 {
return nil, nil
}
if length < paddingFrameOverhead {
return []int{paddingFrameOverhead}, nil
}
frameLengths := make([]int, 0, length/paddingBufferLength+1)
for remaining := length; remaining > 0; {
candidate := min(remaining, maxPaddingFrameLength)
picked := 0
for ; candidate >= paddingFrameOverhead; candidate-- {
if _, ok := paddingFrameDataLength(candidate); !ok {
continue
}
rest := remaining - candidate
if rest > 0 && rest < paddingFrameOverhead {
continue
}
picked = candidate
break
}
if picked == 0 {
return nil, fmt.Errorf("cannot frame %d padding bytes", length)
}
frameLengths = append(frameLengths, picked)
remaining -= picked
}
return frameLengths, nil
return nil
}
func selectPaddingVariant(turn paddingTurn, prefixLength int) (int, []int, []paddingDelayRange, error) {
@@ -1,137 +0,0 @@
package xmc
import (
"bytes"
"testing"
"time"
)
func TestPaddingFrameDataLengthRoundTrip(t *testing.T) {
for dataLength := 0; dataLength <= maxPaddingFrameDataLength; dataLength++ {
frameLength := paddingFrameLength(dataLength)
got, ok := paddingFrameDataLength(frameLength)
if !ok || got != dataLength {
t.Fatalf("data length %d -> frame %d -> data %d (ok=%v)", dataLength, frameLength, got, ok)
}
}
}
func TestPaddingFramePlanTotals(t *testing.T) {
lengths := []int{paddingFrameOverhead, paddingFrameOverhead + 1, 128, 129, 130, 131, 140, 1024, 48 * 1024, maxPaddingFrameLength - 1, maxPaddingFrameLength, maxPaddingFrameLength + 1, 3 * maxPaddingFrameLength, 1024 * 1024}
for _, length := range lengths {
plan, err := paddingFramePlan(length)
if err != nil {
t.Fatalf("plan %d: %v", length, err)
}
total := 0
for _, frameLength := range plan {
if _, ok := paddingFrameDataLength(frameLength); !ok {
t.Fatalf("plan %d contains unreachable frame %d", length, frameLength)
}
if frameLength > maxPaddingFrameLength {
t.Fatalf("plan %d contains oversized frame %d", length, frameLength)
}
total += frameLength
}
if total != length {
t.Fatalf("plan %d totals %d", length, total)
}
}
}
func TestPaddingFramePlanRoundsUpShortTurn(t *testing.T) {
plan, err := paddingFramePlan(2)
if err != nil {
t.Fatal(err)
}
if len(plan) != 1 || plan[0] != paddingFrameOverhead {
t.Fatalf("short turn plan = %v", plan)
}
}
func TestWritePaddingTurnFramesIsMinecraftTraffic(t *testing.T) {
turns := []paddingTurn{
{direction: paddingClientToServer, minLength: 44, maxLength: 44},
{direction: paddingClientToServer, minLength: 2, maxLength: 2},
{direction: paddingClientToServer, minLength: maxPaddingFrameLength, maxLength: maxPaddingFrameLength},
{direction: paddingClientToServer, minLength: 3*maxPaddingFrameLength + 7, maxLength: 3*maxPaddingFrameLength + 7},
}
for i, turn := range turns {
var buffer bytes.Buffer
written, err := writePaddingTurnFrames(&buffer, turn, 0, func(time.Duration) {}, configurationServerboundCustomPayload)
if err != nil {
t.Fatalf("turn %d: %v", i, err)
}
if written != buffer.Len() {
t.Fatalf("turn %d wrote %d bytes, buffer has %d", i, written, buffer.Len())
}
target := turn.minLength
if written < target {
t.Fatalf("turn %d wrote %d bytes, want at least %d", i, written, target)
}
reader := bytes.NewReader(buffer.Bytes())
frames := 0
for reader.Len() > 0 {
packet, err := readPacket(reader)
if err != nil {
t.Fatalf("turn %d frame %d is not a valid packet: %v", i, frames, err)
}
if packet.packetID != configurationServerboundCustomPayload {
t.Fatalf("turn %d frame %d has packet id %d", i, frames, packet.packetID)
}
_, ok, err := parseCustomPayload(packet)
if err != nil {
t.Fatalf("turn %d frame %d: %v", i, frames, err)
}
if ok {
t.Fatalf("turn %d frame %d uses the packet stream channel", i, frames)
}
if len(packet.data) == 0 {
t.Fatalf("turn %d frame %d has no channel", i, frames)
}
frames++
}
}
}
func TestRunPaddingScheduleLeavesPeerTurnsAlone(t *testing.T) {
schedule := []paddingTurn{
{direction: paddingClientToServer, minLength: 44, maxLength: 44},
{direction: paddingServerToClient, minLength: 64, maxLength: 64},
{direction: paddingClientToServer, minLength: 25, maxLength: 25},
}
var out bytes.Buffer
if err := runPaddingSchedule(&out, true, 2, schedule); err != nil {
t.Fatal(err)
}
reader := bytes.NewReader(out.Bytes())
total := 0
for reader.Len() > 0 {
before := reader.Len()
packet, err := readPacket(reader)
if err != nil {
t.Fatalf("padding stream is not a valid packet stream: %v", err)
}
if packet.packetID != configurationServerboundCustomPayload {
t.Fatalf("unexpected packet id %d in padding stream", packet.packetID)
}
total += before - reader.Len()
}
// Turn 0 carries the 2 byte Login Acknowledged prefix, turn 1 is the peer's.
want := 44 - 2 + 25
if total != want {
t.Fatalf("padding stream carries %d payload bytes, want %d", total, want)
}
}
func TestRunPaddingScheduleRejectsInvalidSchedule(t *testing.T) {
schedule := []paddingTurn{
{direction: paddingServerToClient, minLength: 10, maxLength: 10},
}
if err := runPaddingSchedule(&bytes.Buffer{}, true, 2, schedule); err == nil {
t.Fatal("expected an error for a prefixed server-to-client first turn")
}
}
@@ -0,0 +1,618 @@
package xmc
import (
"bytes"
"errors"
"io"
"net"
"strconv"
"strings"
"testing"
"time"
)
func TestPaddingTurnReachesFinalTargetLength(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 128, maxLength: 128}
const prefixLength = 3
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, prefixLength); err != nil {
t.Fatal(err)
}
if got := prefixLength + encoded.Len(); got != turn.minLength {
t.Fatalf("total turn length = %d, want %d", got, turn.minLength)
}
encodedReader := bytes.NewReader(encoded.Bytes())
var recordLength Varint
if err := recordLength.readFrom(encodedReader); err != nil {
t.Fatal(err)
}
if got := int(recordLength); got != encoded.Len() {
t.Fatalf("record length = %d, encoded = %d", got, encoded.Len())
}
if err := readPaddingTurn(bytes.NewReader(encoded.Bytes()), turn, prefixLength); err != nil {
t.Fatal(err)
}
}
func TestPaddingTurnSupportsThreeByteTarget(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 3, maxLength: 3}
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, 0); err != nil {
t.Fatal(err)
}
if got := encoded.Len(); got != 3 {
t.Fatalf("padding length = %d, want 3", got)
}
if err := readPaddingTurn(bytes.NewReader(encoded.Bytes()), turn, 0); err != nil {
t.Fatal(err)
}
}
func TestPaddingTurnVarintBoundaries(t *testing.T) {
for _, targetLength := range []int{127, 128, 16383, 16384} {
t.Run(strconv.Itoa(targetLength), func(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: targetLength, maxLength: targetLength}
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, 0); err != nil {
t.Fatal(err)
}
if encoded.Len() != targetLength {
t.Fatalf("padding length = %d, want %d", encoded.Len(), targetLength)
}
if err := readPaddingTurn(bytes.NewReader(encoded.Bytes()), turn, 0); err != nil {
t.Fatal(err)
}
})
}
}
func TestPaddingTurnRandomRange(t *testing.T) {
turn := paddingTurn{direction: paddingServerToClient, minLength: 127, maxLength: 129}
seen := make(map[int]bool)
for range 100 {
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, 0); err != nil {
t.Fatal(err)
}
if encoded.Len() < turn.minLength || encoded.Len() > turn.maxLength {
t.Fatalf("padding length = %d", encoded.Len())
}
seen[encoded.Len()] = true
}
if len(seen) < 2 {
t.Fatalf("padding range did not vary: %v", seen)
}
}
func TestPaddingTurnUsesRestrictedSendRange(t *testing.T) {
turn := paddingTurn{
direction: paddingServerToClient,
minLength: 3,
maxLength: 100,
sendMinLength: 90,
sendMaxLength: 100,
}
seen := make(map[int]bool)
for range 100 {
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, 0); err != nil {
t.Fatal(err)
}
if encoded.Len() < turn.sendMinLength || encoded.Len() > turn.sendMaxLength {
t.Fatalf("padding length = %d", encoded.Len())
}
seen[encoded.Len()] = true
if err := readPaddingTurn(bytes.NewReader(encoded.Bytes()), turn, 0); err != nil {
t.Fatal(err)
}
}
if len(seen) < 2 {
t.Fatalf("restricted send range did not vary: %v", seen)
}
}
func TestPaddingScheduleSynchronizesDirections(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
schedule := []paddingTurn{
{direction: paddingClientToServer, minLength: 33, maxLength: 33},
{direction: paddingServerToClient, minLength: 4097, maxLength: 4097},
{direction: paddingClientToServer, minLength: 16385, maxLength: 16385},
}
serverDone := make(chan error, 1)
go func() {
serverDone <- runPaddingSchedule(server, server, false, 3, schedule)
}()
if err := runPaddingSchedule(client, client, true, 3, schedule); err != nil {
t.Fatal(err)
}
select {
case err := <-serverDone:
if err != nil {
t.Fatal(err)
}
case <-time.After(time.Second):
t.Fatal("server padding schedule did not complete")
}
}
func TestReadPaddingTurnHandlesFragmentedInput(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 1024, maxLength: 1024}
var encoded bytes.Buffer
if err := writePaddingTurn(&encoded, turn, 0); err != nil {
t.Fatal(err)
}
if err := readPaddingTurn(&oneByteReader{reader: bytes.NewReader(encoded.Bytes())}, turn, 0); err != nil {
t.Fatal(err)
}
}
func TestReadPaddingTurnRejectsInvalidLength(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 64, maxLength: 96}
data := encodePaddingLength(t, 63)
if err := readPaddingTurn(bytes.NewReader(data), turn, 0); err == nil || !strings.Contains(err.Error(), "outside") {
t.Fatalf("error = %v", err)
}
}
func TestReadPaddingTurnRejectsNonCanonicalHeader(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 3, maxLength: 3}
err := readPaddingTurn(bytes.NewReader([]byte{0x83, 0x00, 0x00}), turn, 0)
if err == nil || !strings.Contains(err.Error(), "non-canonical") {
t.Fatalf("error = %v", err)
}
}
func TestReadPaddingTurnRejectsTruncatedBody(t *testing.T) {
turn := paddingTurn{direction: paddingClientToServer, minLength: 64, maxLength: 64}
data := encodePaddingLength(t, 64)
if err := readPaddingTurn(bytes.NewReader(data), turn, 0); err == nil || !strings.Contains(err.Error(), "body") {
t.Fatalf("error = %v", err)
}
}
func TestReadPaddingTurnHonorsConnectionTimeout(t *testing.T) {
client, server := net.Pipe()
defer client.Close()
defer server.Close()
if err := server.SetReadDeadline(time.Now().Add(20 * time.Millisecond)); err != nil {
t.Fatal(err)
}
turn := paddingTurn{direction: paddingClientToServer, minLength: 64, maxLength: 64}
err := readPaddingTurn(server, turn, 0)
var netErr net.Error
if !errors.As(err, &netErr) || !netErr.Timeout() {
t.Fatalf("error = %v, want network timeout", err)
}
}
func TestValidatePaddingSchedule(t *testing.T) {
tests := []struct {
name string
schedule []paddingTurn
prefix int
}{
{name: "empty"},
{name: "bad direction", schedule: []paddingTurn{{direction: 99, minLength: 4, maxLength: 4}}},
{name: "too small", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 0, maxLength: 4}}},
{name: "reversed range", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 7}}},
{name: "wrong first direction", prefix: 3, schedule: []paddingTurn{{direction: paddingServerToClient, minLength: 8, maxLength: 8}}},
{name: "prefix leaves no header", prefix: 8, schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8}}},
{name: "same direction", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8}, {direction: paddingClientToServer, minLength: 8, maxLength: 8}}},
{name: "range with variants", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8, variants: []paddingVariant{paddingVariantFromChunks(8)}}}},
{name: "empty variant", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{{}}}}},
{name: "bad chunk", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{paddingVariantFromChunks(maxPaddingChunkLength + 1)}}}},
{
name: "delay mismatch",
schedule: []paddingTurn{{
direction: paddingClientToServer,
variants: []paddingVariant{{
chunks: []int{4, 4},
delays: []paddingDelayRange{{min: time.Millisecond, max: time.Millisecond}},
}},
}},
},
{name: "reversed start delay", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8, startDelay: paddingDelayRange{min: 2 * time.Millisecond, max: time.Millisecond}}}},
{name: "reversed generated chunk delay", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8, chunkDelay: paddingDelayRange{min: 2 * time.Millisecond, max: time.Millisecond}}}},
{name: "oversized generated chunk", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8, writeChunkLength: maxPaddingChunkLength + 1}}},
{name: "reversed generated chunk range", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 8, writeChunkMinLength: 9, writeChunkLength: 8}}},
{name: "variant with generated chunks", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{paddingVariantFromChunks(8)}, writeChunkLength: 8}}},
{name: "send range outside accepted range", schedule: []paddingTurn{{direction: paddingClientToServer, minLength: 8, maxLength: 16, sendMinLength: 7, sendMaxLength: 12}}},
{name: "variant with send range", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{paddingVariantFromChunks(8)}, sendMinLength: 8, sendMaxLength: 8}}},
{name: "negative chunk delay", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{{chunks: []int{8}, delays: []paddingDelayRange{{min: -time.Millisecond}}}}}}},
{name: "bad send variant", schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{paddingVariantFromChunks(8)}, sendVariants: []int{1}}}},
{name: "prefix splits chunk", prefix: 3, schedule: []paddingTurn{{direction: paddingClientToServer, variants: []paddingVariant{paddingVariantFromChunks(8, 4)}}}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := validatePaddingSchedule(test.schedule, test.prefix); err == nil {
t.Fatal("expected invalid padding schedule")
}
})
}
}
func TestPaddingSchedule2612MatchesCapturedTemplates(t *testing.T) {
wantDirections := []paddingDirection{
paddingClientToServer,
paddingServerToClient,
paddingClientToServer,
paddingServerToClient,
paddingClientToServer,
paddingServerToClient,
}
wantLengths := [][]int{
{44},
{72},
{25},
{41172},
{2},
{7464, 8267, 8790, 8153, 7375, 7347, 8184, 6633, 7670, 8241, 7857, 7254, 8407, 8283, 6804, 8177, 8177, 7929, 8296, 8177},
}
if len(paddingSchedule2612) != len(wantDirections)+33 {
t.Fatalf("padding schedule has %d turns, want %d", len(paddingSchedule2612), len(wantDirections)+33)
}
for i, turn := range paddingSchedule2612[:len(wantDirections)] {
if turn.direction != wantDirections[i] {
t.Fatalf("padding turn %d direction = %d, want %d", i, turn.direction, wantDirections[i])
}
if len(turn.variants) != len(wantLengths[i]) {
t.Fatalf("padding turn %d has %d variants, want %d", i, len(turn.variants), len(wantLengths[i]))
}
for j, variant := range turn.variants {
if got := paddingVariantLength(variant); got != wantLengths[i][j] {
t.Fatalf("padding turn %d variant %d length = %d, want %d", i, j, got, wantLengths[i][j])
}
}
}
wantPlayBounds := [][2]int{
{6, 883},
{346, 58638},
{6, 887},
{388, 61077},
{2, 50},
{575, 65584},
{6, 45},
{86, 63563},
{2, 44},
{42, 51983},
{2, 851},
{309, 25083},
{2, 19},
{74, 63885},
{8, 24},
{30, 66128},
{2, 19},
{26, 35818},
{6, 19},
{35, 59407},
{6, 19},
{37, 65328},
{2, 19},
{26, 60622},
{6, 19},
{11, 60808},
{8, 43},
{55, 62027},
{2, 19},
{427, 65622},
{5, 19},
{35, 59401},
{6, 19},
}
for i, want := range wantPlayBounds {
turn := paddingSchedule2612[len(wantDirections)+i]
wantDirection := paddingClientToServer
if i%2 == 1 {
wantDirection = paddingServerToClient
}
if turn.direction != wantDirection {
t.Fatalf("play turn %d direction = %d, want %d", i, turn.direction, wantDirection)
}
if turn.minLength != want[0] || turn.maxLength != want[1] {
t.Fatalf("play turn %d bounds = %d-%d, want %d-%d", i, turn.minLength, turn.maxLength, want[0], want[1])
}
if len(turn.variants) != 0 {
t.Fatalf("play turn %d unexpectedly has captured variants", i)
}
}
if got := len(paddingSchedule2612[3].variants[0].chunks); got != 30 {
t.Fatalf("registry turn chunks = %d, want 30", got)
}
minimumPlayStart := maxPaddingTurnLength
maximumPlayStart := 0
for _, variant := range paddingSchedule2612[5].variants {
length := paddingVariantLength(variant)
minimumPlayStart = min(minimumPlayStart, length)
maximumPlayStart = max(maximumPlayStart, length)
if variant.chunks[0] != 4941 {
t.Fatalf("play start first chunk = %d, want 4941", variant.chunks[0])
}
}
if minimumPlayStart != 6633 || maximumPlayStart != 8790 {
t.Fatalf("play start bounds = %d-%d, want 6633-8790", minimumPlayStart, maximumPlayStart)
}
if err := validatePaddingSchedule(paddingSchedule2612, 2); err != nil {
t.Fatalf("captured schedule is invalid: %v", err)
}
serverSchedule, err := newServerPaddingSchedule2612()
if err != nil {
t.Fatal(err)
}
if err = validatePaddingSchedule(serverSchedule, 2); err != nil {
t.Fatalf("server schedule is invalid: %v", err)
}
for i, branches := range serverPlayBranches2612 {
turn := serverSchedule[len(startupPaddingSchedule2612)+1+i*2]
got := paddingLengthRange2612{turn.sendMinLength, turn.sendMaxLength}
if got != branches.small && got != branches.large {
t.Fatalf("server play turn %d send range = %v, want %v or %v", i, got, branches.small, branches.large)
}
}
for range 20 {
clientSchedule, clientErr := newClientPaddingSchedule2612()
if clientErr != nil {
t.Fatal(clientErr)
}
if clientErr = validatePaddingSchedule(clientSchedule, 2); clientErr != nil {
t.Fatalf("client schedule is invalid: %v", clientErr)
}
burstCount := 0
for _, burst := range clientPlayBursts2612 {
turn := clientSchedule[len(startupPaddingSchedule2612)+burst.playIndex]
got := paddingLengthRange2612{turn.sendMinLength, turn.sendMaxLength}
switch got {
case burst.regular:
case burst.burst:
burstCount++
default:
t.Fatalf("client play turn %d send range = %v", burst.playIndex, got)
}
}
if burstCount != 1 {
t.Fatalf("client schedule has %d initialization bursts, want 1", burstCount)
}
}
for variantIndex := range paddingSchedule2612[3].variants {
turn := paddingSchedule2612[3]
turn.sendVariants = []int{variantIndex}
var encoded bytes.Buffer
if err = writePaddingTurnWithSleep(&encoded, turn, 0, func(time.Duration) {}); err != nil {
t.Fatal(err)
}
if err = readPaddingTurn(bytes.NewReader(encoded.Bytes()), paddingSchedule2612[3], 0); err != nil {
t.Fatalf("registry variant %d was rejected: %v", variantIndex, err)
}
}
}
func TestPaddingVariantPreservesWriteBoundaries(t *testing.T) {
tests := []struct {
name string
turn paddingTurn
prefix int
want []int
}{
{name: "login acknowledged turn", turn: paddingSchedule2612[0], prefix: 2, want: []int{26, 16}},
{name: "server response turn", turn: paddingSchedule2612[1], want: []int{26, 21, 25}},
{name: "single packet turn", turn: paddingSchedule2612[2], want: []int{25}},
{name: "fixed registry profile", turn: paddingSchedule2612[3], want: paddingSchedule2612[3].variants[0].chunks},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var writer recordingWriter
if err := writePaddingTurnWithSleep(&writer, test.turn, test.prefix, func(time.Duration) {}); err != nil {
t.Fatal(err)
}
if len(writer.writes) != len(test.want) {
t.Fatalf("writes = %v, want %v", writer.writes, test.want)
}
for i := range test.want {
if writer.writes[i] != test.want[i] {
t.Fatalf("writes = %v, want %v", writer.writes, test.want)
}
}
if err := readPaddingTurn(bytes.NewReader(writer.Bytes()), test.turn, test.prefix); err != nil {
t.Fatal(err)
}
})
}
}
func TestPaddingVariantAppliesPacing(t *testing.T) {
turn := paddingTurn{
direction: paddingClientToServer,
startDelay: paddingDelayRange{min: 3 * time.Millisecond, max: 3 * time.Millisecond},
variants: []paddingVariant{{
chunks: []int{3, 5, 7},
delays: []paddingDelayRange{
{},
{min: 2 * time.Millisecond, max: 2 * time.Millisecond},
{min: 4 * time.Millisecond, max: 4 * time.Millisecond},
},
}},
}
var slept []time.Duration
var writer recordingWriter
if err := writePaddingTurnWithSleep(&writer, turn, 3, func(delay time.Duration) {
slept = append(slept, delay)
}); err != nil {
t.Fatal(err)
}
want := []time.Duration{3 * time.Millisecond, 2 * time.Millisecond, 4 * time.Millisecond}
if len(slept) != len(want) {
t.Fatalf("delays = %v, want %v", slept, want)
}
for i := range want {
if slept[i] != want[i] {
t.Fatalf("delays = %v, want %v", slept, want)
}
}
}
func TestGeneratedPaddingChunksApplyPacing(t *testing.T) {
turn := paddingTurn{
direction: paddingServerToClient,
minLength: 100,
maxLength: 100,
writeChunkLength: 32,
chunkDelay: paddingDelayRange{min: 2 * time.Millisecond, max: 2 * time.Millisecond},
}
var slept []time.Duration
var writer recordingWriter
if err := writePaddingTurnWithSleep(&writer, turn, 0, func(delay time.Duration) {
slept = append(slept, delay)
}); err != nil {
t.Fatal(err)
}
wantWrites := []int{32, 32, 32, 4}
if !slicesEqual(writer.writes, wantWrites) {
t.Fatalf("writes = %v, want %v", writer.writes, wantWrites)
}
wantSleeps := []time.Duration{2 * time.Millisecond, 2 * time.Millisecond, 2 * time.Millisecond}
if !slicesEqual(slept, wantSleeps) {
t.Fatalf("delays = %v, want %v", slept, wantSleeps)
}
if err := readPaddingTurn(bytes.NewReader(writer.Bytes()), turn, 0); err != nil {
t.Fatal(err)
}
}
func TestGeneratedPaddingChunkLengthIsRandomized(t *testing.T) {
turn := paddingTurn{
direction: paddingServerToClient,
minLength: 100,
maxLength: 100,
writeChunkMinLength: 16,
writeChunkLength: 32,
}
seen := make(map[int]bool)
for range 100 {
var writer recordingWriter
if err := writePaddingTurnWithSleep(&writer, turn, 0, func(time.Duration) {}); err != nil {
t.Fatal(err)
}
firstWrite := writer.writes[0]
if firstWrite < turn.writeChunkMinLength || firstWrite > turn.writeChunkLength {
t.Fatalf("first write = %d", firstWrite)
}
seen[firstWrite] = true
}
if len(seen) < 2 {
t.Fatalf("generated write chunk length did not vary: %v", seen)
}
}
func TestPaddingDelayRangeIsRandomized(t *testing.T) {
delayRange := millisecondRange(25, 40)
seen := make(map[time.Duration]bool)
for range 100 {
delay, err := randomPaddingDelay(delayRange)
if err != nil {
t.Fatal(err)
}
if delay < delayRange.min || delay > delayRange.max {
t.Fatalf("delay = %s, want %s-%s", delay, delayRange.min, delayRange.max)
}
seen[delay] = true
}
if len(seen) < 2 {
t.Fatalf("padding delay did not vary: %v", seen)
}
}
func TestPaddingSchedule2612UsesCoarseTimingBands(t *testing.T) {
assertDelayRange(t, "turn 3 to 4", paddingSchedule2612[3].startDelay, 20*time.Millisecond, 50*time.Millisecond)
assertDelayRange(t, "turn 5 to 6", paddingSchedule2612[5].startDelay, 35*time.Millisecond, 50*time.Millisecond)
assertDelayRange(t, "first play client turn", paddingSchedule2612[6].startDelay, time.Millisecond, 30*time.Millisecond)
assertDelayRange(t, "first play server turn", paddingSchedule2612[7].startDelay, time.Millisecond, 45*time.Millisecond)
assertDelayRange(t, "play server chunk pacing", paddingSchedule2612[7].chunkDelay, time.Millisecond, 4*time.Millisecond)
if paddingSchedule2612[6].writeChunkLength != 1024 {
t.Fatalf("play client write chunk = %d, want 1024", paddingSchedule2612[6].writeChunkLength)
}
if paddingSchedule2612[7].writeChunkLength != maxPaddingChunkLength {
t.Fatalf("play server write chunk = %d, want %d", paddingSchedule2612[7].writeChunkLength, maxPaddingChunkLength)
}
if paddingSchedule2612[7].writeChunkMinLength != 32*1024 {
t.Fatalf("play server minimum write chunk = %d, want %d", paddingSchedule2612[7].writeChunkMinLength, 32*1024)
}
for i, variant := range paddingSchedule2612[3].variants {
minimum, maximum := paddingVariantDelayBounds(variant)
if minimum != 45*time.Millisecond || maximum != 65*time.Millisecond {
t.Fatalf("turn 4 variant %d duration = %s-%s, want 45ms-65ms", i, minimum, maximum)
}
}
for i, variant := range paddingSchedule2612[5].variants {
minimum, maximum := paddingVariantDelayBounds(variant)
if minimum != 10*time.Millisecond || maximum != 25*time.Millisecond {
t.Fatalf("turn 6 variant %d duration = %s-%s, want 10ms-25ms", i, minimum, maximum)
}
}
}
func assertDelayRange(t *testing.T, name string, got paddingDelayRange, minimum, maximum time.Duration) {
t.Helper()
if got.min != minimum || got.max != maximum {
t.Fatalf("%s delay = %s-%s, want %s-%s", name, got.min, got.max, minimum, maximum)
}
}
func paddingVariantDelayBounds(variant paddingVariant) (time.Duration, time.Duration) {
var minimum time.Duration
var maximum time.Duration
for _, delay := range variant.delays {
minimum += delay.min
maximum += delay.max
}
return minimum, maximum
}
func slicesEqual[T comparable](left, right []T) bool {
if len(left) != len(right) {
return false
}
for i := range left {
if left[i] != right[i] {
return false
}
}
return true
}
func encodePaddingLength(t *testing.T, length int) []byte {
t.Helper()
var encoded bytes.Buffer
value := Varint(length)
if err := value.writeTo(&encoded); err != nil {
t.Fatal(err)
}
return encoded.Bytes()
}
type oneByteReader struct {
reader io.Reader
}
func (r *oneByteReader) Read(p []byte) (int, error) {
if len(p) > 1 {
p = p[:1]
}
return r.reader.Read(p)
}
type recordingWriter struct {
bytes.Buffer
writes []int
}
func (w *recordingWriter) Write(p []byte) (int, error) {
w.writes = append(w.writes, len(p))
return w.Buffer.Write(p)
}
@@ -0,0 +1,33 @@
package xmc
import (
"bytes"
"testing"
)
func TestProfilesFromConfigRejectsEmpty(t *testing.T) {
if _, err := profilesFromConfig(nil); err == nil {
t.Fatal("expected empty profiles error")
}
}
func TestProfilesFromConfig(t *testing.T) {
uuid := bytes.Repeat([]byte{0x2a}, 16)
profiles, err := profilesFromConfig([]*Profile{
{
Username: "SignedUser",
Uuid: uuid,
TexturesValue: "textures-value",
TexturesSignature: "textures-signature",
},
})
if err != nil {
t.Fatalf("build explicit profile: %v", err)
}
if len(profiles) != 1 || profiles[0].Username != "SignedUser" {
t.Fatalf("unexpected profile: %+v", profiles)
}
if profiles[0].TexturesValue != "textures-value" || profiles[0].TexturesSignature != "textures-signature" {
t.Fatalf("textures were not preserved: %+v", profiles[0])
}
}
+1 -5
View File
@@ -8,11 +8,7 @@ import (
)
const (
// Clientbound custom payloads are allowed up to 1 MiB by vanilla, and a
// vanilla configuration state can send large registry data packets. This is
// only an upper bound for decoding; the XMC payload stream keeps its own,
// much smaller, chunk size.
maxPacketDataLength = 1024 * 1024
maxPacketDataLength = 32 * 1024
maxPacketBodyLength = maxPacketDataLength + 5
)
@@ -0,0 +1,56 @@
package xmc
import (
"bytes"
"strings"
"testing"
)
func TestReadPacketDoesNotConsumeFollowingPacket(t *testing.T) {
data := []byte{0x01, 0x80, 0x01, 0x00}
r := bytes.NewReader(data)
if _, err := readPacket(r); err == nil {
t.Fatal("expected truncated packet ID to fail")
}
pkt, err := readPacket(r)
if err != nil {
t.Fatalf("read following packet: %v", err)
}
if pkt.packetID != 0 {
t.Fatalf("packet ID = %d", pkt.packetID)
}
}
func TestPacketWithLengthReportsWireBytes(t *testing.T) {
var wire bytes.Buffer
written, err := writePacketWithLength(&wire, 0x03)
if err != nil {
t.Fatal(err)
}
if written != 2 || !bytes.Equal(wire.Bytes(), []byte{0x01, 0x03}) {
t.Fatalf("wire = %x, length = %d", wire.Bytes(), written)
}
packet, read, err := readPacketWithLength(bytes.NewReader(wire.Bytes()))
if err != nil {
t.Fatal(err)
}
if packet.packetID != 0x03 || read != written {
t.Fatalf("packet ID = %d, read = %d, written = %d", packet.packetID, read, written)
}
}
func TestReadPacketRejectsNonCanonicalLengthVarint(t *testing.T) {
_, _, err := readPacketWithLength(bytes.NewReader([]byte{0x81, 0x00, 0x03}))
if err == nil || !strings.Contains(err.Error(), "non-canonical") {
t.Fatalf("error = %v", err)
}
}
func TestVarintRejectsOversizedFifthByte(t *testing.T) {
var value Varint
err := value.readFrom(bytes.NewReader([]byte{0xff, 0xff, 0xff, 0xff, 0x1f}))
if err == nil || !strings.Contains(err.Error(), "too large") {
t.Fatalf("error = %v", err)
}
}
+3 -3
View File
@@ -245,7 +245,7 @@ func (c *serverConn) handshake() error {
if err = validateLoginAcknowledgedPacket(pkt); err != nil {
return err
}
if err = runPaddingSchedule(c.writer, false, loginAcknowledgedLength, c.paddingSchedule); err != nil {
if err = runPaddingSchedule(c.reader, c.writer, false, loginAcknowledgedLength, c.paddingSchedule); err != nil {
return fmt.Errorf("run startup padding: %w", err)
}
@@ -328,7 +328,7 @@ func (c *serverConn) SetWriteDeadline(t time.Time) error {
return c.deadlines.setWriteDeadline(t)
}
func wrapConnServer(c net.Conn, profiles []loginProfile, password string, rsaPrivateKeyDER []byte, rsaPublicKey []byte, padding []*Padding) (*serverConn, error) {
func wrapConnServer(c net.Conn, profiles []loginProfile, password string, rsaPrivateKeyDER []byte, rsaPublicKey []byte) (*serverConn, error) {
if len(profiles) == 0 {
return nil, fmt.Errorf("empty profiles")
}
@@ -342,7 +342,7 @@ func wrapConnServer(c net.Conn, profiles []loginProfile, password string, rsaPri
if err != nil {
return nil, fmt.Errorf("parse rsa private key: %w", err)
}
paddingSchedule, err := newPaddingSchedule(padding, false)
paddingSchedule, err := newServerPaddingSchedule2612()
if err != nil {
return nil, fmt.Errorf("select padding profile: %w", err)
}
@@ -0,0 +1,81 @@
package xmc
import (
"bytes"
"io"
"testing"
)
type dataAndEOFReader struct {
data []byte
}
func (r *dataAndEOFReader) Read(p []byte) (int, error) {
if len(r.data) == 0 {
return 0, io.EOF
}
n := copy(p, r.data)
r.data = r.data[n:]
return n, io.EOF
}
type shortWriter struct {
bytes.Buffer
}
func (w *shortWriter) Write(p []byte) (int, error) {
if len(p) > 1 {
p = p[:len(p)/2]
}
return w.Buffer.Write(p)
}
func TestCryptoReaderPreservesDataReturnedWithEOF(t *testing.T) {
secret := []byte("0123456789abcdef")
plaintext := []byte("payload returned with EOF")
var encrypted bytes.Buffer
writer, err := newCryptoWriter(&encrypted, secret)
if err != nil {
t.Fatal(err)
}
if _, err = writer.Write(plaintext); err != nil {
t.Fatal(err)
}
reader, err := newCryptoReader(&dataAndEOFReader{data: encrypted.Bytes()}, secret)
if err != nil {
t.Fatal(err)
}
got := make([]byte, len(plaintext))
n, err := reader.Read(got)
if err == nil || n != len(plaintext) {
t.Fatalf("Read = %d, %v", n, err)
}
if !bytes.Equal(got[:n], plaintext) {
t.Fatalf("plaintext = %q", got[:n])
}
}
func TestCryptoWriterHandlesShortWrites(t *testing.T) {
secret := []byte("0123456789abcdef")
plaintext := bytes.Repeat([]byte("short-write"), 100)
var dst shortWriter
writer, err := newCryptoWriter(&dst, secret)
if err != nil {
t.Fatal(err)
}
if n, err := writer.Write(plaintext); err != nil || n != len(plaintext) {
t.Fatalf("Write = %d, %v", n, err)
}
reader, err := newCryptoReader(bytes.NewReader(dst.Bytes()), secret)
if err != nil {
t.Fatal(err)
}
got, err := io.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, plaintext) {
t.Fatal("decrypted payload mismatch")
}
}
+3 -7
View File
@@ -197,8 +197,7 @@ func (c *client) dial(ctx context.Context) error {
}
// udp, _ := strconv.ParseBool(resp.Header.Get(ResponseHeaderUDPEnabled))
remote_rx, _ := strconv.ParseUint(resp.Header.Get(CommonHeaderCCRX), 10, 64)
tx := quicParams.BrutalUp
down, _ := strconv.ParseUint(resp.Header.Get(CommonHeaderCCRX), 10, 64)
errors.LogDebug(context.Background(), "ECHAccepted ", conn.ConnectionState().TLS.ECHAccepted)
switch quicParams.Congestion {
@@ -206,13 +205,10 @@ func (c *client) dial(ctx context.Context) error {
case "bbr":
congestion.UseBBR(conn, bbr.Profile(quicParams.BbrProfile))
case "", "brutal":
if tx == 0 {
if quicParams.BrutalUp == 0 || down == 0 {
congestion.UseBBR(conn, bbr.Profile(quicParams.BbrProfile))
} else {
if remote_rx > 0 {
tx = min(tx, remote_rx)
}
congestion.UseBrutal(conn, tx, quicParams.BrutalDisableLossCompensation)
congestion.UseBrutal(conn, min(quicParams.BrutalUp, down), quicParams.BrutalDisableLossCompensation)
}
case "force-brutal":
congestion.UseBrutal(conn, quicParams.BrutalUp, quicParams.BrutalDisableLossCompensation)
+3 -7
View File
@@ -55,8 +55,7 @@ func (h *httpHandler) AuthHTTP(w http.ResponseWriter, r *http.Request) bool {
}
auth := r.Header.Get(RequestHeaderAuth)
remote_rx, _ := strconv.ParseUint(r.Header.Get(CommonHeaderCCRX), 10, 64)
tx := h.quicParams.BrutalUp
down, _ := strconv.ParseUint(r.Header.Get(CommonHeaderCCRX), 10, 64)
var user *protocol.MemoryUser
var ok bool
@@ -77,13 +76,10 @@ func (h *httpHandler) AuthHTTP(w http.ResponseWriter, r *http.Request) bool {
case "bbr":
congestion.UseBBR(conn, bbr.Profile(quicParams.BbrProfile))
case "", "brutal":
if remote_rx == 0 {
if quicParams.BrutalUp == 0 || down == 0 {
congestion.UseBBR(conn, bbr.Profile(quicParams.BbrProfile))
} else {
if tx > 0 {
remote_rx = min(remote_rx, tx)
}
congestion.UseBrutal(conn, remote_rx, quicParams.BrutalDisableLossCompensation)
congestion.UseBrutal(conn, min(quicParams.BrutalUp, down), quicParams.BrutalDisableLossCompensation)
}
case "force-brutal":
congestion.UseBrutal(conn, quicParams.BrutalUp, quicParams.BrutalDisableLossCompensation)
+1
View File
@@ -1,4 +1,5 @@
//go:build openbsd
// +build openbsd
package internet
+2 -1
View File
@@ -1,4 +1,5 @@
//go:build !windows && !darwin && !linux && !freebsd && !openbsd
//go:build js || netbsd || solaris
// +build js netbsd solaris
package internet
+1
View File
@@ -1,4 +1,5 @@
//go:build darwin
// +build darwin
package tcp
@@ -1,4 +1,5 @@
//go:build freebsd
// +build freebsd
package tcp
+1
View File
@@ -1,4 +1,5 @@
//go:build linux
// +build linux
package tcp
@@ -1,4 +1,5 @@
//go:build linux
// +build linux
package tcp_test
+1
View File
@@ -1,4 +1,5 @@
//go:build !linux && !freebsd && !darwin
// +build !linux,!freebsd,!darwin
package tcp
+1
View File
@@ -1,4 +1,5 @@
//go:build !windows
// +build !windows
package tls
+1
View File
@@ -1,4 +1,5 @@
//go:build windows
// +build windows
package tls
-1
View File
@@ -275,7 +275,6 @@ var ModernFingerprints = map[string]*utls.ClientHelloID{
"hellochrome_120": &utls.HelloChrome_120,
"hellochrome_131": &utls.HelloChrome_131,
"hellochrome_133": &utls.HelloChrome_133,
"hellochrome_155": &utls.HelloChrome_155,
"helloios_13": &utls.HelloIOS_13,
"helloios_14": &utls.HelloIOS_14,
"helloedge_106": &utls.HelloEdge_106,
+1
View File
@@ -1,4 +1,5 @@
//go:build darwin
// +build darwin
package udp
+1
View File
@@ -1,4 +1,5 @@
//go:build freebsd
// +build freebsd
package udp
+1
View File
@@ -1,4 +1,5 @@
//go:build linux
// +build linux
package udp
+1
View File
@@ -1,4 +1,5 @@
//go:build openbsd
// +build openbsd
package udp
@@ -1,4 +1,5 @@
//go:build openbsd
// +build openbsd
package udp
+1
View File
@@ -1,4 +1,5 @@
//go:build !linux && !freebsd && !darwin && !openbsd
// +build !linux,!freebsd,!darwin,!openbsd
package udp
+5 -42
View File
@@ -433,22 +433,14 @@ func (b *doneBody) Close() error {
return err
}
// a connection that has not answered yet is given this long before its
// timeouts retire it, so that a dial or handshake is not cut short again
var stalledGrace = 12 * time.Second
// sessionTransport sends the requests of one session over a connection of
// XMUX, and over another once that one has no requests or time left
type sessionTransport struct {
pool *connPool
mu sync.Mutex
client *splithttp.XmuxClient
closed bool
slowAt, retiredAt time.Time
movedAt time.Time
takenAt time.Time
served bool
mu sync.Mutex
client *splithttp.XmuxClient
closed bool
}
func (t *sessionTransport) RoundTrip(r *http.Request) (*http.Response, error) {
@@ -458,7 +450,7 @@ func (t *sessionTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, errSessionClosed
}
client := t.client
if client == nil || client.XmuxConn.IsClosed() || client.LeftRequests.Add(-1) < 0 || !client.UnreusableAt.IsZero() && time.Now().After(client.UnreusableAt) {
if client == nil || client.LeftRequests.Add(-1) < 0 || !client.UnreusableAt.IsZero() && time.Now().After(client.UnreusableAt) {
t.pool.mu.Lock()
t.client = t.pool.xmux.GetXmuxClient(r.Context())
t.pool.mu.Unlock()
@@ -466,10 +458,8 @@ func (t *sessionTransport) RoundTrip(r *http.Request) (*http.Response, error) {
t.client.LeftRequests.Add(-1)
if client != nil {
client.DoneRunning()
t.movedAt = time.Now()
}
client = t.client
t.takenAt, t.served = time.Now(), false
}
// counted under mu, or another request of the session that moves on could
// close the connection before this one is sent
@@ -478,34 +468,7 @@ func (t *sessionTransport) RoundTrip(r *http.Request) (*http.Response, error) {
conn.inflight++
conn.mu.Unlock()
t.mu.Unlock()
resp, err := conn.roundTrip(r)
if err == nil {
t.mu.Lock()
if t.client == client {
t.served = true
}
t.mu.Unlock()
}
return resp, err
}
func (t *sessionTransport) stalled(began time.Time) {
t.mu.Lock()
defer t.mu.Unlock()
if began.Before(t.movedAt) {
return
}
now := time.Now()
if !t.served && now.Sub(t.takenAt) < stalledGrace {
return
}
slow := now.Sub(t.slowAt) < 3*time.Second
t.slowAt = now
if !slow || t.client == nil || now.Sub(t.retiredAt) < 2*time.Second {
return
}
t.retiredAt = now
t.client.XmuxConn.(*poolConn).Close()
return conn.roundTrip(r)
}
func (t *sessionTransport) Close() {
-154
View File
@@ -987,157 +987,3 @@ func TestDriveXmuxOverH3(t *testing.T) {
}
waitFor(t, "the first connection of XMUX to close", func() bool { return server.closed.Load() == 1 })
}
type stallConn struct{}
func (stallConn) RoundTrip(r *http.Request) (*http.Response, error) {
<-r.Context().Done()
return nil, r.Context().Err()
}
type okConn struct{}
func (okConn) RoundTrip(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(`{"id":"file-1"}`)),
}, nil
}
func TestSessionRetiresStalledConn(t *testing.T) {
oldGrace := stalledGrace
stalledGrace = 0
t.Cleanup(func() { stalledGrace = oldGrace })
made := atomic.Int32{}
transport := &sessionTransport{pool: newConnPool(nil, func(connCtx context.Context) *http.Client {
if made.Add(1) == 1 {
return &http.Client{Transport: stallConn{}}
}
return &http.Client{Transport: okConn{}}
})}
storage := &driveStorage{
driveShared: &driveShared{
token: "token",
tokenExpiry: time.Now().Add(time.Hour),
backoff: time.Millisecond,
},
client: &http.Client{Transport: transport},
inflight: make(chan struct{}, 1),
reserved: make(chan struct{}, 1),
}
ctx := context.WithValue(context.Background(), attemptKey{}, 100*time.Millisecond)
code, body, err := storage.do(ctx, http.MethodPost, "http://drive.invalid/files", "", nil)
if err != nil {
t.Fatalf("the first request over a stalled connection: %v", err)
}
if code != http.StatusOK || string(body) != `{"id":"file-1"}` {
t.Fatalf("the stalled connection gave %d %q", code, body)
}
if n := made.Load(); n != 2 {
t.Fatalf("a stalled connection took %d connections, want it retired and one made after it", n)
}
if _, _, err := storage.do(ctx, http.MethodPost, "http://drive.invalid/files", "", nil); err != nil {
t.Fatalf("the request after the retirement: %v", err)
}
if n := made.Load(); n != 2 {
t.Fatalf("%d connections were made, want the healthy one kept", n)
}
}
func TestSessionIgnoresStallOfRetiredConn(t *testing.T) {
var made atomic.Int32
transport := &sessionTransport{pool: newConnPool(nil, func(connCtx context.Context) *http.Client {
made.Add(1)
return &http.Client{Transport: okConn{}}
})}
send := func() {
req, err := http.NewRequest(http.MethodGet, "http://drive.invalid/files", nil)
if err != nil {
t.Fatal(err)
}
resp, err := transport.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}
send()
old := time.Now()
time.Sleep(2 * time.Millisecond)
transport.stalled(old)
transport.stalled(old)
send()
if n := made.Load(); n != 2 {
t.Fatalf("a stalled connection took %d connections, want it retired and one made after it", n)
}
transport.mu.Lock()
transport.slowAt, transport.retiredAt = time.Time{}, time.Time{}
transport.mu.Unlock()
transport.stalled(old)
transport.stalled(old)
send()
if n := made.Load(); n != 2 {
t.Fatalf("the stall of a retired connection took the healthy one too, %d connections", n)
}
time.Sleep(2 * time.Millisecond)
fresh := time.Now()
transport.stalled(fresh)
transport.stalled(fresh)
send()
if n := made.Load(); n != 3 {
t.Fatalf("a stall of the current connection made %d connections, want it retired", n)
}
}
func TestSessionSparesFreshConn(t *testing.T) {
transport := &sessionTransport{pool: newConnPool(nil, func(connCtx context.Context) *http.Client {
return &http.Client{Transport: okConn{}}
})}
send := func() {
req, err := http.NewRequest(http.MethodGet, "http://drive.invalid/files", nil)
if err != nil {
t.Fatal(err)
}
resp, err := transport.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}
current := func() *poolConn {
transport.mu.Lock()
defer transport.mu.Unlock()
return transport.client.XmuxConn.(*poolConn)
}
send()
conn := current()
transport.mu.Lock()
transport.takenAt, transport.served = time.Now(), false
transport.mu.Unlock()
transport.stalled(time.Now())
transport.stalled(time.Now())
if conn.IsClosed() {
t.Fatal("a fresh connection was retired before its grace period ended")
}
transport.mu.Lock()
transport.takenAt = time.Now().Add(-stalledGrace - time.Second)
transport.mu.Unlock()
transport.stalled(time.Now())
transport.stalled(time.Now())
if !conn.IsClosed() {
t.Fatal("a fresh connection older than its grace period was not retired")
}
send()
conn = current()
transport.mu.Lock()
transport.takenAt, transport.served = time.Now(), true
transport.slowAt, transport.retiredAt = time.Time{}, time.Time{}
transport.mu.Unlock()
transport.stalled(time.Now())
transport.stalled(time.Now())
if !conn.IsClosed() {
t.Fatal("a connection that had answered was not retired")
}
}
+2 -2
View File
@@ -47,12 +47,12 @@ func newConn(ctx context.Context, storage Storage, writePrefix, readPrefix strin
}
}
func newSlotConn(ctx context.Context, store slotStore, write, read slotTable, p params, first []byte, onClose func()) *Conn {
func newSlotConn(ctx context.Context, store slotStore, write, read slotTable, p params, onClose func()) *Conn {
ctx, cancel := context.WithCancel(ctx)
return &Conn{
cancel: cancel,
writer: newSlotWriter(ctx, store, write, p),
reader: newSlotReader(ctx, store, read, p, first),
reader: newSlotReader(ctx, store, read, p),
onClose: onClose,
}
}
+38 -286
View File
@@ -11,7 +11,6 @@ import (
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/xtls/xray-core/common/dice"
@@ -19,18 +18,14 @@ import (
"github.com/xtls/xray-core/transport/internet"
)
// testDriveIO is a nil-by-default test hook observing every Drive request
var testDriveIO func(method, target string, body []byte)
const (
flatSeparator = "~"
driveBoundary = "xdrive-boundary"
drivePageSize = 1000
driveMaxAttempts = 8
driveMaxInflight = 64
driveReservedInflight = 8
driveInlineLimit = 12000
driveBoundary = "xdrive-boundary"
drivePageSize = 1000
driveMaxAttempts = 8
driveMaxInflight = 32
driveInlineLimit = 12000
// driveTimeout ends a request that makes no progress for that long
driveTimeout = 60 * time.Second
)
@@ -41,37 +36,13 @@ var (
driveFilesURL = "https://www.googleapis.com/drive/v3/files"
driveUploadURL = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name"
driveInitialBackoff = 200 * time.Millisecond
driveCooldown = 2 * time.Second
driveSlotRead = 3 * time.Second
driveSlotWrite = 5 * time.Second
tokenRefreshBackoff = 30 * time.Second
maxTokenBackoff = 5 * time.Minute
driveMaxResponse = 32 << 20
driveMaxAnnounce = 1 << 20
)
// maxBodyKey lets a caller tighten the response-body cap (announcements).
type maxBodyKey struct{}
// tokenFlight is one shared foreground token fetch; waiters read its result
// after done closes instead of starting their own fetch.
type tokenFlight struct {
done chan struct{}
token string
lifetime time.Duration
err error
}
type laneKey struct{}
type attemptKey struct{}
type driveStorage struct {
*driveShared
// a session has a client and inflight of its own, see session
client *http.Client
inflight chan struct{}
reserved chan struct{}
release func()
}
@@ -88,23 +59,12 @@ type driveShared struct {
uploadURL string
backoff time.Duration
tokenMu sync.Mutex
token string
tokenExpiry time.Time
tokenLifetime time.Duration
refreshing bool
authErr error
flight *tokenFlight
tokenRetryAt time.Time
tokenBackoff time.Duration
lastTokenErr error
tokenMu sync.Mutex
token string
tokenExpiry time.Time
idMu sync.Mutex
ids map[string]string
coolMu sync.Mutex
coolUntil time.Time
throttle atomic.Int64
}
func newDriveStorage(streamSettings *internet.MemoryStreamConfig, config *Config) (*driveStorage, error) {
@@ -140,7 +100,6 @@ func newDriveStorage(streamSettings *internet.MemoryStreamConfig, config *Config
},
client: client,
inflight: make(chan struct{}, driveMaxInflight),
reserved: make(chan struct{}, driveReservedInflight),
}, nil
}
@@ -152,7 +111,6 @@ func (s *driveStorage) session() Storage {
driveShared: s.driveShared,
client: &http.Client{Transport: transport},
inflight: make(chan struct{}, driveMaxInflight),
reserved: make(chan struct{}, driveReservedInflight),
release: transport.Close,
}
}
@@ -165,130 +123,14 @@ func quoteDriveValue(value string) string {
return strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace(value)
}
type authError struct{ code string }
func (e authError) Error() string {
return "Google rejected the OAuth credentials (" + e.code + "); create a new refresh token, see the XDRIVE setup guide"
}
func isAuthError(err error) bool {
_, ok := err.(authError)
return ok
}
func (s *driveStorage) accessToken(ctx context.Context) (string, error) {
s.tokenMu.Lock()
if s.authErr != nil {
err := s.authErr
s.tokenMu.Unlock()
return "", err
}
if s.token != "" && time.Now().Before(s.tokenExpiry) {
if s.tokenLifetime > 0 && time.Until(s.tokenExpiry) < s.tokenLifetime/5 && !s.refreshing && time.Now().After(s.tokenRetryAt) {
s.refreshing = true
go s.refreshTokenBackground()
}
token := s.token
s.tokenMu.Unlock()
return token, nil
}
if f := s.flight; f != nil {
s.tokenMu.Unlock()
select {
case <-f.done:
case <-ctx.Done():
return "", ctx.Err()
}
if f.err != nil {
return "", f.err
}
return f.token, nil
}
if time.Now().Before(s.tokenRetryAt) && s.lastTokenErr != nil {
err := s.lastTokenErr
s.tokenMu.Unlock()
return "", err
}
f := &tokenFlight{done: make(chan struct{})}
s.flight = f
s.tokenMu.Unlock()
// The shared fetch must outlive any single waiter's cancellation.
fetchCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
token, lifetime, err := s.fetchToken(fetchCtx)
cancel()
s.tokenMu.Lock()
s.flight = nil
f.token, f.lifetime, f.err = token, lifetime, err
if err != nil {
s.lastTokenErr = err
if isAuthError(err) {
s.authErr = err
errors.LogError(ctx, err)
}
s.noteTokenFailure()
} else {
s.storeToken(token, lifetime)
s.tokenBackoff = 0
s.tokenRetryAt = time.Time{}
s.lastTokenErr = nil
}
close(f.done)
s.tokenMu.Unlock()
if err != nil {
return "", err
}
return token, nil
}
// noteTokenFailure backs off further token fetches so a failing endpoint is
// not hit once per API call; called with tokenMu held.
func (s *driveStorage) noteTokenFailure() {
if s.tokenBackoff == 0 {
s.tokenBackoff = tokenRefreshBackoff
} else {
s.tokenBackoff = min(2*s.tokenBackoff, maxTokenBackoff)
}
s.tokenRetryAt = time.Now().Add(s.tokenBackoff)
}
func (s *driveStorage) storeToken(token string, lifetime time.Duration) {
s.token = token
s.tokenLifetime = lifetime
expiry := lifetime
if expiry > time.Minute {
expiry -= time.Minute
}
s.tokenExpiry = time.Now().Add(expiry)
}
func (s *driveStorage) refreshTokenBackground() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
token, lifetime, err := s.fetchToken(ctx)
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
s.refreshing = false
if err != nil {
if isAuthError(err) && s.authErr == nil {
s.authErr = err
errors.LogError(ctx, err)
}
s.lastTokenErr = err
s.noteTokenFailure()
return
}
if s.authErr == nil {
s.storeToken(token, lifetime)
s.tokenBackoff = 0
s.tokenRetryAt = time.Time{}
s.lastTokenErr = nil
}
}
func (s *driveStorage) fetchToken(ctx context.Context) (string, time.Duration, error) {
if s.token != "" && time.Now().Before(s.tokenExpiry) {
return s.token, nil
}
form := url.Values{
"client_id": {s.clientID},
"client_secret": {s.clientSecret},
@@ -297,31 +139,22 @@ func (s *driveStorage) fetchToken(ctx context.Context) (string, time.Duration, e
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.tokenURL, strings.NewReader(form.Encode()))
if err != nil {
return "", 0, errors.New("failed to build the token request").Base(err)
return "", errors.New("failed to build the token request").Base(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.tokenClient.Do(req)
if err != nil {
return "", 0, errors.New("failed to refresh the access token").Base(err)
return "", errors.New("failed to refresh the access token").Base(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return "", 0, errors.New("failed to read the token response").Base(err)
return "", errors.New("failed to read the token response").Base(err)
}
if resp.StatusCode != http.StatusOK {
var rejected struct {
Error string `json:"error"`
}
if json.Unmarshal(body, &rejected) == nil {
switch rejected.Error {
case "invalid_grant", "invalid_client", "unauthorized_client":
return "", 0, authError{rejected.Error}
}
}
return "", 0, errors.New("the token endpoint answered ", resp.StatusCode, ": ", string(body))
return "", errors.New("the token endpoint answered ", resp.StatusCode, ": ", string(body))
}
var parsed struct {
@@ -329,12 +162,19 @@ func (s *driveStorage) fetchToken(ctx context.Context) (string, time.Duration, e
ExpiresIn int64 `json:"expires_in"`
}
if err := json.Unmarshal(body, &parsed); err != nil {
return "", 0, errors.New("failed to parse the token response").Base(err)
return "", errors.New("failed to parse the token response").Base(err)
}
if parsed.AccessToken == "" {
return "", 0, errors.New("the token endpoint returned no access token")
return "", errors.New("the token endpoint returned no access token")
}
return parsed.AccessToken, time.Duration(parsed.ExpiresIn) * time.Second, nil
lifetime := parsed.ExpiresIn
if lifetime > 60 {
lifetime -= 60
}
s.token = parsed.AccessToken
s.tokenExpiry = time.Now().Add(time.Duration(lifetime) * time.Second)
return s.token, nil
}
func jitter(backoff time.Duration) time.Duration {
@@ -391,28 +231,15 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
backoff = driveMaxBackoff
}
}
if err := s.waitCool(ctx); err != nil {
return 0, nil, err
}
if testDriveIO != nil {
testDriveIO(method, target, body)
}
token, err := s.accessToken(ctx)
if err != nil {
if isAuthError(err) {
return 0, nil, err
}
lastErr = err
continue
}
pool := s.inflight
if ctx.Value(laneKey{}) != nil {
pool = s.reserved
}
select {
case pool <- struct{}{}:
case s.inflight <- struct{}{}:
case <-ctx.Done():
return 0, nil, ctx.Err()
}
@@ -421,14 +248,8 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
if body != nil {
reader = bytes.NewReader(body)
}
attemptCtx, cancel := ctx, context.CancelFunc(func() {})
if d, ok := ctx.Value(attemptKey{}).(time.Duration); ok {
attemptCtx, cancel = context.WithTimeout(ctx, d)
}
req, err := http.NewRequestWithContext(attemptCtx, method, target, reader)
req, err := http.NewRequestWithContext(ctx, method, target, reader)
if err != nil {
cancel()
<-pool
return 0, nil, errors.New("failed to build a Drive request").Base(err)
}
req.Header.Set("Authorization", "Bearer "+token)
@@ -436,18 +257,9 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
req.Header.Set("Content-Type", contentType)
}
began := time.Now()
resp, err := s.client.Do(req)
<-pool
<-s.inflight
if err != nil {
timedOut := attemptCtx.Err() == context.DeadlineExceeded && ctx.Err() == nil
cancel()
if timedOut {
s.throttle.Add(1)
if t, ok := s.client.Transport.(*sessionTransport); ok {
t.stalled(began)
}
}
if ctx.Err() != nil {
return 0, nil, ctx.Err()
}
@@ -456,13 +268,8 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
attempt+1, " of ", driveMaxAttempts)
continue
}
limit := int64(driveMaxResponse)
if v, ok := ctx.Value(maxBodyKey{}).(int64); ok && v > 0 {
limit = v
}
payload, err := io.ReadAll(io.LimitReader(resp.Body, limit+1))
payload, err := io.ReadAll(resp.Body)
resp.Body.Close()
cancel()
if err != nil {
if ctx.Err() != nil {
return 0, nil, ctx.Err()
@@ -470,31 +277,22 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
lastErr = errors.New("failed to read the Drive response").Base(err)
continue
}
if int64(len(payload)) > limit {
return 0, nil, errors.New("Drive response exceeds the ", limit, " byte cap")
}
if resp.StatusCode == http.StatusUnauthorized {
s.invalidateToken(token)
s.invalidateToken()
lastErr = errors.New("Drive rejected the access token")
continue
}
if resp.StatusCode == http.StatusForbidden && rateLimited(payload) {
lastErr = errors.New("Drive is rate limiting: ", string(payload))
errors.LogWarning(ctx, "rate limited by Drive, attempt ",
attempt+1, " of ", driveMaxAttempts, ", ", method, " ", len(body),
" bytes, body read ", strings.Contains(target, "alt=media"),
", lane ", ctx.Value(laneKey{}) != nil, ": ", string(payload[:min(len(payload), 300)]))
s.cool()
attempt+1, " of ", driveMaxAttempts)
continue
}
if retryableStatus(resp.StatusCode) {
lastErr = errors.New("Drive answered ", resp.StatusCode, ": ", string(payload))
errors.LogWarning(ctx, "retrying after Drive answered ", resp.StatusCode,
", attempt ", attempt+1, " of ", driveMaxAttempts)
if resp.StatusCode == http.StatusTooManyRequests {
s.cool()
}
continue
}
return resp.StatusCode, payload, nil
@@ -503,36 +301,9 @@ func (s *driveStorage) do(ctx context.Context, method, target, contentType strin
return 0, nil, lastErr
}
func (s *driveStorage) waitCool(ctx context.Context) error {
s.coolMu.Lock()
until := s.coolUntil
s.coolMu.Unlock()
if d := time.Until(until); d > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(d):
}
}
return nil
}
func (s *driveStorage) cool() {
s.throttle.Add(1)
s.coolMu.Lock()
s.coolUntil = time.Now().Add(jitter(driveCooldown))
s.coolMu.Unlock()
}
func (s *driveStorage) throttled() int64 {
return s.throttle.Load()
}
func (s *driveStorage) invalidateToken(used string) {
func (s *driveStorage) invalidateToken() {
s.tokenMu.Lock()
if s.token == used {
s.token = ""
}
s.token = ""
s.tokenMu.Unlock()
}
@@ -771,27 +542,12 @@ func (s *driveStorage) Delete(ctx context.Context, name string) error {
if err != nil {
return err
}
sem := make(chan struct{}, 8)
var wg sync.WaitGroup
var mu sync.Mutex
var first error
for childName, file := range children {
wg.Add(1)
sem <- struct{}{}
go func(name, id string) {
defer wg.Done()
defer func() { <-sem }()
if err := s.deleteID(ctx, name, id); err != nil {
mu.Lock()
if first == nil {
first = err
}
mu.Unlock()
}
}(childName, file.id)
if err := s.deleteID(ctx, childName, file.id); err != nil {
return err
}
}
wg.Wait()
return first
return nil
}
func (s *driveStorage) List(ctx context.Context, prefix string) ([]Entry, error) {
@@ -848,9 +604,6 @@ func (s *driveStorage) makeSlot(ctx context.Context, name string) (string, error
// setSlot implements slotStore, a description and a body go in one request
func (s *driveStorage) setSlot(ctx context.Context, id, description string, body []byte) error {
if body == nil {
ctx = context.WithValue(ctx, attemptKey{}, driveSlotWrite)
}
metadata, err := json.Marshal(map[string]string{"description": description})
if err != nil {
return errors.New("failed to build the slot metadata").Base(err)
@@ -878,7 +631,6 @@ func (s *driveStorage) setSlot(ctx context.Context, id, description string, body
// readDescription implements slotStore
func (s *driveStorage) readDescription(ctx context.Context, id string) (string, error) {
ctx = context.WithValue(ctx, attemptKey{}, driveSlotRead)
status, payload, err := s.do(ctx, http.MethodGet,
s.filesURL+"/"+url.PathEscape(id)+"?fields=description&supportsAllDrives=true", "", nil)
switch {
@@ -1,99 +0,0 @@
package xdrive
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestDecodeRingCaps(t *testing.T) {
good := ring{
Up: slotTable{Slots: []string{"a", "b"}, Watermark: "w"},
Down: slotTable{Slots: []string{"c", "d"}, Watermark: "w"},
}
b, _ := json.Marshal(good)
if _, ok := decodeRing(b); !ok {
t.Fatal("a normal ring was rejected")
}
tooMany := good
tooMany.Up.Slots = make([]string, maxConcurrency+1)
for i := range tooMany.Up.Slots {
tooMany.Up.Slots[i] = "s"
}
b, _ = json.Marshal(tooMany)
if _, ok := decodeRing(b); ok {
t.Fatalf("a ring with %d slots per table was accepted", maxConcurrency+1)
}
longID := good
longID.Up.Slots[0] = strings.Repeat("x", maxSlotIDLen+1)
b, _ = json.Marshal(longID)
if _, ok := decodeRing(b); ok {
t.Fatal("a ring with an over-long slot id was accepted")
}
longWM := good
longWM.Down.Watermark = strings.Repeat("w", maxSlotIDLen+1)
b, _ = json.Marshal(longWM)
if _, ok := decodeRing(b); ok {
t.Fatal("a ring with an over-long watermark id was accepted")
}
emptyID := good
emptyID.Down.Slots[1] = ""
b, _ = json.Marshal(emptyID)
if _, ok := decodeRing(b); ok {
t.Fatal("a ring with an empty slot id was accepted")
}
}
func capTestStorage(t *testing.T, body []byte) (*driveStorage, string) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(body)
}))
t.Cleanup(srv.Close)
return &driveStorage{
driveShared: &driveShared{token: "t", tokenExpiry: time.Now().Add(time.Hour)},
client: srv.Client(),
inflight: make(chan struct{}, 1),
reserved: make(chan struct{}, 1),
}, srv.URL
}
func TestDriveResponseBodyCap(t *testing.T) {
old := driveMaxResponse
driveMaxResponse = 16
t.Cleanup(func() { driveMaxResponse = old })
s, url := capTestStorage(t, make([]byte, 32))
_, _, err := s.do(context.Background(), http.MethodGet, url, "", nil)
if err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("an over-cap body returned %v, want a cap error", err)
}
}
func TestDriveAnnouncementBodyCap(t *testing.T) {
s, url := capTestStorage(t, make([]byte, 32))
ctx := context.WithValue(context.Background(), maxBodyKey{}, int64(8))
_, _, err := s.do(ctx, http.MethodGet, url, "", nil)
if err == nil || !strings.Contains(err.Error(), "exceeds") {
t.Fatalf("an over-cap announcement returned %v, want a cap error", err)
}
}
func TestDriveNormal16MiBSegment(t *testing.T) {
body := make([]byte, 16<<20)
s, url := capTestStorage(t, body)
_, payload, err := s.do(context.Background(), http.MethodGet, url, "", nil)
if err != nil {
t.Fatalf("a normal 16 MiB segment failed: %v", err)
}
if len(payload) != len(body) {
t.Fatalf("read %d bytes, want %d", len(payload), len(body))
}
}
@@ -1,232 +0,0 @@
package xdrive
import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/xtls/xray-core/transport/internet"
)
func livePercentileReport(t *testing.T, label string, samples []time.Duration) {
t.Helper()
if len(samples) == 0 {
t.Logf("%-36s no samples", label)
return
}
sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
q := func(p float64) time.Duration {
return samples[min(int(p*float64(len(samples))), len(samples)-1)]
}
t.Logf("%-36s n=%d p50=%v p95=%v p99=%v max=%v",
label, len(samples), q(0.50), q(0.95), q(0.99), samples[len(samples)-1])
}
func TestLiveDrivePercentiles(t *testing.T) {
config := liveDriveConfig(t)
root, err := newDriveStorage(&internet.MemoryStreamConfig{}, config)
if err != nil {
t.Fatal(err)
}
defer root.Close()
store := root.session().(*driveStorage)
defer store.Close()
ctx := context.Background()
const n = 60
inlineDesc := strings.Repeat("a", 10<<10)
body := make([]byte, 256<<10)
if _, err := rand.Read(body); err != nil {
t.Fatal(err)
}
inlineID, err := store.makeSlot(ctx, "percentiles/inline")
if err != nil {
t.Fatal(err)
}
defer store.deleteID(ctx, flatten("percentiles/inline"), inlineID)
mediaID, err := store.makeSlot(ctx, "percentiles/media")
if err != nil {
t.Fatal(err)
}
defer store.deleteID(ctx, flatten("percentiles/media"), mediaID)
if err := store.setSlot(ctx, mediaID, "media", body); err != nil {
t.Fatal(err)
}
measure := func(label string, parallelism int, op func() error) {
var mu sync.Mutex
samples := make([]time.Duration, 0, n)
var wg sync.WaitGroup
per := n / parallelism
for w := 0; w < parallelism; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < per; i++ {
start := time.Now()
err := op()
elapsed := time.Since(start)
if err != nil {
t.Errorf("%s: %v", label, err)
continue
}
mu.Lock()
samples = append(samples, elapsed)
mu.Unlock()
}
}()
}
wg.Wait()
livePercentileReport(t, fmt.Sprintf("%s (p=%d)", label, parallelism), samples)
}
for _, p := range []int{1, 8} {
measure("inline setSlot 10KiB", p, func() error {
return store.setSlot(ctx, inlineID, inlineDesc, nil)
})
measure("media setSlot 256KiB", p, func() error {
return store.setSlot(ctx, mediaID, "media", body)
})
measure("readDescription", p, func() error {
_, err := store.readDescription(ctx, mediaID)
return err
})
measure("readBody 256KiB", p, func() error {
_, err := store.readBody(ctx, mediaID)
return err
})
}
}
func TestLiveDriveLatencyUnderLoad(t *testing.T) {
config := liveDriveConfig(t)
config.Concurrency = 64
config.FlushIntervalMs = 10
config.MaxPollIntervalMs = 200
settings := &internet.MemoryStreamConfig{
ProtocolName: protocolName,
ProtocolSettings: config,
}
run := func(withBulk bool) []time.Duration {
client, server, cleanup := pairWith(t, settings)
defer cleanup()
if err := server.SetReadDeadline(time.Now().Add(10 * time.Minute)); err != nil {
t.Fatal(err)
}
var writeMu sync.Mutex
writeFrame := func(kind byte, payload []byte) error {
writeMu.Lock()
defer writeMu.Unlock()
var header [5]byte
header[0] = kind
binary.BigEndian.PutUint32(header[1:], uint32(len(payload)))
if _, err := client.Write(header[:]); err != nil {
return err
}
_, err := client.Write(payload)
return err
}
readFrame := func() (byte, []byte, error) {
var header [5]byte
if _, err := io.ReadFull(server, header[:]); err != nil {
return 0, nil, err
}
payload := make([]byte, binary.BigEndian.Uint32(header[1:]))
if _, err := io.ReadFull(server, payload); err != nil {
return 0, nil, err
}
return header[0], payload, nil
}
markers := make(chan time.Duration, 5)
bulkTime := make(chan time.Duration, 1)
go func() {
var bulkRecv int
var bulkStart time.Time
for {
kind, payload, err := readFrame()
if err != nil {
return
}
if kind == 1 {
if bulkRecv == 0 {
bulkStart = time.Now()
}
bulkRecv += len(payload)
if bulkRecv == 8<<20 {
bulkTime <- time.Since(bulkStart)
}
}
if kind == 2 && len(payload) == 16 {
sent := int64(binary.BigEndian.Uint64(payload[:8]))
markers <- time.Since(time.Unix(0, sent))
}
}
}()
bulkDone := make(chan struct{})
if withBulk {
go func() {
defer close(bulkDone)
chunk := make([]byte, 64<<10)
for i := 0; i < 128; i++ {
if err := writeFrame(1, chunk); err != nil {
t.Errorf("bulk frame: %v", err)
return
}
}
}()
time.Sleep(3 * time.Second)
}
for i := 0; i < 5; i++ {
var payload [16]byte
binary.BigEndian.PutUint64(payload[:8], uint64(time.Now().UnixNano()))
if err := writeFrame(2, payload[:]); err != nil {
t.Fatalf("marker frame: %v", err)
}
if i < 4 {
time.Sleep(2 * time.Second)
}
}
if withBulk {
<-bulkDone
}
out := make([]time.Duration, 0, 5)
for i := 0; i < 5; i++ {
select {
case d := <-markers:
out = append(out, d)
case <-time.After(30 * time.Second):
t.Fatal("marker did not arrive")
}
}
if withBulk {
select {
case d := <-bulkTime:
t.Logf("bulk throughput: %.2f MiB/s", 8/d.Seconds())
case <-time.After(60 * time.Second):
t.Log("bulk throughput: bulk did not complete")
}
}
return out
}
t.Log("marker one-way latency with no bulk:")
for i, d := range run(false) {
t.Logf(" marker %d: %v", i+1, d.Round(time.Millisecond))
}
t.Log("marker one-way latency under 8MiB bulk:")
for i, d := range run(true) {
t.Logf(" marker %d: %v", i+1, d.Round(time.Millisecond))
}
}
@@ -7,13 +7,8 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
@@ -348,130 +343,3 @@ func TestLiveDriveListLag(t *testing.T) {
}
t.Logf("worst listing lag: %v", worst.Round(time.Millisecond))
}
func TestLiveDriveLatencyFloor(t *testing.T) {
config := liveDriveConfig(t)
root, err := newDriveStorage(&internet.MemoryStreamConfig{}, config)
if err != nil {
t.Fatal(err)
}
defer root.Close()
store := root.session().(*driveStorage)
defer store.Close()
ctx := context.Background()
const probe = "latency-floor/probe"
id, err := store.makeSlot(ctx, probe)
if err != nil {
t.Fatal(err)
}
defer store.deleteID(ctx, flatten(probe), id)
small := newSlotFrame(1, slotInline, randomPayload(700)).encode()
large := newSlotFrame(1, slotInline, randomPayload(slotInlineLimit)).encode()
file := store.filesURL + "/" + url.PathEscape(id)
type sample struct {
total, ttfb time.Duration
reused bool
}
measure := func(label string, op func(context.Context) error) []sample {
var out []sample
for i := 0; i < 28; i++ {
var wrote, first time.Time
var reused bool
trace := &httptrace.ClientTrace{
GotConn: func(info httptrace.GotConnInfo) { reused = info.Reused },
WroteRequest: func(httptrace.WroteRequestInfo) { wrote = time.Now() },
GotFirstResponseByte: func() { first = time.Now() },
}
start := time.Now()
err := op(httptrace.WithClientTrace(ctx, trace))
total := time.Since(start)
if err != nil {
t.Logf("latency-floor: %s sample failed: %v", label, err)
} else if i >= 3 {
var ttfb time.Duration
if !wrote.IsZero() && !first.IsZero() {
ttfb = first.Sub(wrote)
}
out = append(out, sample{total, ttfb, reused})
}
time.Sleep(250 * time.Millisecond)
}
return out
}
report := func(label string, s []sample) time.Duration {
if len(s) == 0 {
t.Logf("latency-floor: %-30s no samples", label)
return 0
}
totals := make([]time.Duration, len(s))
ttfbs := make([]time.Duration, len(s))
reused := 0
for i, x := range s {
totals[i], ttfbs[i] = x.total, x.ttfb
if x.reused {
reused++
}
}
sort.Slice(totals, func(a, b int) bool { return totals[a] < totals[b] })
sort.Slice(ttfbs, func(a, b int) bool { return ttfbs[a] < ttfbs[b] })
q := func(v []time.Duration, p float64) time.Duration { return v[min(int(p*float64(len(v))), len(v)-1)] }
t.Logf("latency-floor: %-30s n=%d min=%v p50=%v p95=%v ttfb_p50=%v reused=%d/%d",
label, len(s), totals[0], q(totals, 0.5), q(totals, 0.95), q(ttfbs, 0.5), reused, len(s))
return q(ttfbs, 0.5)
}
patch := func(target string, doc map[string]any) func(context.Context) error {
body, _ := json.Marshal(doc)
return func(ctx context.Context) error {
status, reply, err := store.do(ctx, http.MethodPatch, target, "application/json; charset=UTF-8", body)
if err == nil && status != http.StatusOK {
err = fmt.Errorf("status %d: %s", status, reply)
}
return err
}
}
get := func(target string) func(context.Context) error {
return func(ctx context.Context) error {
status, reply, err := store.do(ctx, http.MethodGet, target, "", nil)
if err == nil && status != http.StatusOK {
err = fmt.Errorf("status %d: %s", status, reply)
}
return err
}
}
proto := ""
netFloor := report("net floor (401, no auth)", measure("net", func(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.googleapis.com/drive/v3/about?fields=user", nil)
if err != nil {
return err
}
resp, err := store.client.Do(req)
if err != nil {
return err
}
proto = resp.Proto
io.Copy(io.Discard, resp.Body)
return resp.Body.Close()
}))
t.Logf("latency-floor: negotiated protocol %s", proto)
readDesc := report("GET description (prod)", measure("get", func(ctx context.Context) error {
_, err := store.readDescription(ctx, id)
return err
}))
report("GET fields=id", measure("getid", get(file+"?fields=id&supportsAllDrives=true")))
report("GET description, no allDrives", measure("getnoall", get(file+"?fields=description")))
write1k := report("PATCH inline 1 KB (prod)", measure("p1k", func(ctx context.Context) error {
return store.setSlot(ctx, id, small, nil)
}))
write14k := report("PATCH inline 14 KB (prod)", measure("p14k", func(ctx context.Context) error {
return store.setSlot(ctx, id, large, nil)
}))
report("PATCH 1 KB, no allDrives", measure("pnoall", patch(file+"?fields=id", map[string]any{"description": small})))
report("PATCH name 200 B", measure("pname", patch(file+"?fields=id&supportsAllDrives=true", map[string]any{"name": strings.Repeat("n", 200)})))
report("PATCH appProperties 100 B", measure("pprop", patch(file+"?fields=id&supportsAllDrives=true", map[string]any{"appProperties": map[string]string{"k": strings.Repeat("a", 100)}})))
t.Logf("latency-floor: derived: write backend ~ %v, read backend ~ %v, 14 KB payload cost ~ %v",
write1k-netFloor, readDesc-netFloor, write14k-write1k)
}
@@ -1,203 +0,0 @@
package xdrive
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"io"
"net/http"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/xtls/xray-core/transport/internet"
)
func TestLiveDriveWriteSweep(t *testing.T) {
config := liveDriveConfig(t)
root, err := newDriveStorage(&internet.MemoryStreamConfig{}, config)
if err != nil {
t.Fatal(err)
}
defer root.Close()
store := root.session().(*driveStorage)
defer store.Close()
ctx := context.Background()
inlineID, err := store.makeSlot(ctx, "sweep/inline")
if err != nil {
t.Fatal(err)
}
defer store.deleteID(ctx, flatten("sweep/inline"), inlineID)
mediaID, err := store.makeSlot(ctx, "sweep/media")
if err != nil {
t.Fatal(err)
}
defer store.deleteID(ctx, flatten("sweep/media"), mediaID)
desc := strings.Repeat("a", 10<<10)
body := make([]byte, 256<<10)
if _, err := rand.Read(body); err != nil {
t.Fatal(err)
}
kinds := []struct {
name string
op func(context.Context) error
}{
{"inline 10KiB", func(ctx context.Context) error { return store.setSlot(ctx, inlineID, desc, nil) }},
{"media 256KiB", func(ctx context.Context) error { return store.setSlot(ctx, mediaID, "media", body) }},
}
for _, p := range []int{1, 2, 3, 4, 6, 8, 12} {
for _, kind := range kinds {
levelCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
thr0 := store.throttled()
var mu sync.Mutex
var samples []time.Duration
success := 0
var wg sync.WaitGroup
per := 40 / p
levelStart := time.Now()
for w := 0; w < p; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < per; i++ {
if levelCtx.Err() != nil {
return
}
start := time.Now()
if err := kind.op(levelCtx); err != nil {
continue
}
mu.Lock()
samples = append(samples, time.Since(start))
success++
mu.Unlock()
}
}()
}
wg.Wait()
wall := time.Since(levelStart)
cancel()
if len(samples) == 0 {
t.Logf("sweep p=%-2d %-14s writes=0 wall=%v no samples", p, kind.name, wall.Round(time.Millisecond))
continue
}
sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
q := func(f float64) time.Duration {
return samples[min(int(f*float64(len(samples))), len(samples)-1)]
}
t.Logf("sweep p=%-2d %-14s writes=%d wall=%v writes/s=%.2f p50=%v p95=%v p99=%v max=%v throttled=+%d",
p, kind.name, success, wall.Round(time.Millisecond), float64(success)/wall.Seconds(),
q(0.50), q(0.95), q(0.99), samples[len(samples)-1], store.throttled()-thr0)
}
}
}
func TestLiveDriveBrowsingExchanges(t *testing.T) {
config := liveDriveConfig(t)
config.Concurrency = 64
config.FlushIntervalMs = 10
config.MaxPollIntervalMs = 200
settings := &internet.MemoryStreamConfig{
ProtocolName: protocolName,
ProtocolSettings: config,
}
var slotWrites, ackWrites, announceWrites, reads atomic.Int64
testDriveIO = func(method, target string, body []byte) {
switch method {
case http.MethodGet:
reads.Add(1)
case http.MethodPatch:
var doc struct {
Description string `json:"description"`
}
if json.Unmarshal(body, &doc) != nil {
return
}
if _, ok := decodeSlot(doc.Description); ok {
slotWrites.Add(1)
return
}
if _, err := strconv.ParseInt(doc.Description, 10, 64); err == nil && doc.Description != "" {
ackWrites.Add(1)
return
}
slotWrites.Add(1)
case http.MethodPost, http.MethodPut:
if method == http.MethodPut || strings.Contains(target, "/upload") {
slotWrites.Add(1)
return
}
var doc struct {
Name string `json:"name"`
}
if json.Unmarshal(body, &doc) == nil && strings.HasPrefix(doc.Name, sessionsDir+flatSeparator) {
announceWrites.Add(1)
return
}
slotWrites.Add(1)
}
}
defer func() { testDriveIO = nil }()
client, server, cleanup := pairWith(t, settings)
if err := server.SetReadDeadline(time.Now().Add(10 * time.Minute)); err != nil {
t.Fatal(err)
}
if err := client.SetReadDeadline(time.Now().Add(10 * time.Minute)); err != nil {
t.Fatal(err)
}
answer := randomPayload(30 << 10)
latencies := make([]time.Duration, 0, 40)
for i := 0; i < 40; i++ {
start := time.Now()
request := randomPayload(500)
if _, err := client.Write(request); err != nil {
t.Fatal(err)
}
gotRequest := make([]byte, len(request))
if _, err := io.ReadFull(server, gotRequest); err != nil {
t.Fatal(err)
}
if !bytes.Equal(gotRequest, request) {
t.Fatal("the request came back changed")
}
if _, err := server.Write(answer); err != nil {
t.Fatal(err)
}
got := make([]byte, len(answer))
if _, err := io.ReadFull(client, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, answer) {
t.Fatal("the answer came back changed")
}
latencies = append(latencies, time.Since(start))
if i < 39 {
time.Sleep(400 * time.Millisecond)
}
}
cleanup()
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
q := func(f float64) time.Duration {
return latencies[min(int(f*float64(len(latencies))), len(latencies)-1)]
}
t.Logf("browsing: exchanges=%d p50=%v p95=%v max=%v",
len(latencies), q(0.50), q(0.95), latencies[len(latencies)-1])
total := slotWrites.Load() + ackWrites.Load() + announceWrites.Load()
share := float64(0)
if total > 0 {
share = 100 * float64(ackWrites.Load()) / float64(total)
}
t.Logf("browsing writes: slot-data=%d watermark-ack=%d announcement=%d total=%d ack-share=%.1f%% reads=%d",
slotWrites.Load(), ackWrites.Load(), announceWrites.Load(), total, share, reads.Load())
}
+8 -386
View File
@@ -17,7 +17,6 @@ import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
)
type fakeFile struct {
@@ -30,17 +29,14 @@ type fakeFile struct {
type fakeDrive struct {
server *httptest.Server
mu sync.Mutex
files map[string]*fakeFile
nextID int
failOnce map[string]bool
failStatus map[string]int
failBody map[string]string
tokens int
tokenStatus int
tokenBody string
uploadAt []time.Time
hosts map[string]bool
mu sync.Mutex
files map[string]*fakeFile
nextID int
failOnce map[string]bool
failStatus map[string]int
failBody map[string]string
tokens int
hosts map[string]bool
}
func newFakeDrive(t *testing.T) *fakeDrive {
@@ -93,13 +89,7 @@ func newFakeDrive(t *testing.T) *fakeDrive {
func (d *fakeDrive) handleToken(w http.ResponseWriter, r *http.Request) {
d.mu.Lock()
d.tokens++
status, body := d.tokenStatus, d.tokenBody
d.mu.Unlock()
if status != 0 {
w.WriteHeader(status)
w.Write([]byte(body))
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"access_token": "fake-token",
"expires_in": 3600,
@@ -107,9 +97,6 @@ func (d *fakeDrive) handleToken(w http.ResponseWriter, r *http.Request) {
}
func (d *fakeDrive) handleUpload(w http.ResponseWriter, r *http.Request) {
d.mu.Lock()
d.uploadAt = append(d.uploadAt, time.Now())
d.mu.Unlock()
if d.shouldFail("upload") {
if status, body := d.failure("upload"); status != 0 {
w.WriteHeader(status)
@@ -180,9 +167,6 @@ func (d *fakeDrive) handleFiles(w http.ResponseWriter, r *http.Request) {
}
func (d *fakeDrive) handleCreate(w http.ResponseWriter, r *http.Request) {
d.mu.Lock()
d.uploadAt = append(d.uploadAt, time.Now())
d.mu.Unlock()
if d.shouldFail("upload") {
if status, body := d.failure("upload"); status != 0 {
w.WriteHeader(status)
@@ -757,365 +741,3 @@ func TestDriveFronting(t *testing.T) {
}
}
}
func TestDriveLargeAnnouncement(t *testing.T) {
listenerCtx, cancelListener := context.WithCancel(context.Background())
defer cancelListener()
newFakeDrive(t)
storage := newDriveBackend(t)
ctx := context.Background()
p := paramsFromConfig(driveSettings().ProtocolSettings.(*Config))
session := "large-announcement"
r := ring{
Up: slotTable{Watermark: "up-watermark"},
Down: slotTable{Watermark: "down-watermark"},
}
for i := range 200 {
id := fmt.Sprintf("%s-%03d", strings.Repeat("s", 40), i)
r.Up.Slots = append(r.Up.Slots, id)
r.Down.Slots = append(r.Down.Slots, id)
}
announce, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
if len(announce) <= driveInlineLimit {
t.Fatalf("announcement is %d bytes, want more than %d", len(announce), driveInlineLimit)
}
if err := storage.Put(ctx, announceName(session, time.Now()), announce); err != nil {
t.Fatal(err)
}
entries, err := storage.List(ctx, sessionsDir)
if err != nil {
t.Fatal(err)
}
if len(entries) != 1 || entries[0].Inline != nil {
t.Fatalf("large announcement was inlined: %v", entries)
}
conns := make(chan *Conn, 2)
l := &Listener{
ctx: listenerCtx,
storage: storage,
addConn: func(c stat.Connection) { conns <- c.(*Conn) },
params: p,
active: make(map[string]bool),
handled: make(map[string]time.Time),
idleSince: make(map[string]time.Time),
announceWarned: make(map[string]bool),
}
accepted, err := l.acceptPending(ctx)
if err != nil || !accepted {
t.Fatalf("acceptPending: %v, accepted %v", err, accepted)
}
conn := <-conns
reader, ok := conn.reader.(*slotReader)
if !ok || len(reader.table.Slots) != 200 {
t.Fatalf("large announcement did not become a 200-slot connection: %T", conn.reader)
}
conn.Close()
empty := "empty-announcement"
if err := storage.Put(ctx, announceName(empty, time.Now()), nil); err != nil {
t.Fatal(err)
}
accepted, err = l.acceptPending(ctx)
if err != nil || !accepted {
t.Fatalf("acceptPending empty: %v, accepted %v", err, accepted)
}
conn = <-conns
if _, ok := conn.reader.(*walReader); !ok {
t.Fatalf("empty announcement did not become a segment connection: %T", conn.reader)
}
conn.Close()
}
func TestDriveDeadCredentials(t *testing.T) {
drive := newFakeDrive(t)
drive.mu.Lock()
drive.tokenStatus = http.StatusBadRequest
drive.tokenBody = `{"error":"invalid_grant"}`
drive.mu.Unlock()
storage := newDriveBackend(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
start := time.Now()
_, _, err := storage.do(ctx, http.MethodGet, storage.filesURL, "", nil)
if !isAuthError(err) {
t.Fatalf("do returned %v, want the sticky OAuth error", err)
}
if time.Since(start) > time.Second {
t.Fatalf("do took %v, want it to fail at once", time.Since(start))
}
for range 3 {
if _, err := storage.readDescription(ctx, "id"); !isAuthError(err) {
t.Fatalf("readDescription returned %v, want the sticky OAuth error", err)
}
}
drive.mu.Lock()
tokens := drive.tokens
drive.mu.Unlock()
if tokens != 1 {
t.Fatalf("the fake saw %d token requests, want exactly one", tokens)
}
reader := newSlotReader(ctx, storage, slotTable{Slots: []string{"id"}, Watermark: "w"}, params{concurrency: 1}, nil)
select {
case <-reader.chunks():
case <-time.After(time.Second):
t.Fatal("the slot reader kept polling after the credentials were rejected")
}
if !isAuthError(reader.Err()) {
t.Fatalf("the slot reader ended with %v, want the sticky OAuth error", reader.Err())
}
}
func TestDriveRetryWaitsCooldown(t *testing.T) {
drive := newFakeDrive(t)
oldCooldown := driveCooldown
driveCooldown = 200 * time.Millisecond
t.Cleanup(func() { driveCooldown = oldCooldown })
storage := newDriveBackend(t)
drive.failOnceWith("upload", http.StatusForbidden,
`{"error":{"code":403,"errors":[{"reason":"userRateLimitExceeded"}]}}`)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
start := time.Now()
if err := storage.Put(ctx, "sessions/abc", nil); err != nil {
t.Fatalf("Put did not survive a 403: %v", err)
}
drive.mu.Lock()
times := append([]time.Time(nil), drive.uploadAt...)
drive.mu.Unlock()
if len(times) < 2 {
t.Fatalf("the fake saw %d writes, want a retry", len(times))
}
if gap := times[1].Sub(times[0]); gap < driveCooldown/2 {
t.Fatalf("the retry arrived after %v, less than the jitter floor %v", gap, driveCooldown/2)
}
storage.coolMu.Lock()
until := storage.coolUntil
storage.coolMu.Unlock()
if times[1].Before(until) {
t.Fatalf("the retry reached the fake at %v, before the cooldown ended at %v", times[1], until)
}
if took := time.Since(start); took < until.Sub(times[0]) {
t.Fatalf("the request took %v, less than the cooldown %v", took, until.Sub(times[0]))
}
}
func TestDriveBackgroundTokenRefresh(t *testing.T) {
var mu sync.Mutex
calls := 0
mode := "ok"
delay := 100 * time.Millisecond
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
calls++
n := calls
current := mode
pause := delay
mu.Unlock()
time.Sleep(pause)
switch current {
case "transient":
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"temporarily_unavailable"}`))
case "auth":
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"invalid_grant"}`))
default:
json.NewEncoder(w).Encode(map[string]interface{}{
"access_token": fmt.Sprintf("token-%d", n),
"expires_in": 100,
})
}
}))
defer server.Close()
newStorage := func() *driveStorage {
return &driveStorage{driveShared: &driveShared{
tokenClient: server.Client(),
tokenURL: server.URL,
}}
}
seed := func(s *driveStorage) {
s.tokenMu.Lock()
s.token = "old"
s.tokenLifetime = 100 * time.Second
s.tokenExpiry = time.Now().Add(10 * time.Second)
s.tokenMu.Unlock()
}
setMode := func(m string) {
mu.Lock()
mode = m
mu.Unlock()
}
callCount := func() int {
mu.Lock()
defer mu.Unlock()
return calls
}
t.Run("returns at once inside the window", func(t *testing.T) {
s := newStorage()
seed(s)
start := time.Now()
token, err := s.accessToken(context.Background())
if err != nil || token != "old" {
t.Fatalf("accessToken gave %q, %v", token, err)
}
if time.Since(start) > 50*time.Millisecond {
t.Fatalf("accessToken waited %v for the background refresh", time.Since(start))
}
waitFor(t, "the background refresh", func() bool {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
return s.token != "old" && !s.refreshing
})
})
t.Run("concurrent calls start one refresh", func(t *testing.T) {
mu.Lock()
calls = 0
mu.Unlock()
s := newStorage()
seed(s)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if token, err := s.accessToken(context.Background()); err != nil || token != "old" {
t.Errorf("accessToken gave %q, %v", token, err)
}
}()
}
wg.Wait()
waitFor(t, "one background refresh", func() bool {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
return !s.refreshing && s.token != "old"
})
if n := callCount(); n != 1 {
t.Fatalf("concurrent calls started %d refreshes, want one", n)
}
})
t.Run("transient failure keeps the old token", func(t *testing.T) {
mu.Lock()
calls = 0
mu.Unlock()
setMode("transient")
defer setMode("ok")
s := newStorage()
seed(s)
if token, err := s.accessToken(context.Background()); err != nil || token != "old" {
t.Fatalf("accessToken gave %q, %v", token, err)
}
waitFor(t, "the failed refresh to clear", func() bool {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
return !s.refreshing
})
s.tokenMu.Lock()
token := s.token
s.tokenMu.Unlock()
if token != "old" {
t.Fatalf("a transient failure replaced the token with %q", token)
}
// Simulate the backoff having elapsed so the next call may retry.
s.tokenMu.Lock()
s.tokenRetryAt = time.Time{}
s.tokenBackoff = 0
s.tokenMu.Unlock()
setMode("ok")
if _, err := s.accessToken(context.Background()); err != nil {
t.Fatal(err)
}
waitFor(t, "the later refresh", func() bool {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
return s.token != "old" && !s.refreshing
})
if n := callCount(); n != 2 {
t.Fatalf("the retry made %d token requests, want two", n)
}
})
t.Run("auth failure turns sticky", func(t *testing.T) {
mu.Lock()
calls = 0
mu.Unlock()
setMode("auth")
defer setMode("ok")
s := newStorage()
seed(s)
if _, err := s.accessToken(context.Background()); err != nil {
t.Fatal(err)
}
waitFor(t, "the sticky auth error", func() bool {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
return s.authErr != nil && !s.refreshing
})
before := callCount()
if _, err := s.accessToken(context.Background()); !isAuthError(err) {
t.Fatalf("accessToken gave %v, want the sticky auth error", err)
}
if n := callCount(); n != before {
t.Fatalf("the sticky error made %d more token requests", n-before)
}
})
}
func TestSlotPathDefaults(t *testing.T) {
p := paramsForStorage(&Config{}, &driveStorage{})
if p.concurrency != 64 || p.flushInterval != 10*time.Millisecond || p.maxPollInterval != 200*time.Millisecond {
t.Fatalf("slot defaults gave concurrency %d, flush %v, max poll %v", p.concurrency, p.flushInterval, p.maxPollInterval)
}
if wal, base := paramsForStorage(&Config{}, &localStorage{}), paramsFromConfig(&Config{}); wal != base {
t.Fatalf("wal defaults changed: %+v, want %+v", wal, base)
}
explicit := paramsForStorage(&Config{Concurrency: 4, FlushIntervalMs: 30, MaxPollIntervalMs: 100}, &driveStorage{})
if explicit.concurrency != 4 || explicit.flushInterval != 30*time.Millisecond || explicit.maxPollInterval != 100*time.Millisecond {
t.Fatalf("explicit values did not win: %+v", explicit)
}
if p := paramsForStorage(&Config{PollIntervalMs: 500}, &driveStorage{}); p.maxPollInterval < p.minPollInterval {
t.Fatalf("max poll %v is below min poll %v", p.maxPollInterval, p.minPollInterval)
}
}
func TestDialDefaultRingDepth(t *testing.T) {
newFakeDrive(t)
config := &Config{
RemoteFolder: "folder-id",
Service: "google-drive",
Secrets: []string{"client", "secret", "refresh"},
}
settings := &internet.MemoryStreamConfig{
ProtocolName: protocolName,
ProtocolSettings: config,
}
conn, err := Dial(context.Background(), net.Destination{}, settings)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
writer, ok := conn.(*Conn).writer.(*slotWriter)
if !ok {
t.Fatalf("Dial with unset concurrency built %T, want a slot writer", conn.(*Conn).writer)
}
if len(writer.table.Slots) != 64 {
t.Fatalf("Dial with unset concurrency built %d slots, want 64", len(writer.table.Slots))
}
box := spareBoxFor(config)
box.stop.Store(true)
for box.busy.Load() {
time.Sleep(10 * time.Millisecond)
}
}
@@ -1,174 +0,0 @@
package xdrive
import (
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
type tokenServer struct {
mu sync.Mutex
count int
mode int
delay time.Duration
srv *httptest.Server
}
func newTokenServer(mode int, delay time.Duration) *tokenServer {
ts := &tokenServer{mode: mode, delay: delay}
ts.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts.mu.Lock()
ts.count++
mode, delay := ts.mode, ts.delay
ts.mu.Unlock()
time.Sleep(delay)
if mode != http.StatusOK {
w.WriteHeader(mode)
w.Write([]byte(`{"error":"temporarily_unavailable"}`))
return
}
w.Write([]byte(`{"access_token":"new-token","expires_in":3600}`))
}))
return ts
}
func (ts *tokenServer) requests() int {
ts.mu.Lock()
defer ts.mu.Unlock()
return ts.count
}
func (ts *tokenServer) storage() *driveStorage {
return &driveStorage{driveShared: &driveShared{
tokenClient: ts.srv.Client(),
tokenURL: ts.srv.URL + "/token",
}}
}
func TestTokenSingleFlightDuringOutage(t *testing.T) {
ts := newTokenServer(http.StatusInternalServerError, 200*time.Millisecond)
defer ts.srv.Close()
s := ts.storage()
var wg sync.WaitGroup
errs := make(chan error, 9)
start := make(chan struct{})
wg.Add(1)
go func() { defer wg.Done(); <-start; _, err := s.accessToken(context.Background()); errs <- err }()
close(start)
// Wait until the single flight is in progress, then pile on waiters.
deadline := time.Now().Add(2 * time.Second)
for ts.requests() == 0 && time.Now().Before(deadline) {
time.Sleep(time.Millisecond)
}
for i := 0; i < 7; i++ {
wg.Add(1)
go func() { defer wg.Done(); _, err := s.accessToken(context.Background()); errs <- err }()
}
cancelledCtx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(20 * time.Millisecond)
cancel()
_, err := s.accessToken(cancelledCtx)
errs <- err
}()
wg.Wait()
close(errs)
var cancelledSeen bool
for err := range errs {
if err == nil {
t.Fatal("an outage caller succeeded")
}
if err == context.Canceled {
cancelledSeen = true
}
}
if !cancelledSeen {
t.Fatal("the cancelled waiter did not return ctx.Err")
}
if n := ts.requests(); n != 1 {
t.Fatalf("N concurrent callers caused %d token fetches, want 1", n)
}
}
func TestTokenBackgroundFailureBacksOff(t *testing.T) {
ts := newTokenServer(http.StatusInternalServerError, 0)
defer ts.srv.Close()
s := ts.storage()
s.token = "cached"
s.tokenLifetime = 10 * time.Second
s.tokenExpiry = time.Now().Add(time.Second) // inside the refresh window
if tok, err := s.accessToken(context.Background()); err != nil || tok != "cached" {
t.Fatalf("accessToken=%q err=%v", tok, err)
}
deadline := time.Now().Add(2 * time.Second)
for ts.requests() == 0 && time.Now().Before(deadline) {
time.Sleep(time.Millisecond)
}
time.Sleep(50 * time.Millisecond)
if n := ts.requests(); n != 1 {
t.Fatalf("background refresh made %d requests, want 1", n)
}
// A second call inside the backoff must not hit the endpoint again.
if _, err := s.accessToken(context.Background()); err != nil {
t.Fatalf("cached token should still be returned: %v", err)
}
if n := ts.requests(); n != 1 {
t.Fatalf("a failing endpoint was hit %d times, want 1", n)
}
s.tokenMu.Lock()
backoff := s.tokenBackoff
retryAt := s.tokenRetryAt
s.tokenMu.Unlock()
if backoff < tokenRefreshBackoff {
t.Fatalf("backoff %v below the default %v", backoff, tokenRefreshBackoff)
}
if !retryAt.After(time.Now()) {
t.Fatal("tokenRetryAt not set into the future after a background failure")
}
}
func TestToken401RefetchOnce(t *testing.T) {
ts := newTokenServer(http.StatusOK, 0)
defer ts.srv.Close()
s := ts.storage()
s.token = "old"
s.tokenLifetime = time.Hour
s.tokenExpiry = time.Now().Add(time.Hour)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
s.invalidateToken("old")
if _, err := s.accessToken(context.Background()); err != nil {
t.Errorf("accessToken: %v", err)
}
}()
}
wg.Wait()
if n := ts.requests(); n != 1 {
t.Fatalf("many 401 handlers caused %d refetches, want 1", n)
}
s.tokenMu.Lock()
tok := s.token
s.tokenMu.Unlock()
if tok != "new-token" {
t.Fatalf("token=%q, want new-token", tok)
}
// Invalidating with a stale used token must not clear the fresh one.
s.invalidateToken("old")
s.tokenMu.Lock()
tok = s.token
s.tokenMu.Unlock()
if tok != "new-token" {
t.Fatalf("stale invalidation cleared the fresh token: %q", tok)
}
}
+1 -21
View File
@@ -13,7 +13,7 @@ const (
defaultConcurrency = 8
maxSegmentBytes = 16 * 1024 * 1024
maxConcurrency = 256
maxConcurrency = 64
)
type params struct {
@@ -67,23 +67,3 @@ func paramsFromConfig(c *Config) params {
}
return p
}
// paramsForStorage applies the Drive slot-path defaults only where the storage
// is a slotStore and only to fields the config leaves at 0; wal-based backends
// keep the defaults above because their concurrency and flush mean other things
func paramsForStorage(c *Config, storage Storage) params {
if _, ok := storage.(slotStore); ok {
cc := *c
if cc.Concurrency == 0 {
cc.Concurrency = 64
}
if cc.FlushIntervalMs == 0 {
cc.FlushIntervalMs = 10
}
if cc.MaxPollIntervalMs == 0 {
cc.MaxPollIntervalMs = 200
}
c = &cc
}
return paramsFromConfig(c)
}
+40 -353
View File
@@ -10,7 +10,6 @@ import (
"io"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/xtls/xray-core/common/errors"
@@ -28,6 +27,7 @@ const (
// slotInlineLimit is how large a segment is at most to go in the description
// of its slot, which takes one request less than a body
slotInlineLimit = 14 << 10
slotDepthLimit = 8
// inlineRampSegments segments of one size in a row double it, from
// slotInlineLimit up to segmentBytes
inlineRampSegments = 8
@@ -36,34 +36,24 @@ const (
slotMedia = 2
slotEnd = 3
slotErr = 4
// maxSlotIDLen bounds any slot or watermark id accepted from a peer.
maxSlotIDLen = 128
)
var (
// the head is asked for between slotHuntFloor and slotHuntCeiling, a segment
// behind it at about the pace that segments come
slotHuntFloor = 10 * time.Millisecond
slotHuntCeiling = 25 * time.Millisecond
slotHuntFloor = 50 * time.Millisecond
slotHuntCeiling = 200 * time.Millisecond
slotTailMin = 150 * time.Millisecond
slotTailMax = 800 * time.Millisecond
slotFetchAhead = 12
slotFetchAhead = 6
slotAckRetry = 30 * time.Millisecond
slotWatermarkTTL = 100 * time.Millisecond
slotAckRetry = 100 * time.Millisecond
slotWatermarkTTL = 250 * time.Millisecond
slotStallTimeout = 60 * time.Second
slotCloseGrace = 10 * time.Second
slotGoneTimeout = 60 * time.Second
slotBodyHedge = 2 * time.Second
slotReadAhead int64 = 3
slotReadAheadPoll = 40 * time.Millisecond
slotReadAheadBurst = 500 * time.Millisecond
)
var slotWriteInflight = 12
// slotStore is a storage of objects that are written and read again by id,
// each with a description and a body
type slotStore interface {
@@ -74,12 +64,6 @@ type slotStore interface {
readBody(ctx context.Context, id string) ([]byte, error)
}
// throttleStore is the optional part of a slotStore that reports how often
// Drive throttled or timed out its requests; only driveStorage implements it
type throttleStore interface {
throttled() int64
}
// slotFrame is what the description of a slot says: which segment it holds,
// and either the segment itself or the size and checksum of the body that does
type slotFrame struct {
@@ -144,9 +128,8 @@ type slotTable struct {
// ring is what a client announces a session with, the tables are named from
// its side
type ring struct {
Up slotTable `json:"u"`
Down slotTable `json:"d"`
First []byte `json:"first,omitempty"`
Up slotTable `json:"u"`
Down slotTable `json:"d"`
}
func decodeRing(b []byte) (ring, bool) {
@@ -155,25 +138,12 @@ func decodeRing(b []byte) (ring, bool) {
r.Up.Watermark == "" || r.Down.Watermark == "" {
return ring{}, false
}
if len(r.Up.Slots) > maxConcurrency || len(r.Down.Slots) > maxConcurrency {
return ring{}, false
}
for _, t := range []slotTable{r.Up, r.Down} {
if len(t.Watermark) > maxSlotIDLen {
return ring{}, false
}
for _, id := range t.Slots {
if len(id) == 0 || len(id) > maxSlotIDLen {
return ring{}, false
}
}
}
return r, true
}
// makeRing makes the slots and watermarks of a session, all at once
func makeRing(ctx context.Context, store slotStore, session string, p params) (ring, error) {
depth := max(p.concurrency, 2)
depth := min(max(p.concurrency, 2), slotDepthLimit)
r := ring{
Up: slotTable{Slots: make([]string, depth)},
Down: slotTable{Slots: make([]string, depth)},
@@ -207,7 +177,7 @@ func makeRing(ctx context.Context, store slotStore, session string, p params) (r
}
func readWatermark(ctx context.Context, store slotStore, id string) (int64, error) {
d, err := store.readDescription(context.WithValue(ctx, laneKey{}, true), id)
d, err := store.readDescription(ctx, id)
if err != nil || d == "" {
return -1, err
}
@@ -232,7 +202,6 @@ type slotWriter struct {
held int
closed bool
err error
first chan []byte
// ackMu is held while the watermark is read, which other uploads wait for
ackMu sync.Mutex
@@ -247,7 +216,7 @@ func newSlotWriter(ctx context.Context, store slotStore, table slotTable, p para
store: store,
table: table,
params: p,
slots: make(chan struct{}, min(len(table.Slots), slotWriteInflight)),
slots: make(chan struct{}, len(table.Slots)),
size: min(slotInlineLimit, p.segmentBytes),
acked: -1,
progress: time.Now(),
@@ -302,10 +271,6 @@ func (w *slotWriter) readyToFlush() bool {
grew := len(w.buf) > w.lastSize
w.lastSize = len(w.buf)
if len(w.slots) > 0 && w.held < maxCoalescedTicks && len(w.buf) < w.size/2 {
w.held++
return false
}
if grew && w.held < maxCoalescedTicks {
w.held++
return false
@@ -331,19 +296,11 @@ func (w *slotWriter) flushLocked() error {
return w.err
}
if w.size < w.segmentBytes && len(w.buf) >= w.size && len(w.slots)*4 > cap(w.slots)*3 {
w.size = w.segmentBytes
w.full = 0
}
n := min(len(w.buf), w.size)
if len(w.buf) > 4*slotInlineLimit {
// a burst goes at the full size at once
n = min(len(w.buf), w.segmentBytes)
}
if n > slotInlineLimit && len(w.buf) <= 2*cap(w.slots)*slotInlineLimit {
n = slotInlineLimit
}
if n == w.size && w.size < w.segmentBytes {
if w.full++; w.full == inlineRampSegments {
w.full = 0
@@ -355,12 +312,6 @@ func (w *slotWriter) flushLocked() error {
w.buf = append(w.buf[:0], w.buf[n:]...)
seq := w.seq
w.seq++
if seq == 0 && w.first != nil {
select {
case w.first <- chunk:
default:
}
}
w.lastSize = len(w.buf)
w.held = 0
@@ -381,21 +332,15 @@ func (w *slotWriter) upload(seq int64, chunk []byte) {
if len(chunk) <= slotInlineLimit {
mode = slotInline
}
began := time.Now()
if err := w.await(seq); err != nil {
w.fail(errors.New("failed to store segment ", seq).Base(err))
return
}
awaited := time.Since(began)
putAt := time.Now()
if err := w.write(seq, mode, chunk); err != nil {
w.fail(errors.New("failed to store segment ", seq).Base(err))
// the slot is free, the reader learns there what happened
w.write(seq, slotErr, nil)
return
}
errors.LogDebug(w.ctx, "xdrive write ", slotTag(w.table.Watermark), " seq=", seq, " bytes=", len(chunk),
" mode=", mode, " await=", awaited, " put=", time.Since(putAt))
}
func (w *slotWriter) write(seq int64, mode byte, data []byte) error {
@@ -448,7 +393,7 @@ func (w *slotWriter) await(seq int64) error {
w.ackMu.Lock()
still := time.Since(w.progress)
w.ackMu.Unlock()
if min(time.Since(start), still) > slotStallTimeout || closed && still > slotCloseGrace {
if time.Since(start) > slotStallTimeout || closed && still > slotCloseGrace {
return errors.New("the reader took nothing from slot ", w.slotID(seq), " for ", still).Base(err)
}
@@ -516,7 +461,6 @@ type slotReader struct {
ch chan []byte
wake chan struct{}
acks chan struct{}
grow chan struct{}
mu sync.Mutex
head int64
@@ -524,51 +468,25 @@ type slotReader struct {
// hitAt is when a segment last came and gap the time between them, activeAt
// when a segment came, was seen coming or Wake. Nothing for the eager window
// is idle, and active is closed when that ends
hitAt time.Time
gap time.Duration
activeAt time.Time
active chan struct{}
frontier int64
err error
stop context.CancelFunc
reads atomic.Int64
tailMin time.Duration
tailMax time.Duration
stallTimeout time.Duration
bodyRate float64
hitAt time.Time
gap time.Duration
activeAt time.Time
active chan struct{}
err error
}
func newSlotReader(ctx context.Context, store slotStore, table slotTable, p params, first []byte) *slotReader {
head := int64(0)
if len(first) > 0 {
head = 1
}
runCtx, stop := context.WithCancel(ctx)
func newSlotReader(ctx context.Context, store slotStore, table slotTable, p params) *slotReader {
r := &slotReader{
ctx: runCtx,
stop: stop,
store: store,
table: table,
params: p,
head: head,
started: len(first) > 0,
ch: make(chan []byte, max(p.concurrency, 1)),
wake: make(chan struct{}, 1),
acks: make(chan struct{}, 1),
grow: make(chan struct{}, 1),
hitAt: time.Now(),
activeAt: time.Now(),
active: make(chan struct{}),
tailMin: slotTailMin,
tailMax: slotTailMax,
stallTimeout: slotStallTimeout,
}
if len(first) > 0 {
r.ch <- first
select {
case r.acks <- struct{}{}:
default:
}
ctx: ctx,
store: store,
table: table,
params: p,
ch: make(chan []byte, p.concurrency),
wake: make(chan struct{}, 1),
acks: make(chan struct{}, 1),
hitAt: time.Now(),
activeAt: time.Now(),
active: make(chan struct{}),
}
go r.run()
go r.ackLoop()
@@ -598,20 +516,9 @@ type slotFetch struct {
ready chan struct{}
// promoted is closed when the segment becomes the head
promoted chan struct{}
near chan struct{}
body []byte
end bool
err error
born time.Time
readyAt time.Time
}
func (f *slotFetch) enter() {
select {
case <-f.near:
default:
close(f.near)
}
}
func (r *slotReader) run() {
@@ -622,20 +529,17 @@ func (r *slotReader) run() {
fetches := make(map[int64]*slotFetch)
for {
r.mu.Lock()
head, started, frontier := r.head, r.started, r.frontier
head, started := r.head, r.started
r.mu.Unlock()
window := 1
if started {
window = min(max(int(frontier-head)+slotFetchAhead, slotFetchAhead), len(r.table.Slots))
window = min(slotFetchAhead, len(r.table.Slots))
}
for i := range window {
seq := head + int64(i)
if fetches[seq] == nil {
fetches[seq] = &slotFetch{ready: make(chan struct{}), promoted: make(chan struct{}), near: make(chan struct{}), born: time.Now()}
if int64(i) <= slotReadAhead {
fetches[seq].enter()
}
fetches[seq] = &slotFetch{ready: make(chan struct{}), promoted: make(chan struct{})}
go r.fetch(ctx, seq, i, fetches[seq])
}
}
@@ -643,8 +547,6 @@ func (r *slotReader) run() {
f := fetches[head]
select {
case <-f.ready:
case <-r.grow:
continue
case <-ctx.Done():
return
}
@@ -662,14 +564,8 @@ func (r *slotReader) run() {
r.head++
r.started = true
r.mu.Unlock()
errors.LogDebug(r.ctx, "xdrive read ", slotTag(r.table.Watermark), " seq=", head, " fetch=",
f.readyAt.Sub(f.born), " blocked=", time.Since(f.readyAt), " bytes=", len(f.body),
" reads=", r.reads.Load())
if next := fetches[head+1]; next != nil {
close(next.promoted)
if nf := fetches[head+1+slotReadAhead]; nf != nil {
nf.enter()
}
}
select {
case r.acks <- struct{}{}:
@@ -690,65 +586,26 @@ func (r *slotReader) isHead(seq int64) bool {
return r.head == seq
}
func (r *slotReader) bursting() bool {
r.mu.Lock()
defer r.mu.Unlock()
return time.Since(r.hitAt) <= slotReadAheadBurst
}
func (r *slotReader) reach(seq int64) {
r.mu.Lock()
grown := seq > r.frontier
if grown {
r.frontier = seq
}
r.mu.Unlock()
if grown {
select {
case r.grow <- struct{}{}:
default:
}
}
}
// fetch asks for the slot of seq until it holds seq: the head often, a segment
// behind it soon after it was asked for, then at the pace that segments come
// behind it at the pace that segments come, as it would wait for the head anyway
func (r *slotReader) fetch(ctx context.Context, seq int64, offset int, f *slotFetch) {
defer func() {
f.readyAt = time.Now()
close(f.ready)
}()
if offset > 0 && !r.hold(ctx, r.tailMin, f.promoted, f.near) {
defer close(f.ready)
if offset > 0 && !r.hold(ctx, time.Duration(offset)*r.pace(), f.promoted, nil) {
return
}
id := r.slotID(seq)
backoff := slotHuntFloor
var goneSince, warnedAt, visibleSince time.Time
var goneSince, warnedAt time.Time
for {
var description string
var err error
r.mu.Lock()
hot := time.Since(r.activeAt) <= r.eagerWindow
r.mu.Unlock()
if hot && r.isHead(seq) {
description, err = r.readHead(ctx, id, seq)
} else {
r.reads.Add(1)
description, err = r.store.readDescription(ctx, id)
}
description, err := r.store.readDescription(ctx, id)
frame, ok := decodeSlot(description)
if err == errNotFound || (err == nil && description == "") {
// An absent or empty slot is idle, not a failure.
visibleSince = time.Time{}
}
switch {
case err == nil && ok && frame.seq == seq:
// what follows it is asked for while its body comes
r.mu.Lock()
r.touch()
r.mu.Unlock()
r.reach(seq)
switch frame.mode {
case slotEnd:
f.end = true
@@ -761,37 +618,11 @@ func (r *slotReader) fetch(ctx context.Context, seq int64, offset int, f *slotFe
r.hit()
return
case slotMedia:
body, bodyErr := r.readBodyHedged(ctx, id, frame.size)
if bodyErr == nil && frame.holds(body) {
if body, err := r.store.readBody(ctx, id); err == nil && frame.holds(body) {
f.body = body
r.hit()
return
}
if ctx.Err() == nil && time.Since(warnedAt) > 10*time.Second {
warnedAt = time.Now()
if bodyErr != nil {
errors.LogWarningInner(ctx, bodyErr, "failed to read body of slot ", id)
} else {
errors.LogWarning(ctx, "body of slot ", id, " does not match segment ", seq)
}
}
if visibleSince.IsZero() {
visibleSince = time.Now()
}
if time.Since(visibleSince) > r.stallTimeout {
f.err = errors.New("segment ", seq, " has no usable body after ", r.stallTimeout).Base(bodyErr)
return
}
}
case err == nil && !ok && description != "":
// A present but undecodable description for this slot is not idle;
// fail once it has persisted beyond the stall timeout.
if visibleSince.IsZero() {
visibleSince = time.Now()
}
if time.Since(visibleSince) > r.stallTimeout {
f.err = errors.New("slot ", id, " of segment ", seq, " holds an invalid frame")
return
}
case err == errNotFound:
if goneSince.IsZero() {
@@ -801,10 +632,6 @@ func (r *slotReader) fetch(ctx context.Context, seq int64, offset int, f *slotFe
return
}
case err != nil:
if isAuthError(err) {
f.err = err
return
}
goneSince = time.Time{}
if ctx.Err() == nil && time.Since(warnedAt) > 10*time.Second {
warnedAt = time.Now()
@@ -827,15 +654,7 @@ func (r *slotReader) fetch(ctx context.Context, seq int64, offset int, f *slotFe
// only the head is asked for until something comes
wait, a, b, backoff = 0, f.promoted, active, slotHuntFloor
default:
wait, a, b, backoff = min(max(r.pace(), r.tailMin), r.tailMax), f.promoted, f.near, slotHuntFloor
select {
case <-f.near:
b = nil
if r.bursting() {
wait = slotReadAheadPoll
}
default:
}
wait, a, backoff = min(max(r.pace(), slotTailMin), slotTailMax), f.promoted, slotHuntFloor
}
if !r.hold(ctx, wait, a, b) {
return
@@ -843,126 +662,6 @@ func (r *slotReader) fetch(ctx context.Context, seq int64, offset int, f *slotFe
}
}
func (r *slotReader) readHead(ctx context.Context, id string, seq int64) (string, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = context.WithValue(ctx, laneKey{}, true)
type result struct {
description string
err error
}
results := make(chan result, 4)
outstanding := 0
launch := func() {
outstanding++
go func() {
r.reads.Add(1)
description, err := r.store.readDescription(ctx, id)
results <- result{description, err}
}()
}
launch()
ticker := time.NewTicker(slotHuntCeiling)
defer ticker.Stop()
var last result
seen := false
for {
select {
case res := <-results:
outstanding--
if res.err != nil {
return res.description, res.err
}
if frame, ok := decodeSlot(res.description); ok && frame.seq == seq {
return res.description, nil
}
last, seen = res, true
case <-ticker.C:
r.mu.Lock()
idle := time.Since(r.activeAt) > r.eagerWindow
r.mu.Unlock()
switch {
case idle && seen:
return last.description, nil
case !idle && outstanding < 3:
launch()
}
case <-r.wake:
if outstanding < 3 {
launch()
}
case <-ctx.Done():
return "", ctx.Err()
}
}
}
func (r *slotReader) readBodyHedged(ctx context.Context, id string, size int) ([]byte, error) {
if slotBodyHedge <= 0 {
return r.store.readBody(ctx, id)
}
delay := slotBodyHedge
r.mu.Lock()
rate := r.bodyRate
r.mu.Unlock()
if rate > 0 && size > 0 {
if scaled := time.Duration(float64(2*size) / rate * float64(time.Second)); scaled > delay {
delay = scaled
}
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
start := time.Now()
type result struct {
body []byte
err error
}
results := make(chan result, 2)
launch := func() {
go func() {
body, err := r.store.readBody(ctx, id)
results <- result{body, err}
}()
}
launch()
outstanding := 1
timer := time.NewTimer(delay)
defer timer.Stop()
var last result
for {
select {
case res := <-results:
outstanding--
if res.err == nil {
if elapsed := time.Since(start); elapsed > 0 && len(res.body) > 0 {
instant := float64(len(res.body)) / elapsed.Seconds()
r.mu.Lock()
if r.bodyRate == 0 {
r.bodyRate = instant
} else {
r.bodyRate = (r.bodyRate + instant) / 2
}
r.mu.Unlock()
}
return res.body, nil
}
last = res
if outstanding == 0 {
return nil, last.err
}
case <-timer.C:
if outstanding == 1 {
launch()
outstanding++
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
// pace is about how long segments take to come one after the other
func (r *slotReader) pace() time.Duration {
r.mu.Lock()
@@ -1027,7 +726,7 @@ func (r *slotReader) ackLoop() {
if want <= acked {
break
}
if err := r.store.setSlot(context.WithValue(r.ctx, laneKey{}, true), r.table.Watermark, strconv.FormatInt(want, 10), nil); err == nil {
if err := r.store.setSlot(r.ctx, r.table.Watermark, strconv.FormatInt(want, 10), nil); err == nil {
acked, wait = want, slotAckRetry
continue
}
@@ -1043,15 +742,10 @@ func (r *slotReader) ackLoop() {
func (r *slotReader) setErr(err error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.err == nil {
r.err = err
}
r.mu.Unlock()
// A stored error means the connection is dead: stop the run loop so the
// chunk channel closes and Read returns the error instead of blocking.
if r.stop != nil {
r.stop()
}
}
func (r *slotReader) Err() error {
@@ -1062,10 +756,3 @@ func (r *slotReader) Err() error {
}
return io.EOF
}
func slotTag(id string) string {
if len(id) > 6 {
return id[len(id)-6:]
}
return id
}
@@ -1,77 +0,0 @@
package xdrive
import (
"bytes"
"context"
"strings"
"testing"
"time"
)
// badBodySlots serves a media frame whose body never matches its checksum.
type badBodySlots struct{}
func (badBodySlots) makeSlot(ctx context.Context, name string) (string, error) { return name, nil }
func (badBodySlots) setSlot(ctx context.Context, id, description string, body []byte) error {
return nil
}
func (badBodySlots) readDescription(ctx context.Context, id string) (string, error) {
return newSlotFrame(0, slotMedia, make([]byte, 64)).encode(), nil
}
func (badBodySlots) readBody(ctx context.Context, id string) ([]byte, error) {
return bytes.Repeat([]byte{1}, 64), nil
}
// emptySlots serves an empty description: an idle slot, not a failure.
type emptySlots struct{}
func (emptySlots) makeSlot(ctx context.Context, name string) (string, error) { return name, nil }
func (emptySlots) setSlot(ctx context.Context, id, description string, body []byte) error {
return nil
}
func (emptySlots) readDescription(ctx context.Context, id string) (string, error) { return "", nil }
func (emptySlots) readBody(ctx context.Context, id string) ([]byte, error) { return nil, nil }
func shrinkStallClocks(t *testing.T) {
t.Helper()
// Only the stall timeout is shrunk; readers snapshot it at construction,
// so lingering goroutines from other tests never read a mutated global.
oldStall := slotStallTimeout
slotStallTimeout = 100 * time.Millisecond
t.Cleanup(func() { slotStallTimeout = oldStall })
}
func TestHeadBodyChecksumMismatchFails(t *testing.T) {
shrinkStallClocks(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := newSlotReader(ctx, badBodySlots{}, slotTable{Slots: []string{"id"}, Watermark: "w"},
params{concurrency: 1, eagerWindow: 50 * time.Millisecond, maxPollInterval: 5 * time.Millisecond}, nil)
select {
case _, ok := <-r.chunks():
if ok {
t.Fatal("a mismatched body was delivered")
}
case <-time.After(3 * time.Second):
t.Fatal("a persistent checksum mismatch retried forever")
}
if err := r.Err(); err == nil || !strings.Contains(err.Error(), "no usable body") {
t.Fatalf("reader ended with %v, want a clear body error", err)
}
}
func TestEmptySlotStaysIdle(t *testing.T) {
shrinkStallClocks(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := newSlotReader(ctx, emptySlots{}, slotTable{Slots: []string{"id"}, Watermark: "w"},
params{concurrency: 1, eagerWindow: 50 * time.Millisecond, maxPollInterval: 5 * time.Millisecond}, nil)
select {
case <-r.chunks():
t.Fatal("an empty slot failed the reader instead of staying idle")
case <-time.After(300 * time.Millisecond):
}
cancel()
}
+2 -619
View File
@@ -4,14 +4,12 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"io"
"math/rand"
"os"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
@@ -88,8 +86,8 @@ func slotPair(t *testing.T, store slotStore, concurrency, segmentBytes uint32) (
if err != nil {
t.Fatal(err)
}
client := newSlotConn(context.Background(), store, r.Up, r.Down, p, nil, nil)
server := newSlotConn(context.Background(), store, r.Down, r.Up, p, nil, nil)
client := newSlotConn(context.Background(), store, r.Up, r.Down, p, nil)
server := newSlotConn(context.Background(), store, r.Down, r.Up, p, nil)
t.Cleanup(func() {
client.Close()
server.Close()
@@ -306,621 +304,6 @@ func TestSlotStaleBody(t *testing.T) {
client, server := slotPair(t, &staleSlots{memSlots: newMemSlots(), served: make(map[string]bool)}, 2, 64<<10)
payload := randomPayload(200 << 10)
errs := make(chan error, 1)
go func() {
_, err := client.Write(payload)
if err == nil {
err = client.Close()
}
errs <- err
}()
readAll(t, server, payload)
}
func TestSlotBurstSplitsInline(t *testing.T) {
store := newMemSlots()
client, server := slotPair(t, store, 8, 512<<10)
payload := randomPayload(90 << 10)
errs := make(chan error, 1)
writeAll(client, payload, true, errs)
readAll(t, server, payload)
store.mu.Lock()
bodies := len(store.bodies)
store.mu.Unlock()
if bodies != 0 {
t.Fatalf("a burst of 90 KiB went in %d bodies, want none", bodies)
}
}
type stuckBodySlots struct {
*memSlots
calls atomic.Int32
}
func (s *stuckBodySlots) readBody(ctx context.Context, id string) ([]byte, error) {
if s.calls.Add(1) == 1 {
<-ctx.Done()
return nil, ctx.Err()
}
return s.memSlots.readBody(ctx, id)
}
func TestSlotBodyHedge(t *testing.T) {
old := slotBodyHedge
slotBodyHedge = 50 * time.Millisecond
defer func() { slotBodyHedge = old }()
store := &stuckBodySlots{memSlots: newMemSlots()}
client, server := slotPair(t, store, 2, 64<<10)
payload := randomPayload(200 << 10)
errs := make(chan error, 1)
go func() {
_, err := client.Write(payload)
if err == nil {
err = client.Close()
}
errs <- err
}()
readAll(t, server, payload)
if store.calls.Load() < 2 {
t.Fatalf("the stuck body read was not hedged, %d reads", store.calls.Load())
}
}
type timedBodySlots struct {
*memSlots
calls atomic.Int32
delay time.Duration
stuck atomic.Bool
}
func (s *timedBodySlots) readBody(ctx context.Context, id string) ([]byte, error) {
n := s.calls.Add(1)
if s.stuck.Load() && n == 1 {
<-ctx.Done()
return nil, ctx.Err()
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(s.delay):
}
return make([]byte, 1000), nil
}
func TestSlotBodyHedgeRate(t *testing.T) {
old := slotBodyHedge
slotBodyHedge = 20 * time.Millisecond
t.Cleanup(func() { slotBodyHedge = old })
store := &timedBodySlots{memSlots: newMemSlots(), delay: 60 * time.Millisecond}
r := &slotReader{store: store}
ctx := context.Background()
if _, err := r.readBodyHedged(ctx, "id", 1000); err != nil {
t.Fatal(err)
}
store.calls.Store(0)
start := time.Now()
if _, err := r.readBodyHedged(ctx, "id", 1000); err != nil {
t.Fatal(err)
}
if elapsed := time.Since(start); elapsed < 60*time.Millisecond {
t.Fatalf("the progressing read returned in %v, before its body was ready", elapsed)
}
if n := store.calls.Load(); n != 1 {
t.Fatalf("a progressing read after a learned slow rate hedged, %d reads", n)
}
r.mu.Lock()
rate := r.bodyRate
r.mu.Unlock()
if rate <= 0 {
t.Fatal("no body read rate was learned")
}
expected := max(slotBodyHedge, time.Duration(float64(2*1000)/rate*float64(time.Second)))
store.stuck.Store(true)
store.calls.Store(0)
start = time.Now()
if _, err := r.readBodyHedged(ctx, "id", 1000); err != nil {
t.Fatal(err)
}
elapsed := time.Since(start)
if n := store.calls.Load(); n < 2 {
t.Fatalf("a stuck read after a learned rate was not hedged, %d reads", n)
}
if elapsed < expected/2 {
t.Fatalf("the stuck read hedged after %v, want about %v", elapsed, expected)
}
freshStore := &timedBodySlots{memSlots: newMemSlots()}
freshStore.stuck.Store(true)
fresh := &slotReader{store: freshStore}
store.calls.Store(0)
start = time.Now()
if _, err := fresh.readBodyHedged(ctx, "id", 1000); err != nil {
t.Fatal(err)
}
if elapsed := time.Since(start); elapsed < slotBodyHedge {
t.Fatalf("with no learned rate the hedge came after %v, want at least %v", elapsed, slotBodyHedge)
}
}
func TestTakeSpareSkipsStale(t *testing.T) {
cfg := &Config{}
p := params{sessionTTL: 5 * time.Minute}
st1, err := newLocalStorage(t.TempDir())
if err != nil {
t.Fatal(err)
}
st2, err := newLocalStorage(t.TempDir())
if err != nil {
t.Fatal(err)
}
box := spareBoxFor(cfg)
box.ch <- spareRing{storage: st1, session: "old", at: time.Now().Add(-time.Hour)}
box.ch <- spareRing{storage: st2, session: "new", at: time.Now()}
got, ok := takeSpare(cfg, p)
if !ok || got.session != "new" {
t.Fatalf("takeSpare gave %q, %v, want the fresh one", got.session, ok)
}
if _, ok := takeSpare(cfg, p); ok {
t.Fatal("takeSpare returned a second spare")
}
}
func TestDropSpareDeletesSession(t *testing.T) {
st, err := newLocalStorage(t.TempDir())
if err != nil {
t.Fatal(err)
}
name := sessionPrefix("gone") + "/" + uplinkDir + "/s0"
if err := st.Put(context.Background(), name, []byte("x")); err != nil {
t.Fatal(err)
}
dropSpare(spareRing{storage: st, session: "gone"})
if got, _ := st.List(context.Background(), sessionPrefix("gone")); len(got) != 0 {
t.Fatalf("%d entries left of a dropped spare", len(got))
}
}
type stuckHeadSlots struct {
*memSlots
calls atomic.Int32
}
func (s *stuckHeadSlots) readDescription(ctx context.Context, id string) (string, error) {
if s.calls.Add(1) == 1 {
<-ctx.Done()
return "", ctx.Err()
}
return s.memSlots.readDescription(ctx, id)
}
func TestSlotHeadReadOverlaps(t *testing.T) {
client, server := slotPair(t, &stuckHeadSlots{memSlots: newMemSlots()}, 4, 64<<10)
for _, c := range [][2]*Conn{{client, server}, {server, client}} {
c[0].Write([]byte("ping"))
if _, err := io.ReadFull(c[1], make([]byte, 4)); err != nil {
t.Fatal(err)
}
}
}
func TestSlotBurstReadsTogether(t *testing.T) {
client, server := slotPair(t, newMemSlots(), 16, 512<<10)
payload := randomPayload(8 * slotInlineLimit)
go client.Write(payload)
start := time.Now()
got := make([]byte, len(payload))
if _, err := io.ReadFull(server, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, payload) {
t.Fatal("the burst came back changed")
}
if took := time.Since(start); took > time.Second {
t.Fatalf("a burst of 8 segments took %v to read", took)
}
}
type lagSlots struct{ *memSlots }
func (s lagSlots) readDescription(ctx context.Context, id string) (string, error) {
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(200 * time.Millisecond):
}
return s.memSlots.readDescription(ctx, id)
}
func (s lagSlots) setSlot(ctx context.Context, id, description string, body []byte) error {
if _, ok := decodeSlot(description); ok {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(600 * time.Millisecond):
}
}
return s.memSlots.setSlot(ctx, id, description, body)
}
func TestSlotReadAheadBurst(t *testing.T) {
oldMin, oldMax := slotTailMin, slotTailMax
slotTailMin, slotTailMax = 150*time.Millisecond, 800*time.Millisecond
t.Cleanup(func() { slotTailMin, slotTailMax = oldMin, oldMax })
client, server := slotPair(t, lagSlots{newMemSlots()}, 16, 512<<10)
payload := randomPayload(8 * slotInlineLimit)
go client.Write(payload)
start := time.Now()
got := make([]byte, len(payload))
if _, err := io.ReadFull(server, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, payload) {
t.Fatal("the burst came back changed")
}
if took := time.Since(start); took > 1700*time.Millisecond {
t.Fatalf("a burst of 8 segments took %v to read", took)
}
client.Close()
server.Close()
}
func TestSlotLargeBurstInline(t *testing.T) {
store := newMemSlots()
client, server := slotPair(t, store, 16, 512<<10)
payload := randomPayload(10 * slotInlineLimit)
go client.Write(payload)
got := make([]byte, len(payload))
if _, err := io.ReadFull(server, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, payload) {
t.Fatal("the burst came back changed")
}
store.mu.Lock()
bodies := len(store.bodies)
store.mu.Unlock()
if bodies != 0 {
t.Fatalf("a burst of 140 KiB went in %d bodies, want none", bodies)
}
}
func TestSlotDepthFollowsConcurrency(t *testing.T) {
for _, c := range []struct{ set, want int }{{100, 100}, {maxConcurrency, maxConcurrency}, {1 << 20, maxConcurrency}} {
p := paramsFromConfig(&Config{Concurrency: uint32(c.set)})
r, err := makeRing(context.Background(), newMemSlots(), "session", p)
if err != nil {
t.Fatal(err)
}
if len(r.Up.Slots) != c.want || len(r.Down.Slots) != c.want {
t.Fatalf("concurrency %d made %d and %d slots, want %d", c.set, len(r.Up.Slots), len(r.Down.Slots), c.want)
}
}
}
func TestSlotDeepRingRoundTrip(t *testing.T) {
client, server := slotPair(t, newMemSlots(), 64, 64<<10)
payload := randomPayload(2 << 20)
errs := make(chan error, 1)
writeAll(client, payload, true, errs)
readAll(t, server, payload)
if err := <-errs; err != nil {
t.Fatal(err)
}
}
type pollSlots struct {
*memSlots
pmu sync.Mutex
polled map[string]bool
}
func (s *pollSlots) readDescription(ctx context.Context, id string) (string, error) {
s.pmu.Lock()
s.polled[id] = true
s.pmu.Unlock()
return s.memSlots.readDescription(ctx, id)
}
func TestSlotReaderReachesPastWindow(t *testing.T) {
store := &pollSlots{memSlots: newMemSlots(), polled: make(map[string]bool)}
p := paramsFromConfig(&Config{FlushIntervalMs: 1, PollIntervalMs: 1, MaxPollIntervalMs: 20, EagerWindowMs: 50, Concurrency: 64})
ring, err := makeRing(context.Background(), store, "session", p)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
put := func(seq int64) {
slot := ring.Down.Slots[seq%int64(len(ring.Down.Slots))]
if err := store.setSlot(ctx, slot, newSlotFrame(seq, slotInline, []byte{byte(seq)}).encode(), nil); err != nil {
t.Fatal(err)
}
}
reader := newSlotReader(ctx, store, ring.Down, p, nil)
next := func() []byte {
select {
case chunk := <-reader.chunks():
return chunk
case <-time.After(10 * time.Second):
t.Fatal("no segment came")
return nil
}
}
put(0)
if got := next(); !bytes.Equal(got, []byte{0}) {
t.Fatalf("segment 0 came as %v", got)
}
for seq := int64(2); seq <= 40; seq++ {
put(seq)
}
last := ring.Down.Slots[40]
deadline := time.Now().Add(10 * time.Second)
for {
reader.Wake()
store.pmu.Lock()
polled := store.polled[last]
store.pmu.Unlock()
if polled {
break
}
if time.Now().After(deadline) {
t.Fatal("segment 40 was never asked for while segment 1 was missing")
}
time.Sleep(10 * time.Millisecond)
}
put(1)
for seq := int64(1); seq <= 40; seq++ {
if got := next(); !bytes.Equal(got, []byte{byte(seq)}) {
t.Fatalf("segment %d came as %v", seq, got)
}
}
}
type countSlots struct {
*memSlots
cur atomic.Int32
peak atomic.Int32
}
func (s *countSlots) setSlot(ctx context.Context, id, description string, body []byte) error {
if _, ok := decodeSlot(description); !ok {
return s.memSlots.setSlot(ctx, id, description, body)
}
n := s.cur.Add(1)
for {
p := s.peak.Load()
if n <= p || s.peak.CompareAndSwap(p, n) {
break
}
}
time.Sleep(20 * time.Millisecond)
s.cur.Add(-1)
return s.memSlots.setSlot(ctx, id, description, body)
}
func TestSlotWriteInflightCap(t *testing.T) {
store := &countSlots{memSlots: newMemSlots()}
client, server := slotPair(t, store, 48, 512<<10)
payload := randomPayload(60 * slotInlineLimit)
errs := make(chan error, 1)
go func() {
for off := 0; off < len(payload); off += slotInlineLimit {
if _, err := client.Write(payload[off : off+slotInlineLimit]); err != nil {
errs <- err
return
}
}
errs <- nil
}()
got := make([]byte, len(payload))
if _, err := io.ReadFull(server, got); err != nil {
t.Fatal(err)
}
if err := <-errs; err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, payload) {
t.Fatal("the payload came back changed")
}
if p := int(store.peak.Load()); p > slotWriteInflight {
t.Fatalf("%d segments were written at once, want at most %d", p, slotWriteInflight)
}
}
func TestSlotBurstInlineWaves(t *testing.T) {
for _, c := range []struct {
pieces int
media bool
}{{20, false}, {30, true}} {
store := newMemSlots()
client, server := slotPair(t, store, 16, 512<<10)
payload := randomPayload(c.pieces * slotInlineLimit)
go client.Write(payload)
got := make([]byte, len(payload))
if _, err := io.ReadFull(server, got); err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, payload) {
t.Fatal("the burst came back changed")
}
store.mu.Lock()
bodies := len(store.bodies)
store.mu.Unlock()
if (bodies > 0) != c.media {
t.Fatalf("a burst of %d pieces went in %d bodies, media wanted %v", c.pieces, bodies, c.media)
}
}
}
type holdSlots struct {
*memSlots
target string
hold chan struct{}
}
func (s *holdSlots) setSlot(ctx context.Context, id, description string, body []byte) error {
if id == s.target {
select {
case <-s.hold:
case <-ctx.Done():
return ctx.Err()
}
}
return s.memSlots.setSlot(ctx, id, description, body)
}
func TestSlotFirstAnnouncement(t *testing.T) {
p := params{
concurrency: 2,
segmentBytes: 4096,
flushInterval: time.Millisecond,
minPollInterval: time.Millisecond,
maxPollInterval: 5 * time.Millisecond,
sessionTTL: time.Minute,
}
store := &holdSlots{memSlots: newMemSlots(), hold: make(chan struct{})}
ctx := context.Background()
r, err := makeRing(ctx, store, "first", p)
if err != nil {
t.Fatal(err)
}
store.target = r.Up.Slots[0]
client := newSlotConn(ctx, store, r.Up, r.Down, p, nil, nil)
defer client.Close()
writer := client.writer.(*slotWriter)
writer.first = make(chan []byte, 1)
payload := randomPayload(10000)
errs := make(chan error, 1)
go func() {
_, err := client.Write(payload)
if err == nil {
err = client.Close()
}
errs <- err
}()
var first []byte
select {
case first = <-writer.first:
case <-time.After(5 * time.Second):
t.Fatal("the writer never flushed seq 0")
}
announced := r
announced.First = first
encoded, err := json.Marshal(announced)
if err != nil {
t.Fatal(err)
}
if len(encoded) > driveInlineLimit {
t.Fatalf("the test announcement is %d bytes, want the inline path", len(encoded))
}
decoded, ok := decodeRing(encoded)
if !ok {
t.Fatal("the announcement with First did not decode")
}
server := newSlotConn(ctx, store, decoded.Down, decoded.Up, p, decoded.First, nil)
defer server.Close()
if err := server.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
t.Fatal(err)
}
gotFirst := make([]byte, len(first))
if _, err := io.ReadFull(server, gotFirst); err != nil {
t.Fatalf("the first bytes did not arrive before the ring write: %v", err)
}
if !bytes.Equal(gotFirst, first) {
t.Fatal("the primed first bytes changed")
}
close(store.hold)
rest := make([]byte, len(payload)-len(first))
if _, err := io.ReadFull(server, rest); err != nil {
t.Fatal(err)
}
if !bytes.Equal(append(gotFirst, rest...), payload) {
t.Fatal("the stream was duplicated or changed")
}
if err := <-errs; err != nil {
t.Fatal(err)
}
}
func TestSlotFirstAnnouncementTooLarge(t *testing.T) {
p := params{
concurrency: 2,
segmentBytes: 256 << 10,
flushInterval: time.Millisecond,
sessionTTL: time.Minute,
}
store := newMemSlots()
ctx := context.Background()
r, err := makeRing(ctx, store, "large-first", p)
if err != nil {
t.Fatal(err)
}
client := newSlotConn(ctx, store, r.Up, r.Down, p, nil, nil)
defer client.Close()
writer := client.writer.(*slotWriter)
writer.first = make(chan []byte, 1)
go client.Write(randomPayload(300 << 10))
var first []byte
select {
case first = <-writer.first:
case <-time.After(5 * time.Second):
t.Fatal("the writer never flushed seq 0")
}
if len(first) <= slotInlineLimit {
t.Fatalf("the first segment is %d bytes, want a media segment", len(first))
}
withFirst := r
withFirst.First = first
encoded, err := json.Marshal(withFirst)
if err != nil {
t.Fatal(err)
}
if len(encoded) <= driveInlineLimit {
t.Fatalf("a %d byte first segment fit the inline announcement", len(first))
}
plain, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
decoded, ok := decodeRing(plain)
if !ok || decoded.First != nil {
t.Fatal("the announcement without First did not decode cleanly")
}
}
func TestSlotRingFirstCompatibility(t *testing.T) {
p := params{concurrency: 2, sessionTTL: time.Minute}
store := newMemSlots()
r, err := makeRing(context.Background(), store, "compat", p)
if err != nil {
t.Fatal(err)
}
r.First = []byte("hello")
encoded, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
var oldServer struct {
Up slotTable `json:"u"`
Down slotTable `json:"d"`
}
if err := json.Unmarshal(encoded, &oldServer); err != nil {
t.Fatalf("an old server could not ignore First: %v", err)
}
if len(oldServer.Up.Slots) != len(r.Up.Slots) || len(oldServer.Down.Slots) != len(r.Down.Slots) {
t.Fatal("an old server lost the ring tables")
}
r.First = nil
plain, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
decoded, ok := decodeRing(plain)
if !ok || decoded.First != nil {
t.Fatal("an old client announcement did not decode")
}
}
-8
View File
@@ -6,7 +6,6 @@ import (
"encoding/hex"
"strings"
"sync"
"sync/atomic"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/transport/internet"
@@ -27,18 +26,11 @@ type Storage interface {
Close() error
}
// testStorageFactory lets tests substitute a backend for newStorage; nil in
// production so behaviour is unchanged.
var testStorageFactory atomic.Pointer[func(config *Config) (Storage, error)]
func newStorage(streamSettings *internet.MemoryStreamConfig) (Storage, error) {
config, err := streamConfig(streamSettings)
if err != nil {
return nil, err
}
if f := testStorageFactory.Load(); f != nil {
return (*f)(config)
}
switch config.Service {
case "local":
+45 -245
View File
@@ -9,7 +9,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/xtls/xray-core/common"
@@ -27,11 +26,6 @@ const (
downlinkDir = "s2c"
)
const (
spareCount = 2
spareIdleStop = 10 * time.Minute
)
func init() {
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
return new(Config)
@@ -84,192 +78,48 @@ func streamConfig(streamSettings *internet.MemoryStreamConfig) (*Config, error)
return config, nil
}
type spareRing struct {
storage Storage
session string
ring ring
at time.Time
}
type spareBox struct {
ch chan spareRing
busy atomic.Bool
last atomic.Int64
stop atomic.Bool
}
var spareBoxes sync.Map
func spareBoxFor(config *Config) *spareBox {
if b, ok := spareBoxes.Load(config); ok {
return b.(*spareBox)
}
b, _ := spareBoxes.LoadOrStore(config, &spareBox{ch: make(chan spareRing, spareCount)})
return b.(*spareBox)
}
func dropSpare(s spareRing) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.storage.Delete(ctx, sessionPrefix(s.session))
s.storage.Close()
}
func takeSpare(config *Config, p params) (spareRing, bool) {
box := spareBoxFor(config)
for {
select {
case s := <-box.ch:
if time.Since(s.at) < p.sessionTTL*2/3 {
return s, true
}
go dropSpare(s)
default:
return spareRing{}, false
}
}
}
func warmSpares(streamSettings *internet.MemoryStreamConfig, config *Config, p params) {
spareBoxFor(config).last.Store(time.Now().UnixNano())
fillSpares(streamSettings, config, p)
}
func fillSpares(streamSettings *internet.MemoryStreamConfig, config *Config, p params) {
box := spareBoxFor(config)
if !box.busy.CompareAndSwap(false, true) {
return
}
go func() {
defer box.busy.Store(false)
for n := len(box.ch); n > 0; n-- {
select {
case s := <-box.ch:
if time.Since(s.at) < p.sessionTTL/3 {
box.ch <- s
} else {
dropSpare(s)
}
default:
}
}
for len(box.ch) < cap(box.ch) {
for range 40 {
if box.stop.Load() {
return
}
time.Sleep(50 * time.Millisecond)
}
if box.stop.Load() {
return
}
storage, err := newStorage(streamSettings)
if err != nil {
return
}
storage = sessionStorage(storage)
slots, ok := storage.(slotStore)
if !ok {
storage.Close()
return
}
session, err := newSessionID()
if err != nil {
storage.Close()
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
r, err := makeRing(ctx, slots, session, p)
cancel()
if err != nil {
dropSpare(spareRing{storage: storage, session: session})
return
}
select {
case box.ch <- spareRing{storage: storage, session: session, ring: r, at: time.Now()}:
default:
storage.Close()
return
}
}
time.AfterFunc(p.sessionTTL/4, func() {
if !box.stop.Load() && time.Since(time.Unix(0, box.last.Load())) < spareIdleStop {
fillSpares(streamSettings, config, p)
}
})
}()
}
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
config, err := streamConfig(streamSettings)
if err != nil {
return nil, err
}
p := paramsFromConfig(config)
storage, err := newStorage(streamSettings)
if err != nil {
return nil, err
}
storage = sessionStorage(storage)
spare, warm := takeSpare(config, p)
storage, session, r := spare.storage, spare.session, spare.ring
if !warm {
storage, err = newStorage(streamSettings)
if err != nil {
return nil, err
}
storage = sessionStorage(storage)
session, err = newSessionID()
if err != nil {
storage.Close()
return nil, err
}
session, err := newSessionID()
if err != nil {
storage.Close()
return nil, err
}
p = paramsForStorage(config, storage)
p := paramsFromConfig(config)
var announce []byte
var r ring
slots, ok := storage.(slotStore)
if ok && !warm {
if r, err = makeRing(ctx, slots, session, p); err != nil {
if ok {
if r, err = makeRing(ctx, slots, session, p); err == nil {
announce, _ = json.Marshal(r)
} else {
ok = false
errors.LogWarningInner(ctx, err, "failed to make the slots of session ", session, ", it goes in segments")
}
}
if ok {
warmSpares(streamSettings, config, p)
errors.LogInfo(ctx, "opened session ", session, " in ", len(r.Up.Slots), " slots")
conn := newSlotConn(context.Background(), slots, r.Up, r.Down, p, nil, func() {
storage.Close()
})
writer := conn.writer.(*slotWriter)
writer.first = make(chan []byte, 1)
go func() {
var first []byte
select {
case first = <-writer.first:
case <-time.After(25 * time.Millisecond):
}
announce, _ := json.Marshal(r)
if len(first) > 0 && len(first) <= slotInlineLimit {
withFirst := r
withFirst.First = first
if encoded, err := json.Marshal(withFirst); err == nil && len(encoded) <= driveInlineLimit {
announce = encoded
}
}
// An announce failure surfaces as a connection error on the next Write/Read.
if err := storage.Put(context.Background(), announceName(session, time.Now()), announce); err != nil {
err = errors.New("failed to announce session ", session).Base(err)
writer.fail(err)
conn.reader.(*slotReader).setErr(err)
}
}()
return conn, nil
}
if err := storage.Put(ctx, announceName(session, time.Now()), nil); err != nil {
if err := storage.Put(ctx, announceName(session, time.Now()), announce); err != nil {
storage.Close()
return nil, errors.New("failed to announce session ", session).Base(err)
}
if ok {
errors.LogInfo(ctx, "opened session ", session, " in ", len(r.Up.Slots), " slots")
return newSlotConn(context.Background(), slots, r.Up, r.Down, p, func() {
storage.Close()
}), nil
}
errors.LogInfo(ctx, "opened session ", session)
return newConn(context.Background(), storage,
uplinkPrefix(session), downlinkPrefix(session), p, func() {
@@ -284,12 +134,10 @@ type Listener struct {
addConn internet.ConnHandler
params
mu sync.Mutex
active map[string]bool
handled map[string]time.Time
idleSince map[string]time.Time
announceWarned map[string]bool
firstSeen map[string]time.Time
mu sync.Mutex
active map[string]bool
handled map[string]time.Time
idleSince map[string]time.Time
}
func Serve(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
@@ -305,16 +153,14 @@ func Serve(ctx context.Context, address net.Address, port net.Port, streamSettin
listenerCtx, cancel := context.WithCancel(context.Background())
listener := &Listener{
ctx: listenerCtx,
cancel: cancel,
storage: storage,
addConn: addConn,
params: paramsForStorage(config, storage),
active: make(map[string]bool),
handled: make(map[string]time.Time),
idleSince: make(map[string]time.Time),
announceWarned: make(map[string]bool),
firstSeen: make(map[string]time.Time),
ctx: listenerCtx,
cancel: cancel,
storage: storage,
addConn: addConn,
params: paramsFromConfig(config),
active: make(map[string]bool),
handled: make(map[string]time.Time),
idleSince: make(map[string]time.Time),
}
go listener.acceptLoop(ctx)
@@ -328,7 +174,7 @@ func (l *Listener) acceptLoop(logCtx context.Context) {
active := time.Now()
for {
accepted, err := l.acceptPending(logCtx)
if err != nil && !isAuthError(err) {
if err != nil {
errors.LogWarningInner(logCtx, err, "failed to list sessions")
}
@@ -360,72 +206,27 @@ func (l *Listener) acceptPending(logCtx context.Context) (bool, error) {
}
accepted := false
l.mu.Lock()
if l.firstSeen == nil {
l.firstSeen = make(map[string]time.Time)
}
now := time.Now()
listedEntries := make(map[string]bool, len(sessions))
for _, listed := range sessions {
listedEntries[listed.Name] = true
if _, seen := l.firstSeen[listed.Name]; !seen {
l.firstSeen[listed.Name] = now
}
}
for entry := range l.firstSeen {
if !listedEntries[entry] {
delete(l.firstSeen, entry)
}
}
l.mu.Unlock()
for _, listed := range sessions {
entry := listed.Name
full := sessionsDir + "/" + entry
session, _, ok := parseAnnounce(entry)
session, at, ok := parseAnnounce(entry)
if !ok {
go l.drop(full)
continue
}
l.mu.Lock()
age := now.Sub(l.firstSeen[entry])
l.mu.Unlock()
if age > l.sessionTTL {
if time.Since(at) > l.sessionTTL {
errors.LogInfo(logCtx, "dropping the stale announcement of session ", session)
go l.drop(full)
l.mu.Lock()
protected := l.active[session] || !l.handled[session].IsZero()
l.mu.Unlock()
if !protected {
go l.drop(sessionPrefix(session))
}
go l.drop(sessionPrefix(session))
continue
}
if !l.claim(session) {
continue
}
announce := listed.Inline
if len(announce) == 0 {
if _, ok := sessionStorage(l.storage).(slotStore); ok {
announce, err = l.storage.Get(context.WithValue(l.ctx, maxBodyKey{}, int64(driveMaxAnnounce)), full)
if err != nil {
l.mu.Lock()
delete(l.active, session)
delete(l.handled, session)
warn := !l.announceWarned[session]
l.announceWarned[session] = true
l.mu.Unlock()
if warn {
errors.LogWarningInner(logCtx, err, "failed to read the announcement of session ", session)
}
continue
}
}
}
go l.drop(full)
accepted = true
l.addConn(l.newSessionConn(logCtx, session, announce))
l.addConn(l.newSessionConn(logCtx, session, listed.Inline))
}
return accepted, nil
}
@@ -458,7 +259,7 @@ func (l *Listener) newSessionConn(logCtx context.Context, session string, announ
if slots, ok := storage.(slotStore); ok {
if r, ok := decodeRing(announce); ok {
errors.LogInfo(logCtx, "accepted session ", session, " in ", len(r.Down.Slots), " slots")
return newSlotConn(l.ctx, slots, r.Down, r.Up, l.params, r.First, onClose)
return newSlotConn(l.ctx, slots, r.Down, r.Up, l.params, onClose)
}
}
errors.LogInfo(logCtx, "accepted session ", session)
@@ -519,13 +320,12 @@ func (l *Listener) collect() error {
}
l.mu.Unlock()
var first error
for _, session := range expired {
if err := l.storage.Delete(l.ctx, sessionPrefix(session)); err != nil && first == nil {
first = err
if err := l.storage.Delete(l.ctx, sessionPrefix(session)); err != nil {
return err
}
}
return first
return nil
}
func (l *Listener) Addr() net.Addr {
@@ -1,91 +0,0 @@
package xdrive
import (
"context"
"errors"
"strings"
"testing"
"time"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
)
// failPutStore is a slotStore whose Put always fails, to exercise the announce
// failure path without any network.
type failPutStore struct {
*memSlots
}
func (s *failPutStore) Put(ctx context.Context, name string, data []byte) error {
return errors.New("put refused")
}
func (s *failPutStore) Close() error { return nil }
func (s *failPutStore) Get(ctx context.Context, name string) ([]byte, error) {
return nil, errNotFound
}
func (s *failPutStore) List(ctx context.Context, prefix string) ([]Entry, error) {
return nil, nil
}
func (s *failPutStore) Delete(ctx context.Context, name string) error { return nil }
func TestDialAnnounceFailureFailsRead(t *testing.T) {
store := &failPutStore{memSlots: newMemSlots()}
factory := func(*Config) (Storage, error) { return store, nil }
testStorageFactory.Store(&factory)
t.Cleanup(func() { testStorageFactory.Store(nil) })
settings := &internet.MemoryStreamConfig{
ProtocolName: protocolName,
ProtocolSettings: &Config{
Service: "sim",
RemoteFolder: "x",
Secrets: []string{"a", "b", "c"},
Concurrency: 4,
FlushIntervalMs: 1,
PollIntervalMs: 1,
MaxPollIntervalMs: 5,
SessionTtlSeconds: 60,
},
}
conn, err := Dial(context.Background(), net.Destination{}, settings)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
go conn.Write([]byte("hello"))
type result struct {
n int
err error
}
done := make(chan result, 1)
go func() {
n, err := conn.Read(make([]byte, 16))
done <- result{n, err}
}()
select {
case r := <-done:
if r.err == nil || !strings.Contains(r.err.Error(), "failed to announce") {
t.Fatalf("Read returned %v, want the announce error", r.err)
}
case <-time.After(3 * time.Second):
t.Fatal("Read blocked after the announcement failed")
}
// The reader loop must have exited: it no longer polls the store.
store.mu.Lock()
before := store.reads
store.mu.Unlock()
time.Sleep(100 * time.Millisecond)
store.mu.Lock()
after := store.reads
store.mu.Unlock()
if after != before {
t.Fatalf("reader kept polling after the announce failure: %d -> %d", before, after)
}
}
@@ -1,121 +0,0 @@
package xdrive
import (
"context"
"strings"
"testing"
"time"
"github.com/xtls/xray-core/transport/internet/stat"
)
func announceEntry(name string) string {
return strings.TrimPrefix(name, sessionsDir+"/")
}
func TestAcceptPendingClientClockBehind(t *testing.T) {
l := newTestListener(t, t.TempDir())
var conns int
l.addConn = func(c stat.Connection) { conns++; c.Close() }
ctx := context.Background()
session := "clockbehind"
ring := sessionPrefix(session) + "/" + uplinkDir + "/s0"
if err := l.storage.Put(ctx, ring, []byte("x")); err != nil {
t.Fatal(err)
}
// The client's clock is ten minutes behind the server's.
name := announceName(session, time.Now().Add(-10*time.Minute))
if err := l.storage.Put(ctx, name, []byte("{}")); err != nil {
t.Fatal(err)
}
accepted, err := l.acceptPending(ctx)
if err != nil || !accepted {
t.Fatalf("a slow client clock must not drop the session: accepted=%v err=%v", accepted, err)
}
if conns != 1 {
t.Fatalf("addConn called %d times, want 1", conns)
}
if _, err := l.storage.Get(ctx, ring); err != nil {
t.Fatalf("the ring of an accepted session was dropped: %v", err)
}
}
func TestAcceptPendingStaleLeftoverProtectsActiveRing(t *testing.T) {
l := newTestListener(t, t.TempDir())
l.params.sessionTTL = 50 * time.Millisecond
var conns int
l.addConn = func(c stat.Connection) { conns++; c.Close() }
ctx := context.Background()
session := "active"
l.active[session] = true
l.handled[session] = time.Now()
ring := sessionPrefix(session) + "/" + uplinkDir + "/s0"
if err := l.storage.Put(ctx, ring, []byte("x")); err != nil {
t.Fatal(err)
}
name := announceName(session, time.Now())
if err := l.storage.Put(ctx, name, []byte("{}")); err != nil {
t.Fatal(err)
}
// The listener first saw this leftover entry long ago on its own clock.
l.firstSeen = map[string]time.Time{announceEntry(name): time.Now().Add(-time.Hour)}
accepted, err := l.acceptPending(ctx)
if err != nil || accepted {
t.Fatalf("a stale leftover must not be accepted: accepted=%v err=%v", accepted, err)
}
if conns != 0 {
t.Fatalf("addConn called %d times, want 0", conns)
}
waitFor(t, "the leftover announcement file to drop", func() bool {
_, err := l.storage.Get(ctx, name)
return err == errNotFound
})
if _, err := l.storage.Get(ctx, ring); err != nil {
t.Fatalf("the ring of an active session was dropped: %v", err)
}
}
func TestAcceptPendingTrulyLeftoverDropped(t *testing.T) {
l := newTestListener(t, t.TempDir())
l.params.sessionTTL = 50 * time.Millisecond
var conns int
l.addConn = func(c stat.Connection) { conns++; c.Close() }
ctx := context.Background()
session := "leftover"
ring := sessionPrefix(session) + "/" + uplinkDir + "/s0"
if err := l.storage.Put(ctx, ring, []byte("x")); err != nil {
t.Fatal(err)
}
name := announceName(session, time.Now())
if err := l.storage.Put(ctx, name, []byte("{}")); err != nil {
t.Fatal(err)
}
l.firstSeen = map[string]time.Time{announceEntry(name): time.Now().Add(-time.Hour)}
accepted, err := l.acceptPending(ctx)
if err != nil || accepted {
t.Fatalf("accepted=%v err=%v", accepted, err)
}
waitFor(t, "the leftover announcement and ring to drop", func() bool {
_, e1 := l.storage.Get(ctx, name)
_, e2 := l.storage.Get(ctx, ring)
return e1 == errNotFound && e2 == errNotFound
})
}
func TestAcceptPendingPrunesFirstSeen(t *testing.T) {
l := newTestListener(t, t.TempDir())
l.addConn = func(c stat.Connection) { c.Close() }
ctx := context.Background()
l.firstSeen = map[string]time.Time{"gone-entry": time.Now()}
if _, err := l.acceptPending(ctx); err != nil {
t.Fatal(err)
}
if _, still := l.firstSeen["gone-entry"]; still {
t.Fatal("firstSeen kept an entry that is no longer listed")
}
}
-11
View File
@@ -69,13 +69,6 @@ func pairWith(t *testing.T, streamSettings *internet.MemoryStreamConfig) (client
}
return client, server, func() {
if config, err := streamConfig(streamSettings); err == nil {
box := spareBoxFor(config)
box.stop.Store(true)
for box.busy.Load() {
time.Sleep(10 * time.Millisecond)
}
}
client.Close()
server.Close()
listener.Close()
@@ -375,10 +368,6 @@ func TestStaleAnnounce(t *testing.T) {
if err := listener.storage.Put(ctx, uplinkPrefix("ghost")+"/000000000.seg", []byte("x")); err != nil {
t.Fatalf("Put: %v", err)
}
// The listener first saw this entry an hour ago on its own clock.
listener.firstSeen = map[string]time.Time{
strings.TrimPrefix(stale, sessionsDir+"/"): time.Now().Add(-time.Hour),
}
accepted, err := listener.acceptPending(ctx)
if err != nil {