2023-02-15 00:53:53 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-15 00:53:53 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package sniffing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2023-04-12 23:18:42 +07:00
|
|
|
"time"
|
2023-02-15 00:53:53 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConnSniffer struct {
|
|
|
|
net.Conn
|
2023-04-11 23:25:09 +07:00
|
|
|
*Sniffer
|
2023-02-15 00:53:53 +07:00
|
|
|
}
|
|
|
|
|
2024-07-22 07:51:26 +07:00
|
|
|
func NewConnSniffer(conn net.Conn, timeout time.Duration) *ConnSniffer {
|
2023-02-15 00:53:53 +07:00
|
|
|
s := &ConnSniffer{
|
|
|
|
Conn: conn,
|
2024-07-22 07:51:26 +07:00
|
|
|
Sniffer: NewStreamSniffer(conn, timeout),
|
2023-02-15 00:53:53 +07:00
|
|
|
}
|
|
|
|
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)
|
2023-02-15 00:53:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ConnSniffer) Close() (err error) {
|
|
|
|
var errs []string
|
2023-04-11 23:25:09 +07:00
|
|
|
if err = s.Sniffer.Close(); err != nil {
|
2023-02-15 00:53:53 +07:00
|
|
|
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
|
|
|
|
}
|