mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-04-07 05:47:36 +03:00
platform: Prepare connections list
This commit is contained in:
parent
7ef8b71fa6
commit
d0e2c7e29c
18 changed files with 651 additions and 254 deletions
53
experimental/libbox/command_close_connection.go
Normal file
53
experimental/libbox/command_close_connection.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package libbox
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-box/experimental/clashapi"
|
||||
"github.com/sagernet/sing/common/binary"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
|
||||
"github.com/gofrs/uuid/v5"
|
||||
)
|
||||
|
||||
func (c *CommandClient) CloseConnection(connId string) error {
|
||||
conn, err := c.directConnect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
writer := bufio.NewWriter(conn)
|
||||
err = binary.WriteData(writer, binary.BigEndian, connId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = writer.Flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return readError(conn)
|
||||
}
|
||||
|
||||
func (s *CommandServer) handleCloseConnection(conn net.Conn) error {
|
||||
reader := bufio.NewReader(conn)
|
||||
var connId string
|
||||
err := binary.ReadData(reader, binary.BigEndian, &connId)
|
||||
if err != nil {
|
||||
return E.Cause(err, "read connection id")
|
||||
}
|
||||
service := s.service
|
||||
if service == nil {
|
||||
return writeError(conn, E.New("service not ready"))
|
||||
}
|
||||
clashServer := service.instance.Router().ClashServer()
|
||||
if clashServer == nil {
|
||||
return writeError(conn, E.New("Clash API disabled"))
|
||||
}
|
||||
targetConn := clashServer.(*clashapi.Server).TrafficManager().Connection(uuid.FromStringOrNil(connId))
|
||||
if targetConn == nil {
|
||||
return writeError(conn, E.New("connection already closed"))
|
||||
}
|
||||
targetConn.Close()
|
||||
return writeError(conn, nil)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue