protocols

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 20 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 protocols turns xtune's state into what servers and apps run: the Xray server config, client outbounds and the failover config, share links and subscriptions (with their names and flags), and the configs and profiles of the WireGuard, AmneziaWG, SSH and DNS tunnels, MTProto, OpenVPN and IKEv2. It only builds; deploying and measuring live in the main package.

Index

Constants

View Source
const (
	MTProtoFrameLen = 64
	MTProtoKeyLen   = 16 // the proxy secret
)
View Source
const PeerStale = 24 * time.Hour

PeerStale is how long a follower that stopped reporting stays in subscriptions.

View Source
const ProbeURL = "https://www.gstatic.com/generate_204"

ProbeURL answers 204: tuning and the failover config's health checks fetch it.

Variables

View Source
var Countries = map[string][2]string{
	"ir": {"geosite:category-ir", "geoip:ir"},
	"cn": {"geosite:cn", "geoip:cn"},
	"ru": {"geosite:category-ru", "geoip:ru"},
}

Countries maps a country code to the client routing lists that send its sites direct.

View Source
var DNSResolvers = []string{"udp:8.8.8.8:53", "udp:1.1.1.1:53", "udp:9.9.9.9:53",
	"doh:https://cloudflare-dns.com/dns-query", "doh:https://dns.google/dns-query", "dot:dns.quad9.net:853"}

DNSResolvers are the resolvers DNS tunnel variants are tried through: plain UDP to public resolvers (often the only thing a censor lets out), and DNS over HTTPS and TLS, which look like web traffic.

View Source
var DefaultWarpDomains = []string{"geosite:category-ai-!cn", "geosite:google-gemini"}

DefaultWarpDomains are AI services that block datacenter IPs or whole countries.

View Source
var FragNames = []string{"none", "frag-mild", "frag-aggr"}
View Source
var NamePlaceholder = regexp.MustCompile(`\{(\w+)\}`)

Functions

func AWGAddr

func AWGAddr(e state.Entry, u state.User) netip.Addr

AWGAddr is the user's tunnel address in the entry's subnet: the last two bytes of their WireGuard address. The server is .0.1, which WireGuard addresses never use.

func AWGClientParams

func AWGClientParams(p state.AWGParams, mode string) (jc, jmin, jmax int, i1 string)

AWGClientParams are p as a client in mode sends them.

func AWGConfFile

func AWGConfFile(st *state.State, e state.Entry, u state.User) string

AWGConfFile is the config file the AmneziaVPN and AmneziaWG apps import.

func AWGUserKey

func AWGUserKey(u state.User) (priv, pub string)

func B64hex

func B64hex(s string) string

func ClientPort

func ClientPort(e state.Entry) int

ClientPort is the port clients dial: a Worker answers on 443 whatever port it relays to.

func ConfigName

func ConfigName(st *state.State, e state.Entry, u state.User) string

ConfigName is what apps show for one config of u.

func DNSTTInfo

func DNSTTInfo(st *state.State, e state.Entry, u state.User) string

DNSTTInfo is what SlowDNS/dnstt apps (HTTP Injector, NetMod, ...) need.

func DialAddr

func DialAddr(st *state.State, e state.Entry, k state.Knobs) string

DialAddr is the address the client connects to: a clean IP for CDN and Worker entries when tuning chose one, the entry's usual address otherwise.

func FlagEmoji

func FlagEmoji(cc string) string

FlagEmoji is the flag of an ISO 3166-1 alpha-2 country code ("DE" → 🇩🇪), "" for anything else.

func HostPort

func HostPort(host string, port int) string

func KindLabel

func KindLabel(kind string) string
func Link(st *state.State, e state.Entry, u state.User, k state.Knobs) string
func Links(st *state.State, u state.User) []string

func MTProtoInfo

func MTProtoInfo(st *state.State, e state.Entry, u state.User) string

MTProtoInfo is what to type into Telegram's proxy settings by hand.

func MTProtoKey

func MTProtoKey(u state.User) []byte
func MTProtoLink(st *state.State, e state.Entry, u state.User) string

MTProtoLink opens the proxy in Telegram and doubles as the page's QR code.

func MTProtoSecret

func MTProtoSecret(u state.User) string

MTProtoSecret is the secret Telegram apps take: the "dd" prefix (padded-intermediate transport, no fake TLS) plus the user's 16-byte key in hex.

func MobileConfig

func MobileConfig(st *state.State, u state.User) string

MobileConfig is an Apple configuration profile (iOS, macOS) with the CA and the IKEv2 VPN.

func OVPNProfile

func OVPNProfile(st *state.State, e state.Entry, u state.User) string

OVPNProfile is an OpenVPN client profile; the app asks for the user name and password.

func OpenVPNConf

func OpenVPNConf(st *state.State, e state.Entry, dir string) string

OpenVPNConf is the server config of one OpenVPN entry; files live in dir.

func ParseCertPEM

func ParseCertPEM(s string) (*x509.Certificate, error)

ParseCertPEM decodes the first certificate in a PEM block.

func PeerServers

func PeerServers(st *state.State) []*state.State

PeerServers are this deployment and, on a primary, the followers that reported recently, with their entries named after them.

func RelayStates

func RelayStates(st *state.State) []*state.State

RelayStates are this server's configs as clients reach them through each relay. A relay that forwards to an old address (after a move) is left out until it is synced.

func RelayTarget

func RelayTarget(st *state.State) string

RelayTarget is the address of this server relays forward to.

func Relayable

func Relayable(e state.Entry) bool

Relayable entries can go through a port forwarder: the others are reached by domain (CDN, Worker), through DNS resolvers (dnstt), or are VPNs whose certificate or profile names this server.

func SSHInfo

