From 6b943caf3725d34930da6353b36f1630eb3079db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 7 Sep 2023 09:09:03 +0800 Subject: [PATCH] Reject invalid connection --- go.mod | 10 +++++----- go.sum | 20 ++++++++++---------- test/go.mod | 10 +++++----- test/go.sum | 35 ++++++++++++++--------------------- transport/trojan/protocol.go | 28 ++++++++++++++++++++-------- transport/tuic/client.go | 6 +++++- transport/tuic/packet.go | 10 +++++++++- transport/vless/client.go | 11 +++++++++-- transport/vless/protocol.go | 23 ++++++++++++++++------- 9 files changed, 93 insertions(+), 60 deletions(-) diff --git a/go.mod b/go.mod index 2e81f1ce..1b60811b 100644 --- a/go.mod +++ b/go.mod @@ -25,14 +25,14 @@ require ( github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2 github.com/sagernet/quic-go v0.0.0-20230825040534-0cd917b2ddda github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 - github.com/sagernet/sing v0.2.10-0.20230830132630-30bf19f2833c + github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205 github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1 - github.com/sagernet/sing-mux v0.1.3-0.20230830095209-2a10ebd53ba8 - github.com/sagernet/sing-shadowsocks v0.2.4 - github.com/sagernet/sing-shadowsocks2 v0.1.3 + github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314 + github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0 + github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248 github.com/sagernet/sing-shadowtls v0.1.4 github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641 - github.com/sagernet/sing-vmess v0.1.7 + github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6 github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 diff --git a/go.sum b/go.sum index d9bc4a1a..670854dc 100644 --- a/go.sum +++ b/go.sum @@ -114,22 +114,22 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing v0.2.10-0.20230830132630-30bf19f2833c h1:J2ptRncTNy+ZHfcFYSBfTmpvmgNlSEUZz6sDjh1np/Y= -github.com/sagernet/sing v0.2.10-0.20230830132630-30bf19f2833c/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= +github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205 h1:U/OwMlCH1XFjrDrw5BESGxGsnynT6nDnHvNI9Xv0U78= +github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1 h1:5w+jXz8y/8UQAxO74TjftN5okYkpg5mGvVxXunlKdqI= github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1/go.mod h1:Kg98PBJEg/08jsNFtmZWmPomhskn9Ausn50ecNm4M+8= -github.com/sagernet/sing-mux v0.1.3-0.20230830095209-2a10ebd53ba8 h1:UyUkEUEGqfIGqzOJ7OuJry4slgcT/qb0etDJ+89LTAs= -github.com/sagernet/sing-mux v0.1.3-0.20230830095209-2a10ebd53ba8/go.mod h1:TKxqIvfQQgd36jp2tzsPavGjYTVZilV+atip1cssjIY= -github.com/sagernet/sing-shadowsocks v0.2.4 h1:s/CqXlvFAZhlIoHWUwPw5CoNnQ9Ibki9pckjuugtVfY= -github.com/sagernet/sing-shadowsocks v0.2.4/go.mod h1:80fNKP0wnqlu85GZXV1H1vDPC/2t+dQbFggOw4XuFUM= -github.com/sagernet/sing-shadowsocks2 v0.1.3 h1:WXoLvCFi5JTFBRYorf1YePGYIQyJ/zbsBM6Fwbl5kGA= -github.com/sagernet/sing-shadowsocks2 v0.1.3/go.mod h1:DOhJc/cLeqRv0wuePrQso+iUmDxOnWF4eT/oMcRzYFw= +github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314 h1:P5+NZGMH8KSI3L8lKw1znxdRi0tIpWbGYjmv8GrFHrQ= +github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314/go.mod h1:TKxqIvfQQgd36jp2tzsPavGjYTVZilV+atip1cssjIY= +github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0 h1:9wHYWxH+fcs01PM2+DylA8LNNY3ElnZykQo9rysng8U= +github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0/go.mod h1:80fNKP0wnqlu85GZXV1H1vDPC/2t+dQbFggOw4XuFUM= +github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248 h1:JTFfy/LDmVFEK4KZJEujmC1iO8+aoF4unYhhZZRzRq4= +github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248/go.mod h1:DOhJc/cLeqRv0wuePrQso+iUmDxOnWF4eT/oMcRzYFw= github.com/sagernet/sing-shadowtls v0.1.4 h1:aTgBSJEgnumzFenPvc+kbD9/W0PywzWevnVpEx6Tw3k= github.com/sagernet/sing-shadowtls v0.1.4/go.mod h1:F8NBgsY5YN2beQavdgdm1DPlhaKQlaL6lpDdcBglGK4= github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641 h1:a8lktNrCWZJisB+nPraW+qB73ZofgPtGmlfqNYcO79g= github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641/go.mod h1:+YImslQMLgMQcVgZZ9IK4ue1o/605VSU90amHUcp4hA= -github.com/sagernet/sing-vmess v0.1.7 h1:TM8FFLsXmlXH9XT8/oDgc6PC5BOzrg6OzyEe01is2r4= -github.com/sagernet/sing-vmess v0.1.7/go.mod h1:1qkC1L1T2sxnS/NuO6HU72S8TkltV+EXoKGR29m/Yss= +github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b h1:2ezfJtH5JosiEwJhVa+rimQ6ps/t2+7h+mOzMoiaZnA= +github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b/go.mod h1:1qkC1L1T2sxnS/NuO6HU72S8TkltV+EXoKGR29m/Yss= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0= github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6 h1:Px+hN4Vzgx+iCGVnWH5A8eR7JhNnIV3rGQmBxA7cw6Q= diff --git a/test/go.mod b/test/go.mod index cf854222..0cc75852 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,9 +10,9 @@ require ( github.com/docker/docker v24.0.5+incompatible github.com/docker/go-connections v0.4.0 github.com/gofrs/uuid/v5 v5.0.0 - github.com/sagernet/sing v0.2.10-0.20230830132630-30bf19f2833c - github.com/sagernet/sing-shadowsocks v0.2.4 - github.com/sagernet/sing-shadowsocks2 v0.1.3 + github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205 + github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0 + github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248 github.com/spyzhov/ajson v0.9.0 github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.2.1 @@ -73,10 +73,10 @@ require ( github.com/sagernet/quic-go v0.0.0-20230825040534-0cd917b2ddda // indirect github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 // indirect github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1 // indirect - github.com/sagernet/sing-mux v0.1.3-0.20230830095209-2a10ebd53ba8 // indirect + github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314 // indirect github.com/sagernet/sing-shadowtls v0.1.4 // indirect github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641 // indirect - github.com/sagernet/sing-vmess v0.1.7 // indirect + github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b // indirect github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 // indirect github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6 // indirect github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 // indirect diff --git a/test/go.sum b/test/go.sum index 544e35cb..95848464 100644 --- a/test/go.sum +++ b/test/go.sum @@ -113,8 +113,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= -github.com/quic-go/qtls-go1-20 v0.3.2 h1:rRgN3WfnKbyik4dBV8A6girlJVxGand/d+jVKbQq5GI= -github.com/quic-go/qtls-go1-20 v0.3.2/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= +github.com/quic-go/qtls-go1-20 v0.3.3 h1:17/glZSLI9P9fDAeyCHBFSWSqJcwx1byhLwP5eUIDCM= github.com/quic-go/qtls-go1-20 v0.3.3/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= github.com/sagernet/cloudflare-tls v0.0.0-20221031050923-d70792f4c3a0 h1:KyhtFFt1Jtp5vW2ohNvstvQffTOQ/s5vENuGXzdA+TM= github.com/sagernet/cloudflare-tls v0.0.0-20221031050923-d70792f4c3a0/go.mod h1:D4SFEOkJK+4W1v86ZhX0jPM0rAL498fyQAChqMtes/I= @@ -124,33 +123,28 @@ github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2 h1:dnkKrzapqtAwjTS github.com/sagernet/gvisor v0.0.0-20230627031050-1ab0276e0dd2/go.mod h1:1JUiV7nGuf++YFm9eWZ8q2lrwHmhcUGzptMl/vL1+LA= github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE= github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= -github.com/sagernet/quic-go v0.0.0-20230824033040-30ef72e3be3e h1:URg7GQT9Mp0b6m/LXH1NgVT3P/ybVomvy2kkfA6QIHQ= -github.com/sagernet/quic-go v0.0.0-20230824033040-30ef72e3be3e/go.mod h1:7DXnweBVxZ7CQWsCdc7QAAQ65dFPEtenfz+w6WDESlI= +github.com/sagernet/quic-go v0.0.0-20230825040534-0cd917b2ddda h1:7J/hnOFqCThiCrVpvr0wKO+Dic/XPSulPr5yI8FVJMs= github.com/sagernet/quic-go v0.0.0-20230825040534-0cd917b2ddda/go.mod h1:Iw8Tt3dMqC/61cMHa0nN5i/958oYuuMnQCMOSPx+xcg= github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc= github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing v0.2.10-0.20230821073500-620f3a3b882d h1:4kgoOCE48CuQcBUcoRnE0QTPXkl8yM8i7Nipmzp/978= -github.com/sagernet/sing v0.2.10-0.20230821073500-620f3a3b882d/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= -github.com/sagernet/sing v0.2.10-0.20230824115837-8d731e68853a/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= -github.com/sagernet/sing v0.2.10-0.20230830132630-30bf19f2833c/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= -github.com/sagernet/sing-dns v0.1.9-0.20230731012726-ad50da89b659 h1:1DAKccGNqTYJ8nsBR765FS0LVBVXfuFlFAHqKsGN3EI= -github.com/sagernet/sing-dns v0.1.9-0.20230731012726-ad50da89b659/go.mod h1:W7GHTZFS8RkoLI3bA2LFY27/0E+uoQESWtMFLepO/JA= +github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205 h1:U/OwMlCH1XFjrDrw5BESGxGsnynT6nDnHvNI9Xv0U78= +github.com/sagernet/sing v0.2.10-0.20230907044649-03c21c0a1205/go.mod h1:9uOZwWkhT2Z2WldolLxX34s+1svAX4i4vvz5hy8u1MA= +github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1 h1:5w+jXz8y/8UQAxO74TjftN5okYkpg5mGvVxXunlKdqI= github.com/sagernet/sing-dns v0.1.9-0.20230824120133-4d5cbceb40c1/go.mod h1:Kg98PBJEg/08jsNFtmZWmPomhskn9Ausn50ecNm4M+8= -github.com/sagernet/sing-mux v0.1.3-0.20230811111955-dc1639b5204c h1:35/FowAvt3Z62mck0TXzVc4jS5R5CWq62qcV2P1cp0I= -github.com/sagernet/sing-mux v0.1.3-0.20230811111955-dc1639b5204c/go.mod h1:TKxqIvfQQgd36jp2tzsPavGjYTVZilV+atip1cssjIY= -github.com/sagernet/sing-mux v0.1.3-0.20230830095209-2a10ebd53ba8/go.mod h1:TKxqIvfQQgd36jp2tzsPavGjYTVZilV+atip1cssjIY= -github.com/sagernet/sing-shadowsocks v0.2.4 h1:s/CqXlvFAZhlIoHWUwPw5CoNnQ9Ibki9pckjuugtVfY= -github.com/sagernet/sing-shadowsocks v0.2.4/go.mod h1:80fNKP0wnqlu85GZXV1H1vDPC/2t+dQbFggOw4XuFUM= -github.com/sagernet/sing-shadowsocks2 v0.1.3 h1:WXoLvCFi5JTFBRYorf1YePGYIQyJ/zbsBM6Fwbl5kGA= -github.com/sagernet/sing-shadowsocks2 v0.1.3/go.mod h1:DOhJc/cLeqRv0wuePrQso+iUmDxOnWF4eT/oMcRzYFw= +github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314 h1:P5+NZGMH8KSI3L8lKw1znxdRi0tIpWbGYjmv8GrFHrQ= +github.com/sagernet/sing-mux v0.1.3-0.20230907005326-7befbadbf314/go.mod h1:TKxqIvfQQgd36jp2tzsPavGjYTVZilV+atip1cssjIY= +github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0 h1:9wHYWxH+fcs01PM2+DylA8LNNY3ElnZykQo9rysng8U= +github.com/sagernet/sing-shadowsocks v0.2.5-0.20230907005610-126234728ca0/go.mod h1:80fNKP0wnqlu85GZXV1H1vDPC/2t+dQbFggOw4XuFUM= +github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248 h1:JTFfy/LDmVFEK4KZJEujmC1iO8+aoF4unYhhZZRzRq4= +github.com/sagernet/sing-shadowsocks2 v0.1.4-0.20230907005906-5d2917b29248/go.mod h1:DOhJc/cLeqRv0wuePrQso+iUmDxOnWF4eT/oMcRzYFw= github.com/sagernet/sing-shadowtls v0.1.4 h1:aTgBSJEgnumzFenPvc+kbD9/W0PywzWevnVpEx6Tw3k= github.com/sagernet/sing-shadowtls v0.1.4/go.mod h1:F8NBgsY5YN2beQavdgdm1DPlhaKQlaL6lpDdcBglGK4= github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641 h1:a8lktNrCWZJisB+nPraW+qB73ZofgPtGmlfqNYcO79g= github.com/sagernet/sing-tun v0.1.12-0.20230821065522-7545dc2d5641/go.mod h1:+YImslQMLgMQcVgZZ9IK4ue1o/605VSU90amHUcp4hA= -github.com/sagernet/sing-vmess v0.1.7 h1:TM8FFLsXmlXH9XT8/oDgc6PC5BOzrg6OzyEe01is2r4= -github.com/sagernet/sing-vmess v0.1.7/go.mod h1:1qkC1L1T2sxnS/NuO6HU72S8TkltV+EXoKGR29m/Yss= +github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b h1:2ezfJtH5JosiEwJhVa+rimQ6ps/t2+7h+mOzMoiaZnA= +github.com/sagernet/sing-vmess v0.1.8-0.20230907010359-161fb0ac716b/go.mod h1:1qkC1L1T2sxnS/NuO6HU72S8TkltV+EXoKGR29m/Yss= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0= github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6 h1:Px+hN4Vzgx+iCGVnWH5A8eR7JhNnIV3rGQmBxA7cw6Q= @@ -192,8 +186,7 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= -go4.org/netipx v0.0.0-20230728184502-ec4c8b891b28 h1:zLxFnORHDFTSkJPawMU7LzsuGQJ4MUFS653jJHpORow= -go4.org/netipx v0.0.0-20230728184502-ec4c8b891b28/go.mod h1:TQvodOM+hJTioNQJilmLXu08JNb8i+ccq418+KWu1/Y= +go4.org/netipx v0.0.0-20230824141953-6213f710f925 h1:eeQDDVKFkx0g4Hyy8pHgmZaK0EqB4SD6rvKbUdN3ziQ= go4.org/netipx v0.0.0-20230824141953-6213f710f925/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190404164418-38d8ce5564a5/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= diff --git a/transport/trojan/protocol.go b/transport/trojan/protocol.go index b208c084..09e18782 100644 --- a/transport/trojan/protocol.go +++ b/transport/trojan/protocol.go @@ -204,10 +204,13 @@ func ClientHandshake(conn net.Conn, key [KeyLength]byte, destination M.Socksaddr common.Must1(header.Write(key[:])) common.Must1(header.Write(CRLF)) common.Must(header.WriteByte(CommandTCP)) - common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination)) + err := M.SocksaddrSerializer.WriteAddrPort(header, destination) + if err != nil { + return err + } common.Must1(header.Write(CRLF)) common.Must1(header.Write(payload)) - _, err := conn.Write(header.Bytes()) + _, err = conn.Write(header.Bytes()) if err != nil { return E.Cause(err, "write request") } @@ -219,10 +222,13 @@ func ClientHandshakeBuffer(conn net.Conn, key [KeyLength]byte, destination M.Soc common.Must1(header.Write(key[:])) common.Must1(header.Write(CRLF)) common.Must(header.WriteByte(CommandTCP)) - common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination)) + err := M.SocksaddrSerializer.WriteAddrPort(header, destination) + if err != nil { + return err + } common.Must1(header.Write(CRLF)) - _, err := conn.Write(payload.Bytes()) + _, err = conn.Write(payload.Bytes()) if err != nil { return E.Cause(err, "write request") } @@ -244,7 +250,10 @@ func ClientHandshakePacket(conn net.Conn, key [KeyLength]byte, destination M.Soc common.Must1(header.Write(key[:])) common.Must1(header.Write(CRLF)) common.Must(header.WriteByte(CommandUDP)) - common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination)) + err := M.SocksaddrSerializer.WriteAddrPort(header, destination) + if err != nil { + return err + } common.Must1(header.Write(CRLF)) common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination)) common.Must(binary.Write(header, binary.BigEndian, uint16(payloadLen))) @@ -257,7 +266,7 @@ func ClientHandshakePacket(conn net.Conn, key [KeyLength]byte, destination M.Soc } } - _, err := conn.Write(payload.Bytes()) + _, err = conn.Write(payload.Bytes()) if err != nil { return E.Cause(err, "write payload") } @@ -289,10 +298,13 @@ func WritePacket(conn net.Conn, buffer *buf.Buffer, destination M.Socksaddr) err defer buffer.Release() bufferLen := buffer.Len() header := buf.With(buffer.ExtendHeader(M.SocksaddrSerializer.AddrPortLen(destination) + 4)) - common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination)) + err := M.SocksaddrSerializer.WriteAddrPort(header, destination) + if err != nil { + return err + } common.Must(binary.Write(header, binary.BigEndian, uint16(bufferLen))) common.Must1(header.Write(CRLF)) - _, err := conn.Write(buffer.Bytes()) + _, err = conn.Write(buffer.Bytes()) if err != nil { return E.Cause(err, "write packet") } diff --git a/transport/tuic/client.go b/transport/tuic/client.go index 2658e52d..1ba97044 100644 --- a/transport/tuic/client.go +++ b/transport/tuic/client.go @@ -271,9 +271,13 @@ func (c *clientConn) Read(b []byte) (n int, err error) { func (c *clientConn) Write(b []byte) (n int, err error) { if !c.requestWritten { request := buf.NewSize(2 + addressSerializer.AddrPortLen(c.destination) + len(b)) + defer request.Release() request.WriteByte(Version) request.WriteByte(CommandConnect) - addressSerializer.WriteAddrPort(request, c.destination) + err = addressSerializer.WriteAddrPort(request, c.destination) + if err != nil { + return + } request.Write(b) _, err = c.stream.Write(request.Bytes()) if err != nil { diff --git a/transport/tuic/packet.go b/transport/tuic/packet.go index 4701a6d4..0c7b10db 100644 --- a/transport/tuic/packet.go +++ b/transport/tuic/packet.go @@ -17,6 +17,7 @@ import ( "github.com/sagernet/sing/common/atomic" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/cache" + E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" ) @@ -205,6 +206,9 @@ func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) if buffer.Len() > 0xffff { return quic.ErrMessageTooLarge(0xffff) } + if !destination.IsValid() { + return E.New("invalid destination address") + } packetId := c.packetId.Add(1) if packetId > math.MaxUint16 { c.packetId.Store(0) @@ -246,6 +250,10 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { if len(p) > 0xffff { return 0, quic.ErrMessageTooLarge(0xffff) } + destination := M.SocksaddrFromNet(addr) + if !destination.IsValid() { + return 0, E.New("invalid destination address") + } packetId := c.packetId.Add(1) if packetId > math.MaxUint16 { c.packetId.Store(0) @@ -256,7 +264,7 @@ func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { sessionID: c.sessionID, packetID: uint16(packetId), fragmentTotal: 1, - destination: M.SocksaddrFromNet(addr), + destination: destination, data: buf.As(p), } if !c.udpStream && c.needFragment() && len(p) > c.udpMTU { diff --git a/transport/vless/client.go b/transport/vless/client.go index cb9ca9cb..09150f6d 100644 --- a/transport/vless/client.go +++ b/transport/vless/client.go @@ -150,7 +150,10 @@ func (c *Conn) Write(b []byte) (n int, err error) { func (c *Conn) WriteBuffer(buffer *buf.Buffer) error { if !c.requestWritten { - EncodeRequest(c.request, buf.With(buffer.ExtendHeader(RequestLen(c.request)))) + err := EncodeRequest(c.request, buf.With(buffer.ExtendHeader(RequestLen(c.request)))) + if err != nil { + return err + } c.requestWritten = true } return c.ExtendedConn.WriteBuffer(buffer) @@ -159,7 +162,11 @@ func (c *Conn) WriteBuffer(buffer *buf.Buffer) error { func (c *Conn) WriteVectorised(buffers []*buf.Buffer) error { if !c.requestWritten { buffer := buf.NewSize(RequestLen(c.request)) - EncodeRequest(c.request, buffer) + err := EncodeRequest(c.request, buffer) + if err != nil { + buffer.Release() + return err + } c.requestWritten = true return c.writer.WriteVectorised(append([]*buf.Buffer{buffer}, buffers...)) } diff --git a/transport/vless/protocol.go b/transport/vless/protocol.go index 2cbc2c7f..5cda06e1 100644 --- a/transport/vless/protocol.go +++ b/transport/vless/protocol.go @@ -156,14 +156,17 @@ func WriteRequest(writer io.Writer, request Request, payload []byte) error { ) if request.Command != vmess.CommandMux { - common.Must(vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination)) + err := vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination) + if err != nil { + return err + } } common.Must1(buffer.Write(payload)) return common.Error(writer.Write(buffer.Bytes())) } -func EncodeRequest(request Request, buffer *buf.Buffer) { +func EncodeRequest(request Request, buffer *buf.Buffer) error { var requestLen int requestLen += 1 // version requestLen += 16 // uuid @@ -195,8 +198,12 @@ func EncodeRequest(request Request, buffer *buf.Buffer) { ) if request.Command != vmess.CommandMux { - common.Must(vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination)) + err := vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination) + if err != nil { + return err + } } + return nil } func RequestLen(request Request) int { @@ -251,10 +258,12 @@ func WritePacketRequest(writer io.Writer, request Request, payload []byte) error common.Must(common.Error(buffer.WriteString(request.Flow))) } - common.Must( - buffer.WriteByte(vmess.CommandUDP), - vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination), - ) + common.Must(buffer.WriteByte(vmess.CommandUDP)) + + err := vmess.AddressSerializer.WriteAddrPort(buffer, request.Destination) + if err != nil { + return err + } if len(payload) > 0 { common.Must(