core

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const VerifyPortBase = 29000

VerifyPortBase is the start of the scratch port range used only by short-lived VerifyNodeReal processes. Deliberately distinct from the user's configured ports so verification never collides with an already-running session. Callers running N verifications concurrently should pass ports spaced apart within this range (see app.verifyRealConnectivity).

Variables

View Source
var IranDirectDomains = []string{
	"domain:.ir",
	"domain:.xn--mgba3a4f16a",
	"geosite:ir",
	"geosite:category-ir",
	"geosite:category-ir-gov",
	"geosite:category-ir-bank",
	"geosite:category-ir-shopping",
	"geosite:category-ir-education",
	"geosite:category-ir-news",
	"geosite:category-ir-media",
	"geosite:category-ir-tech",
	"domain:shaparak.ir",
	"domain:divar.ir",
	"domain:snapp.ir",
	"domain:digikala.com",
	"domain:torob.com",
	"domain:aparat.com",
	"domain:telewebion.com",
	"domain:varzesh3.com",
	"domain:filimo.com",
	"domain:cafebazaar.ir",
	"domain:myket.ir",
	"domain:rubika.ir",
	"domain:eitaa.com",
	"domain:bale.ai",
	"domain:splus.ir",
	"domain:nobitex.ir",
	"domain:wallex.ir",
	"domain:abantether.com",
	"domain:mci.ir",
	"domain:irancell.ir",
	"domain:rightel.ir",
	"domain:tci.ir",
	"domain:shatel.ir",
	"domain:asiatech.ir",
	"domain:parsonline.com",
	"domain:hiweb.ir",
}

IranDirectDomains contains prominent Iranian domain suffixes and key domestic infrastructure domains

View Source
var IranDirectIPRanges = []string{
	"geoip:ir",
	"geoip:private",
	"10.0.0.0/8",
	"172.16.0.0/12",
	"192.168.0.0/16",
	"127.0.0.0/8",
	"100.64.0.0/10",
	"169.254.0.0/16",
	"::1/128",
	"fc00::/7",
	"fe80::/10",
}

IranDirectIPRanges contains private LAN ranges and Iran GeoIP

Functions

func EnsureSingBoxBinary

func EnsureSingBoxBinary() (string, error)

EnsureSingBoxBinary finds sing-box or automatically downloads it into ~/.vify/bin/sing-box

func FormatBytes

func FormatBytes(b int64) string

FormatBytes formats byte counts into human-readable strings (KB, MB, GB)

func FormatSpeed

func FormatSpeed(bytesPerSec int64) string

FormatSpeed formats speed bytes/sec into human-readable string

func VerifyNodeReal

func VerifyNodeReal(ctx context.Context, node *model.ProxyNode, testURL string, socksPort, httpPort int) (time.Duration, error)

VerifyNodeReal confirms a candidate node's proxy protocol actually works, not just that its TCP port is open. The initial benchmark (ping.DialTest) only dials a raw socket and, for TLS nodes, completes a generic TLS handshake with certificate verification disabled — that passes for any listening port, or any server willing to complete a TLS handshake, regardless of whether a real VLESS/VMess/Trojan/Shadowsocks proxy is actually behind it. This instead starts a short-lived, real sing-box process for the node and performs one genuine HTTP request through it. socksPort/httpPort must be unique per concurrently-running call.

Types

type Engine

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

Engine orchestrates the VPN / Proxy connection, sing-box runner, TUN and System Proxy modes

func NewEngine

func NewEngine(cfg *config.AppConfig) *Engine

NewEngine creates a new VPN/Proxy Engine instance

func (*Engine) FailoverToNext

func (e *Engine) FailoverToNext()

FailoverToNext seamlessly switches outbound node to the next available healthy node

func (*Engine) GetCurrentNode

func (e *Engine) GetCurrentNode() *model.ProxyNode

GetCurrentNode returns currently connected node

func (*Engine) GetStats

func (e *Engine) GetStats() *TrafficStats

GetStats returns current traffic statistics

func (*Engine) StartNode

