mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:54:01 +03:00
Add pool support everywhere
This commit is contained in:
@@ -63,7 +63,12 @@ func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error {
|
||||
return fmt.Errorf("cannot read initial record: %w", err)
|
||||
}
|
||||
|
||||
clientHello, err := tlstypes.ParseClientHello(helloRecord.Data.Bytes())
|
||||
buf := acquireBytesBuffer()
|
||||
defer releaseBytesBuffer(buf)
|
||||
|
||||
helloRecord.Data.WriteBytes(buf)
|
||||
|
||||
clientHello, err := tlstypes.ParseClientHello(buf.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot parse client hello: %w", err)
|
||||
}
|
||||
|
||||
+11
-8
@@ -28,15 +28,9 @@ func cloak(one, another io.ReadWriteCloser) {
|
||||
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
io.Copy(one, another) // nolint: errcheck
|
||||
}()
|
||||
go cloakPipe(one, another, wg)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
io.Copy(another, one) // nolint: errcheck
|
||||
}()
|
||||
go cloakPipe(another, one, wg)
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
@@ -69,3 +63,12 @@ func cloak(one, another io.ReadWriteCloser) {
|
||||
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
func cloakPipe(one io.Writer, another io.Reader, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
buf := acquireCloakBuffer()
|
||||
defer releaseCloakBuffer(buf)
|
||||
|
||||
io.CopyBuffer(one, another, *buf) // nolint: errcheck
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package faketls
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const cloakBufferSize = 1024
|
||||
|
||||
var (
|
||||
poolBytesBuffer = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
poolCloakBuffer = sync.Pool{
|
||||
New: func() interface{} {
|
||||
rv := make([]byte, cloakBufferSize)
|
||||
return &rv
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func acquireBytesBuffer() *bytes.Buffer {
|
||||
return poolBytesBuffer.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func acquireCloakBuffer() *[]byte {
|
||||
return poolCloakBuffer.Get().(*[]byte)
|
||||
}
|
||||
|
||||
func releaseBytesBuffer(buf *bytes.Buffer) {
|
||||
poolBytesBuffer.Put(buf)
|
||||
}
|
||||
|
||||
func releaseCloakBuffer(buf *[]byte) {
|
||||
poolCloakBuffer.Put(buf)
|
||||
}
|
||||
Reference in New Issue
Block a user