1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-10 11:40:30 +08:00

50 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
源文雨
bbe2c60aa3 feat(me): concurrency send 2024-08-17 21:27:07 +08:00
源文雨
1ba17fca54 feat(config): add inner routing mark y 2024-08-17 21:17:04 +08:00
源文雨
9871bde9f1 chore: make lint happy 2024-08-16 23:54:48 +08:00
源文雨
45c8945c68 fix(link): listen integer divide by zero 2024-08-16 23:46:21 +08:00
源文雨
dfc9f1a7c4 fix(link): early use of new listen buf 2024-08-11 22:23:43 +08:00
源文雨
9942ef2bd0 fix(link): duplicate crc recv (#2) 2024-08-11 21:35:59 +08:00
源文雨
bd5c0092ef optimize(test): use logrus 2024-08-11 19:47:12 +08:00
源文雨
c7cfd94ae2 fix(p2p): tcp sub conn add close write 2024-08-08 13:35:04 +08:00
源文雨
4a2b6c3f90 fix(p2p): close receive on certain errors 2024-08-08 13:26:20 +08:00
源文雨
d5d7a9412f feat(head): use uniform head extract & crc64 -> md5 2024-08-08 12:42:04 +08:00
源文雨
e018aee705 fix(ci): debug log printing 2024-08-07 23:57:18 +08:00
源文雨
a0322b7d21 fix(ci): debug log printing 2024-08-07 23:55:42 +08:00
源文雨
d577ae2e16 feat(p2p): add tcp fast fail except 2024-08-07 23:03:06 +08:00
源文雨
aa6f5ee100 fix(p2p): wrong issub init 2024-08-07 22:56:02 +08:00
源文雨
b0667d5a45 fix(p2p): tcp conn early close 2024-08-06 23:40:02 +08:00
源文雨
1c258fcaa3 fix(p2p): tcp fast fail to main queue 2024-08-06 20:59:34 +08:00
源文雨
b71a0541bd feat(p2p): add more link to tcp 2024-08-06 20:30:33 +08:00
源文雨
ea768f88f9 fix(ci): no test files 2024-08-03 17:00:51 +08:00
源文雨
cf2daf9a3e fix: use nic mtu that minus packet header 2024-08-03 16:42:16 +08:00
源文雨
bafeb149be fix(link): mtu check 2024-08-03 15:51:45 +08:00
源文雨
7e14ca5168 feat(p2p): add socket options to tcp 2024-08-03 15:38:44 +08:00
源文雨
fa9abff1a8 chore: remove debug log at build 2024-08-03 15:32:31 +08:00
源文雨
08688b584b feat(p2p): change magic of tcp 2024-08-03 14:55:23 +08:00
源文雨
ecff222074 fix: wrong drop of same crc packet 2024-07-31 20:54:20 +08:00
源文雨
a4275beced fix(p2p): udplite checksum range 2024-07-31 16:06:22 +08:00
源文雨
aaafcdfa6c optimize: enlarge dup pkt gap time 2024-07-31 15:45:37 +08:00
源文雨
dd51f9f26f feat: add param DoublePacket 2024-07-31 15:21:02 +08:00
源文雨
574d1ccfc4 feat(p2p): change ip protocol to IPComp(0x6C) 2024-07-24 21:47:30 +08:00
36 changed files with 1332 additions and 503 deletions

View File

@@ -11,7 +11,7 @@ jobs:
- name: Set up Go 1.x - name: Set up Go 1.x
uses: actions/setup-go@master uses: actions/setup-go@master
with: with:
go-version: ^1.20 go-version: ^1.23
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@master uses: actions/checkout@master
@@ -19,11 +19,14 @@ jobs:
- name: Get dependencies - name: Get dependencies
run: go mod tidy run: go mod tidy
- name: Enable debug log
run: sed -i 's/false/true/g' config/global.go
- name: Build - name: Build
run: go build -v ./... run: go build -ldflags=-checklinkname=0 -v ./...
- name: Test - 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: lint:
name: Lint name: Lint
@@ -32,7 +35,7 @@ jobs:
- name: Set up Go 1.x - name: Set up Go 1.x
uses: actions/setup-go@master uses: actions/setup-go@master
with: with:
go-version: ^1.20 go-version: ^1.23
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@master uses: actions/checkout@master

View File

@@ -14,16 +14,16 @@ jobs:
steps: steps:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v2 uses: actions/setup-go@master
with: with:
go-version: ^1.20 go-version: ^1.23
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@v2 uses: actions/checkout@master
- name: Cache Go - name: Cache Go
id: cache id: cache
uses: actions/cache@v2 uses: actions/cache@v4
with: with:
# A list of files, directories, and wildcard patterns to cache and restore # A list of files, directories, and wildcard patterns to cache and restore
path: ~/go/pkg/mod path: ~/go/pkg/mod
@@ -33,25 +33,25 @@ jobs:
run: go mod tidy run: go mod tidy
- name: Build linux-x64 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - name: Upload binaries to release
uses: svenstaro/upload-release-action@v2 uses: svenstaro/upload-release-action@v2

View File

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

134
README.md
View File

@@ -41,13 +41,14 @@ EndPoint: 0.0.0.0:56789
MTU: 1504 MTU: 1504
SpeedLoop: 4096 SpeedLoop: 4096
Mask: 0x1234567890abcdef Mask: 0x1234567890abcdef
Base14: true
Peers: Peers:
- -
IP: "192.168.233.2" IP: "192.168.233.2"
PublicKey: 徯萃嵾爻燸攗窍褃冔蒔犡緇袿屿組待族砇嘀 PublicKey: 徯萃嵾爻燸攗窍褃冔蒔犡緇袿屿組待族砇嘀
PresharedKey: 瀸敀爅崾嘊嵜紼樴稍毯攣矐訷蟷扛嬋庩崛昀 PresharedKey: 瀸敀爅崾嘊嵜紼樴稍毯攣矐訷蟷扛嬋庩崛昀
EndPoint: 1.2.3.4:56789 EndPoint: 1.2.3.4:56789
AllowedIPs: ["192.168.233.2/32", "x192.168.233.3/32"] # allow trans to 192.168.233.3, but don not create route AllowedIPs: ["192.168.233.2/32", "x192.168.233.3/32"] # accept packets from 192.168.233.3, but don not create route
KeepAliveSeconds: 0 KeepAliveSeconds: 0
QueryList: ["192.168.233.3"] QueryList: ["192.168.233.3"]
MTU: 1400 MTU: 1400
@@ -60,8 +61,137 @@ Peers:
PublicKey: 牢喨粷詸衭譛浾蘹櫠砙杹蟫瑳叩刋橋経挵蘀 PublicKey: 牢喨粷詸衭譛浾蘹櫠砙杹蟫瑳叩刋橋経挵蘀
PresharedKey: 竅琚喫従痸告烈兇厕趭萨假蔛瀇譄施烸蝫瘀 PresharedKey: 竅琚喫従痸告烈兇厕趭萨假蔛瀇譄施烸蝫瘀
EndPoint: "" EndPoint: ""
AllowedIPs: ["192.168.233.3/32"] AllowedIPs: ["192.168.233.3/32", "y192.168.66.1/32"] # add route to 192.168.66.1 into inner route table but do not add it to system one
MTU: 752 MTU: 752
DoublePacket: true
KeepAliveSeconds: 0 KeepAliveSeconds: 0
AllowTrans: false 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

@@ -2,9 +2,9 @@ package config
import ( import (
"bytes" "bytes"
"log"
"os" "os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -15,9 +15,10 @@ type Config struct {
PrivateKey string `yaml:"PrivateKey"` PrivateKey string `yaml:"PrivateKey"`
Network string `yaml:"Network"` // Network udp, tcp or ws (WIP) Network string `yaml:"Network"` // Network udp, tcp or ws (WIP)
EndPoint string `yaml:"EndPoint"` EndPoint string `yaml:"EndPoint"`
MTU int64 `yaml:"MTU"` MTU int64 `yaml:"MTU"` // MTU of nic (will minus packet header len)
SpeedLoop uint16 `yaml:"SpeedLoop"` SpeedLoop uint16 `yaml:"SpeedLoop"`
Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一 Mask uint64 `yaml:"Mask"` // Mask 是异或报文所用掩码, 必须保证各端统一
Base14 bool `yaml:"Base14"` // Base14 是否将最终报文进行 base16384 编码后再发送
Peers []Peer `yaml:"Peers"` Peers []Peer `yaml:"Peers"`
} }
@@ -33,7 +34,8 @@ type Peer struct {
QuerySeconds int64 `yaml:"QuerySeconds"` QuerySeconds int64 `yaml:"QuerySeconds"`
AllowTrans bool `yaml:"AllowTrans"` AllowTrans bool `yaml:"AllowTrans"`
UseZstd bool `yaml:"UseZstd"` UseZstd bool `yaml:"UseZstd"`
MTU int64 `yaml:"MTU"` DoublePacket bool `yaml:"DoublePacket"`
MTU int64 `yaml:"MTU"` // MTU of PDU passed to p2p
MTURandomRange int64 `yaml:"MTURandomRange"` MTURandomRange int64 `yaml:"MTURandomRange"`
} }
@@ -41,11 +43,11 @@ type Peer struct {
func Parse(path string) (c Config) { func Parse(path string) (c Config) {
file, err := os.ReadFile(path) file, err := os.ReadFile(path)
if err != nil { if err != nil {
log.Fatal("open config file failed:", err) logrus.Fatal("open config file failed:", err)
} }
err = yaml.NewDecoder(bytes.NewReader(file)).Decode(&c) err = yaml.NewDecoder(bytes.NewReader(file)).Decode(&c)
if err != nil { if err != nil {
log.Fatal("invalid config file:", err) logrus.Fatal("invalid config file:", err)
} }
return return
} }

3
config/global.go Normal file
View File

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

34
gold/head/flags.go Normal file
View File

@@ -0,0 +1,34 @@
package head
import "encoding/binary"
type PacketFlags uint16
func (pf PacketFlags) IsValid() bool {
return pf&0x8000 == 0
}
func (pf PacketFlags) DontFrag() bool {
return pf&0x4000 == 0x4000
}
func (pf PacketFlags) NoFrag() bool {
return pf == 0x4000
}
func (pf PacketFlags) IsSingle() bool {
return pf == 0
}
func (pf PacketFlags) ZeroOffset() bool {
return pf&0x1fff == 0
}
func (pf PacketFlags) Offset() uint16 {
return uint16(pf << 3)
}
// Flags extract flags from raw data
func Flags(data []byte) PacketFlags {
return PacketFlags(binary.LittleEndian.Uint16(data[10:12]))
}

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

@@ -4,12 +4,13 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"errors" "errors"
"hash/crc64"
"net" "net"
"github.com/fumiama/WireGold/helper"
blake2b "github.com/fumiama/blake2b-simd" blake2b "github.com/fumiama/blake2b-simd"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/helper"
) )
const PacketHeadLen = 60 const PacketHeadLen = 60
@@ -19,37 +20,6 @@ var (
ErrDataLenLT60 = errors.New("data len < 60") ErrDataLenLT60 = errors.New("data len < 60")
) )
type PacketFlags uint16
func (pf PacketFlags) IsValid() bool {
return pf&0x8000 == 0
}
func (pf PacketFlags) DontFrag() bool {
return pf&0x4000 == 0x4000
}
func (pf PacketFlags) NoFrag() bool {
return pf == 0x4000
}
func (pf PacketFlags) IsSingle() bool {
return pf == 0
}
func (pf PacketFlags) ZeroOffset() bool {
return pf&0x1fff == 0
}
func (pf PacketFlags) Offset() uint16 {
return uint16(pf << 3)
}
// Flags extract flags from raw data
func Flags(data []byte) PacketFlags {
return PacketFlags(binary.LittleEndian.Uint16(data[10:12]))
}
// Packet 是发送和接收的最小单位 // Packet 是发送和接收的最小单位
type Packet struct { type Packet struct {
// idxdatsz len(Data) // idxdatsz len(Data)
@@ -75,7 +45,7 @@ type Packet struct {
// 生成时 Hash 全 0 // 生成时 Hash 全 0
// https://github.com/fumiama/blake2b-simd // https://github.com/fumiama/blake2b-simd
Hash [32]byte Hash [32]byte
// crc64 包头字段的 checksum 值,可以认为在一定时间内唯一 // crc64 包头字段的 checksum 值,可以认为在一定时间内唯一 (现已更改算法为 md5 但名字未变)
crc64 uint64 crc64 uint64
// data 承载的数据 // data 承载的数据
data []byte data []byte
@@ -89,7 +59,6 @@ type Packet struct {
// NewPacket 生成一个新包 // NewPacket 生成一个新包
func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) (p *Packet) { func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) (p *Packet) {
// logrus.Debugln("[packet] new: [proto:", proto, ", srcport:", srcPort, ", dstport:", dstPort, ", dst:", dst, ", data:", data)
p = SelectPacket() p = SelectPacket()
p.Proto = proto p.Proto = proto
p.TTL = 16 p.TTL = 16
@@ -107,8 +76,8 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
err = ErrDataLenLT60 err = ErrDataLenLT60
return return
} }
p.crc64 = binary.LittleEndian.Uint64(data[52:PacketHeadLen]) p.crc64 = CRC64(data)
if crc64.Checksum(data[:52], crc64.MakeTable(crc64.ISO)) != p.crc64 { if CalcCRC64(data) != p.crc64 {
err = ErrBadCRCChecksum err = ErrBadCRCChecksum
return return
} }
@@ -147,7 +116,9 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
if p.rembytes > 0 { if p.rembytes > 0 {
p.rembytes -= copy(p.data[flags.Offset():], data[PacketHeadLen:]) p.rembytes -= copy(p.data[flags.Offset():], data[PacketHeadLen:])
logrus.Debugln("[packet] copied frag", hex.EncodeToString(p.Hash[:]), "rembytes:", p.rembytes) if config.ShowDebugLog {
logrus.Debugln("[packet] copied frag", hex.EncodeToString(p.Hash[:]), "rembytes:", p.rembytes)
}
} }
complete = p.rembytes == 0 complete = p.rembytes == 0
@@ -155,14 +126,15 @@ func (p *Packet) Unmarshal(data []byte) (complete bool, err error) {
return return
} }
// DecreaseAndGetTTL TTL 自减后返回
func (p *Packet) DecreaseAndGetTTL() uint8 {
p.TTL--
return p.TTL
}
// Marshal 将自身数据编码为 []byte // Marshal 将自身数据编码为 []byte
// offset 必须为 8 的倍数,表示偏移的 8 位 // offset 必须为 8 的倍数,表示偏移的 8 位
func (p *Packet) Marshal(src net.IP, teatype uint8, additional uint16, datasz uint32, offset uint16, dontfrag, hasmore bool) ([]byte, func()) { func (p *Packet) Marshal(src net.IP, teatype uint8, additional uint16, datasz uint32, offset uint16, dontfrag, hasmore bool) ([]byte, func()) {
p.TTL--
if p.TTL == 0 {
return nil, nil
}
if src != nil { if src != nil {
p.Src = src p.Src = src
p.idxdatsz = (uint32(teatype) << 27) | (uint32(additional&0x07ff) << 16) | datasz&0xffff p.idxdatsz = (uint32(teatype) << 27) | (uint32(additional&0x07ff) << 16) | datasz&0xffff
@@ -186,7 +158,8 @@ func (p *Packet) Marshal(src net.IP, teatype uint8, additional uint16, datasz ui
w.Write(p.Src.To4()) w.Write(p.Src.To4())
w.Write(p.Dst.To4()) w.Write(p.Dst.To4())
w.Write(p.Hash[:]) w.Write(p.Hash[:])
w.WriteUInt64(crc64.Checksum(w.Bytes(), crc64.MakeTable(crc64.ISO))) p.crc64 = CalcCRC64(w.Bytes())
w.WriteUInt64(p.crc64)
w.Write(p.Body()) w.Write(p.Body())
}) })
} }
@@ -196,10 +169,13 @@ func (p *Packet) FillHash() {
h := blake2b.New256() h := blake2b.New256()
_, err := h.Write(p.Body()) _, err := h.Write(p.Body())
if err != nil { if err != nil {
logrus.Error("[packet] err when fill hash:", err) logrus.Errorln("[packet] err when fill hash:", err)
return return
} }
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(h.Sum(p.Hash[:0]))) hsh := h.Sum(p.Hash[:0])
if config.ShowDebugLog {
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(hsh))
}
} }
// IsVaildHash 验证 packet 合法性 // IsVaildHash 验证 packet 合法性
@@ -207,12 +183,15 @@ func (p *Packet) IsVaildHash() bool {
h := blake2b.New256() h := blake2b.New256()
_, err := h.Write(p.Body()) _, err := h.Write(p.Body())
if err != nil { if err != nil {
logrus.Error("[packet] err when check hash:", err) logrus.Errorln("[packet] err when check hash:", err)
return false return false
} }
var sum [32]byte var sum [32]byte
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(h.Sum(sum[:0]))) _ = h.Sum(sum[:0])
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:])) if config.ShowDebugLog {
logrus.Debugln("[packet] sum calulated:", hex.EncodeToString(sum[:]))
logrus.Debugln("[packet] sum in packet:", hex.EncodeToString(p.Hash[:]))
}
return sum == p.Hash return sum == p.Hash
} }
@@ -236,6 +215,10 @@ func (p *Packet) Put() {
PutPacket(p) PutPacket(p)
} }
func (p *Packet) CRC64() uint64 {
return p.crc64
}
// Body returns data // Body returns data
func (p *Packet) Body() []byte { func (p *Packet) Body() []byte {
return p.data[p.a:p.b] return p.data[p.a:p.b]
@@ -279,3 +262,10 @@ func (p *Packet) Copy() *Packet {
newp.buffered = false newp.buffered = false
return newp return newp
} }
func (p *Packet) CopyWithBody() *Packet {
newp := p.Copy()
newp.data = helper.MakeBytes(len(p.data))
copy(newp.data, p.data)
return newp
}

22
gold/head/raw.go Normal file
View File

@@ -0,0 +1,22 @@
package head
import (
"crypto/md5"
"encoding/binary"
)
// CRC64 extract packet header checksum
func CRC64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data[52:PacketHeadLen])
}
// CalcCRC64 calculate packet header checksum
func CalcCRC64(data []byte) uint64 {
m := md5.Sum(data[:52])
return binary.LittleEndian.Uint64(m[:8])
}
// Hash extract 32 bytes blake2b hash from raw bytes
func Hash(data []byte) []byte {
return data[20:52]
}

