1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00

22 Commits

Author SHA1 Message Date
源文雨
4b60801a0f fix(nat): hello packet dead loop 2025-02-23 00:25:01 +09:00
源文雨
60495227fc fix(nat): panic on nil endpoint 2025-02-22 20:45:27 +09:00
源文雨
85a90aeb86 fix(nat): panic on nil endpoint 2025-02-22 20:29:49 +09:00
源文雨
a205d889ca fix(nat): keep alive blocked by firewall 2025-02-22 03:24:44 +09:00
源文雨
5c65302d67 feat(tcp): skip to keep busy sub conn 2025-02-21 23:08:41 +09:00
源文雨
3377d87d7a fix(recv): panic on short data len 2025-02-21 22:41:16 +09:00
源文雨
ac1d325bf0 chore(workflow): update deprecated package 2025-02-21 21:50:23 +09:00
源文雨
552be0335b fix(recv): panic on short data len 2025-02-21 21:44:39 +09:00
源文雨
fae1b768f2 fix(tcp): close recv on io.EOF 2025-02-21 16:44:58 +09:00
源文雨
956199cf19 fix(listen): ep logging 2025-02-21 15:16:44 +09:00
源文雨
409f0f270b fix(nat): panic on notify nil 2025-02-21 15:14:15 +09:00
源文雨
da99140e10 fix(tcp): close handing 2025-02-21 13:57:38 +09:00
源文雨
3d1bbf57a6 fix(tcp): close handing 2025-02-21 01:26:30 +09:00
源文雨
7f1c4ea4f0 feat(tunnel): add benchmark 2025-02-21 01:10:57 +09:00
源文雨
dbe990cac8 fix(p2p): tcp sync: unlock of unlocked mutex 2025-02-21 00:56:15 +09:00
源文雨
a52f9aa2c0 optimize(tunnel): test format 2025-02-20 23:16:51 +09:00
源文雨
ac04d25bfc chore: make lint happy 2025-02-20 20:28:24 +09:00
源文雨
78a744c5b0 fix(nat): panic on notify nil 2025-02-20 17:57:19 +09:00
源文雨
f0776751dd feat(crypto): use new xor version 2025-02-01 15:46:45 +08:00
源文雨
d679f45931 fix: -checklinkname=0 2025-02-01 10:06:00 +08:00
源文雨
82937b9b10 fix: -checklinkname=0 2025-02-01 10:03:10 +08:00
源文雨
e6298d3459 feat: add base14 en/decoding 2025-02-01 09:49:59 +08:00
21 changed files with 613 additions and 245 deletions

View File

@@ -11,7 +11,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@master
with:
go-version: ^1.20
go-version: ^1.23
- name: Check out code into the Go module directory
uses: actions/checkout@master
@@ -23,10 +23,10 @@ jobs:
run: sed -i 's/false/true/g' config/global.go
- name: Build
run: go build -v ./...
run: go build -ldflags=-checklinkname=0 -v ./...
- name: Test
run: sudo go test $(go list ./...) # ip test needs sudo
run: sudo go test -ldflags=-checklinkname=0 $(go list ./...) # ip test needs sudo
lint:
name: Lint
@@ -35,7 +35,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@master
with:
go-version: ^1.20
go-version: ^1.23
- name: Check out code into the Go module directory
uses: actions/checkout@master

View File

@@ -14,16 +14,16 @@ jobs:
steps:
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@master
with:
go-version: ^1.20
go-version: ^1.23
- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@master
- name: Cache Go
id: cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
# A list of files, directories, and wildcard patterns to cache and restore
path: ~/go/pkg/mod
@@ -33,25 +33,25 @@ jobs:
run: go mod tidy
- name: Build linux-x64
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/wg-linux-x64 -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-x64 -trimpath
- name: Build linux-x86
run: CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-s -w" -o artifacts/wg-linux-x86 -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-x86 -trimpath
- name: Build windows-x64
run: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o artifacts/wg-windows-x64.exe -trimpath
run: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-windows-x64.exe -trimpath
- name: Build windows-x86
run: CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="-s -w" -o artifacts/wg-windows-x86.exe -trimpath
run: CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-windows-x86.exe -trimpath
- name: Build arm64
run: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags="-s -w" -o artifacts/wg-linux-arm64 -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GOARM=7 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-arm64 -trimpath
- name: Build armv6
run: CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w" -o artifacts/wg-linux-armv6 -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-armv6 -trimpath
- name: Build mips
run: CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags="-s -w" -o artifacts/wg-linux-mips -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=mips go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-mips -trimpath
- name: Build mips-softfloat
run: CGO_ENABLED=0 GOOS=linux GOARCH=mips GOMIPS=softfloat go build -ldflags="-s -w" -o artifacts/wg-linux-mips-softfloat -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=mips GOMIPS=softfloat go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-mips-softfloat -trimpath
- name: Build mipsel
run: CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w" -o artifacts/wg-linux-mipsel -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-mipsel -trimpath
- name: Build mipsel-softfloat
run: CGO_ENABLED=0 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -ldflags="-s -w" -o artifacts/wg-linux-mipsel-softfloat -trimpath
run: CGO_ENABLED=0 GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -ldflags="-s -w -checklinkname=0" -o artifacts/wg-linux-mipsel-softfloat -trimpath
- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2

View File

@@ -24,7 +24,7 @@ linters:
- dogsled
- dupl
- errcheck
- exportloopref
#- exportloopref
- exhaustive
#- funlen
#- goconst

129
README.md
View File

