hysteria/pkg/transport/pktconns/udp/obfs.go
2022-11-16 22:57:35 -08:00

100 lines
2.1 KiB
Go

package udp
import (
"net"
"os"
"sync"
"syscall"
"time"
"github.com/apernet/hysteria/pkg/transport/pktconns/obfs"
)
const udpBufferSize = 4096
type ObfsUDPPacketConn struct {
orig *net.UDPConn
obfs obfs.Obfuscator
readBuf []byte
readMutex sync.Mutex
writeBuf []byte
writeMutex sync.Mutex
}
func NewObfsUDPConn(orig *net.UDPConn, obfs obfs.Obfuscator) *ObfsUDPPacketConn {
return &ObfsUDPPacketConn{
orig: orig,
obfs: obfs,
readBuf: make([]byte, udpBufferSize),
writeBuf: make([]byte, udpBufferSize),
}
}
func (c *ObfsUDPPacketConn) ReadFrom(p []byte) (int, net.Addr, error) {
for {
c.readMutex.Lock()
n, addr, err := c.orig.ReadFrom(c.readBuf)
if n <= 0 {
c.readMutex.Unlock()
return 0, addr, err
}
newN := c.obfs.Deobfuscate(c.readBuf[:n], p)
c.readMutex.Unlock()
if newN > 0 {
// Valid packet
return newN, addr, err
} else if err != nil {
// Not valid and orig.ReadFrom had some error
return 0, addr, err
}
}
}
func (c *ObfsUDPPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
c.writeMutex.Lock()
bn := c.obfs.Obfuscate(p, c.writeBuf)
_, err = c.orig.WriteTo(c.writeBuf[:bn], addr)
c.writeMutex.Unlock()
if err != nil {
return 0, err
} else {
return len(p), nil
}
}
func (c *ObfsUDPPacketConn) Close() error {
return c.orig.Close()
}
func (c *ObfsUDPPacketConn) LocalAddr() net.Addr {
return c.orig.LocalAddr()
}
func (c *ObfsUDPPacketConn) SetDeadline(t time.Time) error {
return c.orig.SetDeadline(t)
}
func (c *ObfsUDPPacketConn) SetReadDeadline(t time.Time) error {
return c.orig.SetReadDeadline(t)
}
func (c *ObfsUDPPacketConn) SetWriteDeadline(t time.Time) error {
return c.orig.SetWriteDeadline(t)
}
func (c *ObfsUDPPacketConn) SetReadBuffer(bytes int) error {
return c.orig.SetReadBuffer(bytes)
}
func (c *ObfsUDPPacketConn) SetWriteBuffer(bytes int) error {
return c.orig.SetWriteBuffer(bytes)
}
func (c *ObfsUDPPacketConn) SyscallConn() (syscall.RawConn, error) {
return c.orig.SyscallConn()
}
func (c *ObfsUDPPacketConn) File() (f *os.File, err error) {
return c.orig.File()
}