/
manager.go
164 lines (142 loc) · 3.45 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package statistic
import (
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/pkg/log"
"sync"
"time"
"go.uber.org/atomic"
)
var DefaultManager *Manager
func init() {
DefaultManager = &Manager{
proxy: &Statistic{
uploadTemp: atomic.NewInt64(0),
downloadTemp: atomic.NewInt64(0),
uploadBlip: atomic.NewInt64(0),
downloadBlip: atomic.NewInt64(0),
uploadTotal: atomic.NewInt64(0),
downloadTotal: atomic.NewInt64(0),
},
direct: &Statistic{
uploadTemp: atomic.NewInt64(0),
downloadTemp: atomic.NewInt64(0),
uploadBlip: atomic.NewInt64(0),
downloadBlip: atomic.NewInt64(0),
uploadTotal: atomic.NewInt64(0),
downloadTotal: atomic.NewInt64(0),
},
}
go DefaultManager.handle(constants.PolicyProxy)
go DefaultManager.handle(constants.PolicyDirect)
}
type Statistic struct {
uploadTemp *atomic.Int64
downloadTemp *atomic.Int64
uploadBlip *atomic.Int64
downloadBlip *atomic.Int64
uploadTotal *atomic.Int64
downloadTotal *atomic.Int64
}
type Manager struct {
connections sync.Map
proxy *Statistic
direct *Statistic
}
func (m *Manager) Join(c tracker) {
m.connections.Store(c.ID(), c)
}
func (m *Manager) Leave(c tracker) {
m.connections.Delete(c.ID())
}
func (m *Manager) getStatistic(rule constants.Policy) *Statistic {
if rule == constants.PolicyProxy {
return m.proxy
}
if rule == constants.PolicyDirect {
return m.direct
}
return nil
}
func (m *Manager) PushUploaded(size int64, rule constants.Policy) {
s := m.getStatistic(rule)
if s != nil {
s.uploadTemp.Add(size)
s.uploadTotal.Add(size)
}
}
func (m *Manager) PushDownloaded(size int64, rule constants.Policy) {
s := m.getStatistic(rule)
if s != nil {
s.downloadTemp.Add(size)
s.downloadTotal.Add(size)
}
}
func (m *Manager) Now(rule constants.Policy) (up int64, down int64) {
s := m.getStatistic(rule)
if s != nil {
return s.uploadBlip.Load(), s.downloadBlip.Load()
}
return 0, 0
}
func (m *Manager) Connections() []tracker {
var connections []tracker
connections = []tracker{}
m.connections.Range(func(key, value interface{}) bool {
connections = append(connections, value.(tracker))
return true
})
return connections
}
func (m *Manager) CloseAllConnections() {
connections := m.Connections()
for _, c := range connections {
err := c.Close()
if err != nil {
log.Debugln(log.FormatLog(log.ExecutorPrefix, "fail to close connection, err: %v"), err)
}
}
}
func (m *Manager) GetTotal() *Total {
return &Total{
SnapshotStatistic{
m.proxy.downloadTotal.Load(),
m.proxy.uploadTotal.Load(),
},
SnapshotStatistic{
m.direct.downloadTotal.Load(),
m.direct.uploadTotal.Load(),
},
}
}
func (m *Manager) ResetStatistic(rule constants.Policy) {
s := m.getStatistic(rule)
if s != nil {
s.uploadTemp.Store(0)
s.uploadBlip.Store(0)
s.uploadTotal.Store(0)
s.downloadTemp.Store(0)
s.downloadBlip.Store(0)
s.downloadTotal.Store(0)
}
}
func (m *Manager) handle(rule constants.Policy) {
s := m.getStatistic(rule)
if s == nil {
return
}
ticker := time.NewTicker(time.Second)
for range ticker.C {
s.uploadBlip.Store(s.uploadTemp.Load())
s.uploadTemp.Store(0)
s.downloadBlip.Store(s.downloadTemp.Load())
s.downloadTemp.Store(0)
}
}
type SnapshotStatistic struct {
DownloadTotal int64 `json:"download"`
UploadTotal int64 `json:"upload"`
}
type Total struct {
Proxy SnapshotStatistic `json:"proxy"`
Direct SnapshotStatistic `json:"direct"`
}