View File

@@ -121,57 +121,47 @@ func decode(aead cipher.AEAD, additional uint16, b []byte) ([]byte, error) {
} }
// xorenc 按 8 字节, 以初始 m.mask 循环异或编码 data // xorenc 按 8 字节, 以初始 m.mask 循环异或编码 data
func (m *Me) xorenc(data []byte) []byte { func (m *Me) xorenc(data []byte, seq uint32) []byte {
batchsz := len(data) / 8 batchsz := len(data) / 8
remain := len(data) % 8 remain := len(data) % 8
sum := m.mask sum := m.mask
if remain > 0 { newdat := helper.MakeBytes(8 + batchsz*8 + 8) // seqrand dat tail
var buf [8]byte binary.LittleEndian.PutUint32(newdat[:4], seq)
p := batchsz * 8 _, _ = rand.Read(newdat[4:8]) // seqrand
copy(buf[:], data[p:]) sum ^= binary.LittleEndian.Uint64(newdat[:8]) // init from seqrand
sum ^= binary.LittleEndian.Uint64(buf[:]) binary.LittleEndian.PutUint64(newdat[:8], sum)
binary.LittleEndian.PutUint64(buf[:], sum) for i := 0; i < batchsz; i++ { // range on batch data
copy(data[p:], buf[:])
}
for i := batchsz - 1; i >= 0; i-- {
a := i * 8 a := i * 8
b := (i + 1) * 8 b := (i + 1) * 8
sum ^= binary.LittleEndian.Uint64(data[a:b]) sum ^= binary.LittleEndian.Uint64(data[a:b])
binary.LittleEndian.PutUint64(data[a:b], sum) binary.LittleEndian.PutUint64(newdat[a+8:b+8], sum)
} }
return data 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 // xordec 按 8 字节, 以初始 m.mask 循环异或解码 data
func (m *Me) xordec(data []byte) []byte { func (m *Me) xordec(data []byte) (uint32, []byte) {
batchsz := len(data) / 8 if len(data) < 16 || len(data)%8 != 0 {
remain := len(data) % 8 return 0, nil
this := uint64(0)
next := uint64(0)
if len(data) >= 8 {
next = binary.LittleEndian.Uint64(data[:8])
} }
for i := 0; i < batchsz-1; i++ { batchsz := len(data) / 8
sum := m.mask
for i := 0; i < batchsz; i++ {
a := i * 8 a := i * 8
b := (i + 1) * 8 b := (i + 1) * 8
this = next x := binary.LittleEndian.Uint64(data[a:b])
next = binary.LittleEndian.Uint64(data[a+8 : b+8]) sum ^= x
binary.LittleEndian.PutUint64(data[a:b], this^next) binary.LittleEndian.PutUint64(data[a:b], sum)
sum = x
} }
if remain > 0 { remain := data[len(data)-1]
var buf [8]byte if remain >= 8 {
a := (batchsz - 1) * 8 return 0, nil
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)
} }
return data 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) buf := make([]byte, 4096)
buf2 := make([]byte, 4096) buf2 := make([]byte, 4096)
for i := 1; i < 4096; i++ { for i := 0; i < 4096; i++ {
data := buf[:i] data := buf[:i]
orgdata := buf2[:i] orgdata := buf2[:i]
r1 := bytes.NewBuffer(data[:0]) r1 := bytes.NewBuffer(data[:0])
@@ -27,8 +27,12 @@ func TestXOR(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(m.xordec(m.xorenc(r1.Bytes())), r2.Bytes()) { seq, dec := m.xordec(m.xorenc(r1.Bytes(), uint32(i)))
t.Fatal("unexpected xor at", 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" "errors"
"net" "net"
"sync/atomic" "sync/atomic"
"time"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/p2p" "github.com/fumiama/WireGold/gold/p2p"
@@ -38,24 +39,20 @@ type Link struct {
keys [32]cipher.AEAD keys [32]cipher.AEAD
// 本机信息 // 本机信息
me *Me me *Me
// 连接的状态,详见下方 const // 最后一次收到报文的时间
status int8 lastalive *time.Time
// 是否允许转发 // 是否允许转发
allowtrans bool allowtrans bool
// 是否对数据进行 zstd 压缩 // 是否对数据进行 zstd 压缩
usezstd bool usezstd bool
// 是否采用双倍发包对抗强丢包
doublepacket bool
// udp 数据包的最大大小 // udp 数据包的最大大小
mtu uint16 mtu uint16
// 随机放缩 mtu 范围 (只减不增) // 随机放缩 mtu 范围 (只减不增)
mturandomrange uint16 mturandomrange uint16
} }
const (
LINK_STATUS_DOWN = iota
LINK_STATUS_HALFUP
LINK_STATUS_UP
)
// Connect 初始化与 peer 的连接 // Connect 初始化与 peer 的连接
func (m *Me) Connect(peer string) (*Link, error) { func (m *Me) Connect(peer string) (*Link, error) {
p, ok := m.IsInPeer(net.ParseIP(peer).String()) p, ok := m.IsInPeer(net.ParseIP(peer).String())
@@ -67,7 +64,7 @@ func (m *Me) Connect(peer string) (*Link, error) {
// Close 关闭到 peer 的连接 // Close 关闭到 peer 的连接
func (l *Link) Close() { func (l *Link) Close() {
l.status = LINK_STATUS_DOWN l.Destroy()
} }
// Destroy 从 connections 移除 peer // Destroy 从 connections 移除 peer

View File

@@ -8,11 +8,14 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
"sync/atomic"
"time" "time"
"unsafe"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/p2p" "github.com/fumiama/WireGold/gold/p2p"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
@@ -20,6 +23,52 @@ import (
const lstnbufgragsz = 65536 const lstnbufgragsz = 65536
type lstnq struct {
index int
addr p2p.EndPoint
buf []byte
}
type listenqueue chan lstnq
func (q listenqueue) listen(m *Me, hasntfinished []sync.Mutex) {
recvtotlcnt := uint64(0)
recvloopcnt := uint16(0)
recvlooptime := time.Now().UnixMilli()
for lstn := range q {
recvtotlcnt += uint64(len(lstn.buf))
recvloopcnt++
if recvloopcnt%m.speedloop == 0 {
now := time.Now().UnixMilli()
logrus.Infof("[listen] queue recv avg speed: %.2f KB/s", float64(recvtotlcnt)/float64(now-recvlooptime))
recvtotlcnt = 0
recvlooptime = now
}
packet := m.wait(lstn.buf[:len(lstn.buf):lstnbufgragsz])
if packet == nil {
if lstn.index < 0 {
if config.ShowDebugLog {
logrus.Debugln("[listen] queue waiting")
}
helper.PutBytes(lstn.buf)
continue
}
if config.ShowDebugLog {
logrus.Debugln("[listen] queue waiting, unlock index", lstn.index)
}
hasntfinished[lstn.index].Unlock()
continue
}
if lstn.index >= 0 {
go m.dispatch(packet, lstn.addr, lstn.index, hasntfinished[lstn.index].Unlock)
} else {
go m.dispatch(packet, lstn.addr, lstn.index, func() {
helper.PutBytes(lstn.buf)
})
}
}
}
// 监听本机 endpoint // 监听本机 endpoint
func (m *Me) listen() (conn p2p.Conn, err error) { func (m *Me) listen() (conn p2p.Conn, err error) {
conn, err = m.ep.Listen() conn, err = m.ep.Listen()
@@ -29,27 +78,39 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
m.ep = conn.LocalAddr() m.ep = conn.LocalAddr()
logrus.Infoln("[listen] at", m.ep) logrus.Infoln("[listen] at", m.ep)
go func() { go func() {
recvtotlcnt := uint64(0) n := uint(runtime.NumCPU())
recvloopcnt := uint16(0)
recvlooptime := time.Now().UnixMilli()
n := runtime.NumCPU()
if n > 64 { if n > 64 {
n = 64 // 只用最多 64 核 n = 64 // 只用最多 64 核
} }
logrus.Infoln("[listen] use cpu num:", n) logrus.Infoln("[listen] use cpu num:", n)
listenbuff := make([]byte, lstnbufgragsz*n) listenbuf := make([]byte, lstnbufgragsz*n)
hasntfinished := make([]sync.Mutex, n) hasntfinished := make([]sync.Mutex, n)
for i := 0; err == nil; i++ { q := make(listenqueue, n)
i %= n defer close(q)
go q.listen(m, hasntfinished)
for {
usenewbuf := false
i := uint(0)
for !hasntfinished[i].TryLock() { for !hasntfinished[i].TryLock() {
i++ i++
i %= n i %= n
if i == 0 { // looked up a full round if i == 0 { // looked up a full round, make a new buf
time.Sleep(time.Millisecond * 10) usenewbuf = true
if config.ShowDebugLog {
logrus.Debugln("[listen] use new buf")
}
break
} }
} }
logrus.Debugln("[listen] lock index", i) if config.ShowDebugLog && !usenewbuf {
lbf := listenbuff[i*lstnbufgragsz : (i+1)*lstnbufgragsz] logrus.Debugln("[listen] lock index", i)
}
var lbf []byte
if usenewbuf {
lbf = helper.MakeBytes(lstnbufgragsz)
} else {
lbf = listenbuf[i*lstnbufgragsz : (i+1)*lstnbufgragsz]
}
n, addr, err := conn.ReadFromPeer(lbf) n, addr, err := conn.ReadFromPeer(lbf)
if m.connections == nil || errors.Is(err, net.ErrClosed) { if m.connections == nil || errors.Is(err, net.ErrClosed) {
logrus.Warnln("[listen] quit listening") logrus.Warnln("[listen] quit listening")
@@ -62,27 +123,30 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
logrus.Errorln("[listen] reconnect udp err:", err) logrus.Errorln("[listen] reconnect udp err:", err)
return return
} }
logrus.Debugln("[listen] unlock index", i) if !usenewbuf {
hasntfinished[i].Unlock() if config.ShowDebugLog {
i-- logrus.Debugln("[listen] unlock index", i)
}
hasntfinished[i].Unlock()
i--
}
continue continue
} }
recvtotlcnt += uint64(n) if n <= 0 {
recvloopcnt++ if config.ShowDebugLog {
if recvloopcnt%m.speedloop == 0 { logrus.Debugln("[listen] unexpected read n =", n)
now := time.Now().UnixMilli() }
logrus.Infof("[listen] recv avg speed: %.2f KB/s", float64(recvtotlcnt)/float64(now-recvlooptime))
recvtotlcnt = 0
recvlooptime = now
}
packet := m.wait(lbf[:n:lstnbufgragsz])
if packet == nil {
logrus.Debugln("[listen] waiting, unlock index", i)
hasntfinished[i].Unlock()
i--
continue continue
} }
go m.dispatch(packet, addr, i, hasntfinished[i].Unlock) lq := lstnq{
index: -1,
addr: addr,
buf: lbf[:n],
}
if !usenewbuf {
lq.index = int(i)
}
q <- lq
} }
}() }()
return return
@@ -90,8 +154,10 @@ func (m *Me) listen() (conn p2p.Conn, err error) {
func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish func()) { func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish func()) {
defer finish() defer finish()
defer logrus.Debugln("[listen] dispatched, unlock index", index) if config.ShowDebugLog {
logrus.Debugln("[listen] start dispatching index", index) defer logrus.Debugln("[listen] dispatched, unlock index", index)
logrus.Debugln("[listen] start dispatching index", index)
}
r := packet.Len() - packet.BodyLen() r := packet.Len() - packet.BodyLen()
if r > 0 { if r > 0 {
logrus.Warnln("[listen] @", index, "packet from endpoint", addr, "len", packet.BodyLen(), "is smaller than it declared len", packet.Len(), ", drop it") logrus.Warnln("[listen] @", index, "packet from endpoint", addr, "len", packet.BodyLen(), "is smaller than it declared len", packet.Len(), ", drop it")
@@ -99,21 +165,25 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
return return
} }
p, ok := m.IsInPeer(packet.Src.String()) p, ok := m.IsInPeer(packet.Src.String())
logrus.Debugln("[listen] @", index, "recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst) if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "recv from endpoint", addr, "src", packet.Src, "dst", packet.Dst)
}
if !ok { if !ok {
logrus.Warnln("[listen] @", index, "packet from", packet.Src, "to", packet.Dst, "is refused") logrus.Warnln("[listen] @", index, "packet from", packet.Src, "to", packet.Dst, "is refused")
packet.Put() packet.Put()
return return
} }
if p.endpoint == nil || !p.endpoint.Euqal(addr) { if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
if m.ep.Network() == "udp" { if m.ep.Network() == "tcp" && !addr.Euqal(p.endpoint) {
logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String()) logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr 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()) logrus.Infoln("[listen] @", index, "set endpoint of peer", p.peerip, "to", addr.String())
p.endpoint = addr p.endpoint = addr
} }
} }
now := time.Now()
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&p.lastalive)), unsafe.Pointer(&now))
switch { switch {
case p.IsToMe(packet.Dst): case p.IsToMe(packet.Dst):
if !p.Accept(packet.Src) { if !p.Accept(packet.Src) {
@@ -125,7 +195,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
var err error var err error
data, err := p.Decode(packet.CipherIndex(), addt, packet.Body()) data, err := p.Decode(packet.CipherIndex(), addt, packet.Body())
if err != nil { if err != nil {
logrus.Debugln("[listen] @", index, "drop invalid packet key idx:", packet.CipherIndex(), "addt:", addt, "err:", err) if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "drop invalid packet key idx:", packet.CipherIndex(), "addt:", addt, "err:", err)
}
packet.Put() packet.Put()
return return
} }
@@ -137,31 +209,35 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
_, err = io.Copy(w, dec) _, err = io.Copy(w, dec)
dec.Close() dec.Close()
if err != nil { if err != nil {
logrus.Debugln("[listen] @", index, "drop invalid zstd packet:", err) if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "drop invalid zstd packet:", err)
}
packet.Put() packet.Put()
return return
} }
packet.SetBody(w.Bytes(), true) packet.SetBody(w.Bytes(), true)
} }
if !packet.IsVaildHash() { if !packet.IsVaildHash() {
logrus.Debugln("[listen] @", index, "drop invalid hash packet") if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "drop invalid hash packet")
}
packet.Put() packet.Put()
return return
} }
switch packet.Proto { switch packet.Proto {
case head.ProtoHello: case head.ProtoHello:
switch p.status { switch {
case LINK_STATUS_DOWN: case len(packet.Body()) == 0:
n, err := p.WriteAndPut(head.NewPacket(head.ProtoHello, m.SrcPort(), p.peerip, m.DstPort(), nil), false) 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 err == nil {
logrus.Debugln("[listen] @", index, "send", n, "bytes hello ack packet") logrus.Infoln("[listen] @", index, "recv hello, send", n, "bytes hello ack packet")
p.status = LINK_STATUS_HALFUP
} else { } else {
logrus.Errorln("[listen] @", index, "send hello ack packet error:", err) logrus.Errorln("[listen] @", index, "send hello ack packet error:", err)
} }
case LINK_STATUS_HALFUP: default:
p.status = LINK_STATUS_UP logrus.Infoln("[listen] @", index, "recv hello ack packet, do nothing")
case LINK_STATUS_UP:
} }
packet.Put() packet.Put()
case head.ProtoNotify: case head.ProtoNotify:
@@ -174,17 +250,19 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
packet.Put() packet.Put()
case head.ProtoData: case head.ProtoData:
if p.pipe != nil { if p.pipe != nil {
p.pipe <- packet p.pipe <- packet.CopyWithBody()
logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip) if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip)
}
} else { } else {
_, err := m.nic.Write(packet.Body()) _, err := m.nic.Write(packet.Body())
if err != nil { if err != nil {
logrus.Errorln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic err:", err) logrus.Errorln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic err:", err)
} else { } else if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic") logrus.Debugln("[listen] @", index, "deliver", packet.BodyLen(), "bytes data to nic")
} }
packet.Put()
} }
packet.Put()
default: default:
logrus.Warnln("[listen] @", index, "recv unknown proto:", packet.Proto) logrus.Warnln("[listen] @", index, "recv unknown proto:", packet.Proto)
packet.Put() packet.Put()
@@ -204,7 +282,9 @@ func (m *Me) dispatch(packet *head.Packet, addr p2p.EndPoint, index int, finish
} }
n, err := lnk.WriteAndPut(packet, true) n, err := lnk.WriteAndPut(packet, true)
if err == nil { if err == nil {
logrus.Debugln("[listen] @", index, "trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort))) if config.ShowDebugLog {
logrus.Debugln("[listen] @", index, "trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
}
} else { } else {
logrus.Errorln("[listen] @", index, "trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err) logrus.Errorln("[listen] @", index, "trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err)
} }

View File

@@ -13,13 +13,17 @@ import (
"github.com/fumiama/water/waterutil" "github.com/fumiama/water/waterutil"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/p2p" "github.com/fumiama/WireGold/gold/p2p"
"github.com/fumiama/WireGold/helper"
"github.com/fumiama/WireGold/lower" "github.com/fumiama/WireGold/lower"
) )
// Me 是本机的抽象 // Me 是本机的抽象
type Me struct { type Me struct {
// 用于自我重连
cfg *MyConfig
// 本机私钥 // 本机私钥
// 利用 Curve25519 生成 // 利用 Curve25519 生成
// https://pkg.go.dev/golang.org/x/crypto/curve25519 // https://pkg.go.dev/golang.org/x/crypto/curve25519
@@ -38,17 +42,19 @@ type Me struct {
// 本机监听的连接端点, 也用于向对端直接发送报文 // 本机监听的连接端点, 也用于向对端直接发送报文
conn p2p.Conn conn p2p.Conn
// 本机网卡 // 本机网卡
nic lower.NICIO nic *lower.NICIO
// 本机路由表 // 本机路由表
router *Router router *Router
// 本机未接收完全分片池 // 本机未接收完全分片池
recving *ttl.Cache[[32]byte, *head.Packet] recving *ttl.Cache[uint64, *head.Packet]
// 抗重放攻击记录池 // 抗重放攻击记录池
recved *ttl.Cache[uint64, bool] recved *ttl.Cache[uint64, bool]
// 本机上层配置 // 本机上层配置
srcport, dstport, mtu, speedloop uint16 srcport, dstport, mtu, speedloop uint16
// 报头掩码 // 报头掩码
mask uint64 mask uint64
// 是否进行 base16384 编码
base14 bool
// 本机网络端点初始化配置 // 本机网络端点初始化配置
networkconfigs []any networkconfigs []any
} }
@@ -59,13 +65,21 @@ type MyConfig struct {
Network string Network string
NetworkConfigs []any NetworkConfigs []any
PrivateKey *[32]byte PrivateKey *[32]byte
NIC lower.NICIO NICConfig *NICConfig
SrcPort, DstPort, MTU, SpeedLoop uint16 SrcPort, DstPort, MTU, SpeedLoop uint16
Mask uint64 Mask uint64
Base14 bool
}
type NICConfig struct {
IP net.IP
SubNet *net.IPNet
CIDRs []string
} }
// NewMe 设置本机参数 // NewMe 设置本机参数
func NewMe(cfg *MyConfig) (m Me) { func NewMe(cfg *MyConfig) (m Me) {
m.cfg = cfg
m.privKey = *cfg.PrivateKey m.privKey = *cfg.PrivateKey
var err error var err error
nw := cfg.Network nw := cfg.Network
@@ -83,12 +97,15 @@ func NewMe(cfg *MyConfig) (m Me) {
} }
m.me = ip m.me = ip
m.subnet = *cidr m.subnet = *cidr
m.speedloop = cfg.SpeedLoop
if m.speedloop == 0 {
m.speedloop = 4096
}
m.conn, err = m.listen() m.conn, err = m.listen()
if err != nil { if err != nil {
panic(err) panic(err)
} }
m.connections = make(map[string]*Link) m.connections = make(map[string]*Link)
m.nic = cfg.NIC
m.router = &Router{ m.router = &Router{
list: make([]*net.IPNet, 1, 16), list: make([]*net.IPNet, 1, 16),
table: make(map[string]*Link, 16), table: make(map[string]*Link, 16),
@@ -97,20 +114,50 @@ func NewMe(cfg *MyConfig) (m Me) {
m.router.SetDefault(nil) m.router.SetDefault(nil)
m.srcport = cfg.SrcPort m.srcport = cfg.SrcPort
m.dstport = cfg.DstPort m.dstport = cfg.DstPort
m.mtu = cfg.MTU & 0xfff8 m.mtu = (cfg.MTU - head.PacketHeadLen) & 0xfff8
m.speedloop = cfg.SpeedLoop if cfg.NICConfig != nil {
if m.speedloop == 0 { m.nic = lower.NewNIC(
m.speedloop = 4096 cfg.NICConfig.IP, cfg.NICConfig.SubNet,
strconv.FormatUint(uint64(m.MTU()), 10), cfg.NICConfig.CIDRs...,
)
} }
m.mask = cfg.Mask m.mask = cfg.Mask
m.base14 = cfg.Base14
var buf [8]byte var buf [8]byte
binary.BigEndian.PutUint64(buf[:], m.mask) binary.BigEndian.PutUint64(buf[:], m.mask)
logrus.Infoln("[me] xor mask", hex.EncodeToString(buf[:])) logrus.Infoln("[me] xor mask", hex.EncodeToString(buf[:]))
m.recving = ttl.NewCache[[32]byte, *head.Packet](time.Second * 30) m.recving = ttl.NewCache[uint64, *head.Packet](time.Second * 30)
m.recved = ttl.NewCache[uint64, bool](time.Second * 30) m.recved = ttl.NewCache[uint64, bool](time.Second * 30)
return 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 { func (m *Me) SrcPort() uint16 {
return m.srcport return m.srcport
} }
@@ -129,7 +176,7 @@ func (m *Me) EndPoint() p2p.EndPoint {
func (m *Me) Close() error { func (m *Me) Close() error {
m.connections = nil m.connections = nil
if m.conn != nil { if helper.IsNonNilInterface(m.conn) {
_ = m.conn.Close() _ = m.conn.Close()
m.conn = nil m.conn = nil
} }
@@ -151,7 +198,9 @@ func (m *Me) Close() error {
func (m *Me) Write(packet []byte) (n int, err error) { func (m *Me) Write(packet []byte) (n int, err error) {
n = m.sendAllSameDst(packet) n = m.sendAllSameDst(packet)
logrus.Debugln("[me] writer ate", len(packet), "bytes, remain", len(packet)-n, "bytes") if config.ShowDebugLog {
logrus.Debugln("[me] writer ate", len(packet), "bytes, remain", len(packet)-n, "bytes")
}
return return
} }
@@ -180,7 +229,9 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
} }
n += pktl n += pktl
rem = packet[n:] rem = packet[n:]
logrus.Debugln("[me] skip to send", len(packet), "bytes ipv6 packet") if config.ShowDebugLog {
logrus.Debugln("[me] skip to send", len(packet), "bytes ipv6 packet")
}
} }
if len(rem) == 0 || !waterutil.IsIPv4(rem) { if len(rem) == 0 || !waterutil.IsIPv4(rem) {
logrus.Warnln("[me] skip to send", len(packet), "bytes full packet") logrus.Warnln("[me] skip to send", len(packet), "bytes full packet")
@@ -193,12 +244,16 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
for len(ptr) > 20 && p.issame(ptr) { for len(ptr) > 20 && p.issame(ptr) {
totl := waterutil.IPv4TotalLength(ptr) totl := waterutil.IPv4TotalLength(ptr)
if int(totl) > len(ptr) { if int(totl) > len(ptr) {
logrus.Debugln("[me] wrap got invalid totl, break") if config.ShowDebugLog {
logrus.Debugln("[me] wrap got invalid totl, break")
}
break break
} }
i += int(totl) i += int(totl)
ptr = rem[i:] ptr = rem[i:]
logrus.Debugln("[me] wrap", totl, "bytes packet to send together") if config.ShowDebugLog {
logrus.Debugln("[me] wrap", totl, "bytes packet to send together")
}
} }
if i == 0 { if i == 0 {
return return
@@ -207,9 +262,13 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
packet = rem[:i] packet = rem[:i]
rem = rem[i:] rem = rem[i:]
dst := waterutil.IPv4Destination(packet) dst := waterutil.IPv4Destination(packet)
logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), "remain:", len(rem), "bytes") if config.ShowDebugLog {
logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), "remain:", len(rem), "bytes")
}
if m.me.Equal(dst) { // is to myself, write to nic (pipe not allow loopback) if m.me.Equal(dst) { // is to myself, write to nic (pipe not allow loopback)
logrus.Debugln("[me] loopback packet") if config.ShowDebugLog {
logrus.Debugln("[me] loopback packet")
}
_, err := m.nic.Write(packet) _, err := m.nic.Write(packet)
if err != nil { if err != nil {
logrus.Warnln("[me] write to loopback err:", err) logrus.Warnln("[me] write to loopback err:", err)
@@ -221,9 +280,14 @@ func (m *Me) sendAllSameDst(packet []byte) (n int) {
logrus.Warnln("[me] drop packet to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), ": nil nexthop") logrus.Warnln("[me] drop packet to", dst.String()+":"+strconv.Itoa(int(m.DstPort())), ": nil nexthop")
return return
} }
_, err := lnk.WriteAndPut(head.NewPacket(head.ProtoData, m.SrcPort(), lnk.peerip, m.DstPort(), packet), false) pcp := helper.MakeBytes(len(packet))
if err != nil { copy(pcp, packet)
logrus.Warnln("[me] write to peer", lnk.peerip, "err:", err) go func(packet []byte) {
} defer helper.PutBytes(packet)
_, err := lnk.WriteAndPut(head.NewPacket(head.ProtoData, m.SrcPort(), lnk.peerip, m.DstPort(), packet), false)
if err != nil {
logrus.Warnln("[me] write to peer", lnk.peerip, "err:", err)
}
}(pcp)
return return
} }

View File

@@ -2,10 +2,13 @@ package link
import ( import (
"encoding/json" "encoding/json"
"sync/atomic"
"time" "time"
"unsafe"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/p2p" "github.com/fumiama/WireGold/gold/p2p"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
@@ -22,11 +25,21 @@ func (l *Link) keepAlive(dur int64) {
if l.me.connections == nil { if l.me.connections == nil {
return 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 { if err == nil {
logrus.Infoln("[nat] send", n, "bytes keep alive packet") logrus.Infoln("[nat] send", n, "bytes keep alive packet")
} else { } else {
logrus.Errorln("[nat] send keep alive packet error:", err) logrus.Warnln("[nat] send keep alive packet error:", err)
} }
} }
} }
@@ -56,14 +69,16 @@ func (l *Link) onNotify(packet []byte) {
if err == nil { if err == nil {
p, ok := l.me.IsInPeer(peer) p, ok := l.me.IsInPeer(peer)
if ok { if ok {
if !p.endpoint.Euqal(addr) { if helper.IsNilInterface(p.endpoint) || !p.endpoint.Euqal(addr) {
p.endpoint = addr p.endpoint = addr
logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep) logrus.Infoln("[nat] notify set ep of peer", peer, "to", ep)
} }
continue continue
} }
} }
logrus.Debugln("[nat] notify drop invalid peer:", peer, "ep:", ep) if config.ShowDebugLog {
logrus.Debugln("[nat] notify drop invalid peer:", peer, "ep:", ep)
}
} }
} }
@@ -94,6 +109,9 @@ func (l *Link) onQuery(packet []byte) {
lnk, ok := l.me.IsInPeer(p) lnk, ok := l.me.IsInPeer(p)
eps := "" eps := ""
if l.me.ep.Network() == "udp" { // udp has real p2p if l.me.ep.Network() == "udp" { // udp has real p2p
if helper.IsNilInterface(lnk.endpoint) {
continue
}
eps = lnk.endpoint.String() eps = lnk.endpoint.String()
} }
if eps == "" { if eps == "" {
@@ -102,7 +120,7 @@ func (l *Link) onQuery(packet []byte) {
if eps == "" { if eps == "" {
continue continue
} }
if ok { if ok && helper.IsNonNilInterface(lnk.endpoint) {
notify[p] = [2]string{ notify[p] = [2]string{
lnk.endpoint.Network(), lnk.endpoint.Network(),
eps, eps,

View File

@@ -22,6 +22,7 @@ type PeerConfig struct {
MTURandomRange uint16 MTURandomRange uint16
AllowTrans, NoPipe bool AllowTrans, NoPipe bool
UseZstd bool UseZstd bool
DoublePacket bool
} }
// AddPeer 添加一个 peer // AddPeer 添加一个 peer
@@ -32,7 +33,7 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
if ok { if ok {
return return
} }
if cfg.MTU == 0 || (m.mtu != 0 && cfg.MTU > m.mtu) { if cfg.MTU == 0 {
panic("invalid mtu for peer " + cfg.PeerIP) panic("invalid mtu for peer " + cfg.PeerIP)
} }
l = &Link{ l = &Link{
@@ -41,6 +42,7 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
rawep: cfg.EndPoint, rawep: cfg.EndPoint,
allowtrans: cfg.AllowTrans, allowtrans: cfg.AllowTrans,
usezstd: cfg.UseZstd, usezstd: cfg.UseZstd,
doublepacket: cfg.DoublePacket,
me: m, me: m,
mtu: cfg.MTU, mtu: cfg.MTU,
mturandomrange: cfg.MTURandomRange, mturandomrange: cfg.MTURandomRange,
@@ -87,7 +89,8 @@ func (m *Me) AddPeer(cfg *PeerConfig) (l *Link) {
continue continue
} }
noroute := ipnet[0] == 'x' noroute := ipnet[0] == 'x'
if noroute { innerroute := ipnet[0] == 'y'
if noroute || innerroute {
ipnet = ipnet[1:] ipnet = ipnet[1:]
if len(ipnet) == 0 { if len(ipnet) == 0 {
continue continue

View File

@@ -3,10 +3,12 @@ package link
import ( import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"hash/crc64"
"strconv" "strconv"
"unsafe"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
base14 "github.com/fumiama/go-base16384"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -17,6 +19,9 @@ func (l *Link) Read() *head.Packet {
func (m *Me) wait(data []byte) *head.Packet { func (m *Me) wait(data []byte) *head.Packet {
if len(data) < head.PacketHeadLen { // not a valid packet if len(data) < head.PacketHeadLen { // not a valid packet
if config.ShowDebugLog {
logrus.Debugln("[recv] invalid data len", len(data))
}
return nil return nil
} }
bound := 64 bound := 64
@@ -25,56 +30,106 @@ func (m *Me) wait(data []byte) *head.Packet {
bound = len(data) bound = len(data)
endl = "." endl = "."
} }
logrus.Debugln("[recv] data bytes", hex.EncodeToString(data[:bound]), endl) if config.ShowDebugLog {
data = m.xordec(data) logrus.Debugln("[recv] data bytes, len", len(data), "val", hex.EncodeToString(data[:bound]), endl)
logrus.Debugln("[recv] data xored", 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, 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) flags := head.Flags(data)
if !flags.IsValid() { if !flags.IsValid() {
logrus.Debugln("[recv] drop invalid flags packet:", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11])) if config.ShowDebugLog {
logrus.Debugln("[recv] drop invalid flags packet:", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
}
return nil return nil
} }
crc := binary.LittleEndian.Uint64(data[52:head.PacketHeadLen]) crc := head.CRC64(data)
if m.recved.Get(crc) { // 是重放攻击 crclog := crc
logrus.Warnln("[recv] ignore duplicated crc packet", strconv.FormatUint(crc, 16)) crc ^= (uint64(seq) << 16)
if config.ShowDebugLog {
logrus.Debugf("[recv] packet crc %016x, seq %08x, xored crc %016x", crclog, seq, crc)
}
if m.recved.Get(crc) {
if config.ShowDebugLog {
logrus.Debugln("[recv] ignore duplicated crc packet", strconv.FormatUint(crc, 16))
}
return nil return nil
} }
logrus.Debugln("[recv]", len(data), "bytes data with flag", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11])) m.recved.Set(crc, true)
if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), len(data), "bytes data with flag", hex.EncodeToString(data[11:12]), hex.EncodeToString(data[10:11]))
}
if flags.IsSingle() || flags.NoFrag() { if flags.IsSingle() || flags.NoFrag() {
h := head.SelectPacket() h := head.SelectPacket()
_, err := h.Unmarshal(data) _, err := h.Unmarshal(data)
if err != nil { if err != nil {
logrus.Errorln("[recv] unmarshal err:", err) logrus.Errorln("[recv]", strconv.FormatUint(crc, 16), "unmarshal err:", err)
return nil return nil
} }
m.recved.Set(crc, true)
return h return h
} }
hashd := data[20:52] crchash := crc64.New(crc64.MakeTable(crc64.ISO))
hsh := *(*[32]byte)(*(*unsafe.Pointer)(unsafe.Pointer(&hashd))) _, _ = crchash.Write(head.Hash(data))
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], seq)
_, _ = crchash.Write(buf[:])
hsh := crchash.Sum64()
h := m.recving.Get(hsh) h := m.recving.Get(hsh)
if h != nil { if h != nil {
logrus.Debugln("[recv] get another frag part of", hex.EncodeToString(hashd)) if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "get another frag part of", strconv.FormatUint(hsh, 16))
}
ok, err := h.Unmarshal(data) ok, err := h.Unmarshal(data)
if err == nil { if err == nil {
if ok { if ok {
m.recving.Delete(hsh) m.recving.Delete(hsh)
m.recved.Set(crc, true) if config.ShowDebugLog {
logrus.Debugln("[recv] all parts of", hex.EncodeToString(hashd), "has reached") logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "all parts of", strconv.FormatUint(hsh, 16), "has reached")
}
return h return h
} }
} else { } else {
h.Put() h.Put()
logrus.Errorln("[recv] unmarshal err:", err) logrus.Errorln("[recv]", strconv.FormatUint(crc, 16), "unmarshal err:", err)
} }
return nil return nil
} }
logrus.Debugln("[recv] get new frag part of", hex.EncodeToString(hashd)) if config.ShowDebugLog {
logrus.Debugln("[recv]", strconv.FormatUint(crc, 16), "get new frag part of", strconv.FormatUint(hsh, 16))
}
h = head.SelectPacket() h = head.SelectPacket()
_, err := h.Unmarshal(data) _, err := h.Unmarshal(data)
if err != nil { if err != nil {
h.Put() h.Put()
logrus.Errorln("[recv] unmarshal err:", err) logrus.Errorln("[recv]", strconv.FormatUint(crc, 16), "unmarshal err:", err)
return nil return nil
} }
m.recving.Set(hsh, h) m.recving.Set(hsh, h)

View File

@@ -7,6 +7,8 @@ import (
"github.com/FloatTech/ttl" "github.com/FloatTech/ttl"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
) )
type Router struct { type Router struct {
@@ -45,7 +47,9 @@ func (r *Router) SetDefault(l *Link) {
func (r *Router) NextHop(ip string) (l *Link) { func (r *Router) NextHop(ip string) (l *Link) {
l = r.cache.Get(ip) l = r.cache.Get(ip)
if l != nil { if l != nil {
logrus.Debugln("[router] get cached nexthop to", ip, "link", l) if config.ShowDebugLog {
logrus.Debugln("[router] get cached nexthop to", ip, "link", l)
}
return return
} }
ipb := net.ParseIP(ip) ipb := net.ParseIP(ip)
@@ -62,7 +66,9 @@ func (r *Router) NextHop(ip string) (l *Link) {
for _, c := range r.list { for _, c := range r.list {
if c.Contains(ipb) { if c.Contains(ipb) {
l = r.table[c.String()] l = r.table[c.String()]
logrus.Debugln("[router] get nexthop to", ipb, "-->", c, "link", l) if config.ShowDebugLog {
logrus.Debugln("[router] get nexthop to", ipb, "-->", c, "link", l)
}
r.cache.Set(ip, l) r.cache.Set(ip, l)
return l return l
} }

View File

@@ -2,16 +2,21 @@ package link
import ( import (
"bytes" "bytes"
crand "crypto/rand"
"encoding/binary"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper"
base14 "github.com/fumiama/go-base16384"
) )
var ( var (
@@ -24,11 +29,17 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
defer p.Put() defer p.Put()
teatype := l.randkeyidx() teatype := l.randkeyidx()
sndcnt := uint16(l.incgetsndcnt()) sndcnt := uint16(l.incgetsndcnt())
var buf [4]byte
_, _ = crand.Read(buf[:2])
binary.BigEndian.PutUint16(buf[2:4], sndcnt)
seq := binary.BigEndian.Uint32(buf[:])
mtu := l.mtu mtu := l.mtu
if l.mturandomrange > 0 { if l.mturandomrange > 0 {
mtu -= uint16(rand.Intn(int(l.mturandomrange))) mtu -= uint16(rand.Intn(int(l.mturandomrange)))
} }
logrus.Debugln("[send] mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype) if config.ShowDebugLog {
logrus.Debugln("[send] mtu:", mtu, ", addt:", sndcnt&0x07ff, ", key index:", teatype)
}
if !istransfer { if !istransfer {
l.encrypt(p, sndcnt, teatype) l.encrypt(p, sndcnt, teatype)
} }
@@ -39,7 +50,7 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
} }
remlen := p.BodyLen() remlen := p.BodyLen()
if remlen <= delta { if remlen <= delta {
return l.write(p, teatype, sndcnt, uint32(remlen), 0, istransfer, false) return l.write(p, teatype, sndcnt, uint32(remlen), 0, istransfer, false, seq)
} }
if istransfer && p.Flags.DontFrag() && remlen > delta { if istransfer && p.Flags.DontFrag() && remlen > delta {
return 0, ErrDropBigDontFragPkt return 0, ErrDropBigDontFragPkt
@@ -50,9 +61,11 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
packet := p.Copy() packet := p.Copy()
for remlen > delta { for remlen > delta {
remlen -= delta remlen -= delta
logrus.Debugln("[send] split frag [", pos, "~", pos+delta, "], remain:", remlen) if config.ShowDebugLog {
logrus.Debugln("[send] split frag [", pos, "~", pos+delta, "], remain:", remlen)
}
packet.CropBody(pos, pos+delta) packet.CropBody(pos, pos+delta)
cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true) cnt, err := l.write(packet, teatype, sndcnt, totl, uint16(pos>>3), istransfer, true, seq)
n += cnt n += cnt
if err != nil { if err != nil {
return n, err return n, err
@@ -62,10 +75,12 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
} }
packet.Put() packet.Put()
if remlen > 0 { if remlen > 0 {
logrus.Debugln("[send] last frag [", pos, "~", pos+remlen, "]") if config.ShowDebugLog {
logrus.Debugln("[send] last frag [", pos, "~", pos+remlen, "]")
}
p.CropBody(pos, pos+remlen) p.CropBody(pos, pos+remlen)
cnt := 0 cnt := 0
cnt, err = l.write(p, teatype, sndcnt, totl, uint16(pos>>3), istransfer, false) cnt, err = l.write(p, teatype, sndcnt, totl, uint16(pos>>3), istransfer, false, seq)
n += cnt n += cnt
} }
return n, err return n, err
@@ -73,7 +88,9 @@ func (l *Link) WriteAndPut(p *head.Packet, istransfer bool) (n int, err error) {
func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) { func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
p.FillHash() p.FillHash()
logrus.Debugln("[send] data len before encrypt:", p.BodyLen()) if config.ShowDebugLog {
logrus.Debugln("[send] data len before encrypt:", p.BodyLen())
}
data := p.Body() data := p.Body()
if l.usezstd { if l.usezstd {
w := helper.SelectWriter() w := helper.SelectWriter()
@@ -82,16 +99,31 @@ func (l *Link) encrypt(p *head.Packet, sndcnt uint16, teatype uint8) {
_, _ = io.Copy(enc, bytes.NewReader(data)) _, _ = io.Copy(enc, bytes.NewReader(data))
enc.Close() enc.Close()
data = w.Bytes() data = w.Bytes()
logrus.Debugln("[send] data len after zstd:", len(data)) if config.ShowDebugLog {
logrus.Debugln("[send] data len after zstd:", len(data))
}
} }
p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data), true) p.SetBody(l.Encode(teatype, sndcnt&0x07ff, data), true)
logrus.Debugln("[send] data len after xchacha20:", p.BodyLen(), "addt:", sndcnt) if config.ShowDebugLog {
logrus.Debugln("[send] data len after xchacha20:", p.BodyLen(), "addt:", sndcnt)
}
}
// write 向 peer 发包
func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool, seq uint32) (int, error) {
if p.DecreaseAndGetTTL() <= 0 {
return 0, ErrTTL
}
if l.doublepacket {
_, _ = l.writeonce(p, teatype, additional, datasz, offset, istransfer, hasmore, seq)
}
return l.writeonce(p, teatype, additional, datasz, offset, istransfer, hasmore, seq)
} }
// write 向 peer 发一个包 // write 向 peer 发一个包
func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz uint32, offset uint16, istransfer, hasmore bool) (int, error) { 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 peerep := l.endpoint
if peerep == nil { if helper.IsNilInterface(peerep) {
return 0, errors.New("nil endpoint of " + p.Dst.String()) return 0, errors.New("nil endpoint of " + p.Dst.String())
} }
@@ -103,9 +135,6 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui
} else { } else {
d, cl = p.Marshal(l.me.me, teatype, additional, datasz, offset, false, hasmore) d, cl = p.Marshal(l.me.me, teatype, additional, datasz, offset, false, hasmore)
} }
if d == nil {
return 0, ErrTTL
}
defer cl() defer cl()
bound := 64 bound := 64
@@ -114,9 +143,22 @@ func (l *Link) write(p *head.Packet, teatype uint8, additional uint16, datasz ui
bound = len(d) bound = len(d)
endl = "." endl = "."
} }
logrus.Debugln("[send] write", len(d), "bytes data from ep", l.me.conn.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset)) conn := l.me.conn
logrus.Debugln("[send] data bytes", hex.EncodeToString(d[:bound]), endl) if conn == nil {
d = l.me.xorenc(d) return 0, io.ErrClosedPipe
logrus.Debugln("[send] data xored", hex.EncodeToString(d[:bound]), endl) }
return l.me.conn.WriteToPeer(d, peerep) if config.ShowDebugLog {
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 conn.WriteToPeer(d, peerep)
} }

View File

@@ -13,7 +13,7 @@ func NewEndpoint(endpoint string, configs ...any) (p2p.EndPoint, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
ptcl := uint(0x04) // IPIP ptcl := uint(0x6C) // IPComp https://datatracker.ietf.org/doc/html/rfc3173
if len(configs) > 0 { if len(configs) > 0 {
ptcl = configs[0].(uint) ptcl = configs[0].(uint)
} }

View File

@@ -12,6 +12,7 @@ import (
type Config struct { type Config struct {
DialTimeout time.Duration DialTimeout time.Duration
PeersTimeout time.Duration PeersTimeout time.Duration
KeepInterval time.Duration
ReceiveChannelSize int ReceiveChannelSize int
} }
@@ -34,6 +35,7 @@ func newEndpoint(endpoint string, configs ...any) (*EndPoint, error) {
addr: net.TCPAddrFromAddrPort(addr), addr: net.TCPAddrFromAddrPort(addr),
dialtimeout: cfg.DialTimeout, dialtimeout: cfg.DialTimeout,
peerstimeout: cfg.PeersTimeout, peerstimeout: cfg.PeersTimeout,
keepinterval: cfg.KeepInterval,
recvchansize: cfg.ReceiveChannelSize, recvchansize: cfg.ReceiveChannelSize,
}, nil }, nil
} }

View File

@@ -7,6 +7,7 @@ import (
"net" "net"
"time" "time"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -20,16 +21,14 @@ type packetType uint8
const ( const (
packetTypeKeepAlive packetType = iota packetTypeKeepAlive packetType = iota
packetTypeNormal packetTypeNormal
packetTypeSubKeepAlive
packetTypeTop packetTypeTop
) )
const magic = 0x12d3fde9 var (
magicbuf = []byte("GET ")
var magicbuf [4]byte magic = binary.LittleEndian.Uint32(magicbuf)
)
func init() {
binary.LittleEndian.PutUint32(magicbuf[:], magic)
}
type packet struct { type packet struct {
typ packetType typ packetType
@@ -44,7 +43,7 @@ func (p *packet) pack() (net.Buffers, func()) {
w.WriteByte(byte(p.typ)) w.WriteByte(byte(p.typ))
w.WriteUInt16(p.len) w.WriteUInt16(p.len)
}) })
return net.Buffers{magicbuf[:], d, p.dat}, cl return net.Buffers{magicbuf, d, p.dat}, cl
} }
func (p *packet) Read(_ []byte) (int, error) { func (p *packet) Read(_ []byte) (int, error) {
@@ -64,6 +63,9 @@ func (p *packet) ReadFrom(r io.Reader) (n int64, err error) {
} }
if binary.LittleEndian.Uint32(buf[:]) != magic { if binary.LittleEndian.Uint32(buf[:]) != magic {
err = ErrInvalidMagic err = ErrInvalidMagic
if config.ShowDebugLog {
logrus.Debugf("[tcp] expect magic %08x but got %08x", magic, binary.LittleEndian.Uint32(buf[:]))
}
return return
} }
cnt, err = io.ReadFull(r, buf[:3]) cnt, err = io.ReadFull(r, buf[:3])
@@ -89,11 +91,11 @@ func (p *packet) WriteTo(w io.Writer) (n int64, err error) {
return io.Copy(w, &buf) return io.Copy(w, &buf)
} }
func isvalid(tcpconn *net.TCPConn) bool { func isvalid(tcpconn *net.TCPConn, timeout time.Duration) (issub, ok bool) {
pckt := packet{} pckt := packet{}
stopch := make(chan struct{}) stopch := make(chan struct{})
t := time.AfterFunc(time.Second, func() { t := time.AfterFunc(timeout, func() {
stopch <- struct{}{} stopch <- struct{}{}
}) })
@@ -106,21 +108,29 @@ func isvalid(tcpconn *net.TCPConn) bool {
select { select {
case <-stopch: case <-stopch:
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "timeout") if config.ShowDebugLog {
return false logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "timeout")
}
return
case <-copych: case <-copych:
t.Stop() t.Stop()
} }
if err != nil { if err != nil {
logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "err:", err) if config.ShowDebugLog {
return false logrus.Debugln("[tcp] validate recv from", tcpconn.RemoteAddr(), "err:", err)
}
return
} }
if pckt.typ != packetTypeKeepAlive { if pckt.typ != packetTypeKeepAlive && pckt.typ != packetTypeSubKeepAlive {
logrus.Debugln("[tcp] validate got invalid typ", pckt.typ, "from", tcpconn.RemoteAddr()) if config.ShowDebugLog {
return false logrus.Debugln("[tcp] validate got invalid typ", pckt.typ, "from", tcpconn.RemoteAddr())
}
return
} }
logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr()) if config.ShowDebugLog {
return true logrus.Debugln("[tcp] passed validate recv from", tcpconn.RemoteAddr())
}
return pckt.typ == packetTypeSubKeepAlive, true
} }

View File

@@ -10,15 +10,18 @@ import (
"time" "time"
"github.com/FloatTech/ttl" "github.com/FloatTech/ttl"
"github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/p2p" "github.com/fumiama/WireGold/gold/p2p"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/sirupsen/logrus"
) )
type EndPoint struct { type EndPoint struct {
addr *net.TCPAddr addr *net.TCPAddr
dialtimeout time.Duration dialtimeout time.Duration
peerstimeout time.Duration peerstimeout time.Duration
keepinterval time.Duration
recvchansize int recvchansize int
} }
@@ -62,17 +65,23 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
addr: ep, addr: ep,
lstn: lstn, lstn: lstn,
peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){ peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){
nil, nil, func(_ string, t *net.TCPConn) { func(_ string, t *net.TCPConn) {
_ = t.SetLinger(0)
_ = t.SetNoDelay(true)
}, nil, func(_ string, t *net.TCPConn) {
err := t.CloseWrite() err := t.CloseWrite()
if err != nil { if config.ShowDebugLog {
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err) if err != nil {
} else { logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr(), "err:", err)
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr()) } else {
logrus.Debugln("[tcp] close write from", t.LocalAddr(), "to", t.RemoteAddr())
}
} }
}, nil, }, nil,
}), }),
recv: make(chan *connrecv, chansz), recv: make(chan *connrecv, chansz),
cplk: &sync.Mutex{}, cplk: &sync.Mutex{},
sblk: &sync.RWMutex{},
} }
go conn.accept() go conn.accept()
return conn, nil return conn, nil
@@ -84,13 +93,22 @@ type connrecv struct {
pckt packet pckt packet
} }
type subconn struct {
cplk sync.Mutex
last time.Time // last active time
conn *net.TCPConn
}
// Conn 伪装成无状态的有状态连接 // Conn 伪装成无状态的有状态连接
type Conn struct { type Conn struct {
addr *EndPoint addr *EndPoint
lstn *net.TCPListener lstn *net.TCPListener
peers *ttl.Cache[string, *net.TCPConn] peers *ttl.Cache[string, *net.TCPConn]
recv chan *connrecv recv chan *connrecv
cplk *sync.Mutex cplk *sync.Mutex
sblk *sync.RWMutex
subs []*subconn
suberr bool
} }
func (conn *Conn) accept() { func (conn *Conn) accept() {
@@ -108,53 +126,113 @@ func (conn *Conn) accept() {
_ = conn.Close() _ = conn.Close()
newc, err := conn.addr.Listen() newc, err := conn.addr.Listen()
if err != nil { if err != nil {
logrus.Warn("[tcp] re-listen on", conn.addr, "err:", err) logrus.Warnln("[tcp] re-listen on", conn.addr, "err:", err)
return return
} }
*conn = *newc.(*Conn) *conn = *newc.(*Conn)
logrus.Info("[tcp] re-listen on", conn.addr) logrus.Infoln("[tcp] re-listen on", conn.addr)
continue continue
} }
go conn.receive(tcpconn, false) go conn.receive(tcpconn, false)
} }
} }
func delsubs(i int, subs []*subconn) []*subconn {
tcpconn := subs[i].conn
err := tcpconn.CloseWrite()
if config.ShowDebugLog {
if err != nil {
logrus.Debugln("[tcp] close sub write from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr(), "err:", err)
} else {
logrus.Debugln("[tcp] close sub write from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
}
}
switch i {
case 0:
subs = subs[1:]
case len(subs) - 1:
subs = subs[:len(subs)-1]
default:
subs = append(subs[:i], subs[i+1:]...)
}
return subs
}
func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) { func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
if conn.peers == nil {
return
}
ep, _ := newEndpoint(tcpconn.RemoteAddr().String(), &Config{ ep, _ := newEndpoint(tcpconn.RemoteAddr().String(), &Config{
DialTimeout: conn.addr.dialtimeout, DialTimeout: conn.addr.dialtimeout,
PeersTimeout: conn.addr.peerstimeout, PeersTimeout: conn.addr.peerstimeout,
KeepInterval: conn.addr.keepinterval,
ReceiveChannelSize: conn.addr.recvchansize, ReceiveChannelSize: conn.addr.recvchansize,
}) })
if !hasvalidated { issub, ok := false, false
if !isvalid(tcpconn) {
return
}
logrus.Debugln("[tcp] accept from", ep)
conn.peers.Set(ep.String(), tcpconn)
}
peerstimeout := conn.addr.peerstimeout peerstimeout := conn.addr.peerstimeout
if peerstimeout < time.Second*30 { if peerstimeout < time.Second*30 {
peerstimeout = time.Second * 30 peerstimeout = time.Second * 30
} }
peerstimeout *= 2 peerstimeout *= 2
defer conn.peers.Delete(ep.String())
if !hasvalidated {
issub, ok = isvalid(tcpconn, peerstimeout)
if !ok {
return
}
if config.ShowDebugLog {
logrus.Debugln("[tcp] accept from", ep, "issub:", issub)
}
if issub {
conn.sblk.Lock()
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)
}
}
if issub {
defer func() {
conn.sblk.Lock()
subs := conn.subs
for i, sub := range subs {
if sub.conn == tcpconn {
conn.subs = delsubs(i, conn.subs)
break
}
}
conn.sblk.Unlock()
}()
} else {
if conn.peers == nil {
return
}
defer conn.peers.Delete(ep.String())
}
go conn.keep(ep)
for { for {
r := &connrecv{addr: ep} r := &connrecv{addr: ep}
if conn.addr == nil || conn.lstn == nil || conn.peers == nil || conn.recv == nil { if conn.addr == nil || conn.lstn == nil || conn.peers == nil || conn.recv == nil {
return return
} }
tcpconn := conn.peers.Get(ep.String()) if !issub {
if tcpconn == nil { tcpconn = conn.peers.Get(ep.String())
return if tcpconn == nil {
return
}
} }
r.conn = tcpconn r.conn = tcpconn
stopch := make(chan struct{}) t := time.NewTimer(peerstimeout)
t := time.AfterFunc(peerstimeout, func() {
stopch <- struct{}{}
})
var err error var err error
copych := make(chan struct{}) copych := make(chan struct{})
@@ -164,43 +242,116 @@ func (conn *Conn) receive(tcpconn *net.TCPConn, hasvalidated bool) {
}() }()
select { select {
case <-stopch: case <-t.C:
logrus.Debugln("[tcp] recv from", ep, "timeout") if config.ShowDebugLog {
logrus.Debugln("[tcp] recv from", ep, "timeout")
}
_ = tcpconn.CloseRead() _ = tcpconn.CloseRead()
return return
case <-copych: case <-copych:
t.Stop() t.Stop()
} }
if err != nil { if conn.addr == nil || conn.lstn == nil || conn.peers == nil || conn.recv == nil {
logrus.Debugln("[tcp] recv from", ep, "err:", err)
_ = tcpconn.CloseRead()
return return
} }
if err != nil {
if config.ShowDebugLog {
logrus.Debugln("[tcp] recv from", ep, "err:", err)
}
if errors.Is(err, net.ErrClosed) ||
errors.Is(err, io.ErrClosedPipe) ||
errors.Is(err, io.EOF) ||
errors.Is(err, ErrInvalidMagic) {
_ = tcpconn.CloseRead()
return
}
continue
}
if r.pckt.typ >= packetTypeTop { if r.pckt.typ >= packetTypeTop {
logrus.Debugln("[tcp] close reading invalid conn from", ep, "typ", r.pckt.typ, "len", r.pckt.len) if config.ShowDebugLog {
logrus.Debugln("[tcp] close reading invalid conn from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
}
_ = tcpconn.CloseRead() _ = tcpconn.CloseRead()
return return
} }
logrus.Debugln("[tcp] dispatch packet from", ep, "typ", r.pckt.typ, "len", r.pckt.len) if config.ShowDebugLog {
logrus.Debugln("[tcp] dispatch packet from", ep, "typ", r.pckt.typ, "len", r.pckt.len)
}
conn.recv <- r conn.recv <- r
} }
} }
func (conn *Conn) keep(ep *EndPoint) {
keepinterval := ep.keepinterval
if keepinterval < time.Second*10 {
keepinterval = time.Second * 10
}
t := time.NewTicker(keepinterval)
defer t.Stop()
for range t.C {
if conn.addr == nil || conn.peers == nil {
return
}
tcpconn := conn.peers.Get(ep.String())
if tcpconn != nil {
_, err := io.Copy(tcpconn, &packet{typ: packetTypeKeepAlive})
if conn.addr == nil {
return
}
if err != nil {
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 from", conn, "to", ep)
}
}
conn.sblk.RLock()
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 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 { func (conn *Conn) Close() error {
if conn.lstn != nil { lstn := conn.lstn
_ = conn.lstn.Close() peers := conn.peers
} recv := conn.recv
if conn.peers != nil {
conn.peers.Destroy()
}
if conn.recv != nil {
close(conn.recv)
}
conn.addr = nil conn.addr = nil
conn.lstn = nil conn.lstn = nil
conn.peers = nil conn.peers = nil
conn.recv = nil conn.recv = nil
if lstn != nil {
_ = lstn.Close()
}
if peers != nil {
peers.Destroy()
}
if recv != nil {
close(recv)
}
return nil return nil
} }
@@ -219,6 +370,9 @@ func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
if p == nil { if p == nil {
return 0, nil, net.ErrClosed return 0, nil, net.ErrClosed
} }
if conn.peers == nil {
return 0, nil, net.ErrClosed
}
conn.peers.Set(p.addr.String(), p.conn) conn.peers.Set(p.addr.String(), p.conn)
if p.pckt.typ == packetTypeNormal { if p.pckt.typ == packetTypeNormal {
break break
@@ -229,26 +383,36 @@ func (conn *Conn) ReadFromPeer(b []byte) (int, p2p.EndPoint, error) {
return n, p.addr, nil return n, p.addr, nil
} }
func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) { // writeToPeer after acquiring lock
tcpep, ok := ep.(*EndPoint) func (conn *Conn) writeToPeer(b []byte, tcpep *EndPoint, issub bool) (n int, err error) {
if !ok {
return 0, p2p.ErrEndpointTypeMistatch
}
blen := len(b)
if blen >= 65536 {
return 0, errors.New("data size " + strconv.Itoa(blen) + " is too large")
}
retried := false retried := false
conn.cplk.Lock() ok := false
defer conn.cplk.Unlock() var (
tcpconn := conn.peers.Get(tcpep.String()) tcpconn *net.TCPConn
subc *subconn
)
RECONNECT: RECONNECT:
if issub {
conn.sblk.RLock()
for _, sub := range conn.subs {
if sub.cplk.TryLock() {
tcpconn = sub.conn
subc = sub
break
}
}
conn.sblk.RUnlock()
} else {
tcpconn = conn.peers.Get(tcpep.String())
}
if tcpconn == nil { if tcpconn == nil {
dialtimeout := tcpep.dialtimeout dialtimeout := tcpep.dialtimeout
if dialtimeout < time.Second { if dialtimeout < time.Second {
dialtimeout = time.Second dialtimeout = time.Second
} }
logrus.Debugln("[tcp] dial to", tcpep.addr, "timeout", dialtimeout) if config.ShowDebugLog {
logrus.Debugln("[tcp] dial to", tcpep.addr, "timeout", dialtimeout, "issub", issub)
}
var cn net.Conn var cn net.Conn
// must use another port to send because there's no exsiting conn // must use another port to send because there's no exsiting conn
cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout) cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout)
@@ -259,31 +423,88 @@ RECONNECT:
if !ok { if !ok {
return 0, errors.New("expect *net.TCPConn but got " + reflect.ValueOf(cn).Type().String()) return 0, errors.New("expect *net.TCPConn but got " + reflect.ValueOf(cn).Type().String())
} }
_, err = io.Copy(tcpconn, &packet{ pkt := &packet{}
typ: packetTypeKeepAlive, if issub {
}) pkt.typ = packetTypeSubKeepAlive
} else {
pkt.typ = packetTypeKeepAlive
}
_, err = io.Copy(tcpconn, pkt)
if err != nil { if err != nil {
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, but write err:", err) if config.ShowDebugLog {
logrus.Debugln("[tcp] dial to", tcpep.addr, "issub", issub, "success, but write err:", err)
}
return 0, err return 0, err
} }
logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr()) if config.ShowDebugLog {
conn.peers.Set(tcpep.String(), tcpconn) logrus.Debugln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr(), "issub", issub)
}
if !issub {
conn.peers.Set(tcpep.String(), tcpconn)
} else {
conn.sblk.Lock()
conn.subs = append(conn.subs, &subconn{conn: tcpconn, last: time.Now()})
conn.sblk.Unlock()
}
go conn.receive(tcpconn, true) go conn.receive(tcpconn, true)
} else { } else if config.ShowDebugLog {
logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr()) logrus.Debugln("[tcp] reuse tcpconn from", tcpconn.LocalAddr(), "to", tcpconn.RemoteAddr())
} }
cnt, err := io.Copy(tcpconn, &packet{ cnt, err := io.Copy(tcpconn, &packet{
typ: packetTypeNormal, typ: packetTypeNormal,
len: uint16(blen), len: uint16(len(b)),
dat: b, dat: b,
}) })
if err != nil { if err != nil {
conn.peers.Delete(tcpep.String()) if subc == nil {
conn.peers.Delete(tcpep.String())
} else {
conn.sblk.Lock()
subs := conn.subs
for i, sub := range subs {
if sub == subc {
conn.subs = delsubs(i, conn.subs)
break
}
}
conn.sblk.Unlock()
}
if !retried { if !retried {
logrus.Warnln("[tcp] reconnect due to write to", tcpconn.RemoteAddr(), "err:", err)
retried = true retried = true
tcpconn = nil tcpconn = nil
goto RECONNECT goto RECONNECT
} }
} }
if subc != nil {
subc.last = time.Now()
subc.cplk.Unlock()
}
return int(cnt) - 3, err return int(cnt) - 3, err
} }
func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
tcpep, ok := ep.(*EndPoint)
if !ok {
return 0, p2p.ErrEndpointTypeMistatch
}
if len(b) >= 65536 {
return 0, errors.New("data size " + strconv.Itoa(len(b)) + " is too large")
}
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
}
conn.cplk.Lock() // add to main queue
}
defer conn.cplk.Unlock()
return conn.writeToPeer(b, tcpep, false)
}

