controller

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: GPL-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package controller serves the RESTful "external controller" API of Clash and mihomo, so web panels such as zashboard, metacubexd and yacd can watch and steer sudoku. The routes, JSON shapes and status codes follow mihomo; see https://wiki.metacubex.one/api/.

Index

Constants

View Source
const (
	TypeDirect      = "Direct"
	TypeReject      = "Reject"
	TypeSelector    = "Selector"
	TypeLoadBalance = "LoadBalance"
	TypeSudoku      = "Sudoku"
)

Proxy types, as mihomo names them.

Variables

This section is empty.

Functions

func Start

func Start(opts Options) (stop func(), err error)

Start serves the API on opts.Addr in the background until stop is called.

Types

type Client

type Client interface {
	Mode() string // "rule", "global" or "direct"
	SetMode(mode string) error
	MixedPort() int

	// Proxies lists every outbound and group, groups first in the order panels should show them.
	Proxies() []Proxy
	// Select makes the selector group use the named member.
	Select(group, name string) error
	// DialContext connects to addr through the named outbound or group, as traffic would.
	DialContext(ctx context.Context, proxy, addr string) (net.Conn, error)

	Rules() []Rule
	RuleProviders() []RuleProvider
	// UpdateRuleProvider starts reloading a provider; false means there is none by that name.
	UpdateRuleProvider(name string) bool
}

Client is what the API needs from a running client.

type DelayHistory

type DelayHistory struct {
	Time  time.Time `json:"time"`
	Delay uint16    `json:"delay"` // ms; 0 = the test failed
}

type Flow

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

Flow is one tracked connection. Upload counts bytes toward the destination.

func (*Flow) AddDown

func (f *Flow) AddDown(n int)

func (*Flow) AddUp

func (f *Flow) AddUp(n int)

func (*Flow) Done

func (f *Flow) Done()

Done stops tracking the flow, keeping its bytes in the totals. It is safe to call twice.

type Metadata

type Metadata struct {
	Network           string   `json:"network"`
	Type              string   `json:"type"`
	SourceIP          string   `json:"sourceIP"`
	DestinationIP     string   `json:"destinationIP"`
	SourceGeoIP       []string `json:"sourceGeoIP"`
	DestinationGeoIP  []string `json:"destinationGeoIP"`
	SourceIPASN       string   `json:"sourceIPASN"`
	DestinationIPASN  string   `json:"destinationIPASN"`
	SourcePort        string   `json:"sourcePort"`
	DestinationPort   string   `json:"destinationPort"`
	InboundIP         string   `json:"inboundIP"`
	InboundPort       string   `json:"inboundPort"`
	InboundName       string   `json:"inboundName"`
	InboundUser       string   `json:"inboundUser"`
	Host              string   `json:"host"`
	DNSMode           string   `json:"dnsMode"`
	UID               uint32   `json:"uid"`
	Process           string   `json:"process"`
	ProcessPath       string   `json:"processPath"`
	SpecialProxy      string   `json:"specialProxy"`
	SpecialRules      string   `json:"specialRules"`
	RemoteDestination string   `json:"remoteDestination"`
	DSCP              uint8    `json:"dscp"`
	SniffHost         string   `json:"sniffHost"`
}

Metadata describes a connection with the field names mihomo uses. Fields sudoku has no notion of stay empty but present, since panels read them unconditionally.

type Options

type Options struct {
	Addr   string // host:port to listen on
	Secret string // bearer token; empty leaves the API open

	Client  Client
	Tracker *Tracker

	Users func() []users.Stat
}

Options selects what the API exposes. A client sets Client and Tracker; a server sets Users.

type Proxy

type Proxy struct {
	Name string
	Type string
	UDP  bool
	Now  string   // the member a Selector uses
	All  []string // a group's members; nil for anything else
}

type Route

type Route struct {
	Chains      []string
	Rule        string
	RulePayload string
}

Route is how a connection left: Chains runs from the outbound actually used up through the groups that chose it, e.g. ["node", "Balance", "PROXY"] or ["DIRECT"]. Rule and RulePayload name the rule that matched, empty when the mode alone decided.

type Rule

type Rule struct {
	Type    string // e.g. "RuleSet" or "Match"
	Payload string
	Proxy   string
}

type RuleProvider

type RuleProvider struct {
	Name      string
	RuleCount int
	UpdatedAt time.Time
}

type Tracker

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

Tracker lists the connections a client is relaying and counts their bytes. A nil *Tracker tracks nothing, so callers need no checks when the API is off.

func NewTracker

func NewTracker() *Tracker

func (*Tracker) Close

func (t *Tracker) Close(id string)

Close ends the connection with the given id, if it is still open.

func (*Tracker) CloseAll

func (t *Tracker) CloseAll()

CloseAll ends every tracked connection.

func (*Tracker) Open

func (t *Tracker) Open(meta Metadata, route Route, closer io.Closer) *Flow

Open starts tracking a connection that closer ends. The caller counts its bytes and must call Done once it is over.

func (*Tracker) Totals

func (t *Tracker) Totals() (up, down uint64)

Totals returns the bytes uploaded and downloaded since start, closed connections included.

func (*Tracker) TrackConn

func (t *Tracker) TrackConn(c net.Conn, meta Metadata, route Route) net.Conn

TrackConn wraps an outbound connection so its bytes are counted and closing it ends the flow.

Jump to

Keyboard shortcuts

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