SpoofDPI/proxy/server.go

66 lines
1.2 KiB
Go
Raw Normal View History

2024-07-22 10:59:11 +00:00
package proxy
2022-01-04 16:47:18 +00:00
import (
2022-05-12 12:21:54 +00:00
"errors"
2022-06-05 05:50:55 +00:00
"io"
2022-01-04 16:47:18 +00:00
"net"
2022-03-04 23:46:47 +00:00
"time"
2022-01-11 15:05:16 +00:00
log "github.com/sirupsen/logrus"
2022-01-04 16:47:18 +00:00
)
2024-08-14 08:01:14 +00:00
const TLSHeaderLen = 5
func ReadBytes(conn *net.TCPConn, dest []byte) ([]byte, error) {
n, err := readBytesInternal(conn, dest)
return dest[:n], err
}
2022-05-12 12:21:54 +00:00
2024-08-08 20:52:59 +00:00
func readBytesInternal(conn *net.TCPConn, dest []byte) (int, error) {
totalRead, err := conn.Read(dest)
if err != nil {
switch err.(type) {
case *net.OpError:
return totalRead, errors.New("timed out")
default:
return totalRead, err
2022-11-29 07:54:28 +00:00
}
}
2024-08-08 20:52:59 +00:00
return totalRead, nil
2022-01-04 16:47:18 +00:00
}
func Serve(from *net.TCPConn, to *net.TCPConn, proto string, fd string, td string, timeout int, bufferSize int) {
defer func() {
from.Close()
to.Close()
log.Debug("[HTTPS] Closing proxy connection: ", fd, " -> ", td)
}()
proto += " "
buf := make([]byte, bufferSize)
2022-11-29 07:54:28 +00:00
for {
if timeout > 0 {
from.SetReadDeadline(
time.Now().Add(time.Millisecond * time.Duration(timeout)),
)
}
2023-09-08 08:35:41 +00:00
bytesRead, err := ReadBytes(from, buf)
2022-11-29 07:54:28 +00:00
if err != nil {
if err == io.EOF {
log.Debug(proto, "Finished ", fd)
return
}
log.Debug(proto, "Error reading from ", fd, " ", err)
return
}
if _, err := to.Write(bytesRead); err != nil {
2022-11-29 07:54:28 +00:00
log.Debug(proto, "Error Writing to ", td)
return
}
}
2022-01-04 16:47:18 +00:00
}