Feature: add experimental connections API
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
)
|
||||
|
||||
@ -14,6 +15,7 @@ const (
|
||||
UDP
|
||||
|
||||
HTTP Type = iota
|
||||
HTTPCONNECT
|
||||
SOCKS
|
||||
REDIR
|
||||
)
|
||||
@ -27,18 +29,41 @@ func (n *NetWork) String() string {
|
||||
return "udp"
|
||||
}
|
||||
|
||||
func (n NetWork) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(n.String())
|
||||
}
|
||||
|
||||
type Type int
|
||||
|
||||
func (t Type) String() string {
|
||||
switch t {
|
||||
case HTTP:
|
||||
return "HTTP"
|
||||
case HTTPCONNECT:
|
||||
return "HTTP Connect"
|
||||
case SOCKS:
|
||||
return "Socks5"
|
||||
case REDIR:
|
||||
return "Redir"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func (t Type) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
// Metadata is used to store connection address
|
||||
type Metadata struct {
|
||||
NetWork NetWork
|
||||
Type Type
|
||||
SrcIP *net.IP
|
||||
DstIP *net.IP
|
||||
SrcPort string
|
||||
DstPort string
|
||||
AddrType int
|
||||
Host string
|
||||
NetWork NetWork `json:"network"`
|
||||
Type Type `json:"type"`
|
||||
SrcIP net.IP `json:"sourceIP"`
|
||||
DstIP net.IP `json:"destinationIP"`
|
||||
SrcPort string `json:"sourcePort"`
|
||||
DstPort string `json:"destinationPort"`
|
||||
AddrType int `json:"-"`
|
||||
Host string `json:"host"`
|
||||
}
|
||||
|
||||
func (m *Metadata) RemoteAddress() string {
|
||||
|
@ -24,7 +24,7 @@ func (rt RuleType) String() string {
|
||||
case DomainKeyword:
|
||||
return "DomainKeyword"
|
||||
case GEOIP:
|
||||
return "GEOIP"
|
||||
return "GeoIP"
|
||||
case IPCIDR:
|
||||
return "IPCIDR"
|
||||
case SrcIPCIDR:
|
||||
@ -34,7 +34,7 @@ func (rt RuleType) String() string {
|
||||
case DstPort:
|
||||
return "DstPort"
|
||||
case MATCH:
|
||||
return "MATCH"
|
||||
return "Match"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Traffic struct {
|
||||
up chan int64
|
||||
down chan int64
|
||||
upCount int64
|
||||
downCount int64
|
||||
upTotal int64
|
||||
downTotal int64
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
func (t *Traffic) Up() chan<- int64 {
|
||||
return t.up
|
||||
}
|
||||
|
||||
func (t *Traffic) Down() chan<- int64 {
|
||||
return t.down
|
||||
}
|
||||
|
||||
func (t *Traffic) Now() (up int64, down int64) {
|
||||
return t.upTotal, t.downTotal
|
||||
}
|
||||
|
||||
func (t *Traffic) handle() {
|
||||
go t.handleCh(t.up, &t.upCount, &t.upTotal)
|
||||
go t.handleCh(t.down, &t.downCount, &t.downTotal)
|
||||
}
|
||||
|
||||
func (t *Traffic) handleCh(ch <-chan int64, count *int64, total *int64) {
|
||||
ticker := time.NewTicker(t.interval)
|
||||
for {
|
||||
select {
|
||||
case n := <-ch:
|
||||
*count += n
|
||||
case <-ticker.C:
|
||||
*total = *count
|
||||
*count = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewTraffic(interval time.Duration) *Traffic {
|
||||
t := &Traffic{
|
||||
up: make(chan int64),
|
||||
down: make(chan int64),
|
||||
interval: interval,
|
||||
}
|
||||
go t.handle()
|
||||
return t
|
||||
}
|
Reference in New Issue
Block a user