-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpacket.go
More file actions
112 lines (97 loc) · 3.57 KB
/
Copy pathpacket.go
File metadata and controls
112 lines (97 loc) · 3.57 KB
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
/*
Copyright (C) 2025 dyhkwong
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package juicity
import (
"encoding/binary"
"net"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
)
var (
_ network.NetPacketConn = (*udpPacketConn)(nil)
_ network.EarlyWriter = (*udpPacketConn)(nil)
_ network.FrontHeadroom = (*udpPacketConn)(nil)
)
type udpPacketConn struct {
net.Conn
readWaitOptions network.ReadWaitOptions
}
func (c *udpPacketConn) FrontHeadroom() int {
if clientConn, ok := c.Conn.(*clientConn); ok && !clientConn.requestWritten {
return 1 + metadata.MaxSocksaddrLength + metadata.MaxSocksaddrLength + 2
}
return metadata.MaxSocksaddrLength + 2
}
func (c *udpPacketConn) NeedHandshakeForWrite() bool {
if clientConn, ok := c.Conn.(*clientConn); ok && !clientConn.requestWritten {
return true
}
return false
}
func (c *udpPacketConn) ReadPacket(buffer *buf.Buffer) (destination metadata.Socksaddr, err error) {
// The official Juicity server implementation always responses with IPv4-mapped IPv6 address for IPv4, and AddressSerializer.ReadAddrPort has already converted it to the correct one so we don't need to convert it ourselves.
// This is not documented in Juicity Specification, and this is a bug of the official Juicity server implementation.
destination, err = AddressSerializer.ReadAddrPort(c.Conn)
if err != nil {
return
}
var length uint16
err = binary.Read(c.Conn, binary.BigEndian, &length)
if err != nil {
return
}
_, err = buffer.ReadFullFrom(c.Conn, int(length))
return
}
func (c *udpPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
buffer := buf.With(p)
var destination metadata.Socksaddr
destination, err = c.ReadPacket(buffer)
if err != nil {
return
}
if destination.IsDomain() {
addr = destination
} else {
addr = destination.UDPAddr()
}
n = buffer.Len()
return
}
func (c *udpPacketConn) WritePacket(buffer *buf.Buffer, destination metadata.Socksaddr) (err error) {
defer buffer.Release()
bufferLen := buffer.Len()
// The description of UDP header in Juicity Specification is incorrect.
// The correct one is like: [handshake][address_of_payload0][length_of_payload0][payload0][address_of_payload1][length_of_payload1][payload1]...
// Where "handshake" is like: [0x03 (Network UDP)][address]. The "address" in handshake should be the same as "address_of_payload0" and it is useless for a bind socket.
header := buf.With(buffer.ExtendHeader(metadata.SocksaddrSerializer.AddrPortLen(destination) + 2))
err = metadata.SocksaddrSerializer.WriteAddrPort(header, destination)
if err != nil {
return
}
err = binary.Write(header, binary.BigEndian, uint16(bufferLen))
if err != nil {
return
}
_, err = c.Conn.Write(buffer.Bytes())
return
}
func (c *udpPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return bufio.WritePacketBuffer(c, buf.As(p), metadata.SocksaddrFromNet(addr))
}
func (c *udpPacketConn) Upstream() any {
return c.Conn
}