2021-01-23 14:49:46 +08:00
|
|
|
package statistic
|
2019-10-27 21:44:07 +08:00
|
|
|
|
|
|
|
import (
|
2022-03-30 23:54:52 +08:00
|
|
|
"errors"
|
2019-10-27 21:44:07 +08:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-03-30 23:54:52 +08:00
|
|
|
"github.com/Dreamacro/clash/common/snifer/tls"
|
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2019-10-27 21:44:07 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-03-30 23:54:52 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-03-31 11:41:40 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2020-10-29 17:51:14 +08:00
|
|
|
"go.uber.org/atomic"
|
2019-10-27 21:44:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type tracker interface {
|
|
|
|
ID() string
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type trackerInfo struct {
|
2020-10-29 17:51:14 +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:"-"`
|
|
|
|
*trackerInfo
|
|
|
|
manager *Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *tcpTracker) ID() string {
|
|
|
|
return tt.UUID.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *tcpTracker) Read(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Read(b)
|
|
|
|
download := int64(n)
|
2020-09-02 16:34:12 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *tcpTracker) Write(b []byte) (int, error) {
|
|
|
|
n, err := tt.Conn.Write(b)
|
|
|
|
upload := int64(n)
|
2020-09-02 16:34:12 +08:00
|
|
|
tt.manager.PushUploaded(upload)
|
2022-04-04 10:43:25 +08:00
|
|
|
if tt.UploadTotal.Load() < 128 && tt.Metadata.Host == "" && (tt.Metadata.DstPort == "443" || tt.Metadata.DstPort == "8443" || tt.Metadata.DstPort == "993" || tt.Metadata.DstPort == "465" || tt.Metadata.DstPort == "995") {
|
2022-03-30 23:54:52 +08:00
|
|
|
header, err := tls.SniffTLS(b)
|
|
|
|
if err != nil {
|
|
|
|
// log.Errorln("Expect no error but actually %s %s:%s:%s", err.Error(), tt.Metadata.Host, tt.Metadata.DstIP.String(), tt.Metadata.DstPort)
|
|
|
|
} else {
|
2022-03-31 11:41:40 +08:00
|
|
|
resolver.InsertHostByIP(tt.Metadata.DstIP, header.Domain())
|
|
|
|
log.Warnln("use sni update host: %s ip: %s", header.Domain(), tt.Metadata.DstIP.String())
|
2022-03-30 23:54:52 +08:00
|
|
|
tt.manager.Leave(tt)
|
|
|
|
tt.Conn.Close()
|
2022-03-31 11:41:40 +08:00
|
|
|
return n, errors.New("sni update, break current link to avoid leaks")
|
2022-03-30 23:54:52 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-29 17:51:14 +08:00
|
|
|
tt.UploadTotal.Add(upload)
|
2022-03-30 23:54:52 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *tcpTracker) Close() error {
|
|
|
|
tt.manager.Leave(tt)
|
|
|
|
return tt.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.Rule) *tcpTracker {
|
2019-10-27 21:44:07 +08:00
|
|
|
uuid, _ := uuid.NewV4()
|
2019-10-28 12:58:39 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
t := &tcpTracker{
|
|
|
|
Conn: conn,
|
|
|
|
manager: manager,
|
|
|
|
trackerInfo: &trackerInfo{
|
2020-10-29 17:51:14 +08:00
|
|
|
UUID: uuid,
|
|
|
|
Start: time.Now(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Chain: conn.Chains(),
|
|
|
|
Rule: "",
|
|
|
|
UploadTotal: atomic.NewInt64(0),
|
|
|
|
DownloadTotal: atomic.NewInt64(0),
|
2019-10-27 21:44:07 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:28:56 +08:00
|
|
|
if rule != nil {
|
|
|
|
t.trackerInfo.Rule = rule.RuleType().String()
|
|
|
|
t.trackerInfo.RulePayload = rule.Payload()
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
manager.Join(t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type udpTracker struct {
|
|
|
|
C.PacketConn `json:"-"`
|
|
|
|
*trackerInfo
|
|
|
|
manager *Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ut *udpTracker) ID() string {
|
|
|
|
return ut.UUID.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ut *udpTracker) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
n, addr, err := ut.PacketConn.ReadFrom(b)
|
|
|
|
download := int64(n)
|
2020-09-02 16:34:12 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ut *udpTracker) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
|
|
n, err := ut.PacketConn.WriteTo(b, addr)
|
|
|
|
upload := int64(n)
|
2020-09-02 16:34:12 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-01-23 14:49:46 +08:00
|
|
|
func NewUDPTracker(conn C.PacketConn, manager *Manager, metadata *C.Metadata, rule C.Rule) *udpTracker {
|
2019-10-27 21:44:07 +08:00
|
|
|
uuid, _ := uuid.NewV4()
|
2019-10-28 12:58:39 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
ut := &udpTracker{
|
|
|
|
PacketConn: conn,
|
|
|
|
manager: manager,
|
|
|
|
trackerInfo: &trackerInfo{
|
2020-10-29 17:51:14 +08:00
|
|
|
UUID: uuid,
|
|
|
|
Start: time.Now(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Chain: conn.Chains(),
|
|
|
|
Rule: "",
|
|
|
|
UploadTotal: atomic.NewInt64(0),
|
|
|
|
DownloadTotal: atomic.NewInt64(0),
|
2019-10-27 21:44:07 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:28:56 +08:00
|
|
|
if rule != nil {
|
|
|
|
ut.trackerInfo.Rule = rule.RuleType().String()
|
|
|
|
ut.trackerInfo.RulePayload = rule.Payload()
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
manager.Join(ut)
|
|
|
|
return ut
|
|
|
|
}
|