apiary/honeypot/ssh/conn.go

68 lines
1.4 KiB
Go
Raw Normal View History

2021-10-21 10:36:01 +00:00
package ssh
2021-04-10 05:58:01 +00:00
import (
"net"
"time"
"github.com/fujiwara/shapeio"
"github.com/google/uuid"
)
type throttledConn struct {
ID uuid.UUID
conn net.Conn
2021-04-11 00:24:39 +00:00
writer *shapeio.Writer
reader *shapeio.Reader
2021-04-10 05:58:01 +00:00
CloseCallback func(c *throttledConn)
}
func newThrottledConn(conn net.Conn) *throttledConn {
id := uuid.Must(uuid.NewRandom())
2021-04-11 00:24:39 +00:00
return &throttledConn{
ID: id,
conn: conn,
writer: shapeio.NewWriter(conn),
reader: shapeio.NewReader(conn),
}
2021-04-10 05:58:01 +00:00
}
func (sc *throttledConn) SetSpeed(bytesPerSec float64) {
2021-04-11 00:24:39 +00:00
sc.writer.SetRateLimit(bytesPerSec)
sc.reader.SetRateLimit(bytesPerSec)
2021-04-10 05:58:01 +00:00
}
func (sc *throttledConn) Read(b []byte) (n int, err error) {
2021-04-11 00:24:39 +00:00
return sc.reader.Read(b)
2021-04-10 05:58:01 +00:00
}
func (sc *throttledConn) Write(b []byte) (n int, err error) {
2021-04-11 00:24:39 +00:00
return sc.writer.Write(b)
2021-04-10 05:58:01 +00:00
}
func (sc *throttledConn) Close() error {
if sc.CloseCallback != nil {
sc.CloseCallback(sc)
}
return sc.conn.Close()
}
func (sc *throttledConn) LocalAddr() net.Addr {
return sc.conn.LocalAddr()
}
func (sc *throttledConn) RemoteAddr() net.Addr {
return sc.conn.RemoteAddr()
}
func (sc *throttledConn) SetDeadline(t time.Time) error {
return sc.conn.SetDeadline(t)
}
func (sc *throttledConn) SetReadDeadline(t time.Time) error {
return sc.conn.SetReadDeadline(t)
}
func (sc *throttledConn) SetWriteDeadline(t time.Time) error {
return sc.conn.SetWriteDeadline(t)
}