dae/component/sniffing/conn_sniffer.go

45 lines
795 B
Go
Raw Normal View History

/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
*/
package sniffing
import (
"errors"
"net"
"strings"
2023-04-12 23:18:42 +07:00
"time"
)
type ConnSniffer struct {
net.Conn
2023-04-11 23:25:09 +07:00
*Sniffer
}
func NewConnSniffer(conn net.Conn, timeout time.Duration) *ConnSniffer {
s := &ConnSniffer{
Conn: conn,
Sniffer: NewStreamSniffer(conn, timeout),
}
return s
}
func (s *ConnSniffer) Read(p []byte) (n int, err error) {
2023-04-11 23:25:09 +07:00
return s.Sniffer.Read(p)
}
func (s *ConnSniffer) Close() (err error) {
var errs []string
2023-04-11 23:25:09 +07:00
if err = s.Sniffer.Close(); err != nil {
errs = append(errs, err.Error())
}
if err = s.Conn.Close(); err != nil {
errs = append(errs, err.Error())
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return nil
}