2022-05-23 03:55:48 +00:00
|
|
|
package shadowsocks_2022
|
|
|
|
|
|
|
|
import (
|
|
|
|
B "github.com/sagernet/sing/common/buf"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2023-02-21 11:19:47 +00:00
|
|
|
"github.com/xtls/xray-core/common/singbridge"
|
2022-05-23 03:55:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
|
|
|
|
|
|
|
type packetConnWrapper struct {
|
|
|
|
buf.Reader
|
|
|
|
buf.Writer
|
|
|
|
net.Conn
|
|
|
|
Dest net.Destination
|
|
|
|
cached buf.MultiBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *packetConnWrapper) ReadPacket(buffer *B.Buffer) (M.Socksaddr, error) {
|
|
|
|
if w.cached != nil {
|
|
|
|
mb, bb := buf.SplitFirst(w.cached)
|
|
|
|
if bb == nil {
|
|
|
|
w.cached = nil
|
|
|
|
} else {
|
|
|
|
buffer.Write(bb.Bytes())
|
|
|
|
w.cached = mb
|
|
|
|
var destination net.Destination
|
|
|
|
if bb.UDP != nil {
|
|
|
|
destination = *bb.UDP
|
|
|
|
} else {
|
|
|
|
destination = w.Dest
|
|
|
|
}
|
|
|
|
bb.Release()
|
2023-02-21 11:19:47 +00:00
|
|
|
return singbridge.ToSocksaddr(destination), nil
|
2022-05-23 03:55:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mb, err := w.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
|
|
|
return M.Socksaddr{}, err
|
|
|
|
}
|
|
|
|
nb, bb := buf.SplitFirst(mb)
|
|
|
|
if bb == nil {
|
|
|
|
return M.Socksaddr{}, nil
|
|
|
|
} else {
|
|
|
|
buffer.Write(bb.Bytes())
|
|
|
|
w.cached = nb
|
|
|
|
var destination net.Destination
|
|
|
|
if bb.UDP != nil {
|
|
|
|
destination = *bb.UDP
|
|
|
|
} else {
|
|
|
|
destination = w.Dest
|
|
|
|
}
|
|
|
|
bb.Release()
|
2023-02-21 11:19:47 +00:00
|
|
|
return singbridge.ToSocksaddr(destination), nil
|
2022-05-23 03:55:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *packetConnWrapper) WritePacket(buffer *B.Buffer, destination M.Socksaddr) error {
|
|
|
|
vBuf := buf.New()
|
|
|
|
vBuf.Write(buffer.Bytes())
|
2023-02-21 11:19:47 +00:00
|
|
|
endpoint := singbridge.ToDestination(destination, net.Network_UDP)
|
2022-05-23 03:55:48 +00:00
|
|
|
vBuf.UDP = &endpoint
|
|
|
|
return w.Writer.WriteMultiBuffer(buf.MultiBuffer{vBuf})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *packetConnWrapper) Close() error {
|
|
|
|
buf.ReleaseMulti(w.cached)
|
|
|
|
return nil
|
|
|
|
}
|