ipc

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2025 License: GPL-3.0 Imports: 33 Imported by: 0

Documentation

Overview

Package ipc implements the IPC server for communicating between the client and the VPN service. It provides HTTP endpoints for retrieving statistics, managing groups, selecting outbounds, changing modes, and closing connections.

Index

Constants

View Source
const (
	StatusInitializing = "initializing"
	StatusConnecting   = "connecting"
	StatusRunning      = "running"
	StatusClosing      = "closing"
	StatusClosed       = "closed"
)

Variables

View Source
var (
	ErrServiceIsNotReady = errors.New("service is not ready")
	ErrIPCNotRunning     = errors.New("IPC not running")
)

Functions

func CloseConnections

func CloseConnections(ctx context.Context, connIDs []string) error

CloseConnections closes connections by their IDs. If connIDs is empty, all connections will be closed.

func CloseService

func CloseService(ctx context.Context) error

CloseService sends a request to shutdown the service

func GetActiveOutbound

func GetActiveOutbound(ctx context.Context) (group, tag string, err error)

GetActiveOutbound retrieves the outbound that is actively being used, resolving nested groups if necessary.

func GetClashMode

func GetClashMode(ctx context.Context) (string, error)

GetClashMode retrieves the current mode from the Clash server.

func GetSelected

func GetSelected(ctx context.Context) (group, tag string, err error)

GetSelected retrieves the currently selected outbound and its group.

func GetStatus

func GetStatus(ctx context.Context) (string, error)

GetStatus retrieves the current status of the service.

func SelectOutbound

func SelectOutbound(ctx context.Context, groupTag, outboundTag string) error

SelectOutbound selects an outbound within a group.

func SetClashMode

func SetClashMode(ctx context.Context, mode string) error

SetClashMode sets the mode of the Clash server.

func SetSocketPath

func SetSocketPath(path string)

SetSocketPath sets the path for the Unix domain socket file for client connections.

func StartService

func StartService(ctx context.Context, group, tag string) error

StartService sends a request to start the service

func StopService

func StopService(ctx context.Context) error

StopService sends a request to stop the service (IPC server stays up)

Types

type Connection

type Connection struct {
	ID           string
	Inbound      string
	IPVersion    int
	Network      string
	Source       string
	Destination  string
	Domain       string
	Protocol     string
	FromOutbound string
	CreatedAt    int64
	ClosedAt     int64
	Uplink       int64
	Downlink     int64
	Rule         string
	Outbound     string
	ChainList    []string
}

Connection represents a network connection with relevant metadata.

func GetConnections

func GetConnections(ctx context.Context) ([]Connection, error)

GetConnections retrieves the list of current and recently closed connections.

type Metrics

type Metrics struct {
	Memory      uint64
	Goroutines  int
	Connections int

	// UplinkTotal and DownlinkTotal are only available when the service is running and there are
	// active connections.
	// In bytes.
	UplinkTotal int64
	// In bytes.
	DownlinkTotal int64
}

Metrics represents the runtime metrics of the service.

func GetMetrics

func GetMetrics(ctx context.Context) (Metrics, error)

GetMetrics retrieves the current runtime metrics of the service.

type OutboundGroup

type OutboundGroup struct {
	Tag       string
	Type      string
	Selected  string
	Outbounds []Outbounds
}

OutboundGroup represents a group of outbounds.

func GetGroups

func GetGroups(ctx context.Context) ([]OutboundGroup, error)

GetGroups retrieves the list of group outbounds.

type Outbounds

type Outbounds struct {
	Tag  string
	Type string
}

Outbounds represents outbounds within a group.

type Selector

type Selector interface {
	adapter.OutboundGroup
	SelectOutbound(tag string) bool
}

Selector is helper interface to check if an outbound is a selector or wrapper of selector.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the IPC server that communicates over a Unix domain socket for Unix-like systems, and a named pipe for Windows.

func NewServer

func NewServer(service Service) *Server

NewServer creates a new Server instance with the provided Service.

func (*Server) Close

func (s *Server) Close() error

Close shuts down the IPC server.

func (*Server) GetStatus

func (s *Server) GetStatus() string

func (*Server) SetService

func (s *Server) SetService(svc Service) Service

SetService updates the service attached to the server and returns the old service, if any. Typically called when starting or replacing the VPN tunnel.

func (*Server) Start

func (s *Server) Start(basePath string, fn StartFn) error

Start starts the IPC server. The socket file will be created in the "basePath" directory. On Windows, the "basePath" is ignored and a default named pipe path is used.

func (*Server) StartService

func (s *Server) StartService(ctx context.Context, group, tag string) error

func (*Server) StopService

func (s *Server) StopService(ctx context.Context) error

type Service

type Service interface {
	Ctx() context.Context
	Status() string
	ClashServer() *clashapi.Server
	Close() error
}

Service defines the interface that the IPC server uses to interact with the underlying VPN service.

type StartFn

type StartFn func(ctx context.Context, group, tag string) (Service, error)

type StatusUpdateEvent

type StatusUpdateEvent struct {
	events.Event
	Status VPNStatus
}

StatusUpdateEvent is emitted when the VPN status changes.

type VPNStatus

type VPNStatus string
const (
	Connected     VPNStatus = "connected"
	Disconnected  VPNStatus = "disconnected"
	Connecting    VPNStatus = "connecting"
	Disconnecting VPNStatus = "disconnecting"
	ErrorStatus   VPNStatus = "error"
)

Possible VPN statuses

func (*VPNStatus) String

func (vpn *VPNStatus) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL