SpoofDPI/net/conn.go

87 lines
1.5 KiB
Go
Raw Normal View History

package net
2022-01-04 16:47:18 +00:00
import (
"net"
2022-01-11 15:05:16 +00:00
log "github.com/sirupsen/logrus"
2022-01-04 16:47:18 +00:00
)
const BUF_SIZE = 1024
type Conn struct {
Conn net.Conn
}
func (conn *Conn) Close() {
conn.Conn.Close()
}
2022-01-11 15:05:16 +00:00
func (conn *Conn) RemoteAddr() net.Addr {
return conn.Conn.RemoteAddr()
}
func (conn *Conn) LocalAddr() net.Addr {
return conn.Conn.LocalAddr()
}
func (conn *Conn) Read(b []byte) (n int, err error) {
return conn.Conn.Read(b)
}
func (conn *Conn) Write(b []byte) (n int, err error) {
return conn.Conn.Write(b)
}
func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) {
total := 0
for i := 0; i < len(c); i++ {
b, err := conn.Write(c[i])
if err != nil {
return 0, nil
}
b += total
}
return total, nil
}
func (conn *Conn) ReadBytes() ([]byte, error) {
2022-01-04 16:47:18 +00:00
ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE)
for {
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
ret = append(ret, buf[:n]...)
if n < BUF_SIZE {
break
}
}
return ret, nil
}
func (from *Conn) Serve(to Conn, proto string) {
2022-01-04 16:47:18 +00:00
for {
buf, err := from.ReadBytes()
2022-01-04 16:47:18 +00:00
if err != nil {
2022-01-11 15:05:16 +00:00
log.Debug("["+proto+"]"+" Error reading from ", from.RemoteAddr())
log.Debug("["+proto+"]", err)
log.Debug("[" + proto + "]" + " Exiting Serve() method. ")
2022-01-04 16:47:18 +00:00
break
}
2022-01-11 15:05:16 +00:00
log.Debug(from.RemoteAddr(), " sent data: ", len(buf), "bytes")
2022-01-04 16:47:18 +00:00
2022-01-11 15:05:16 +00:00
if _, err := to.Write(buf); err != nil {
log.Debug("["+proto+"]"+"Error Writing to ", to.RemoteAddr())
log.Debug("["+proto+"]", err)
log.Debug("[" + proto + "]" + " Exiting Serve() method. ")
2022-01-04 16:47:18 +00:00
break
}
}
}