add socks5 udp relay

This commit is contained in:
ginuerzh
2021-11-07 23:39:35 +08:00
parent e8f040cbdf
commit 16f34d3e94
39 changed files with 728 additions and 131 deletions

View File

@ -2,12 +2,6 @@ package bufpool
import "sync"
var (
smallBufferSize = 1 * 1024 // 1KB buffer
mediumBufferSize = 8 * 1024 // 8KB buffer
largeBufferSize = 64 * 1024 // 64KB buffer
)
var (
pools = []struct {
size int
@ -77,24 +71,32 @@ var (
},
},
},
{
size: 65 * 1024,
pool: sync.Pool{
New: func() interface{} {
return make([]byte, 65*1024)
},
},
},
}
)
// Get returns a buffer size range from (0, 64]KB,
// panic if size > 64KB.
// Get returns a buffer size range from (0, 65]KB,
// panic if size > 65KB.
func Get(size int) []byte {
for i := range pools {
if size <= pools[i].size {
return pools[i].pool.Get().([]byte)
}
}
panic("size too large (max=64KB)")
panic("size too large (max=65KB)")
}
func Put(b []byte) {
for i := range pools {
if len(b) == pools[i].size {
pools[i].pool.Put(b)
if cap(b) == pools[i].size {
pools[i].pool.Put(b[:cap(b)])
}
}
}

View File

@ -1,4 +1,4 @@
package utils
package kcp
import (
"net"

View File

@ -0,0 +1,75 @@
package mux
import (
"net"
smux "github.com/xtaci/smux"
)
type MuxSession struct {
conn net.Conn
session *smux.Session
}
func NewMuxSession(conn net.Conn) (*MuxSession, error) {
// Upgrade connection to multiplex stream.
s, err := smux.Client(conn, smux.DefaultConfig())
if err != nil {
return nil, err
}
return &MuxSession{
conn: conn,
session: s,
}, nil
}
func (session *MuxSession) GetConn() (net.Conn, error) {
stream, err := session.session.OpenStream()
if err != nil {
return nil, err
}
return &muxStreamConn{Conn: session.conn, stream: stream}, nil
}
func (session *MuxSession) Accept() (net.Conn, error) {
stream, err := session.session.AcceptStream()
if err != nil {
return nil, err
}
return &muxStreamConn{Conn: session.conn, stream: stream}, nil
}
func (session *MuxSession) Close() error {
if session.session == nil {
return nil
}
return session.session.Close()
}
func (session *MuxSession) IsClosed() bool {
if session.session == nil {
return true
}
return session.session.IsClosed()
}
func (session *MuxSession) NumStreams() int {
return session.session.NumStreams()
}
type muxStreamConn struct {
net.Conn
stream *smux.Stream
}
func (c *muxStreamConn) Read(b []byte) (n int, err error) {
return c.stream.Read(b)
}
func (c *muxStreamConn) Write(b []byte) (n int, err error) {
return c.stream.Write(b)
}
func (c *muxStreamConn) Close() error {
return c.stream.Close()
}

View File

@ -1,4 +1,4 @@
package utils
package quic
import (
"crypto/aes"

View File

@ -0,0 +1,18 @@
package socks
const (
// MethodTLS is an extended SOCKS5 method with tls encryption support.
MethodTLS uint8 = 0x80
// MethodTLSAuth is an extended SOCKS5 method with tls encryption and authentication support.
MethodTLSAuth uint8 = 0x82
// MethodMux is an extended SOCKS5 method for stream multiplexing.
MethodMux = 0x88
)
const (
// CmdMuxBind is an extended SOCKS5 request CMD for
// multiplexing transport with the binding server.
CmdMuxBind uint8 = 0xF2
// CmdUDPTun is an extended SOCKS5 request CMD for UDP over TCP.
CmdUDPTun uint8 = 0xF3
)

View File

@ -1,4 +1,4 @@
package utils
package ss
import (
"bytes"

View File

@ -1,4 +1,4 @@
package utils
package tls
import (
"crypto/tls"

View File

@ -1,4 +1,4 @@
package utils
package ws
import (
"net"