func SSHInfo(st *state.State, e state.Entry, u state.User) string

SSHInfo is what HTTP Injector, NetMod and similar apps need for an SSH entry: where to connect, whether to wrap it in TLS (with which SNI), and the payload to send first.

func ServerFlag

func ServerFlag(st *state.State) string

ServerFlag is the country code behind {flag} and {country}, "" when unknown or off.

func SswanProfile

func SswanProfile(st *state.State, u state.User) string

SswanProfile is a strongSwan app profile: the CA is included, the app asks for the password.

func SubName

func SubName(st *state.State, u state.User) string

SubName is the subscription's name in apps (Profile-Title) and of the failover config.

func Subscription

func Subscription(st *state.State, u state.User) string

Subscription is the base64 body most clients accept as an imported subscription.

func SwanctlConf

func SwanctlConf(st *state.State, users []state.User) string

SwanctlConf is strongSwan's config for the ikev2 entry and the active users' EAP secrets. The helper installs the certificate and key into strongSwan's x509 and private directories, the only place it loads unencrypted keys from.

func TunnelPassword

func TunnelPassword(u state.User) string

TunnelPassword is the user's password for SSH, OpenVPN and IKEv2.

func UAPIShared

func UAPIShared(p state.AWGParams, b *strings.Builder)

func VPNSubnet

func VPNSubnet(e state.Entry) string

VPNSubnet is each VPN entry's client address range.

func VPNUsers

func VPNUsers(users []state.User) map[string]VPNUser

func ValidCountryCode

func ValidCountryCode(cc string) bool

func WGConf

func WGConf(st *state.State, e state.Entry, u state.User) string

WGConf is the config file for the official WireGuard apps.

func WGMTU

func WGMTU(k state.Knobs) int

func WGUserKey

func WGUserKey(u state.User) (priv, pub string)

func WarpDomains

func WarpDomains(w *state.Warp) []string

Types

type AWGConf

type AWGConf struct {
	Params  state.AWGParams `json:"params"`
	Entries []state.Entry   `json:"entries"`
	Peers   []AWGPeer       `json:"peers"`
	Stats   string          `json:"stats"` // where the server writes its counters (tunnelStats)
}

AWGConf is what `xtune awg run` serves.

func AWGServerConf

func AWGServerConf(st *state.State, entries []state.Entry, users []state.User, statsPath string) AWGConf

AWGServerConf builds the AmneziaWG server's config for these users (enabled ones only).

func (AWGConf) UAPIPeers

func (c AWGConf) UAPIPeers(e state.Entry) string

func (AWGConf) UAPIServer

func (c AWGConf) UAPIServer(e state.Entry) string

UAPIServer is the device config of entry e. The server sends junk packets too when it starts a handshake, so it gets the deployment's Jc/Jmin/Jmax.

type AWGPeer

type AWGPeer struct {
	Name string `json:"name"`
	Pub  string `json:"pub"`
	WGIP string `json:"wgIp"`
}

type J

type J = map[string]any

func CertFiles

func CertFiles(dir string) []J

func ClientConfig

func ClientConfig(out J) J

ClientConfig is a ready-to-run client config (socks 10808, http 10809).

func FailoverConfig

func FailoverConfig(st *state.State, u state.User) J

FailoverConfig is a full client config with every deployed config behind one balancer: Xray health-checks them and uses the fastest working one, so a blocked config fails over by itself (to the CDN entry, for example). Private addresses, and the chosen country's sites, go direct; ads are blocked. Domain strategy AsIs keeps DNS lookups for proxied sites off the local network.

func FragOutbounds

func FragOutbounds() []J

func Inbound

func Inbound(st *state.State, e state.Entry, users []state.User) J

func Outbound

func Outbound(st *state.State, e state.Entry, u state.User, k state.Knobs, tag string) J

func ServerConfig

func ServerConfig(st *state.State, entries []state.Entry, users []state.User) J

ServerConfig builds the full Xray config for the given entries and their enabled users.

type NamedLink struct{ Name, URL string }

NamedLink is a link and what to call it: a config's share link, a profile file, an app's import link.

func NamedLinks(st *state.State, u state.User) []NamedLink

NamedLinks are the user's share links on every server, named by config.

type Profile

type Profile struct {
	Name string // shown to the user
	File string
	Type string // MIME type
	Body string
}

Profile is a file a user imports into an app.

func Profiles

func Profiles(st *state.State, u state.User) []Profile

Profiles are the user's files for the entries that have no share link, and a WireGuard config for the official app.

type TunnelConf

type TunnelConf struct {
	Entries   []state.Entry `json:"entries"`
	Users     []TunnelUser  `json:"users"`
	HostKey   string        `json:"hostKey"`
	CertPEM   string        `json:"certPem"`
	KeyPEM    string        `json:"keyPem"`
	DNSTTKey  string        `json:"dnsttKey,omitempty"`
	DNSDomain string        `json:"dnsDomain,omitempty"`
	Stats     string        `json:"stats"` // where the server writes its counters
}

TunnelConf is what the tunnel server runs.

func TunnelServerConf

func TunnelServerConf(st *state.State, entries []state.Entry, users []state.User, statsPath string) TunnelConf

TunnelServerConf builds the tunnel server's config for these users.

type TunnelUser

type TunnelUser struct {
	Name    string `json:"name"`
	Pass    string `json:"pass"`
	MT      string `json:"mt,omitempty"` // MTProto proxy secret (hex), when an mtproto config is deployed
	Devices int    `json:"devices,omitempty"`
}

type VPNUser

type VPNUser struct {
	Pass    string `json:"pass"`
	Devices int    `json:"devices,omitempty"`
}

VPNUser is what `xtune vpn-auth` checks an OpenVPN login against.

Jump to

Keyboard shortcuts

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