View File

@@ -75,11 +75,11 @@ func listenUDPLite(network string, laddr *net.UDPAddr) (*net.UDPConn, error) {
} }
var errsys error var errsys error
err = rc.Control(func(fd uintptr) { err = rc.Control(func(fd uintptr) {
errsys = syscall.SetsockoptInt(int(fd), SOL_UDPLITE, UDPLITE_SEND_CSCOV, head.PacketHeadLen) errsys = syscall.SetsockoptInt(int(fd), SOL_UDPLITE, UDPLITE_SEND_CSCOV, head.PacketHeadLen+8) // for xor rand
if errsys != nil { if errsys != nil {
return return
} }
errsys = syscall.SetsockoptInt(int(fd), SOL_UDPLITE, UDPLITE_RECV_CSCOV, head.PacketHeadLen) errsys = syscall.SetsockoptInt(int(fd), SOL_UDPLITE, UDPLITE_RECV_CSCOV, head.PacketHeadLen+8) // for xor rand
}) })
if err != nil { if err != nil {
_ = conn.Close() _ = conn.Close()

View File

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

View File

@@ -131,7 +131,7 @@ func (w *Writer) Skip(n int) (int, error) {
} }
return 0, io.EOF return 0, io.EOF
} }
n = min(n, len(b.buf[b.off:])) n = minnum(n, len(b.buf[b.off:]))
b.off += n b.off += n
if n > 0 { if n > 0 {
b.lastRead = opRead b.lastRead = opRead
@@ -168,8 +168,8 @@ const (
opReadRune4 readOp = 4 // Read rune of size 4. opReadRune4 readOp = 4 // Read rune of size 4.
) )
// min 返回两数最小值,该函数将被内联 // minnum 返回两数最小值,该函数将被内联
func min[T int | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64](a, b T) T { func minnum[T int | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64](a, b T) T {
if a > b { if a > b {
return b return b
} }

View File

@@ -1,7 +1,6 @@
package lower package lower
import ( import (
"io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@@ -11,14 +10,8 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type NICIO interface { // NICIO 虚拟网卡
io.ReadWriteCloser type NICIO struct {
Up()
Down()
}
// NIC 虚拟网卡
type NIC struct {
ifce *water.Interface ifce *water.Interface
ip net.IP ip net.IP
subnet *net.IPNet subnet *net.IPNet
@@ -31,17 +24,17 @@ type NIC struct {
// 网卡地址为 ip, 所属子网为 subnet // 网卡地址为 ip, 所属子网为 subnet
// 以本网卡为下一跳的所有子网为 cidrs // 以本网卡为下一跳的所有子网为 cidrs
// cidrs 不包括本网卡 subnet // cidrs 不包括本网卡 subnet
func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO { func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) *NICIO {
ifce, err := water.New(water.Config{DeviceType: water.TUN}) ifce, err := water.New(water.Config{DeviceType: water.TUN})
if err != nil { if err != nil {
logrus.Error(err) logrus.Errorln(err)
os.Exit(1) os.Exit(1)
} }
subn, bitsn := subnet.Mask.Size() subn, bitsn := subnet.Mask.Size()
if bitsn != 32 { if bitsn != 32 {
panic("mask len " + strconv.Itoa(bitsn) + " is not supported") panic("mask len " + strconv.Itoa(bitsn) + " is not supported")
} }
n := &NIC{ n := &NICIO{
ifce: ifce, ifce: ifce,
ip: ip, ip: ip,
subnet: subnet, subnet: subnet,
@@ -53,16 +46,16 @@ func NewNIC(ip net.IP, subnet *net.IPNet, mtu string, cidrs ...string) NICIO {
} }
// Read 匹配 PacketsIO Interface // Read 匹配 PacketsIO Interface
func (nc *NIC) Read(buf []byte) (int, error) { func (nc *NICIO) Read(buf []byte) (int, error) {
return nc.ifce.Read(buf) return nc.ifce.Read(buf)
} }
func (nc *NIC) Write(packet []byte) (int, error) { func (nc *NICIO) Write(packet []byte) (int, error) {
return nc.ifce.Write(packet) return nc.ifce.Write(packet)
} }
// Close 关闭网卡 // Close 关闭网卡
func (n *NIC) Close() error { func (n *NICIO) Close() error {
return n.ifce.Close() return n.ifce.Close()
} }

View File

@@ -5,7 +5,7 @@ package lower
import "net" import "net"
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159 execute("ifconfig", n.ifce.Name(), "mtu", n.mtu) // max: 9159
execute( execute(
"ifconfig", n.ifce.Name(), "ifconfig", n.ifce.Name(),
@@ -19,7 +19,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name()) execute("route", "delete", n.subnet.String(), "-interface", n.ifce.Name())
for _, c := range n.cidrs { for _, c := range n.cidrs {
execute("route", "delete", c, "-interface", n.ifce.Name()) execute("route", "delete", c, "-interface", n.ifce.Name())

View File

@@ -3,7 +3,7 @@
package lower package lower
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu) execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "mtu", n.mtu)
execute("/sbin/ip", "addr", "add", n.rawipnet, "dev", n.ifce.Name()) execute("/sbin/ip", "addr", "add", n.rawipnet, "dev", n.ifce.Name())
execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up") execute("/sbin/ip", "link", "set", "dev", n.ifce.Name(), "up")
@@ -12,7 +12,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
for _, c := range n.cidrs { for _, c := range n.cidrs {
execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name()) execute("/sbin/ip", "route", "del", c, "dev", n.ifce.Name())
} }

View File

@@ -3,10 +3,10 @@
package lower package lower
func (n *NIC) Up() { func (n *NICIO) Up() {
panic("not support lower on this os now") panic("not support lower on this os now")
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
panic("not support lower on this os now") panic("not support lower on this os now")
} }

View File

@@ -5,7 +5,7 @@ package lower
import "net" import "net"
func (n *NIC) Up() { func (n *NICIO) Up() {
execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip.String()+"\" mask=\""+(net.IP)(n.subnet.Mask).String()+"\" gateway=none") execute("cmd", "/c", "netsh interface ip set address name=\""+n.ifce.Name()+"\" source=static addr=\""+n.ip.String()+"\" mask=\""+(net.IP)(n.subnet.Mask).String()+"\" gateway=none")
execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu) execute("cmd", "/c", "netsh interface ipv4 set subinterface \""+n.ifce.Name()+"\" mtu="+n.mtu)
for _, c := range n.cidrs { for _, c := range n.cidrs {
@@ -17,7 +17,7 @@ func (n *NIC) Up() {
} }
} }
func (n *NIC) Down() { func (n *NICIO) Down() {
// execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled") // execute("netsh", "interface", "set", "interface", n.ifce.Name(), "disabled")
for _, c := range n.cidrs { for _, c := range n.cidrs {
ip, _, err := net.ParseCIDR(c) ip, _, err := net.ParseCIDR(c)

View File

@@ -15,6 +15,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/fumiama/WireGold/upper" "github.com/fumiama/WireGold/upper"
"github.com/fumiama/WireGold/upper/services/wg" "github.com/fumiama/WireGold/upper/services/wg"
@@ -144,8 +145,8 @@ func main() {
if c.EndPoint == "" { if c.EndPoint == "" {
displayHelp("nil endpoint") displayHelp("nil endpoint")
} }
if c.MTU == 0 { if c.MTU <= head.PacketHeadLen {
displayHelp("nil mtu") displayHelp("invalid mtu")
} }
w, err := wg.NewWireGold(&c) w, err := wg.NewWireGold(&c)
if err != nil { if err != nil {

View File

@@ -13,6 +13,7 @@ import (
_ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection _ "github.com/fumiama/WireGold/gold/p2p/udp" // support udp connection
_ "github.com/fumiama/WireGold/gold/p2p/udplite" // support udplite connection _ "github.com/fumiama/WireGold/gold/p2p/udplite" // support udplite connection
"github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/head" "github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/gold/link" "github.com/fumiama/WireGold/gold/link"
) )
@@ -41,7 +42,7 @@ func Create(me *link.Me, peer string) (s Tunnel, err error) {
} }
func (s *Tunnel) Start(srcport, destport, mtu uint16) { 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.src = srcport
s.dest = destport s.dest = destport
s.mtu = mtu s.mtu = mtu
@@ -107,14 +108,20 @@ func (s *Tunnel) handleWrite() {
end = len(b) end = len(b)
endl = "." endl = "."
} }
logrus.Debugln("[tunnel] write send", hex.EncodeToString(b[:end]), endl) if config.ShowDebugLog {
logrus.Debugln("[tunnel] write send", hex.EncodeToString(b[:end]), endl)
}
if b == nil { if b == nil {
logrus.Errorln("[tunnel] write recv nil") logrus.Errorln("[tunnel] write recv nil")
break break
} }
logrus.Debugln("[tunnel] writing", len(b), "bytes...") if config.ShowDebugLog {
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
}
for len(b) > int(s.mtu)-4 { for len(b) > int(s.mtu)-4 {
logrus.Debugln("[tunnel] seq", seq, "split buffer") if config.ShowDebugLog {
logrus.Debugln("[tunnel] seq", seq, "split buffer")
}
binary.LittleEndian.PutUint32(buf[:4], seq) binary.LittleEndian.PutUint32(buf[:4], seq)
seq++ seq++
copy(buf[4:], b[:s.mtu-4]) copy(buf[4:], b[:s.mtu-4])
@@ -125,7 +132,9 @@ func (s *Tunnel) handleWrite() {
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err) logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
return return
} }
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded") if config.ShowDebugLog {
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
}
b = b[s.mtu-4:] b = b[s.mtu-4:]
} }
binary.LittleEndian.PutUint32(buf[:4], seq) binary.LittleEndian.PutUint32(buf[:4], seq)
@@ -138,7 +147,9 @@ func (s *Tunnel) handleWrite() {
logrus.Errorln("[tunnel] seq", seq-1, "write err:", err) logrus.Errorln("[tunnel] seq", seq-1, "write err:", err)
break break
} }
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded") if config.ShowDebugLog {
logrus.Debugln("[tunnel] seq", seq-1, "write succeeded")
}
} }
} }
@@ -147,7 +158,9 @@ func (s *Tunnel) handleRead() {
seqmap := make(map[uint32]*head.Packet) seqmap := make(map[uint32]*head.Packet)
for { for {
if p, ok := seqmap[seq]; ok { if p, ok := seqmap[seq]; ok {
logrus.Debugln("[tunnel] dispatch cached seq", seq) if config.ShowDebugLog {
logrus.Debugln("[tunnel] dispatch cached seq", seq)
}
delete(seqmap, seq) delete(seqmap, seq)
seq++ seq++
s.out <- p s.out <- p
@@ -164,15 +177,21 @@ func (s *Tunnel) handleRead() {
end = p.BodyLen() end = p.BodyLen()
endl = "." endl = "."
} }
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Body()[:end]), endl) if config.ShowDebugLog {
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Body()[:end]), endl)
}
recvseq := binary.LittleEndian.Uint32(p.Body()[:4]) recvseq := binary.LittleEndian.Uint32(p.Body()[:4])
if recvseq == seq { if recvseq == seq {
logrus.Debugln("[tunnel] dispatch seq", seq) if config.ShowDebugLog {
logrus.Debugln("[tunnel] dispatch seq", seq)
}
seq++ seq++
s.out <- p s.out <- p
continue continue
} }
seqmap[recvseq] = p seqmap[recvseq] = p
logrus.Debugln("[tunnel] cache seq", recvseq) if config.ShowDebugLog {
logrus.Debugln("[tunnel] cache seq", recvseq)
}
} }
} }

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt"
"io" "io"
"runtime" "runtime"
"strings" "strings"
@@ -17,7 +18,84 @@ import (
"github.com/fumiama/WireGold/helper" "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) selfpk, err := curve.New(nil)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -51,6 +129,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
SrcPort: 1, SrcPort: 1,
DstPort: 1, DstPort: 1,
MTU: mtu, MTU: mtu,
Base14: isbase14,
}) })
defer m.Close() defer m.Close()
@@ -62,6 +141,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
SrcPort: 1, SrcPort: 1,
DstPort: 1, DstPort: 1,
MTU: mtu, MTU: mtu,
Base14: isbase14,
}) })
defer p.Close() defer p.Close()
@@ -81,6 +161,7 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
MTU: mtu, MTU: mtu,
MTURandomRange: mtu / 2, MTURandomRange: mtu / 2,
UseZstd: true, UseZstd: true,
DoublePacket: true,
}) })
p.AddPeer(&link.PeerConfig{ p.AddPeer(&link.PeerConfig{
PeerIP: "192.168.1.2", PeerIP: "192.168.1.2",
@@ -106,17 +187,17 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
time.Sleep(time.Second) // wait link up time.Sleep(time.Second) // wait link up
sendb := ([]byte)("1234") sendb := ([]byte)("1234")
tunnme.Write(sendb) go tunnme.Write(sendb)
buf := make([]byte, 4) buf := make([]byte, 4)
tunnpeer.Read(buf) tunnpeer.Read(buf)
if string(sendb) != string(buf) { if string(sendb) != string(buf) {
t.Log("error: recv", buf) logrus.Errorln("error: recv", buf, "expect", sendb)
t.Fail() t.Fail()
} }
sendb = make([]byte, 4096) sendb = make([]byte, 4096)
rand.Read(sendb) rand.Read(sendb)
tunnme.Write(sendb) go tunnme.Write(sendb)
buf = make([]byte, 4096) buf = make([]byte, 4096)
_, err = io.ReadFull(&tunnpeer, buf) _, err = io.ReadFull(&tunnpeer, buf)
if err != nil { if err != nil {
@@ -126,20 +207,31 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
t.Fatal("error: recv 4096 bytes data") t.Fatal("error: recv 4096 bytes data")
} }
sendb = make([]byte, 65535) sendbufs := make(chan []byte, 32)
go func() {
time.Sleep(time.Second)
for i := 0; i < 32; i++ {
sendb := make([]byte, 65535)
rand.Read(sendb)
n, _ := tunnme.Write(sendb)
sendbufs <- sendb
logrus.Debugln("loop", i, "write", n, "bytes")
}
close(sendbufs)
}()
buf = make([]byte, 65535) buf = make([]byte, 65535)
for i := 0; i < 32; i++ { i := 0
rand.Read(sendb) for sendb := range sendbufs {
n, _ := tunnme.Write(sendb) n, err := io.ReadFull(&tunnpeer, buf)
t.Log("loop", i, "write", n, "bytes")
n, err = io.ReadFull(&tunnpeer, buf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Log("loop", i, "read", n, "bytes") logrus.Debugln("loop", i, "read", n, "bytes")
if string(sendb) != string(buf) { if string(sendb) != string(buf) {
t.Fatal("loop", i, "error: recv 65535 bytes data") t.Fatal("loop", i, "error: recv 65535 bytes data")
} }
i++
} }
rand.Read(sendb) rand.Read(sendb)
@@ -161,138 +253,164 @@ func testTunnel(t *testing.T, nw string, isplain bool, pshk *[32]byte, mtu uint1
} }
} }
func TestTunnelUDP(t *testing.T) { func benchmarkTunnel(b *testing.B, sz int, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) {
logrus.SetLevel(logrus.DebugLevel) selfpk, err := curve.New(nil)
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[:])
if err != nil { if err != nil {
panic(err) 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.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false}) 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 var buf [32]byte
_, err := rand.Read(buf[:]) _, err := rand.Read(buf[:])
if err != nil { if err != nil {
panic(err) 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) { func benchmarkTunnelNetwork(b *testing.B, nw string, mtu uint16) {
if runtime.GOOS == "darwin" { logrus.SetLevel(logrus.ErrorLevel)
return
}
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logFormat{enableColor: false}) logrus.SetFormatter(&logFormat{enableColor: false})
testTunnel(t, "udplite", true, nil, 4096) // test plain text for i := 1; i <= 4; i++ {
sz := 1024 * i
testTunnel(t, "udplite", false, nil, 4096) // test normal b.Run(fmt.Sprintf("%d-plain-nob14", sz), func(b *testing.B) {
benchmarkTunnel(b, sz, nw, true, false, nil, mtu) // test plain text
var buf [32]byte })
_, err := rand.Read(buf[:]) b.Run(fmt.Sprintf("%d-normal-nob14", sz), func(b *testing.B) {
if err != nil { benchmarkTunnel(b, sz, nw, false, false, nil, mtu) // test normal
panic(err) })
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 // logFormat specialize for go-cqhttp

View File

@@ -17,7 +17,6 @@ import (
"github.com/fumiama/WireGold/config" "github.com/fumiama/WireGold/config"
"github.com/fumiama/WireGold/gold/link" "github.com/fumiama/WireGold/gold/link"
"github.com/fumiama/WireGold/helper" "github.com/fumiama/WireGold/helper"
"github.com/fumiama/WireGold/lower"
) )
const suffix32 = "㴄" const suffix32 = "㴄"
@@ -60,7 +59,7 @@ func (wg *WG) Start(srcport, destport uint16) {
func (wg *WG) Run(srcport, destport uint16) { func (wg *WG) Run(srcport, destport uint16) {
wg.init(srcport, destport) wg.init(srcport, destport)
_, _ = wg.me.ListenNIC() _, _ = wg.me.ListenNIC()
logrus.Info("[wg] stopped") logrus.Infoln("[wg] stopped")
} }
func (wg *WG) Stop() { func (wg *WG) Stop() {
@@ -80,7 +79,7 @@ func (wg *WG) init(srcport, dstport uint16) {
} }
for _, p := range wg.c.Peers { for _, p := range wg.c.Peers {
for _, ip := range p.AllowedIPs { for _, ip := range p.AllowedIPs {
if len(ip) == 0 || ip[0] == 'x' { if len(ip) == 0 || ip[0] == 'x' || ip[0] == 'y' {
continue continue
} }
ipnet, _, err := net.ParseCIDR(ip) ipnet, _, err := net.ParseCIDR(ip)
@@ -104,12 +103,17 @@ func (wg *WG) init(srcport, dstport uint16) {
MyEndpoint: wg.c.EndPoint, MyEndpoint: wg.c.EndPoint,
Network: wg.c.Network, Network: wg.c.Network,
PrivateKey: &wg.key, PrivateKey: &wg.key,
NIC: lower.NewNIC(myip, mysubnet, strconv.FormatInt(wg.c.MTU, 10), cidrs...), NICConfig: &link.NICConfig{
SrcPort: srcport, IP: myip,
DstPort: dstport, SubNet: mysubnet,
MTU: uint16(wg.c.MTU), CIDRs: cidrs,
SpeedLoop: wg.c.SpeedLoop, },
Mask: wg.c.Mask, SrcPort: srcport,
DstPort: dstport,
MTU: uint16(wg.c.MTU),
SpeedLoop: wg.c.SpeedLoop,
Mask: wg.c.Mask,
Base14: wg.c.Base14,
}) })
for _, peer := range wg.c.Peers { for _, peer := range wg.c.Peers {
@@ -154,6 +158,7 @@ func (wg *WG) init(srcport, dstport uint16) {
AllowTrans: peer.AllowTrans, AllowTrans: peer.AllowTrans,
NoPipe: true, NoPipe: true,
UseZstd: peer.UseZstd, UseZstd: peer.UseZstd,
DoublePacket: peer.DoublePacket,
}) })
} }
} }