2019-07-25 09:47:39 +00:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
|
2019-10-11 12:11:18 +00:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
2019-07-25 09:47:39 +00:00
|
|
|
"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
|
2019-07-25 09:47:39 +00:00
|
|
|
buffer *bytes.Buffer
|
2019-10-11 12:11:18 +00:00
|
|
|
bufRef []byte
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-07-25 09:47:39 +00:00
|
|
|
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
|
|
|
|
}
|