2021-01-23 14:49:46 +08:00
|
|
|
package statistic
|
2019-10-27 21:44:07 +08:00
|
|
|
|
|
|
|
import (
|
2023-04-25 23:01:05 +08:00
|
|
|
"io"
|
2019-10-27 21:44:07 +08:00
|
|
|
"net"
|
2023-04-27 10:32:25 +08:00
|
|
|
"net/netip"
|
2019-10-27 21:44:07 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/atomic"
|
|
|
|
"github.com/metacubex/mihomo/common/buf"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2022-03-31 11:41:40 +08:00
|
|
|
|
2023-04-09 15:40:17 +08:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2019-10-27 21:44:07 +08:00
|
|
|
)
|
|
|
|
|
2023-06-26 17:46:14 +08:00
|
|
|
type Tracker interface {
|
2019-10-27 21:44:07 +08:00
|
|
|
ID() string
|
|
|
|
Close() error
|
2023-06-26 18:13:17 +08:00
|
|
|
Info() *TrackerInfo
|
2023-04-12 18:50:51 +08:00
|
|
|
C.Connection
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 18:13:17 +08:00
|
|
|
type TrackerInfo struct {
|
2023-10-10 16:34:33 +08:00
|
|
|
UUID uuid.UUID `json:"id"`
|
|
|
|
Metadata *C.Metadata `json:"metadata"`
|
|
|
|
UploadTotal atomic.Int64 `json:"upload"`
|
|
|
|
DownloadTotal atomic.Int64 `json:"download"`
|
|
|
|
Start time.Time `json:"start"`
|
|
|
|
Chain C.Chain `json:"chains"`
|
|
|
|
Rule string `json:"rule"`
|
|
|
|
RulePayload string `json:"rulePayload"`
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tcpTracker struct {
|
|
|
|
C.Conn `json:"-"`
|
2023-06-26 18:13:17 +08:00
|
|
|
*TrackerInfo
|
2023-04-02 22:24:46 +08:00
|
|
|
manager *Manager
|
2023-04-11 23:58:56 +08:00
|
|
|
|
|
|
|
pushToManager bool `json:"-"`
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *tcpTracker) ID() string {
|
|
|
|
return tt.UUID.String()
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:13:17 +08:00
|
|
|
func (tt *tcpTracker) Info() *TrackerInfo {
|
|
|
|
return tt.TrackerInfo
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (tt *tcpTracker) Read(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Read(b)
|
|
|
|
download := int64(n)
|
2023-04-11 23:58:56 +08:00
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushDownloaded(download)
|
|
|
|
}
|
2020-10-29 17:51:14 +08:00
|
|
|
tt.DownloadTotal.Add(download)
|
2019-10-27 21:44:07 +08:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (tt *tcpTracker) ReadBuffer(buffer *buf.Buffer) (err error) {
|
2023-04-02 22:24:46 +08:00
|
|
|
err = tt.Conn.ReadBuffer(buffer)
|
2023-01-16 09:42:03 +08:00
|
|
|
download := int64(buffer.Len())
|
2023-04-11 23:58:56 +08:00
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushDownloaded(download)
|
|
|
|
}
|
2023-01-16 09:42:03 +08:00
|
|
|
tt.DownloadTotal.Add(download)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:01:05 +08:00
|
|
|
func (tt *tcpTracker) UnwrapReader() (io.Reader, []N.CountFunc) {
|
|
|
|
return tt.Conn, []N.CountFunc{func(download int64) {
|
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushDownloaded(download)
|
|
|
|
}
|
|
|
|
tt.DownloadTotal.Add(download)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (tt *tcpTracker) Write(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Write(b)
|
|
|
|
upload := int64(n)
|
2023-04-11 23:58:56 +08:00
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushUploaded(upload)
|
|
|
|
}
|
2020-10-29 17:51:14 +08:00
|
|
|
tt.UploadTotal.Add(upload)
|
2019-10-27 21:44:07 +08:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (tt *tcpTracker) WriteBuffer(buffer *buf.Buffer) (err error) {
|
2023-01-17 21:36:16 +08:00
|
|
|
upload := int64(buffer.Len())
|
2023-04-02 22:24:46 +08:00
|
|
|
err = tt.Conn.WriteBuffer(buffer)
|
2023-04-11 23:58:56 +08:00
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushUploaded(upload)
|
|
|
|
}
|
2023-01-16 09:42:03 +08:00
|
|
|
tt.UploadTotal.Add(upload)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:01:05 +08:00
|
|
|
func (tt *tcpTracker) UnwrapWriter() (io.Writer, []N.CountFunc) {
|
|
|
|
return tt.Conn, []N.CountFunc{func(upload int64) {
|
|
|
|
if tt.pushToManager {
|
|
|
|
tt.manager.PushUploaded(upload)
|
|
|
|
}
|
|
|
|
tt.UploadTotal.Add(upload)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (tt *tcpTracker) Close() error {
|
|
|
|
tt.manager.Leave(tt)
|
|
|
|
return tt.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (tt *tcpTracker) Upstream() any {
|
|
|
|
return tt.Conn
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:32:25 +08:00
|
|
|
func parseRemoteDestination(addr net.Addr, conn C.Connection) string {
|
|
|
|
if addr == nil && conn != nil {
|
|
|
|
return conn.RemoteDestination()
|
|
|
|
}
|
|
|
|
if addrPort, err := netip.ParseAddrPort(addr.String()); err == nil && addrPort.Addr().IsValid() {
|
|
|
|
return addrPort.Addr().String()
|
|
|
|
} else {
|
|
|
|
if conn != nil {
|
|
|
|
return conn.RemoteDestination()
|
2022-05-28 20:00:02 +08:00
|
|
|
} else {
|
2023-04-27 10:32:25 +08:00
|
|
|
return ""
|
2022-05-28 20:00:02 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-27 10:32:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *tcpTracker {
|
|
|
|
if conn != nil {
|
|
|
|
metadata.RemoteDst = parseRemoteDestination(conn.RemoteAddr(), conn)
|
|
|
|
}
|
2019-10-28 12:58:39 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
t := &tcpTracker{
|
|
|
|
Conn: conn,
|
|
|
|
manager: manager,
|
2023-06-26 18:13:17 +08:00
|
|
|
TrackerInfo: &TrackerInfo{
|
2023-03-15 10:10:03 +08:00
|
|
|
UUID: utils.NewUUIDV4(),
|
2020-10-29 17:51:14 +08:00
|
|
|
Start: time.Now(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Chain: conn.Chains(),
|
|
|
|
Rule: "",
|
2023-02-25 19:41:01 +08:00
|
|
|
UploadTotal: atomic.NewInt64(uploadTotal),
|
|
|
|
DownloadTotal: atomic.NewInt64(downloadTotal),
|
2019-10-27 21:44:07 +08:00
|
|
|
},
|
2023-04-11 23:58:56 +08:00
|
|
|
pushToManager: pushToManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
if pushToManager {
|
|
|
|
if uploadTotal > 0 {
|
|
|
|
manager.PushUploaded(uploadTotal)
|
|
|
|
}
|
|
|
|
if downloadTotal > 0 {
|
|
|
|
manager.PushDownloaded(downloadTotal)
|
|
|
|
}
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:28:56 +08:00
|
|
|
if rule != nil {
|
2023-06-26 18:13:17 +08:00
|
|
|
t.TrackerInfo.Rule = rule.RuleType().String()
|
|
|
|
t.TrackerInfo.RulePayload = rule.Payload()
|
2020-06-07 17:28:56 +08:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
manager.Join(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type udpTracker struct {
|
|
|
|
C.PacketConn `json:"-"`
|
2023-06-26 18:13:17 +08:00
|
|
|
*TrackerInfo
|
2019-10-27 21:44:07 +08:00
|
|
|
manager *Manager
|
2023-04-11 23:58:56 +08:00
|
|
|
|
|
|
|
pushToManager bool `json:"-"`
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ut *udpTracker) ID() string {
|
|
|
|
return ut.UUID.String()
|
|
|
|
}
|
|
|
|
|
2023-06-26 18:13:17 +08:00
|
|
|
func (ut *udpTracker) Info() *TrackerInfo {
|
|
|
|
return ut.TrackerInfo
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
n, addr, err := ut.PacketConn.ReadFrom(b)
|
|
|
|
download := int64(n)
|
2023-04-11 23:58:56 +08:00
|
|
|
if ut.pushToManager {
|
|
|
|
ut.manager.PushDownloaded(download)
|
|
|
|
}
|
2020-10-29 17:51:14 +08:00
|
|
|
ut.DownloadTotal.Add(download)
|
2019-10-27 21:44:07 +08:00
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:47:51 +08:00
|
|
|
func (ut *udpTracker) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
|
|
|
data, put, addr, err = ut.PacketConn.WaitReadFrom()
|
|
|
|
download := int64(len(data))
|
|
|
|
if ut.pushToManager {
|
|
|
|
ut.manager.PushDownloaded(download)
|
|
|
|
}
|
|
|
|
ut.DownloadTotal.Add(download)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (ut *udpTracker) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
|
|
n, err := ut.PacketConn.WriteTo(b, addr)
|
|
|
|
upload := int64(n)
|
2023-04-11 23:58:56 +08:00
|
|
|
if ut.pushToManager {
|
|
|
|
ut.manager.PushUploaded(upload)
|
|
|
|
}
|
2020-10-29 17:51:14 +08:00
|
|
|
ut.UploadTotal.Add(upload)
|
2019-10-27 21:44:07 +08:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ut *udpTracker) Close() error {
|
|
|
|
ut.manager.Leave(ut)
|
|
|
|
return ut.PacketConn.Close()
|
|
|
|
}
|
|
|
|
|
2023-05-12 09:14:27 +08:00
|
|
|
func (ut *udpTracker) Upstream() any {
|
|
|
|
return ut.PacketConn
|
|
|
|
}
|
|
|
|
|
2023-04-11 23:58:56 +08:00
|
|
|
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule, uploadTotal int64, downloadTotal int64, pushToManager bool) *udpTracker {
|
2023-04-27 10:32:25 +08:00
|
|
|
metadata.RemoteDst = parseRemoteDestination(nil, conn)
|
2019-10-28 12:58:39 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
ut := &udpTracker{
|
|
|
|
PacketConn: conn,
|
|
|
|
manager: manager,
|
2023-06-26 18:13:17 +08:00
|
|
|
TrackerInfo: &TrackerInfo{
|
2023-03-15 10:10:03 +08:00
|
|
|
UUID: utils.NewUUIDV4(),
|
2020-10-29 17:51:14 +08:00
|
|
|
Start: time.Now(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Chain: conn.Chains(),
|
|
|
|
Rule: "",
|
2023-02-25 19:41:01 +08:00
|
|
|
UploadTotal: atomic.NewInt64(uploadTotal),
|
|
|
|
DownloadTotal: atomic.NewInt64(downloadTotal),
|
2019-10-27 21:44:07 +08:00
|
|
|
},
|
2023-04-11 23:58:56 +08:00
|
|
|
pushToManager: pushToManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
if pushToManager {
|
|
|
|
if uploadTotal > 0 {
|
|
|
|
manager.PushUploaded(uploadTotal)
|
|
|
|
}
|
|
|
|
if downloadTotal > 0 {
|
|
|
|
manager.PushDownloaded(downloadTotal)
|
|
|
|
}
|
2019-10-27 21:44:07 +08:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:28:56 +08:00
|
|
|
if rule != nil {
|
2023-06-26 18:13:17 +08:00
|
|
|
ut.TrackerInfo.Rule = rule.RuleType().String()
|
|
|
|
ut.TrackerInfo.RulePayload = rule.Payload()
|
2020-06-07 17:28:56 +08:00
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
manager.Join(ut)
|
|
|
|
return ut
|
|
|
|
}
|