sing-box/experimental/clashapi/trafficontrol/tracker.go

233 lines
4.8 KiB
Go
Raw Normal View History

2022-07-22 09:29:13 +08:00
package trafficontrol
2022-07-19 22:16:49 +08:00
import (
2023-04-14 20:55:05 +08:00
"encoding/json"
2022-07-19 22:16:49 +08:00
"net"
"net/netip"
"time"
"github.com/sagernet/sing-box/adapter"
2022-07-22 13:51:08 +08:00
"github.com/sagernet/sing/common"
2023-04-14 20:55:05 +08:00
"github.com/sagernet/sing/common/atomic"
2023-04-24 19:01:10 +08:00
"github.com/sagernet/sing/common/bufio"
2022-07-19 22:16:49 +08:00
N "github.com/sagernet/sing/common/network"
2023-04-14 20:55:05 +08:00
"github.com/gofrs/uuid/v5"
2022-07-19 22:16:49 +08:00
)
type Metadata struct {
NetWork string `json:"network"`
Type string `json:"type"`
SrcIP netip.Addr `json:"sourceIP"`
DstIP netip.Addr `json:"destinationIP"`
SrcPort string `json:"sourcePort"`
DstPort string `json:"destinationPort"`
Host string `json:"host"`
DNSMode string `json:"dnsMode"`
ProcessPath string `json:"processPath"`
}
type tracker interface {
ID() string
Close() error
2022-07-26 06:56:13 +08:00
Leave()
2022-07-19 22:16:49 +08:00
}
type trackerInfo struct {
UUID uuid.UUID `json:"id"`
Metadata Metadata `json:"metadata"`
UploadTotal *atomic.Int64 `json:"upload"`
DownloadTotal *atomic.Int64 `json:"download"`
Start time.Time `json:"start"`
Chain []string `json:"chains"`
Rule string `json:"rule"`
RulePayload string `json:"rulePayload"`
}
2023-04-14 20:55:05 +08:00
func (t trackerInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"id": t.UUID.String(),
"metadata": t.Metadata,
"upload": t.UploadTotal.Load(),
"download": t.DownloadTotal.Load(),
"start": t.Start,
"chains": t.Chain,
"rule": t.Rule,
"rulePayload": t.RulePayload,
})
}
2022-07-19 22:16:49 +08:00
type tcpTracker struct {
2022-09-26 15:31:02 +08:00
N.ExtendedConn `json:"-"`
2022-07-19 22:16:49 +08:00
*trackerInfo
manager *Manager
}
func (tt *tcpTracker) ID() string {
return tt.UUID.String()
}
func (tt *tcpTracker) Close() error {
tt.manager.Leave(tt)
2022-09-26 15:31:02 +08:00
return tt.ExtendedConn.Close()
2022-07-19 22:16:49 +08:00
}
2022-07-26 06:56:13 +08:00
func (tt *tcpTracker) Leave() {
tt.manager.Leave(tt)
}
2022-08-08 20:56:53 +08:00
func (tt *tcpTracker) Upstream() any {
2022-09-26 15:31:02 +08:00
return tt.ExtendedConn
}
func (tt *tcpTracker) ReaderReplaceable() bool {
return true
}
func (tt *tcpTracker) WriterReplaceable() bool {
return true
2022-08-08 20:56:53 +08:00
}
2022-11-24 12:37:29 +08:00
func NewTCPTracker(conn net.Conn, manager *Manager, metadata Metadata, router adapter.Router, rule adapter.Rule) *tcpTracker {
2022-07-19 22:16:49 +08:00
uuid, _ := uuid.NewV4()
2022-07-22 13:51:08 +08:00
var chain []string
var next string
if rule == nil {
2022-07-30 00:29:22 +08:00
next = router.DefaultOutbound(N.NetworkTCP).Tag()
2022-07-22 13:51:08 +08:00
} else {
next = rule.Outbound()
}
for {
chain = append(chain, next)
detour, loaded := router.Outbound(next)
if !loaded {
break
}
group, isGroup := detour.(adapter.OutboundGroup)
if !isGroup {
break
}
next = group.Now()
}
2023-04-14 20:55:05 +08:00
upload := new(atomic.Int64)
download := new(atomic.Int64)
2022-09-26 15:31:02 +08:00
2022-07-19 22:16:49 +08:00
t := &tcpTracker{
2023-04-24 19:01:10 +08:00
ExtendedConn: bufio.NewCounterConn(conn, []N.CountFunc{func(n int64) {
2022-10-01 09:56:09 +08:00
upload.Add(n)
manager.PushUploaded(n)
2023-04-24 19:01:10 +08:00
}}, []N.CountFunc{func(n int64) {
2022-10-01 09:56:09 +08:00
download.Add(n)
manager.PushDownloaded(n)
2023-04-24 19:01:10 +08:00
}}),
2022-10-01 09:56:09 +08:00
manager: manager,
2022-07-19 22:16:49 +08:00
trackerInfo: &trackerInfo{
UUID: uuid,
Start: time.Now(),
Metadata: metadata,
2022-07-22 13:51:08 +08:00
Chain: common.Reverse(chain),
2022-07-19 22:16:49 +08:00
Rule: "",
2022-09-26 15:31:02 +08:00
UploadTotal: upload,
DownloadTotal: download,
2022-07-19 22:16:49 +08:00
},
}
if rule != nil {
2022-07-22 13:51:08 +08:00
t.trackerInfo.Rule = rule.String() + " => " + rule.Outbound()
} else {
t.trackerInfo.Rule = "final"
2022-07-19 22:16:49 +08:00
}
manager.Join(t)
return t
}
type udpTracker struct {
N.PacketConn `json:"-"`
*trackerInfo
manager *Manager
}
func (ut *udpTracker) ID() string {
return ut.UUID.String()
}
func (ut *udpTracker) Close() error {
ut.manager.Leave(ut)
return ut.PacketConn.Close()
}
2022-07-26 06:56:13 +08:00
func (ut *udpTracker) Leave() {
ut.manager.Leave(ut)
}
2022-08-08 20:56:53 +08:00
func (ut *udpTracker) Upstream() any {
return ut.PacketConn
}
2022-09-26 15:31:02 +08:00
func (ut *udpTracker) ReaderReplaceable() bool {
return true
}
func (ut *udpTracker) WriterReplaceable() bool {
return true
}
2022-07-22 13:51:08 +08:00
func NewUDPTracker(conn N.PacketConn, manager *Manager, metadata Metadata, router adapter.Router, rule adapter.Rule) *udpTracker {
2022-07-19 22:16:49 +08:00
uuid, _ := uuid.NewV4()
2022-07-22 13:51:08 +08:00
var chain []string
var next string
if rule == nil {
2022-07-30 00:29:22 +08:00
next = router.DefaultOutbound(N.NetworkUDP).Tag()
2022-07-22 13:51:08 +08:00
} else {
next = rule.Outbound()
}
for {
chain = append(chain, next)
detour, loaded := router.Outbound(next)
if !loaded {
break
}
group, isGroup := detour.(adapter.OutboundGroup)
if !isGroup {
break
}
next = group.Now()
}
2023-04-14 20:55:05 +08:00
upload := new(atomic.Int64)
download := new(atomic.Int64)
2022-09-26 15:31:02 +08:00
2022-07-19 22:16:49 +08:00
ut := &udpTracker{
2023-04-24 19:01:10 +08:00
PacketConn: bufio.NewCounterPacketConn(conn, []N.CountFunc{func(n int64) {
2022-10-01 09:56:09 +08:00
upload.Add(n)
manager.PushUploaded(n)
2023-04-24 19:01:10 +08:00
}}, []N.CountFunc{func(n int64) {
2022-10-01 09:56:09 +08:00
download.Add(n)
manager.PushDownloaded(n)
2023-04-24 19:01:10 +08:00
}}),
2022-10-01 09:56:09 +08:00
manager: manager,
2022-07-19 22:16:49 +08:00
trackerInfo: &trackerInfo{
UUID: uuid,
Start: time.Now(),
Metadata: metadata,
2022-07-22 13:51:08 +08:00
Chain: common.Reverse(chain),
2022-07-19 22:16:49 +08:00
Rule: "",
2022-09-26 15:31:02 +08:00
UploadTotal: upload,
DownloadTotal: download,
2022-07-19 22:16:49 +08:00
},
}
if rule != nil {
2022-07-22 13:51:08 +08:00
ut.trackerInfo.Rule = rule.String() + " => " + rule.Outbound()
} else {
ut.trackerInfo.Rule = "final"
2022-07-19 22:16:49 +08:00
}
manager.Join(ut)
return ut
}