-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
273 lines (242 loc) · 5.57 KB
/
misc.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package gota
import (
"bytes"
"container/ring"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
)
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)
func (b ByteSize) String() string {
switch {
case b >= YB:
return fmt.Sprintf("%.2fYB", b/YB)
case b >= ZB:
return fmt.Sprintf("%.2fZB", b/ZB)
case b >= EB:
return fmt.Sprintf("%.2fEB", b/EB)
case b >= PB:
return fmt.Sprintf("%.2fPB", b/PB)
case b >= TB:
return fmt.Sprintf("%.2fTB", b/TB)
case b >= GB:
return fmt.Sprintf("%.2fGB", b/GB)
case b >= MB:
return fmt.Sprintf("%.2fMB", b/MB)
case b >= KB:
return fmt.Sprintf("%.2fKB", b/KB)
}
return fmt.Sprintf("%.2fB", b)
}
// Statistic contains the traffic status
type Statistic struct {
SentBytes uint64
ReceivedBytes uint64
StartSeconds int64
sendRing *ring.Ring
receivedRing *ring.Ring
}
type secondStats struct {
unixTime int64
bytes uint64
}
func NewStatistic(n int) *Statistic {
s := &Statistic{
SentBytes: 0,
ReceivedBytes: 0,
StartSeconds: time.Now().Unix(),
//recentSeconds: n,
sendRing: ring.New(n),
receivedRing: ring.New(n),
}
s.sendRing.Value = &secondStats{
unixTime: time.Now().Unix(),
bytes: 0,
}
s.receivedRing.Value = &secondStats{
unixTime: time.Now().Unix(),
bytes: 0,
}
return s
}
// AddSentBytes update the SentBytes by bytes
func (s *Statistic) AddSentBytes(c uint64) {
cur := time.Now().Unix()
ss := s.sendRing.Value.(*secondStats)
if cur == ss.unixTime {
ss.bytes += c
} else {
s.sendRing = s.sendRing.Next()
s.sendRing.Value = &secondStats{
unixTime: cur,
bytes: c,
}
}
s.SentBytes += c
}
// AddReceivedBytes update the ReceivedBytes by bytes
func (s *Statistic) AddReceivedBytes(c uint64) {
cur := time.Now().Unix()
ss := s.receivedRing.Value.(*secondStats)
if cur == ss.unixTime {
ss.bytes += c
} else {
s.receivedRing = s.receivedRing.Next()
s.receivedRing.Value = &secondStats{
unixTime: cur,
bytes: c,
}
}
s.ReceivedBytes += c
}
// SendSpeed return the bytes sent per second
func (s *Statistic) SendSpeed() uint64 {
t := time.Now().Unix() - s.StartSeconds
return s.SentBytes / uint64(t)
}
// SendSpeedSecond return the bytes sent per second
func (s *Statistic) SendSpeedSecond(n int64) uint64 {
cur := time.Now().Unix()
var bs uint64
s.sendRing.Do(func(value interface{}) {
ss, ok := value.(*secondStats)
if !ok {
return
}
if cur-ss.unixTime <= n {
bs += ss.bytes
}
})
return bs / uint64(n)
}
// ReceiveSpeed return the bytes received per second
func (s *Statistic) ReceiveSpeed() uint64 {
t := time.Now().Unix() - s.StartSeconds
return s.ReceivedBytes / uint64(t)
}
// ReceiveSpeedSecond return the bytes received per second
func (s *Statistic) ReceiveSpeedSecond(n int64) uint64 {
cur := time.Now().Unix()
var bs uint64
s.receivedRing.Do(func(value interface{}) {
ss, ok := value.(*secondStats)
if !ok {
return
}
if cur-ss.unixTime <= n {
bs += ss.bytes
}
})
return bs / uint64(n)
}
var ErrNoMoreBytes = errors.New("Read io.EOF, received bytes count less than required")
// ReadNBytes read N bytes from io.Reader,
// it never returns the io.EOF.
//
// If it read N bytes from io.Reader, returns nil.
// If it read io.EOF, but less than N bytes, return ErrNoMoreBytes.
// If it read other errors, returns them.
func ReadNBytes(r io.Reader, n int) ([]byte, error) {
var buf bytes.Buffer
remain := n
for remain > 0 {
data := make([]byte, remain)
rn, err := r.Read(data)
if err != nil && err != io.EOF {
return nil, err
}
remain = remain - rn
buf.Write(data[:rn])
if err == io.EOF {
break
}
}
if remain > 0 {
return buf.Bytes(), ErrNoMoreBytes
}
return buf.Bytes(), nil
}
// WriteNBytes write N bytes to io.Writer
func WriteNBytes(w io.Writer, n int, d []byte) error {
for wrote := 0; wrote < n; {
wn, err := w.Write(d[wrote:])
if err != nil {
return err
}
wrote = wrote + wn
}
return nil
}
// RWCloseWriter add CloseWrite to io.ReadWriteCloser for only closing write
// after call func CloseWrite, io.ReadWriteCloser can still be read
type RWCloseWriter interface {
CloseWrite() error
io.ReadWriteCloser
}
// RWCloseReader add CloseRead to io.ReadWriteCloser for only closing read
// after call func CloseRead, io.ReadWriteCloser can still be write
type RWCloseReader interface {
CloseRead() error
io.ReadWriteCloser
}
// CompareGotaFrame compares GotaFrame a and b
// If they are same GotaFrame, return true else return false
func CompareGotaFrame(a, b *GotaFrame) bool {
if a.Control == b.Control &&
a.ConnID == b.ConnID &&
a.SeqNum == b.SeqNum &&
a.Length == b.Length &&
bytes.Compare(a.Payload, b.Payload) == 0 {
return true
}
return false
}
func NewBasicAuthGotaFrame(username, password string) *GotaFrame {
auth := username + ":" + password
authBytes := []byte(base64.StdEncoding.EncodeToString([]byte(auth)))
gf := &GotaFrame{
Control: true,
SeqNum: uint32(TMTunnelAuthSeq),
Length: len(authBytes),
Payload: authBytes,
}
return gf
}
func SetLogLevel(l string) {
switch strings.ToLower(l) {
case "info":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
default:
log.SetLevel(log.DebugLevel)
}
}
func ShutdownGota() {
process, _ := os.FindProcess(os.Getpid())
process.Signal(os.Interrupt)
}