-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathxudp.go
185 lines (170 loc) · 4.28 KB
/
xudp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package vmess
import (
"encoding/binary"
"io"
"net"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
type XUDPConn struct {
net.Conn
writer N.ExtendedWriter
destination M.Socksaddr
requestWritten bool
}
func NewXUDPConn(conn net.Conn, destination M.Socksaddr) *XUDPConn {
return &XUDPConn{
Conn: conn,
writer: bufio.NewExtendedWriter(conn),
destination: destination,
}
}
func (c *XUDPConn) Read(p []byte) (n int, err error) {
n, _, err = c.ReadFrom(p)
return
}
func (c *XUDPConn) Write(p []byte) (n int, err error) {
return c.WriteTo(p, c.destination)
}
func (c *XUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
buffer := buf.With(p)
var destination M.Socksaddr
destination, err = c.ReadPacket(buffer)
if err != nil {
return
}
if destination.IsFqdn() {
addr = destination
} else {
addr = destination.UDPAddr()
}
n = buffer.Len()
return
}
func (c *XUDPConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
start := buffer.Start()
_, err = buffer.ReadFullFrom(c.Conn, 6)
if err != nil {
return
}
var length uint16
err = binary.Read(buffer, binary.BigEndian, &length)
if err != nil {
return
}
header, err := buffer.ReadBytes(4)
if err != nil {
return
}
switch header[2] {
case StatusNew:
return M.Socksaddr{}, E.New("unexpected frame new")
case StatusKeep:
if length != 4 {
_, err = buffer.ReadFullFrom(c.Conn, int(length)-2)
if err != nil {
return
}
buffer.Advance(1)
destination, err = AddressSerializer.ReadAddrPort(buffer)
if err != nil {
return
}
destination = destination.Unwrap()
} else {
_, err = buffer.ReadFullFrom(c.Conn, 2)
if err != nil {
return
}
destination = c.destination
}
case StatusEnd:
return M.Socksaddr{}, io.EOF
case StatusKeepAlive:
default:
return M.Socksaddr{}, E.New("unexpected frame: ", buffer.Byte(2))
}
// option error
if header[3]&2 == 2 {
return M.Socksaddr{}, E.Cause(net.ErrClosed, "remote closed")
}
// option data
if header[3]&1 != 1 {
buffer.Resize(start, 0)
return c.ReadPacket(buffer)
} else {
err = binary.Read(buffer, binary.BigEndian, &length)
if err != nil {
return
}
buffer.Resize(start, 0)
_, err = buffer.ReadFullFrom(c.Conn, int(length))
return
}
}
func (c *XUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return bufio.WritePacketBuffer(c, buf.As(p), M.SocksaddrFromNet(addr))
}
func (c *XUDPConn) frontHeadroom(addrLen int) int {
if !c.requestWritten {
var headerLen int
headerLen += 2 // frame len
headerLen += 5 // frame header
headerLen += addrLen
headerLen += 2 // payload len
return headerLen
} else {
return 7 + addrLen + 2
}
}
func (c *XUDPConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
dataLen := buffer.Len()
addrLen := M.SocksaddrSerializer.AddrPortLen(destination)
if !c.requestWritten {
header := buf.With(buffer.ExtendHeader(c.frontHeadroom(addrLen)))
common.Must(
binary.Write(header, binary.BigEndian, uint16(5+addrLen)),
header.WriteByte(0),
header.WriteByte(0),
header.WriteByte(1), // frame type new
header.WriteByte(1), // option data
header.WriteByte(NetworkUDP),
)
err := AddressSerializer.WriteAddrPort(header, destination)
if err != nil {
return err
}
common.Must(binary.Write(header, binary.BigEndian, uint16(dataLen)))
c.requestWritten = true
} else {
header := buffer.ExtendHeader(c.frontHeadroom(addrLen))
binary.BigEndian.PutUint16(header, uint16(5+addrLen))
header[2] = 0
header[3] = 0
header[4] = 2 // frame keep
header[5] = 1 // option data
header[6] = NetworkUDP
err := AddressSerializer.WriteAddrPort(buf.With(header[7:]), destination)
if err != nil {
return err
}
binary.BigEndian.PutUint16(header[7+addrLen:], uint16(dataLen))
}
return c.writer.WriteBuffer(buffer)
}
func (c *XUDPConn) FrontHeadroom() int {
return c.frontHeadroom(M.MaxSocksaddrLength)
}
func (c *XUDPConn) NeedHandshake() bool {
return !c.requestWritten
}
func (c *XUDPConn) NeedAdditionalReadDeadline() bool {
return true
}
func (c *XUDPConn) Upstream() any {
return c.Conn
}