clash/proxy/socks/utils.go

40 lines
732 B
Go
Raw Normal View History

package socks
import (
"bytes"
"net"
2019-10-11 12:11:18 +00:00
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/component/socks5"
)
type fakeConn struct {
net.PacketConn
remoteAddr net.Addr
2019-10-11 12:11:18 +00:00
targetAddr socks5.Addr
buffer *bytes.Buffer
2019-10-11 12:11:18 +00:00
bufRef []byte
}
func (c *fakeConn) Read(b []byte) (n int, err error) {
return c.buffer.Read(b)
}
func (c *fakeConn) Write(b []byte) (n int, err error) {
2019-10-11 12:11:18 +00:00
packet, err := socks5.EncodeUDPPacket(c.targetAddr, b)
if err != nil {
return
}
return c.PacketConn.WriteTo(packet, c.remoteAddr)
}
func (c *fakeConn) RemoteAddr() net.Addr {
return c.remoteAddr
}
2019-10-11 12:11:18 +00:00
func (c *fakeConn) Close() error {
err := c.PacketConn.Close()
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
return err
}