mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 14:26:31 +00:00
27 lines
562 B
Go
27 lines
562 B
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"net"
|
||
|
)
|
||
|
|
||
|
func ReadBytes(ctx context.Context, conn *net.TCPConn, dest []byte) ([]byte, error) {
|
||
|
n, err := readBytesInternal(ctx, conn, dest)
|
||
|
return dest[:n], err
|
||
|
}
|
||
|
|
||
|
func readBytesInternal(ctx context.Context, conn *net.TCPConn, dest []byte) (int, error) {
|
||
|
totalRead, err := conn.Read(dest)
|
||
|
if err != nil {
|
||
|
var opError *net.OpError
|
||
|
switch {
|
||
|
case errors.As(err, &opError) && opError.Timeout():
|
||
|
return totalRead, errors.New("timed out")
|
||
|
default:
|
||
|
return totalRead, err
|
||
|
}
|
||
|
}
|
||
|
return totalRead, nil
|
||
|
}
|