@@ -41,6 +41,7 @@ EndPoint: 0.0.0.0:56789
MTU: 1504
SpeedLoop: 4096
Mask: 0x1234567890abcdef
Base14: true
Peers:
-
IP: "192.168.233.2"
@@ -66,3 +67,131 @@ Peers:
KeepAliveSeconds: 0
AllowTrans: false
```
## Benckmark on localhost
> This benckmark is tested on Mac Book Air M1 within battery mode.
### UDP MTU 4096
```bash
goos: darwin
goarch: arm64
pkg: github.com/fumiama/WireGold/upper/services/tunnel
cpu: Apple M1
BenchmarkTunnelUDP/1024-plain-nob14-8 4753 250629 ns/op 4.09 MB/s 3570503 B/op 117 allocs/op
BenchmarkTunnelUDP/1024-normal-nob14-8 4473 261136 ns/op 3.92 MB/s 3570565 B/op 118 allocs/op
BenchmarkTunnelUDP/1024-plain-b14-8 4250 275495 ns/op 3.72 MB/s 3575369 B/op 121 allocs/op
BenchmarkTunnelUDP/1024-normal-b14-8 4204 278062 ns/op 3.68 MB/s 3575844 B/op 122 allocs/op
BenchmarkTunnelUDP/1024-preshared-nob14-8 4196 282707 ns/op 3.62 MB/s 3570427 B/op 118 allocs/op
BenchmarkTunnelUDP/1024-preshared-b14-8 4130 290609 ns/op 3.52 MB/s 3575814 B/op 121 allocs/op
BenchmarkTunnelUDP/2048-plain-nob14-8 4162 280754 ns/op 7.29 MB/s 3578825 B/op 117 allocs/op
BenchmarkTunnelUDP/2048-normal-nob14-8 4088 292733 ns/op 7.00 MB/s 3578858 B/op 118 allocs/op
BenchmarkTunnelUDP/2048-plain-b14-8 4023 290264 ns/op 7.06 MB/s 3589002 B/op 121 allocs/op
BenchmarkTunnelUDP/2048-normal-b14-8 3904 298841 ns/op 6.85 MB/s 3589116 B/op 122 allocs/op
BenchmarkTunnelUDP/2048-preshared-nob14-8 3861 285661 ns/op 7.17 MB/s 3578690 B/op 117 allocs/op
BenchmarkTunnelUDP/2048-preshared-b14-8 4344 273054 ns/op 7.50 MB/s 3589101 B/op 122 allocs/op
BenchmarkTunnelUDP/3072-plain-nob14-8 4293 273633 ns/op 11.23 MB/s 3582928 B/op 121 allocs/op
BenchmarkTunnelUDP/3072-normal-nob14-8 4086 287291 ns/op 10.69 MB/s 3583011 B/op 122 allocs/op
BenchmarkTunnelUDP/3072-plain-b14-8 4144 295141 ns/op 10.41 MB/s 3600854 B/op 128 allocs/op
BenchmarkTunnelUDP/3072-normal-b14-8 3759 310645 ns/op 9.89 MB/s 3601217 B/op 130 allocs/op
BenchmarkTunnelUDP/3072-preshared-nob14-8 3805 305162 ns/op 10.07 MB/s 3583013 B/op 122 allocs/op
BenchmarkTunnelUDP/3072-preshared-b14-8 3574 320329 ns/op 9.59 MB/s 3601409 B/op 130 allocs/op
BenchmarkTunnelUDP/4096-plain-nob14-8 2448 482246 ns/op 8.49 MB/s 7157679 B/op 241 allocs/op
BenchmarkTunnelUDP/4096-normal-nob14-8 2328 508454 ns/op 8.06 MB/s 7158430 B/op 244 allocs/op
BenchmarkTunnelUDP/4096-plain-b14-8 2290 527956 ns/op 7.76 MB/s 7181296 B/op 254 allocs/op
BenchmarkTunnelUDP/4096-normal-b14-8 2276 528681 ns/op 7.75 MB/s 7181960 B/op 256 allocs/op
BenchmarkTunnelUDP/4096-preshared-nob14-8 2284 515491 ns/op 7.95 MB/s 7158606 B/op 243 allocs/op
BenchmarkTunnelUDP/4096-preshared-b14-8 2026 560506 ns/op 7.31 MB/s 7181980 B/op 256 allocs/op
```
### UDP MTU 1024
```bash
goos: darwin
goarch: arm64
pkg: github.com/fumiama/WireGold/upper/services/tunnel
cpu: Apple M1
BenchmarkTunnelUDPSmallMTU/1024-plain-nob14-8 3766 326631 ns/op 3.14 MB/s 3568299 B/op 126 allocs/op
BenchmarkTunnelUDPSmallMTU/1024-normal-nob14-8 3728 319552 ns/op 3.20 MB/s 3568454 B/op 128 allocs/op
BenchmarkTunnelUDPSmallMTU/1024-plain-b14-8 3625 323816 ns/op 3.16 MB/s 3575638 B/op 137 allocs/op
BenchmarkTunnelUDPSmallMTU/1024-normal-b14-8 3446 333510 ns/op 3.07 MB/s 3575925 B/op 138 allocs/op
BenchmarkTunnelUDPSmallMTU/1024-preshared-nob14-8 3537 335220 ns/op 3.05 MB/s 3568481 B/op 128 allocs/op
BenchmarkTunnelUDPSmallMTU/1024-preshared-b14-8 3486 337967 ns/op 3.03 MB/s 3575890 B/op 138 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-plain-nob14-8 3303 349278 ns/op 5.86 MB/s 3592804 B/op 140 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-normal-nob14-8 3331 376226 ns/op 5.44 MB/s 3593065 B/op 142 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-plain-b14-8 3116 421630 ns/op 4.86 MB/s 3605117 B/op 157 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-normal-b14-8 2634 381676 ns/op 5.37 MB/s 3606455 B/op 158 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-preshared-nob14-8 3138 391760 ns/op 5.23 MB/s 3591788 B/op 142 allocs/op
BenchmarkTunnelUDPSmallMTU/2048-preshared-b14-8 2959 391663 ns/op 5.23 MB/s 3605364 B/op 158 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-plain-nob14-8 3046 421705 ns/op 7.28 MB/s 3620443 B/op 156 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-normal-nob14-8 3001 413043 ns/op 7.44 MB/s 3631990 B/op 157 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-plain-b14-8 2503 406906 ns/op 7.55 MB/s 3640574 B/op 177 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-normal-b14-8 2776 416946 ns/op 7.37 MB/s 3643066 B/op 179 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-preshared-nob14-8 2947 422378 ns/op 7.27 MB/s 3626004 B/op 157 allocs/op
BenchmarkTunnelUDPSmallMTU/3072-preshared-b14-8 2547 459951 ns/op 6.68 MB/s 3648033 B/op 179 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-plain-nob14-8 1776 628904 ns/op 6.51 MB/s 7232490 B/op 285 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-normal-nob14-8 1782 643967 ns/op 6.36 MB/s 7238574 B/op 288 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-plain-b14-8 1549 674359 ns/op 6.07 MB/s 7262233 B/op 317 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-normal-b14-8 1826 690961 ns/op 5.93 MB/s 7260027 B/op 319 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-preshared-nob14-8 1868 649732 ns/op 6.30 MB/s 7242787 B/op 288 allocs/op
BenchmarkTunnelUDPSmallMTU/4096-preshared-b14-8 1654 682244 ns/op 6.00 MB/s 7255985 B/op 318 allocs/op
```
### TCP MTU 4096
```bash
goos: darwin
goarch: arm64
pkg: github.com/fumiama/WireGold/upper/services/tunnel
cpu: Apple M1
BenchmarkTunnelTCP/1024-plain-nob14-8 2323 459188 ns/op 2.23 MB/s 3576540 B/op 166 allocs/op
BenchmarkTunnelTCP/1024-normal-nob14-8 2472 438347 ns/op 2.34 MB/s 3576692 B/op 168 allocs/op
BenchmarkTunnelTCP/1024-plain-b14-8 2728 418395 ns/op 2.45 MB/s 3583603 B/op 171 allocs/op
BenchmarkTunnelTCP/1024-normal-b14-8 2668 463060 ns/op 2.21 MB/s 3584519 B/op 172 allocs/op
BenchmarkTunnelTCP/1024-preshared-nob14-8 2660 454945 ns/op 2.25 MB/s 3576708 B/op 168 allocs/op
BenchmarkTunnelTCP/1024-preshared-b14-8 2690 437373 ns/op 2.34 MB/s 3584515 B/op 172 allocs/op
BenchmarkTunnelTCP/2048-plain-nob14-8 2580 455416 ns/op 4.50 MB/s 3590368 B/op 168 allocs/op
BenchmarkTunnelTCP/2048-normal-nob14-8 2294 458178 ns/op 4.47 MB/s 3590512 B/op 171 allocs/op
BenchmarkTunnelTCP/2048-plain-b14-8 2414 462412 ns/op 4.43 MB/s 3605344 B/op 174 allocs/op
BenchmarkTunnelTCP/2048-normal-b14-8 2511 508527 ns/op 4.03 MB/s 3605658 B/op 177 allocs/op
BenchmarkTunnelTCP/2048-preshared-nob14-8 2433 482086 ns/op 4.25 MB/s 3590571 B/op 171 allocs/op
BenchmarkTunnelTCP/2048-preshared-b14-8 2361 494409 ns/op 4.14 MB/s 3605739 B/op 177 allocs/op
BenchmarkTunnelTCP/3072-plain-nob14-8 2487 498395 ns/op 6.16 MB/s 3600311 B/op 199 allocs/op
BenchmarkTunnelTCP/3072-normal-nob14-8 2170 542424 ns/op 5.66 MB/s 3600596 B/op 202 allocs/op
BenchmarkTunnelTCP/3072-plain-b14-8 2259 524854 ns/op 5.85 MB/s 3621274 B/op 205 allocs/op
BenchmarkTunnelTCP/3072-normal-b14-8 2307 537656 ns/op 5.71 MB/s 3621514 B/op 209 allocs/op
BenchmarkTunnelTCP/3072-preshared-nob14-8 1855 545493 ns/op 5.63 MB/s 3600418 B/op 201 allocs/op
BenchmarkTunnelTCP/3072-preshared-b14-8 2198 535328 ns/op 5.74 MB/s 3621536 B/op 208 allocs/op
BenchmarkTunnelTCP/4096-plain-nob14-8 2043 587272 ns/op 6.97 MB/s 7181814 B/op 391 allocs/op
BenchmarkTunnelTCP/4096-normal-nob14-8 1832 609909 ns/op 6.72 MB/s 7182940 B/op 394 allocs/op
BenchmarkTunnelTCP/4096-plain-b14-8 2044 572149 ns/op 7.16 MB/s 7209279 B/op 405 allocs/op
BenchmarkTunnelTCP/4096-normal-b14-8 2019 655180 ns/op 6.25 MB/s 7210261 B/op 409 allocs/op
BenchmarkTunnelTCP/4096-preshared-nob14-8 1652 636402 ns/op 6.44 MB/s 7182914 B/op 394 allocs/op
BenchmarkTunnelTCP/4096-preshared-b14-8 1885 624237 ns/op 6.56 MB/s 7210327 B/op 408 allocs/op
```
### TCP MTU 1024
```bash
goos: darwin
goarch: arm64
pkg: github.com/fumiama/WireGold/upper/services/tunnel
cpu: Apple M1
BenchmarkTunnelTCPSmallMTU/1024-plain-nob14-8 2061 582289 ns/op 1.76 MB/s 3577539 B/op 234 allocs/op
BenchmarkTunnelTCPSmallMTU/1024-normal-nob14-8 2172 561002 ns/op 1.83 MB/s 3577725 B/op 237 allocs/op
BenchmarkTunnelTCPSmallMTU/1024-plain-b14-8 2002 625224 ns/op 1.64 MB/s 3584694 B/op 244 allocs/op
BenchmarkTunnelTCPSmallMTU/1024-normal-b14-8 1957 590091 ns/op 1.74 MB/s 3585060 B/op 247 allocs/op
BenchmarkTunnelTCPSmallMTU/1024-preshared-nob14-8 2127 552614 ns/op 1.85 MB/s 3577669 B/op 236 allocs/op
BenchmarkTunnelTCPSmallMTU/1024-preshared-b14-8 2084 602128 ns/op 1.70 MB/s 3585057 B/op 247 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-plain-nob14-8 1899 595303 ns/op 3.44 MB/s 3596277 B/op 320 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-normal-nob14-8 1656 604450 ns/op 3.39 MB/s 3596115 B/op 323 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-plain-b14-8 1729 624733 ns/op 3.28 MB/s 3610414 B/op 339 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-normal-b14-8 1568 653317 ns/op 3.13 MB/s 3611234 B/op 342 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-preshared-nob14-8 1858 664597 ns/op 3.08 MB/s 3595764 B/op 322 allocs/op
BenchmarkTunnelTCPSmallMTU/2048-preshared-b14-8 1404 767077 ns/op 2.67 MB/s 3609789 B/op 339 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-plain-nob14-8 1761 846583 ns/op 3.63 MB/s 3614569 B/op 410 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-normal-nob14-8 1887 743407 ns/op 4.13 MB/s 3612869 B/op 411 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-plain-b14-8 1582 679431 ns/op 4.52 MB/s 3639650 B/op 435 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-normal-b14-8 1688 720574 ns/op 4.26 MB/s 3634744 B/op 435 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-preshared-nob14-8 1762 731901 ns/op 4.20 MB/s 3616570 B/op 414 allocs/op
BenchmarkTunnelTCPSmallMTU/3072-preshared-b14-8 1656 716281 ns/op 4.29 MB/s 3636078 B/op 434 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-plain-nob14-8 1482 847378 ns/op 4.83 MB/s 7214173 B/op 666 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-normal-nob14-8 1354 818199 ns/op 5.01 MB/s 7219760 B/op 665 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-plain-b14-8 1557 784260 ns/op 5.22 MB/s 7243407 B/op 697 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-normal-b14-8 1316 811760 ns/op 5.05 MB/s 7241275 B/op 699 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-preshared-nob14-8 1299 806369 ns/op 5.08 MB/s 7216648 B/op 666 allocs/op
BenchmarkTunnelTCPSmallMTU/4096-preshared-b14-8 1278 858201 ns/op 4.77 MB/s 7242324 B/op 703 allocs/op
```

View File

@@ -17,7 +17,8 @@ type Config struct {
EndPoint string `yaml:"EndPoint"`
MTU int64 `yaml:"MTU"` // MTU of nic (will minus packet header len)
SpeedLoop uint16 `yaml:"SpeedLoop"`
Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一
Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一
Base14 bool `yaml:"Base14"` // Base14 是否将最终报文进行 base16384 编码后再发送
Peers []Peer `yaml:"Peers"`
}

View File

@@ -1,3 +1,3 @@
package config
const ShowDebugLog = false
const ShowDebugLog = true

8
gold/head/hello.go Normal file
View File

@@ -0,0 +1,8 @@
package head
type Hello uint8
const (
HelloPing Hello = iota
HelloPong
)

View File

@@ -125,61 +125,43 @@ func (m *Me) xorenc(data []byte, seq uint32) []byte {
batchsz := len(data) / 8
remain := len(data) % 8
sum := m.mask
newdat := helper.MakeBytes(len(data) + 8)
newdat := helper.MakeBytes(8 + batchsz*8 + 8) // seqrand dat tail
binary.LittleEndian.PutUint32(newdat[:4], seq)
_, _ = rand.Read(newdat[4:8])
if remain > 0 {
var buf [8]byte
p := batchsz * 8
copy(buf[:], data[p:])
sum ^= binary.LittleEndian.Uint64(buf[:])
binary.LittleEndian.PutUint64(buf[:], sum)
copy(newdat[8+p:], buf[:])
}
for i := batchsz - 1; i >= 0; i-- {
_, _ = rand.Read(newdat[4:8]) // seqrand
sum ^= binary.LittleEndian.Uint64(newdat[:8]) // init from seqrand
binary.LittleEndian.PutUint64(newdat[:8], sum)
for i := 0; i < batchsz; i++ { // range on batch data
a := i * 8
b := (i + 1) * 8
sum ^= binary.LittleEndian.Uint64(data[a:b])
binary.LittleEndian.PutUint64(newdat[a+8:b+8], sum)
}
sum ^= binary.LittleEndian.Uint64(newdat[:8])
binary.LittleEndian.PutUint64(newdat[:8], sum)
p := batchsz * 8
copy(newdat[8+p:], data[p:])
newdat[len(newdat)-1] = byte(remain)
sum ^= binary.LittleEndian.Uint64(newdat[8+p:])
binary.LittleEndian.PutUint64(newdat[8+p:], sum)
return newdat
}
// xordec 按 8 字节, 以初始 m.mask 循环异或解码 data
func (m *Me) xordec(data []byte) (uint32, []byte) {
if len(data) <= 8 {
if len(data) < 16 || len(data)%8 != 0 {
return 0, nil
}
batchsz := len(data) / 8
remain := len(data) % 8
this := uint64(0)
next := uint64(0)
if len(data) >= 8 {
next = binary.LittleEndian.Uint64(data[:8])
}
for i := 0; i < batchsz-1; i++ {
sum := m.mask
for i := 0; i < batchsz; i++ {
a := i * 8
b := (i + 1) * 8
this = next
next = binary.LittleEndian.Uint64(data[a+8 : b+8])
binary.LittleEndian.PutUint64(data[a:b], this^next)
x := binary.LittleEndian.Uint64(data[a:b])
sum ^= x
binary.LittleEndian.PutUint64(data[a:b], sum)
sum = x
}
if remain > 0 {
var buf [8]byte
a := (batchsz - 1) * 8
b := batchsz * 8
copy(buf[:], data[b:])
this = next
next = binary.LittleEndian.Uint64(buf[:]) | (m.mask & (uint64(0xffffffff_ffffffff) << (uint64(remain) * 8)))
if batchsz > 0 {
binary.LittleEndian.PutUint64(data[a:b], this^next)
}
binary.LittleEndian.PutUint64(buf[:], next^m.mask)
copy(data[b:], buf[:])
} else {
binary.LittleEndian.PutUint64(data[len(data)-8:], next^m.mask)
remain := data[len(data)-1]
if remain >= 8 {
return 0, nil
}
return binary.LittleEndian.Uint32(data[:4]), data[8:]
return binary.LittleEndian.Uint32(data[:4]), data[8 : len(data)-8+int(remain)]
}

View File

@@ -17,7 +17,7 @@ func TestXOR(t *testing.T) {
}
buf := make([]byte, 4096)
buf2 := make([]byte, 4096)
for i := 1; i < 4096; i++ {
for i := 0; i < 4096; i++ {
data := buf[:i]
orgdata := buf2[:i]
r1 := bytes.NewBuffer(data[:0])
@@ -28,7 +28,10 @@ func TestXOR(t *testing.T) {
t.Fatal(err)
}
seq, dec := m.xordec(m.xorenc(r1.Bytes(), uint32(i)))
if !bytes.Equal(dec, r2.Bytes()) || seq != uint32(i) {
if !bytes.Equal(dec, r2.Bytes()) {
t.Fatal("unexpected xor at", i, "except", hex.EncodeToString(r2.Bytes()), "got", hex.EncodeToString(dec))
}
if seq != uint32(i) {
t.Fatal("unexpected xor at", i, "seq", seq)
}
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
"net"
"sync/atomic"
"time"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/p2p"
@@ -38,8 +39,8 @@ type Link struct {
keys [32]cipher.AEAD
// 本机信息
me *Me
// 连接的状态,详见下方 const
status int8
// 最后一次收到报文的时间
lastalive *time.Time
// 是否允许转发
allowtrans bool
// 是否对数据进行 zstd 压缩
@@ -52,12 +53,6 @@ type Link struct {
mturandomrange uint16
}
const (
LINK_STATUS_DOWN = iota
LINK_STATUS_HALFUP
LINK_STATUS_UP
)
// Connect 初始化与 peer 的连接
func (m *Me) Connect(peer string) (*Link, error) {
p, ok := m.IsInPeer(net.ParseIP(peer).String())
@@ -69,7 +64,7 @@ func (m *Me) Connect(peer string) (*Link, error) {
// Close 关闭到 peer 的连接
func (l *Link) Close() {
l.status = LINK_STATUS_DOWN
l.Destroy()
}
// Destroy 从 connections 移除 peer

View File

@@ -8,7 +8,9 @@ import (
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/klauspost/compress/zstd"
"github.com/sirupsen/logrus"
@@ -130,6 +132,12 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
}
continue
}
if n <= 0 {
if config.ShowDebugLog {
logrus.Debugln("[listen] unexpected read n =", n)
}
continue
}
lq := lstnq{
index: -1,
addr: addr,
@@ -165,15 +173,17 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
packet.Put()
return
}
if p.endpoint == nil || !p.endpoint.Euqal(addr) {
if m.ep.Network() == "udp" {
if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
if m.ep.Network() == "tcp" && !addr.Euqal(p.endpoint) {
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr
} else if !addr.Euqal(p.endpoint) && p.rawep == "" { // tcp/ws, ep not registered
} else { // others are all no status link
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr
}
}
now := time.Now()
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.lastalive)), unsafe.Pointer(&now))
switch {
case p.IsToMe(packet.Dst):
if !p.Accept(packet.Src) {
@@ -216,20 +226,18 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
}
switch packet.Proto {
case head.ProtoHello:
switch p.status {
case LINK_STATUS_DOWN:
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false)
switch {
case len(packet.Body()) == 0:
logrus.Warnln("[listen] @", index, "recv old hello packet, do nothing")
case packet.Body()[0] == byte(head.HelloPing):
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), []byte{byte(head.HelloPong)}), false)
if err == nil {
if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "send", n, "bytes hello ack packet")
}
p.status = LINK_STATUS_HALFUP
logrus.Infoln("[listen] @", index, "recv hello, send", n, "bytes hello ack packet")
} else {
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
}
case LINK_STATUS_HALFUP:
p.status = LINK_STATUS_UP
case LINK_STATUS_UP:
default:
logrus.Infoln("[listen] @", index, "recv hello ack packet, do nothing")
}
packet.Put()
case head.ProtoNotify:

View File

@@ -22,6 +22,8 @@ import (
// Me 是本机的抽象
type Me struct {
// 用于自我重连
cfg *MyConfig
// 本机私钥
// 利用 Curve25519 生成
// https://pkg.go.dev/golang.org/x/crypto/curve25519
@@ -51,6 +53,8 @@ type Me struct {
srcport, dstport, mtu, speedloop uint16
// 报头掩码
mask uint64
// 是否进行 base16384 编码
base14 bool
// 本机网络端点初始化配置
networkconfigs []any
}
@@ -64,6 +68,7 @@ type MyConfig struct {
NICConfig *NICConfig
SrcPort, DstPort, MTU, SpeedLoop uint16
Mask uint64
Base14 bool
}
type NICConfig struct {
@@ -74,6 +79,7 @@ type NICConfig struct {
// NewMe 设置本机参数
func NewMe(cfg *MyConfig) (m Me) {
m.cfg = cfg
m.privKey = *cfg.PrivateKey
var err error
nw := cfg.Network
@@ -116,6 +122,7 @@ func NewMe(cfg *MyConfig) (m Me) {
)
}
m.mask = cfg.Mask
m.base14 = cfg.Base14
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], m.mask)
logrus.Infoln("[me] xor mask", hex.EncodeToString(buf[:]))
@@ -124,6 +131,33 @@ func NewMe(cfg *MyConfig) (m Me) {
return
}
// Restart 重新连接
func (m *Me) Restart() error {
oldconn := m.conn
m.conn = nil
if helper.IsNonNilInterface(oldconn) {
_ = oldconn.Close()
}
var err error
nw := m.cfg.Network
if nw == "" {
nw = "udp"
}
m.networkconfigs = m.cfg.NetworkConfigs
m.ep, err = p2p.NewEndPoint(nw, m.cfg.MyEndpoint, m.networkconfigs...)
if err != nil {
return err
}
ip, cidr, err := net.ParseCIDR(m.cfg.MyIPwithMask)
if err != nil {
return err
}
m.me = ip
m.subnet = *cidr
m.conn, err = m.listen()
return err
}
func (m *Me) SrcPort() uint16 {
return m.srcport
}
@@ -142,7 +176,7 @@ func (m *Me) EndPoint() p2p.EndPoint {
func (m *Me) Close() error {
m.connections = nil
if m.conn != nil {
if helper.IsNonNilInterface(m.conn) {
_ = m.conn.Close()
m.conn = nil
}

View File

@@ -2,7 +2,9 @@ package link
import (
"encoding/json"
"sync/atomic"
"time"
"unsafe"
"github.com/sirupsen/logrus"
@@ -23,11 +25,21 @@ func (l *Link) keepAlive(dur int64) {
if l.me.connections == nil {
return
}
n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, nil), false)
la := (*time.Time)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&l.lastalive))))
if la != nil && time.Since(*la) > 10*time.Second*time.Duration(dur) { // 可能已经被阻断, 断开重连
logrus.Warnln("[nat] no response after 10 keep alive tries, re-connecting...")
err := l.me.Restart()
if err != nil {
logrus.Errorln("[nat] re-connect me err:", err)
} else {
logrus.Infoln("[nat] re-connect me succeeded")
}
}
n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, []byte{byte(head.HelloPing)}), false)
if err == nil {
logrus.Infoln("[nat] send", n, "bytes keep alive packet")
} else {
logrus.Errorln("[nat] send keep alive packet error:", err)
logrus.Warnln("[nat] send keep alive packet error:", err)
}
}
}
@@ -57,7 +69,7 @@ func (l *Link) onNotify(packet []byte) {
if err == nil {
p, ok := l.me.IsInPeer(peer)
if ok {
if !p.endpoint.Euqal(addr) {
if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
p.endpoint = addr
logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep)
}
@@ -97,6 +109,9 @@ func (l *Link) onQuery(packet []byte) {
lnk, ok := l.me.IsInPeer(p)
eps := ""
if l.me.ep.Network() == "udp" { // udp has real p2p
if helper.IsNilInterface(lnk.endpoint) {
continue
}
eps = lnk.endpoint.String()
}
if eps == "" {
@@ -105,7 +120,7 @@ func (l *Link) onQuery(packet []byte) {
if eps == "" {
continue
}
if ok {
if ok && helper.IsNonNilInterface(lnk.endpoint) {
notify[p] = [2]string{
lnk.endpoint.Network(),
eps,

View File

@@ -8,6 +8,7 @@ import (
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
base14 "github.com/fumiama/go-base16384"
"github.com/sirupsen/logrus"
)
@@ -18,6 +19,9 @@ func (l *Link) Read() *head.Packet {
func (m *Me) wait(data []byte) *head.Packet {
if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", len(data))
}
return nil
}
bound := 64
@@ -27,11 +31,37 @@ func (m *Me) wait(data []byte) *head.Packet {
endl = "."
}
if config.ShowDebugLog {
logrus.Debugln("[recv] data bytes", hex.EncodeToString(data[:bound]), endl)
logrus.Debugln("[recv] data bytes, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
}
if m.base14 {
data = base14.Decode(data)
if len(data) < bound {
bound = len(data)
endl = "."
}
if config.ShowDebugLog {
logrus.Debugln("[recv] data b14ed, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
}
if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", len(data))
}
return nil
}
}
seq, data := m.xordec(data)
if len(data) < bound {
bound = len(data)
endl = "."
}
if config.ShowDebugLog {
logrus.Debugln("[recv] data xored", hex.EncodeToString(data[:bound]), endl)
logrus.Debugln("[recv] data xored, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
}
if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", len(data))
}
return nil
}
flags := head.Flags(data)
if !flags.IsValid() {

View File

@@ -16,6 +16,7 @@ import (
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper"
base14 "github.com/fumiama/go-base16384"
)
var (
@@ -122,7 +123,7 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui
// write 向 peer 发一个包
func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) {
peerep := l.endpoint
if peerep == nil {
if helper.IsNilInterface(peerep) {
return 0, errors.New("nil endpoint of " + p.Dst.String())
}
@@ -142,14 +143,22 @@ func (l *Link) writeonce(p *head.Packet, teatype uint8, additional uint16, datas
bound = len(d)
endl = "."
}
conn := l.me.conn
if conn == nil {
return 0, io.ErrClosedPipe
}
if config.ShowDebugLog {
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.conn.LocalAddr(), "to", peerep, "offset", fmt.Sprintf("%04x", offset), "crc", fmt.Sprintf("%016x", p.CRC64()))
logrus.Debugln("[send] write", len(d), "bytes data from ep", conn.LocalAddr(), "to", peerep, "offset", fmt.Sprintf("%04x", offset), "crc", fmt.Sprintf("%016x", p.CRC64()))
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl)
}
d = l.me.xorenc(d, seq)
if l.me.base14 {
d = base14.Encode(d)
}
if config.ShowDebugLog {
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl)
}
defer helper.PutBytes(d)
return l.me.conn.WriteToPeer(d, peerep)
return conn.WriteToPeer(d, peerep)
}

View File

@@ -63,6 +63,9 @@ func (p *packet) ReadFrom(r io.Reader) (n int64, err error) {
}
if binary.LittleEndian.Uint32(buf[:]) != magic {
err = ErrInvalidMagic
if config.ShowDebugLog {
logrus.Debugf("[tcp] expect magic %08x but got %08x", magic, binary.LittleEndian.Uint32(buf[:]))
}
return
}
cnt, err = io.ReadFull(r, buf[:3])

View File

@@ -95,6 +95,7 @@ type connrecv struct {
type subconn struct {
cplk sync.Mutex
last time.Time // last active time
conn *net.TCPConn
}
@@ -158,6 +159,10 @@ func delsubs(i int, subs []*subconn) []*subconn {
}
func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
if conn.peers == nil {
return
}
ep, _ := newEndpoint(tcpconn.RemoteAddr().String(), &Config{
DialTimeout: conn.addr.dialtimeout,
PeersTimeout: conn.addr.peerstimeout,
@@ -183,9 +188,12 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
}
if issub {
conn.sblk.Lock()
conn.subs = append(conn.subs, &subconn{conn: tcpconn})
conn.subs = append(conn.subs, &subconn{conn: tcpconn, last: time.Now()})
conn.sblk.Unlock()
} else {
if conn.peers == nil {
return
}
conn.peers.Set(ep.String(), tcpconn)
}
}
@@ -193,7 +201,8 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
if issub {
defer func() {
conn.sblk.Lock()
for i, sub := range conn.subs {
subs := conn.subs
for i, sub := range subs {
if sub.conn == tcpconn {
conn.subs = delsubs(i, conn.subs)
break
@@ -202,6 +211,9 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
conn.sblk.Unlock()
}()
} else {
if conn.peers == nil {
return
}
defer conn.peers.Delete(ep.String())
}
@@ -220,10 +232,7 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
}
r.conn = tcpconn
stopch := make(chan struct{})
t := time.AfterFunc(peerstimeout, func() {
stopch <- struct{}{}
})
t := time.NewTimer(peerstimeout)
var err error
copych := make(chan struct{})
@@ -233,7 +242,7 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
}()
select {
case <-stopch:
case <-t.C:
if config.ShowDebugLog {
logrus.Debugln("[tcp] recv from", ep, "timeout")
}
@@ -251,7 +260,10 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
if config.ShowDebugLog {
logrus.Debugln("[tcp] recv from", ep, "err:", err)
}
if errors.Is(err, net.ErrClosed) || errors.Is(err, io.ErrClosedPipe) {
if errors.Is(err, net.ErrClosed) ||
errors.Is(err, io.ErrClosedPipe) ||
errors.Is(err, io.EOF) ||
errors.Is(err, ErrInvalidMagic) {
_ = tcpconn.CloseRead()
return
}
@@ -273,13 +285,13 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
func (conn *Conn) keep(ep *EndPoint) {
keepinterval := ep.keepinterval
if keepinterval < time.Second*4 {
keepinterval = time.Second * 4
if keepinterval < time.Second*10 {
keepinterval = time.Second * 10
}
t := time.NewTicker(keepinterval)
defer t.Stop()
for range t.C {
if conn.addr == nil {
if conn.addr == nil || conn.peers == nil {
return
}
tcpconn := conn.peers.Get(ep.String())
@@ -289,42 +301,57 @@ func (conn *Conn) keep(ep *EndPoint) {
return
}
if err != nil {
logrus.Warnln("[tcp] keep main conn alive to", ep, "err:", err)
logrus.Warnln("[tcp] keep main conn alive from", conn, "to", ep, "err:", err)
conn.peers.Delete(ep.String())
} else if config.ShowDebugLog {
logrus.Debugln("[tcp] keep main conn alive to", ep)
logrus.Debugln("[tcp] keep main conn alive from", conn, "to", ep)
}
}
conn.sblk.RLock()
for i, sub := range conn.subs {
subs := conn.subs
for i, sub := range subs {
if time.Since(sub.last) < keepinterval {
if config.ShowDebugLog {
logrus.Debugln("[tcp] skip to keep busy sub conn from", conn, "to", ep)
}
continue
}
_, err := io.Copy(sub.conn, &packet{typ: packetTypeSubKeepAlive})
if conn.addr == nil {
return
}
if err != nil {
logrus.Warnln("[tcp] keep sub conn alive to", sub.conn.RemoteAddr(), "err:", err)
logrus.Warnln("[tcp] keep sub conn alive from", conn, "to", sub.conn.RemoteAddr(), "err:", err)
conn.subs = delsubs(i, conn.subs) // del 1 link at once
break
}
if config.ShowDebugLog {
logrus.Debugln("[tcp] keep sub conn alive from", conn, "to", ep)
}
}
conn.sblk.RUnlock()
}
}
func (conn *Conn) Close() error {
if conn.lstn != nil {
_ = conn.lstn.Close()
}
if conn.peers != nil {
conn.peers.Destroy()
}
if conn.recv != nil {
close(conn.recv)
}
lstn := conn.lstn
peers := conn.peers
recv := conn.recv
conn.addr = nil
conn.lstn = nil
conn.peers = nil
conn.recv = nil
if lstn != nil {
_ = lstn.Close()
}
if peers != nil {
peers.Destroy()
}
if recv != nil {
close(recv)
}
return nil
}
@@ -343,6 +370,9 @@ func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
if p == nil {
return 0, nil, net.ErrClosed
}
if conn.peers == nil {
return 0, nil, net.ErrClosed
}
conn.peers.Set(p.addr.String(), p.conn)
if p.pckt.typ == packetTypeNormal {
break
@@ -413,7 +443,7 @@ RECONNECT:
conn.peers.Set(tcpep.String(), tcpconn)
} else {
conn.sblk.Lock()
conn.subs = append(conn.subs, &subconn{conn: tcpconn})
conn.subs = append(conn.subs, &subconn{conn: tcpconn, last: time.Now()})
conn.sblk.Unlock()
}
go conn.receive(tcpconn, true)
@@ -430,7 +460,8 @@ RECONNECT:
conn.peers.Delete(tcpep.String())
} else {
conn.sblk.Lock()
for i, sub := range conn.subs {
subs := conn.subs
for i, sub := range subs {
if sub == subc {
conn.subs = delsubs(i, conn.subs)
break
@@ -446,6 +477,7 @@ RECONNECT:
}
}
if subc != nil {
subc.last = time.Now()
subc.cplk.Unlock()
}
return int(cnt) - 3, err
@@ -459,16 +491,19 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
if len(b) >= 65536 {
return 0, errors.New("data size " + strconv.Itoa(len(b)) + " is too large")
}
if (!conn.suberr || len(conn.subs) > 0) && !conn.cplk.TryLock() {
if config.ShowDebugLog {
logrus.Debug("[tcp] try sub write to", tcpep)
locked := conn.cplk.TryLock()
if !locked {
if !conn.suberr || len(conn.subs) > 0 {
if config.ShowDebugLog {
logrus.Debug("[tcp] try sub write to", tcpep)
}
n, err = conn.writeToPeer(b, tcpep, true) // try sub write
if err == nil {
return
}
conn.suberr = true // fast fail
}
n, err = conn.writeToPeer(b, tcpep, true) // try sub write
if err == nil {
return
}
conn.suberr = true // fast fail
conn.cplk.Lock() // add to main queue
conn.cplk.Lock() // add to main queue
}
defer conn.cplk.Unlock()
return conn.writeToPeer(b, tcpep, false)

View File

@@ -1,6 +1,7 @@
package helper
import (
"reflect"
"unsafe"
)
@@ -30,3 +31,11 @@ func StringToBytes(s string) (b []byte) {
bh.cap = sh.len
return b
}
func IsNilInterface(x any) bool {
return x == nil || reflect.ValueOf(x).IsZero()
}
func IsNonNilInterface(x any) bool {
return !IsNilInterface(x)
}

View File

@@ -42,7 +42,7 @@ func Create(me *link.Me, peer string) (s Tunnel, err error) {
}
func (s *Tunnel) Start(srcport, destport, mtu uint16) {
logrus.Infoln("[tunnel] start from", srcport, "to", destport)
logrus.Infoln("[tunnel] start port through", srcport, "->", destport, "mtu", mtu)
s.src = srcport
s.dest = destport
s.mtu = mtu

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"runtime"
"strings"
@@ -17,7 +18,84 @@ import (
"github.com/fumiama/WireGold/helper"
)
func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint16) {
func TestTunnelUDP(t *testing.T) {
testTunnelNetwork(t, "udp", 4096)
}
func TestTunnelUDPSmallMTU(t *testing.T) {
testTunnelNetwork(t, "udp", 1024)
}
func TestTunnelUDPLite(t *testing.T) {
if runtime.GOOS == "darwin" {
return
}
testTunnelNetwork(t, "udplite", 4096)
}
func TestTunnelUDPLiteSmallMTU(t *testing.T) {
if runtime.GOOS == "darwin" {
return
}
testTunnelNetwork(t, "udplite", 1024)
}
func TestTunnelTCP(t *testing.T) {
testTunnelNetwork(t, "tcp", 4096)
}
func TestTunnelTCPSmallMTU(t *testing.T) {
testTunnelNetwork(t, "tcp", 1024)
}
func TestTunnelIP(t *testing.T) {
testTunnelNetwork(t, "ip", 4096)
}
func TestTunnelIPSmallMTU(t *testing.T) {
testTunnelNetwork(t, "ip", 1024)
}
func BenchmarkTunnelUDP(b *testing.B) {
benchmarkTunnelNetwork(b, "udp", 4096)
}
func BenchmarkTunnelUDPSmallMTU(b *testing.B) {
benchmarkTunnelNetwork(b, "udp", 1024)
}
func BenchmarkTunnelUDPLite(b *testing.B) {
if runtime.GOOS == "darwin" {
return
}
benchmarkTunnelNetwork(b, "udplite", 4096)
}
func BenchmarkTunnelUDPLiteSmallMTU(b *testing.B) {
if runtime.GOOS == "darwin" {
return
}
benchmarkTunnelNetwork(b, "udplite", 1024)
}
func BenchmarkTunnelTCP(b *testing.B) {
benchmarkTunnelNetwork(b, "tcp", 4096)
}
func BenchmarkTunnelTCPSmallMTU(b *testing.B) {
benchmarkTunnelNetwork(b, "tcp", 1024)
}
func BenchmarkTunnelIP(b *testing.B) {
benchmarkTunnelNetwork(b, "ip", 4096)
}
func BenchmarkTunnelIPSmallMTU(b *testing.B) {
benchmarkTunnelNetwork(b, "ip", 1024)
}
func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) {
fmt.Println("start", nw, "testing")
selfpk, err := curve.New(nil)
if err != nil {
panic(err)
@@ -51,6 +129,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
SrcPort: 1,
DstPort: 1,
MTU: mtu,
Base14: isbase14,
})
defer m.Close()
@@ -62,6 +141,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
SrcPort: 1,
DstPort: 1,
MTU: mtu,
Base14: isbase14,
})
defer p.Close()
@@ -136,7 +216,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
rand.Read(sendb)
n, _ := tunnme.Write(sendb)
sendbufs <- sendb
logrus.Infoln("loop", i, "write", n, "bytes")
logrus.Debugln("loop", i, "write", n, "bytes")
}
close(sendbufs)
}()
@@ -147,7 +227,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
if err != nil {
t.Fatal(err)
}
logrus.Infoln("loop", i, "read", n, "bytes")
logrus.Debugln("loop", i, "read", n, "bytes")
if string(sendb) != string(buf) {
t.Fatal("loop", i, "error: recv 65535 bytes data")
}
@@ -173,138 +253,164 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
}
}
func TestTunnelUDP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "udp", true, nil, 4096) // test plain text
testTunnel(t, "udp", false, nil, 4096) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
func benchmarkTunnel(b *testing.B, sz int, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) {
selfpk, err := curve.New(nil)
if err != nil {
panic(err)
}
testTunnel(t, "udp", false, &buf, 4096) // test preshared
peerpk, err := curve.New(nil)
if err != nil {
panic(err)
}
epm := "127.0.0.1"
if nw != "ip" {
epm += ":0"
}
// under macos you need to run
//
// sudo ifconfig lo0 alias 127.0.0.2
epp := "127.0.0.2"
if nw != "ip" {
epp += ":0"
}
m := link.NewMe(&link.MyConfig{
MyIPwithMask: "192.168.1.2/32",
MyEndpoint: epm,
Network: nw,
PrivateKey: selfpk.Private(),
SrcPort: 1,
DstPort: 1,
MTU: mtu,
Base14: isbase14,
})
defer m.Close()
p := link.NewMe(&link.MyConfig{
MyIPwithMask: "192.168.1.3/32",
MyEndpoint: epp,
Network: nw,
PrivateKey: peerpk.Private(),
SrcPort: 1,
DstPort: 1,
MTU: mtu,
Base14: isbase14,
})
defer p.Close()
ppp := peerpk.Public()
spp := selfpk.Public()
if isplain {
ppp = nil
spp = nil
}
m.AddPeer(&link.PeerConfig{
PeerIP: "192.168.1.3",
EndPoint: p.EndPoint().String(),
AllowedIPs: []string{"192.168.1.3/32"},
PubicKey: ppp,
PresharedKey: pshk,
MTU: mtu,
MTURandomRange: mtu / 2,
UseZstd: true,
DoublePacket: true,
})
p.AddPeer(&link.PeerConfig{
PeerIP: "192.168.1.2",
EndPoint: m.EndPoint().String(),
AllowedIPs: []string{"192.168.1.2/32"},
PubicKey: spp,
PresharedKey: pshk,
MTU: mtu,
MTURandomRange: mtu / 2,
UseZstd: true,
})
tunnme, err := Create(&m, "192.168.1.3")
if err != nil {
b.Fatal(err)
}
tunnme.Start(1, 1, 4096)
tunnpeer, err := Create(&p, "192.168.1.2")
if err != nil {
b.Fatal(err)
}
tunnpeer.Start(1, 1, 4096)
time.Sleep(time.Second) // wait link up
b.SetBytes(int64(sz))
b.ResetTimer()
sendb := make([]byte, sz)
for i := 0; i < b.N; i++ {
rand.Read(sendb)
go tunnme.Write(sendb)
buf := make([]byte, sz)
_, err = io.ReadFull(&tunnpeer, buf)
if err != nil {
b.Fatal(err)
}
}
b.StopTimer()
tunnme.Stop()
tunnpeer.Stop()
}
func TestTunnelUDPSmallMTU(t *testing.T) {
func testTunnelNetwork(t *testing.T, nw string, mtu uint16) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "udp", true, nil, 1024) // test plain text
// test without base14
testTunnel(t, nw, true, false, nil, mtu) // test plain text
testTunnel(t, nw, false, false, nil, mtu) // test normal
testTunnel(t, "udp", false, nil, 1024) // test normal
// test with base14
testTunnel(t, nw, true, true, nil, mtu) // test plain text
testTunnel(t, nw, false, true, nil, mtu) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "udp", false, &buf, 1024) // test preshared
// test without base14
testTunnel(t, nw, false, false, &buf, mtu) // test preshared
// test with base14
testTunnel(t, nw, false, true, &buf, mtu) // test preshared
}
func TestTunnelUDPLite(t *testing.T) {
if runtime.GOOS == "darwin" {
return
}
logrus.SetLevel(logrus.DebugLevel)
func benchmarkTunnelNetwork(b *testing.B, nw string, mtu uint16) {
logrus.SetLevel(logrus.ErrorLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "udplite", true, nil, 4096) // test plain text
testTunnel(t, "udplite", false, nil, 4096) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
for i := 1; i <= 4; i++ {
sz := 1024 * i
b.Run(fmt.Sprintf("%d-plain-nob14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, true, false, nil, mtu) // test plain text
})
b.Run(fmt.Sprintf("%d-normal-nob14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, false, false, nil, mtu) // test normal
})
b.Run(fmt.Sprintf("%d-plain-b14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, true, true, nil, mtu) // test plain text
})
b.Run(fmt.Sprintf("%d-normal-b14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, false, true, nil, mtu) // test normal
})
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
b.Run(fmt.Sprintf("%d-preshared-nob14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, false, false, &buf, mtu) // test preshared
})
b.Run(fmt.Sprintf("%d-preshared-b14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, false, true, &buf, mtu) // test preshared
})
}
testTunnel(t, "udplite", false, &buf, 4096) // test preshared
}
func TestTunnelUDPLiteSmallMTU(t *testing.T) {
if runtime.GOOS == "darwin" {
return
}
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "udplite", true, nil, 1024) // test plain text
testTunnel(t, "udplite", false, nil, 1024) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "udplite", false, &buf, 1024) // test preshared
}
func TestTunnelTCP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "tcp", true, nil, 4096) // test plain text
testTunnel(t, "tcp", false, nil, 4096) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "tcp", false, &buf, 4096) // test preshared
}
func TestTunnelTCPSmallMTU(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "tcp", true, nil, 1024) // test plain text
testTunnel(t, "tcp", false, nil, 1024) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "tcp", false, &buf, 1024) // test preshared
}
func TestTunnelIP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "ip", true, nil, 4096) // test plain text
testTunnel(t, "ip", false, nil, 4096) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "ip", false, &buf, 4096) // test preshared
}
func TestTunnelIPSmallMTU(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "ip", true, nil, 1024) // test plain text
testTunnel(t, "ip", false, nil, 1024) // test normal
var buf [32]byte
_, err := rand.Read(buf[:])
if err != nil {
panic(err)
}
testTunnel(t, "ip", false, &buf, 1024) // test preshared
}
// logFormat specialize for go-cqhttp

View File

@@ -113,6 +113,7 @@ func (wg *WG) init(srcport, dstport uint16) {
MTU: uint16(wg.c.MTU),
SpeedLoop: wg.c.SpeedLoop,
Mask: wg.c.Mask,
Base14: wg.c.Base14,
})
for _, peer := range wg.c.Peers {