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)])
}
}
}