2021-04-29 16:48:47 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2021-11-13 21:30:24 +08:00
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
2021-04-29 16:48:47 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/monitor"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
"net"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 发送监控流量
|
|
|
|
|
func init() {
|
|
|
|
|
events.On(events.EventStart, func() {
|
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
|
|
|
go func() {
|
|
|
|
|
for range ticker.C {
|
|
|
|
|
// 加入到数据队列中
|
2021-11-13 21:30:24 +08:00
|
|
|
if teaconst.InTrafficBytes > 0 {
|
2021-04-29 16:48:47 +08:00
|
|
|
monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemTrafficIn, maps.Map{
|
2021-11-13 21:30:24 +08:00
|
|
|
"total": teaconst.InTrafficBytes,
|
2021-04-29 16:48:47 +08:00
|
|
|
})
|
|
|
|
|
}
|
2021-11-13 21:30:24 +08:00
|
|
|
if teaconst.OutTrafficBytes > 0 {
|
2021-04-29 16:48:47 +08:00
|
|
|
monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemTrafficOut, maps.Map{
|
2021-11-13 21:30:24 +08:00
|
|
|
"total": teaconst.OutTrafficBytes,
|
2021-04-29 16:48:47 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置数据
|
2021-11-13 21:30:24 +08:00
|
|
|
atomic.StoreUint64(&teaconst.InTrafficBytes, 0)
|
|
|
|
|
atomic.StoreUint64(&teaconst.OutTrafficBytes, 0)
|
2021-04-29 16:48:47 +08:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
// ClientConn 客户端连接
|
|
|
|
|
type ClientConn struct {
|
2021-09-29 09:19:45 +08:00
|
|
|
rawConn net.Conn
|
|
|
|
|
isClosed bool
|
2021-04-29 16:48:47 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-25 19:00:42 +08:00
|
|
|
func NewClientConn(conn net.Conn, quickClose bool) net.Conn {
|
|
|
|
|
if quickClose {
|
|
|
|
|
tcpConn, ok := conn.(*net.TCPConn)
|
|
|
|
|
if ok {
|
|
|
|
|
// TODO 可以设置此值
|
2021-10-30 22:35:05 +08:00
|
|
|
_ = tcpConn.SetLinger(3)
|
2021-10-25 19:00:42 +08:00
|
|
|
}
|
2021-10-20 22:32:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &ClientConn{rawConn: conn}
|
2021-04-29 16:48:47 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) Read(b []byte) (n int, err error) {
|
2021-04-29 16:48:47 +08:00
|
|
|
n, err = this.rawConn.Read(b)
|
|
|
|
|
if n > 0 {
|
2021-11-13 21:30:24 +08:00
|
|
|
atomic.AddUint64(&teaconst.InTrafficBytes, uint64(n))
|
2021-04-29 16:48:47 +08:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) Write(b []byte) (n int, err error) {
|
2021-04-29 16:48:47 +08:00
|
|
|
n, err = this.rawConn.Write(b)
|
|
|
|
|
if n > 0 {
|
2021-11-13 21:30:24 +08:00
|
|
|
atomic.AddUint64(&teaconst.OutTrafficBytes, uint64(n))
|
2021-04-29 16:48:47 +08:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) Close() error {
|
2021-09-29 09:19:45 +08:00
|
|
|
this.isClosed = true
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) LocalAddr() net.Addr {
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.LocalAddr()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) RemoteAddr() net.Addr {
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.RemoteAddr()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) SetDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.SetDeadline(t)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) SetReadDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.SetReadDeadline(t)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) SetWriteDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
return this.rawConn.SetWriteDeadline(t)
|
|
|
|
|
}
|
2021-09-29 09:19:45 +08:00
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
func (this *ClientConn) IsClosed() bool {
|
2021-09-29 09:19:45 +08:00
|
|
|
return this.isClosed
|
|
|
|
|
}
|