func (e *Engine) StartNode(ctx context.Context, node *model.ProxyNode, mode model.ConnectionMode, backupNodes []*model.ProxyNode) error

StartNode initiates connection to a specific proxy node

func (*Engine) Stop

func (e *Engine) Stop() error

Stop cleanly terminates proxy process, restores system settings, and cleans session

type SingBoxConfig

type SingBoxConfig struct {
	Log          map[string]interface{}   `json:"log"`
	DNS          map[string]interface{}   `json:"dns"`
	Inbounds     []map[string]interface{} `json:"inbounds"`
	Outbounds    []map[string]interface{} `json:"outbounds"`
	Route        map[string]interface{}   `json:"route"`
	Experimental map[string]interface{}   `json:"experimental,omitempty"`
}

SingBoxConfig generates valid sing-box JSON configuration for TUN or System Proxy modes

func GenerateConfig

func GenerateConfig(node *model.ProxyNode, mode model.ConnectionMode, socksPort, httpPort, clashAPIPort int, bypassIran bool) (*SingBoxConfig, error)

GenerateConfig generates a complete sing-box configuration struct. clashAPIPort enables sing-box's Clash-compatible API (used to read live traffic totals) on 127.0.0.1:clashAPIPort; pass 0 to omit it entirely — callers spinning up multiple concurrent short-lived instances (e.g. real connectivity verification) that don't need traffic stats MUST pass 0, since every instance would otherwise fight over the same hardcoded port and fail to start.

func (*SingBoxConfig) ToJSON

func (c *SingBoxConfig) ToJSON() ([]byte, error)

ToJSON serializes SingBoxConfig to formatted JSON

type SystemProxyController

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

SystemProxyController manages enabling and disabling OS system proxy

func NewSystemProxyController

func NewSystemProxyController(httpPort, socksPort int) *SystemProxyController

NewSystemProxyController creates a new controller

func (*SystemProxyController) Disable

func (s *SystemProxyController) Disable() error

Disable turns off system proxy settings and restores original state

func (*SystemProxyController) Enable

func (s *SystemProxyController) Enable() error

Enable sets the system proxy for the current OS

type TrafficStats

type TrafficStats struct {
	BytesUpload   int64
	BytesDownload int64
	UploadSpeed   int64 // bytes per second
	DownloadSpeed int64 // bytes per second
	// contains filtered or unexported fields
}

TrafficStats tracks bandwidth and upload/download rates

func NewTrafficStats

func NewTrafficStats() *TrafficStats

NewTrafficStats initializes a new traffic monitor

func (*TrafficStats) AddDownload

func (s *TrafficStats) AddDownload(n int64)

AddDownload records downloaded bytes

func (*TrafficStats) AddUpload

func (s *TrafficStats) AddUpload(n int64)

AddUpload records uploaded bytes

func (*TrafficStats) SetTotals

func (s *TrafficStats) SetTotals(download, upload int64)

SetTotals overwrites cumulative byte counters with absolute totals reported by an external source (e.g. sing-box's Clash API).

func (*TrafficStats) Snapshot

func (s *TrafficStats) Snapshot() (uploadTotal, downloadTotal, upSpeed, downSpeed int64)

Snapshot returns a copy of current metrics

func (*TrafficStats) Tick

func (s *TrafficStats) Tick()

Tick calculates current speed rates (should be called once per second)

type Watchdog

type Watchdog struct {
	Interval       time.Duration
	TestURL        string
	ProxyURL       string
	FailThreshold  int
	OnFailover     func(reason string)
	OnHealthUpdate func(latency time.Duration, ok bool)
	// contains filtered or unexported fields
}

Watchdog monitors the active proxy connection and triggers failover when disconnected

func NewWatchdog

func NewWatchdog(intervalSec int, socksPort int, testURL string) *Watchdog

NewWatchdog creates a new connection Watchdog

func (*Watchdog) Start

func (w *Watchdog) Start(ctx context.Context)

Start begins background health monitoring

func (*Watchdog) Stop

func (w *Watchdog) Stop()

Stop terminates the watchdog loop

Jump to

Keyboard shortcuts

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