WTF is this

This commit is contained in:
世界 2024-06-24 09:49:15 +08:00
parent bda93d516b
commit 923d3222b0
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
41 changed files with 421 additions and 507 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/sagernet/sing/common/binary"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/varbin"
"github.com/gofrs/uuid/v5"
)
@ -19,14 +20,18 @@ import (
func (c *CommandClient) handleConnectionsConn(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
var connections Connections
var (
rawConnections []Connection
connections Connections
)
for {
rawConnections = nil
err := binary.ReadData(reader, binary.BigEndian, &connections.connections)
err := varbin.Read(reader, binary.BigEndian, &rawConnections)
if err != nil {
c.handler.Disconnected(err.Error())
return
}
connections.input = rawConnections
c.handler.WriteConnections(&connections)
}
}
@ -70,7 +75,7 @@ func (s *CommandServer) handleConnectionsConn(conn net.Conn) error {
for _, connection := range trafficManager.ClosedConnections() {
outConnections = append(outConnections, newConnection(connections, connection, true))
}
err = binary.WriteData(writer, binary.BigEndian, outConnections)
err = varbin.Write(writer, binary.BigEndian, outConnections)
if err != nil {
return err
}
@ -93,33 +98,32 @@ const (
)
type Connections struct {
connections []Connection
filteredConnections []Connection
outConnections *[]Connection
input []Connection
filtered []Connection
}
func (c *Connections) FilterState(state int32) {
c.filteredConnections = c.filteredConnections[:0]
c.filtered = c.filtered[:0]
switch state {
case ConnectionStateAll:
c.filteredConnections = append(c.filteredConnections, c.connections...)
c.filtered = append(c.filtered, c.input...)
case ConnectionStateActive:
for _, connection := range c.connections {
for _, connection := range c.input {
if connection.ClosedAt == 0 {
c.filteredConnections = append(c.filteredConnections, connection)
c.filtered = append(c.filtered, connection)
}
}
case ConnectionStateClosed:
for _, connection := range c.connections {
for _, connection := range c.input {
if connection.ClosedAt != 0 {
c.filteredConnections = append(c.filteredConnections, connection)
c.filtered = append(c.filtered, connection)
}
}
}
}
func (c *Connections) SortByDate() {
slices.SortStableFunc(c.filteredConnections, func(x, y Connection) int {
slices.SortStableFunc(c.filtered, func(x, y Connection) int {
if x.CreatedAt < y.CreatedAt {
return 1
} else if x.CreatedAt > y.CreatedAt {
@ -131,7 +135,7 @@ func (c *Connections) SortByDate() {
}
func (c *Connections) SortByTraffic() {
slices.SortStableFunc(c.filteredConnections, func(x, y Connection) int {
slices.SortStableFunc(c.filtered, func(x, y Connection) int {
xTraffic := x.Uplink + x.Downlink
yTraffic := y.Uplink + y.Downlink
if xTraffic < yTraffic {
@ -145,7 +149,7 @@ func (c *Connections) SortByTraffic() {
}
func (c *Connections) SortByTrafficTotal() {
slices.SortStableFunc(c.filteredConnections, func(x, y Connection) int {
slices.SortStableFunc(c.filtered, func(x, y Connection) int {
xTraffic := x.UplinkTotal + x.DownlinkTotal
yTraffic := y.UplinkTotal + y.DownlinkTotal
if xTraffic < yTraffic {
@ -159,7 +163,7 @@ func (c *Connections) SortByTrafficTotal() {
}
func (c *Connections) Iterator() ConnectionIterator {
return newPtrIterator(c.filteredConnections)
return newPtrIterator(c.filtered)
}
type Connection struct {