33a6579a3a
* Refactor ssr stream cipher to expose iv and key References: https://github.com/Dreamacro/go-shadowsocks2 https://github.com/sh4d0wfiend/go-shadowsocksr2 * Implement ssr obfs Reference: https://github.com/mzz2017/shadowsocksR * Implement ssr protocol References: https://github.com/mzz2017/shadowsocksR https://github.com/shadowsocksRb/shadowsocksr-libev https://github.com/shadowsocksr-backup/shadowsocksr
42 lines
936 B
Go
42 lines
936 B
Go
package protocol
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
)
|
|
|
|
// NewPacketConn returns a net.NewPacketConn with protocol decoding/encoding
|
|
func NewPacketConn(pc net.PacketConn, p Protocol) net.PacketConn {
|
|
return &PacketConn{PacketConn: pc, Protocol: p.initForConn(nil)}
|
|
}
|
|
|
|
// PacketConn represents a protocol packet connection
|
|
type PacketConn struct {
|
|
net.PacketConn
|
|
Protocol
|
|
}
|
|
|
|
func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
buf := pool.Get(pool.RelayBufferSize)
|
|
defer pool.Put(buf)
|
|
buf, err := c.EncodePacket(b)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
_, err = c.PacketConn.WriteTo(buf, addr)
|
|
return len(b), err
|
|
}
|
|
|
|
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
n, addr, err := c.PacketConn.ReadFrom(b)
|
|
if err != nil {
|
|
return n, addr, err
|
|
}
|
|
bb, length, err := c.DecodePacket(b[:n])
|
|
if err != nil {
|
|
return n, addr, err
|
|
}
|
|
copy(b, bb)
|
|
return length, addr, err
|
|
}
|