1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/common/net/packetaddr/connection_adaptor.go

181 lines
4.1 KiB
Go
Raw Normal View History

2021-11-20 18:24:21 -05:00
package packetaddr
import (
2021-12-12 08:43:14 -05:00
"context"
2021-11-20 18:24:21 -05:00
gonet "net"
"sync"
"time"
2021-12-31 16:22:16 -05:00
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/errors"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/features/routing"
"github.com/v2fly/v2ray-core/v5/transport"
2021-11-20 18:24:21 -05:00
)
2021-12-31 16:22:16 -05:00
var (
errNotPacketConn = errors.New("not a packet connection")
errUnsupported = errors.New("unsupported action")
)
2021-11-20 18:24:21 -05:00
func ToPacketAddrConn(link *transport.Link, dest net.Destination) (net.PacketConn, error) {
if !dest.Address.Family().IsDomain() {
return nil, errNotPacketConn
}
switch dest.Address.Domain() {
case seqPacketMagicAddress:
return &packetConnectionAdaptor{
readerAccess: &sync.Mutex{},
readerBuffer: nil,
link: link,
}, nil
default:
return nil, errNotPacketConn
}
}
2021-12-12 08:43:14 -05:00
func CreatePacketAddrConn(ctx context.Context, dispatcher routing.Dispatcher, isStream bool) (net.PacketConn, error) {
2021-11-20 18:24:21 -05:00
if isStream {
2021-12-12 08:43:14 -05:00
return nil, errUnsupported
}
packetDest := net.Destination{
Address: net.DomainAddress(seqPacketMagicAddress),
Port: 0,
Network: net.Network_UDP,
}
link, err := dispatcher.Dispatch(ctx, packetDest)
if err != nil {
return nil, err
2021-11-20 18:24:21 -05:00
}
return &packetConnectionAdaptor{
2021-12-12 08:43:14 -05:00
readerAccess: &sync.Mutex{},
readerBuffer: nil,
link: link,
}, nil
2021-11-20 18:24:21 -05:00
}
type packetConnectionAdaptor struct {
readerAccess *sync.Mutex
readerBuffer buf.MultiBuffer
link *transport.Link
}
func (c *packetConnectionAdaptor) ReadFrom(p []byte) (n int, addr gonet.Addr, err error) {
c.readerAccess.Lock()
defer c.readerAccess.Unlock()
if c.readerBuffer.IsEmpty() {
c.readerBuffer, err = c.link.Reader.ReadMultiBuffer()
2021-12-29 07:12:58 -05:00
if err != nil {
return 0, nil, err
}
2021-11-20 18:24:21 -05:00
}
c.readerBuffer, n = buf.SplitFirstBytes(c.readerBuffer, p)
2021-12-30 10:12:32 -05:00
var w *buf.Buffer
w, addr, err = ExtractAddressFromPacket(buf.FromBytes(p[:n]))
n = copy(p, w.Bytes())
w.Release()
2021-11-20 18:24:21 -05:00
return
}
func (c *packetConnectionAdaptor) WriteTo(p []byte, addr gonet.Addr) (n int, err error) {
payloadLen := len(p)
2021-12-30 10:12:32 -05:00
var buffer *buf.Buffer
buffer, err = AttachAddressToPacket(buf.FromBytes(p), addr)
2021-12-31 16:22:16 -05:00
if err != nil {
return 0, err
}
2021-11-20 18:24:21 -05:00
mb := buf.MultiBuffer{buffer}
err = c.link.Writer.WriteMultiBuffer(mb)
if err != nil {
return 0, err
}
return payloadLen, nil
}
func (c *packetConnectionAdaptor) Close() error {
c.readerAccess.Lock()
defer c.readerAccess.Unlock()
c.readerBuffer = buf.ReleaseMulti(c.readerBuffer)
return common.Interrupt(c.link)
}
func (c packetConnectionAdaptor) LocalAddr() gonet.Addr {
return &gonet.UnixAddr{Name: "unsupported"}
}
func (c packetConnectionAdaptor) SetDeadline(t time.Time) error {
return nil
}
func (c packetConnectionAdaptor) SetReadDeadline(t time.Time) error {
return nil
}
func (c packetConnectionAdaptor) SetWriteDeadline(t time.Time) error {
return nil
}
2021-12-25 09:13:41 -05:00
func ToPacketAddrConnWrapper(conn net.PacketConn, isStream bool) FusedConnection {
2021-12-25 09:13:41 -05:00
return &packetConnWrapper{conn}
}
type packetConnWrapper struct {
net.PacketConn
}
func (pc *packetConnWrapper) RemoteAddr() gonet.Addr {
return nil
}
type FusedConnection interface {
net.PacketConn
net.Conn
2021-12-25 09:13:41 -05:00
}
func (pc *packetConnWrapper) Read(p []byte) (n int, err error) {
recbuf := buf.StackNew()
recbuf.Extend(2048)
n, addr, err := pc.PacketConn.ReadFrom(recbuf.Bytes())
2021-12-25 09:13:41 -05:00
if err != nil {
2021-12-29 07:12:58 -05:00
return 0, err
2021-12-25 09:13:41 -05:00
}
2021-12-30 10:12:32 -05:00
recbuf.Resize(0, int32(n))
result, err := AttachAddressToPacket(&recbuf, addr)
if err != nil {
return 0, err
}
n = copy(p, result.Bytes())
result.Release()
2021-12-25 09:13:41 -05:00
return n, nil
}
func (pc *packetConnWrapper) Write(p []byte) (n int, err error) {
2021-12-30 10:12:32 -05:00
data, addr, err := ExtractAddressFromPacket(buf.FromBytes(p))
if err != nil {
return 0, err
}
_, err = pc.PacketConn.WriteTo(data.Bytes(), addr)
2021-12-25 09:13:41 -05:00
if err != nil {
return 0, err
}
2021-12-30 10:12:32 -05:00
data.Release()
2021-12-25 09:13:41 -05:00
return len(p), nil
}
func (pc *packetConnWrapper) Close() error {
2021-12-29 07:12:58 -05:00
return pc.PacketConn.Close()
2021-12-25 09:13:41 -05:00
}
func GetDestinationSubsetOf(dest net.Destination) (bool, error) {
if !dest.Address.Family().IsDomain() {
return false, errNotPacketConn
}
switch dest.Address.Domain() {
case seqPacketMagicAddress:
return false, nil
default:
return false, errNotPacketConn
}
}