mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 20:59:19 +02:00
Refactor: FullCone TPROXY Inbound & Socks Outbound
https://t.me/projectXray/116037
This commit is contained in:
parent
13ad3fddf6
commit
2da07e0f8a
|
@ -279,7 +279,8 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
|
||||||
src: source,
|
src: source,
|
||||||
}
|
}
|
||||||
if originalDest.IsValid() {
|
if originalDest.IsValid() {
|
||||||
id.dest = originalDest
|
//id.dest = originalDest
|
||||||
|
b.UDP = &originalDest
|
||||||
}
|
}
|
||||||
conn, existing := w.getConnection(id)
|
conn, existing := w.getConnection(id)
|
||||||
|
|
||||||
|
|
|
@ -173,12 +173,15 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
||||||
if d.sockopt != nil {
|
if d.sockopt != nil {
|
||||||
sockopt.Mark = d.sockopt.Mark
|
sockopt.Mark = d.sockopt.Mark
|
||||||
}
|
}
|
||||||
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
|
to := net.DestinationFromAddr(conn.RemoteAddr())
|
||||||
|
tConn, err := internet.DialSystem(ctx, to, sockopt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
writer = NewPacketWriter(tConn, &dest, ctx, &to, sockopt)
|
||||||
|
defer writer.(*PacketWriter).Close()
|
||||||
|
/*
|
||||||
defer tConn.Close()
|
defer tConn.Close()
|
||||||
|
|
||||||
writer = &buf.SequentialWriter{Writer: tConn}
|
writer = &buf.SequentialWriter{Writer: tConn}
|
||||||
tReader := buf.NewPacketReader(tConn)
|
tReader := buf.NewPacketReader(tConn)
|
||||||
requestCount++
|
requestCount++
|
||||||
|
@ -193,6 +196,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,3 +219,65 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPacketWriter(conn net.Conn, d *net.Destination, ctx context.Context, to *net.Destination, sockopt *internet.SocketConfig) buf.Writer {
|
||||||
|
writer := &PacketWriter{
|
||||||
|
conn: conn,
|
||||||
|
conns: make(map[net.Destination]net.Conn),
|
||||||
|
ctx: ctx,
|
||||||
|
to: to,
|
||||||
|
sockopt: sockopt,
|
||||||
|
}
|
||||||
|
writer.conns[*d] = conn
|
||||||
|
return writer
|
||||||
|
}
|
||||||
|
|
||||||
|
type PacketWriter struct {
|
||||||
|
conn net.Conn
|
||||||
|
conns map[net.Destination]net.Conn
|
||||||
|
ctx context.Context
|
||||||
|
to *net.Destination
|
||||||
|
sockopt *internet.SocketConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
|
for {
|
||||||
|
mb2, b := buf.SplitFirst(mb)
|
||||||
|
mb = mb2
|
||||||
|
if b == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if b.UDP != nil && b.UDP.Address.Family().IsIP() {
|
||||||
|
conn := w.conns[*b.UDP]
|
||||||
|
if conn == nil {
|
||||||
|
w.sockopt.BindAddress = b.UDP.Address.IP()
|
||||||
|
w.sockopt.BindPort = uint32(b.UDP.Port)
|
||||||
|
conn, _ = internet.DialSystem(w.ctx, *w.to, w.sockopt)
|
||||||
|
if conn == nil {
|
||||||
|
b.Release()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
w.conns[*b.UDP] = conn
|
||||||
|
}
|
||||||
|
_, err = conn.Write(b.Bytes())
|
||||||
|
} else {
|
||||||
|
_, err = w.conn.Write(b.Bytes())
|
||||||
|
}
|
||||||
|
b.Release()
|
||||||
|
if err != nil {
|
||||||
|
buf.ReleaseMulti(mb)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *PacketWriter) Close() error {
|
||||||
|
for _, conn := range w.conns {
|
||||||
|
if conn != nil {
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -138,11 +138,12 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
|
||||||
defer udpConn.Close()
|
defer udpConn.Close()
|
||||||
requestFunc = func() error {
|
requestFunc = func() error {
|
||||||
defer timer.SetTimeout(p.Timeouts.DownlinkOnly)
|
defer timer.SetTimeout(p.Timeouts.DownlinkOnly)
|
||||||
return buf.Copy(link.Reader, &buf.SequentialWriter{Writer: NewUDPWriter(request, udpConn)}, buf.UpdateActivity(timer))
|
writer := &UDPWriter{Writer: udpConn, Request: request}
|
||||||
|
return buf.Copy(link.Reader, writer, buf.UpdateActivity(timer))
|
||||||
}
|
}
|
||||||
responseFunc = func() error {
|
responseFunc = func() error {
|
||||||
defer timer.SetTimeout(p.Timeouts.UplinkOnly)
|
defer timer.SetTimeout(p.Timeouts.UplinkOnly)
|
||||||
reader := &UDPReader{reader: udpConn}
|
reader := &UDPReader{Reader: udpConn}
|
||||||
return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer))
|
return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,47 +360,59 @@ func EncodeUDPPacket(request *protocol.RequestHeader, data []byte) (*buf.Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
type UDPReader struct {
|
type UDPReader struct {
|
||||||
reader io.Reader
|
Reader io.Reader
|
||||||
}
|
|
||||||
|
|
||||||
func NewUDPReader(reader io.Reader) *UDPReader {
|
|
||||||
return &UDPReader{reader: reader}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
func (r *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
||||||
b := buf.New()
|
buffer := buf.New()
|
||||||
if _, err := b.ReadFrom(r.reader); err != nil {
|
_, err := buffer.ReadFrom(r.Reader)
|
||||||
|
if err != nil {
|
||||||
|
buffer.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if _, err := DecodeUDPPacket(b); err != nil {
|
u, err := DecodeUDPPacket(buffer)
|
||||||
|
if err != nil {
|
||||||
|
buffer.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return buf.MultiBuffer{b}, nil
|
dest := u.Destination()
|
||||||
|
buffer.UDP = &dest
|
||||||
|
return buf.MultiBuffer{buffer}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type UDPWriter struct {
|
type UDPWriter struct {
|
||||||
request *protocol.RequestHeader
|
Writer io.Writer
|
||||||
writer io.Writer
|
Request *protocol.RequestHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPWriter(request *protocol.RequestHeader, writer io.Writer) *UDPWriter {
|
func (w *UDPWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
return &UDPWriter{
|
for {
|
||||||
request: request,
|
mb2, b := buf.SplitFirst(mb)
|
||||||
writer: writer,
|
mb = mb2
|
||||||
|
if b == nil {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
request := w.Request
|
||||||
|
if b.UDP != nil {
|
||||||
// Write implements io.Writer.
|
request = &protocol.RequestHeader{
|
||||||
func (w *UDPWriter) Write(b []byte) (int, error) {
|
Address: b.UDP.Address,
|
||||||
eb, err := EncodeUDPPacket(w.request, b)
|
Port: b.UDP.Port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
packet, err := EncodeUDPPacket(request, b.Bytes())
|
||||||
|
b.Release()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
buf.ReleaseMulti(mb)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
defer eb.Release()
|
_, err = w.Writer.Write(packet.Bytes())
|
||||||
if _, err := w.writer.Write(eb.Bytes()); err != nil {
|
packet.Release()
|
||||||
return 0, err
|
if err != nil {
|
||||||
|
buf.ReleaseMulti(mb)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return len(b), nil
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) {
|
func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) {
|
||||||
|
|
|
@ -20,14 +20,14 @@ func TestUDPEncoding(t *testing.T) {
|
||||||
Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
|
Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
|
||||||
Port: 1024,
|
Port: 1024,
|
||||||
}
|
}
|
||||||
writer := &buf.SequentialWriter{Writer: NewUDPWriter(request, b)}
|
writer := &UDPWriter{Writer: b, Request: request}
|
||||||
|
|
||||||
content := []byte{'a'}
|
content := []byte{'a'}
|
||||||
payload := buf.New()
|
payload := buf.New()
|
||||||
payload.Write(content)
|
payload.Write(content)
|
||||||
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload}))
|
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload}))
|
||||||
|
|
||||||
reader := NewUDPReader(b)
|
reader := &UDPReader{Reader: b}
|
||||||
|
|
||||||
decodedPayload, err := reader.ReadMultiBuffer()
|
decodedPayload, err := reader.ReadMultiBuffer()
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
|
|
Loading…
Reference in New Issue