Documentation
¶
Index ¶
- Constants
- Variables
- func ConfigTaskPorts(t *Tunnel) (ports, targets []int, ok bool)
- func GetAccountMap(multiAccount *MultiAccount) map[string]string
- func GetMapKeys(m *sync.Map, isSort bool, sortKey, order string) (keys []int)
- func GlobalConnections() (active, limit int64)
- func SetFlowLimitActual(actual bool)
- func SetGlobalMaxConn(limit int)
- func WatchAccessLists(ctx context.Context, interval time.Duration)
- type Client
- func (s *Client) ControlGroup() *byterate.Group
- func (s *Client) CurrentConnections() int32
- func (s *Client) CurrentRates() (int64, int64)
- func (s *Client) CutConn()
- func (s *Client) EnsureWebPassword()
- func (s *Client) GetConn() bool
- func (s *Client) GetTunnelNum() (num int)
- func (s *Client) HasHost(h *Host) (hh *Host, exist bool)
- func (s *Client) HasTunnel(t *Tunnel) (tt *Tunnel, exist bool)
- func (s *Client) LastSeenTime() string
- func (s *Client) MarkSeen(now time.Time)
- func (s *Client) RecordConnection(online bool, now time.Time)
- func (s *Client) RegisterHoursLimit(def int) int
- func (s *Client) SetMaxConn(limit int)
- type Config
- type ConnectionEvent
- type ConnectionHistory
- type DbUtils
- func (s *DbUtils) DelClient(id int) error
- func (s *DbUtils) DelHost(id int) error
- func (s *DbUtils) DelTask(id int) error
- func (s *DbUtils) FindCertByHost(host string) (*Host, error)
- func (s *DbUtils) GetClient(id int) (c *Client, err error)
- func (s *DbUtils) GetClientIdByBlake2bVkey(vkey string) (id int, err error)
- func (s *DbUtils) GetClientIdByMd5Vkey(vkey string) (id int, err error)
- func (s *DbUtils) GetClientList(start, length int, search, sort, order string, clientId int) ([]*Client, int)
- func (s *DbUtils) GetGlobal() (c *Glob)
- func (s *DbUtils) GetHost(start, length int, id int, search string) ([]*Host, int)
- func (s *DbUtils) GetHostById(id int) (h *Host, err error)
- func (s *DbUtils) GetIdByVerifyKey(vKey, addr, localAddr string, hashFunc func(string) string) (id int, err error)
- func (s *DbUtils) GetInfoByHost(host string, r *http.Request) (h *Host, err error)
- func (s *DbUtils) GetTask(id int) (t *Tunnel, err error)
- func (s *DbUtils) GetTaskByMd5Password(p string) (t *Tunnel)
- func (s *DbUtils) GetTaskByMd5PasswordOld(p string) (t *Tunnel)
- func (s *DbUtils) GlobalDenied(addr net.Addr, what string) bool
- func (s *DbUtils) GlobalDeniedString(addr, what string) bool
- func (s *DbUtils) IsHostExist(h *Host) bool
- func (s *DbUtils) IsHostModify(h *Host) bool
- func (s *DbUtils) IsPubClient(id int) bool
- func (s *DbUtils) NewClient(c *Client) error
- func (s *DbUtils) NewHost(t *Host) error
- func (s *DbUtils) NewTask(t *Tunnel) error
- func (s *DbUtils) RangeClientCertHosts(clientID int, fn func(*Host) bool)
- func (s *DbUtils) RefreshAccessLists()
- func (s *DbUtils) ReserveTaskPassword(t *Tunnel) error
- func (s *DbUtils) SaveGlobal(t *Glob) error
- func (s *DbUtils) UpdateClient(t *Client) error
- func (s *DbUtils) UpdateTask(t *Tunnel) error
- func (s *DbUtils) VerifyUserName(username string, id int) (res bool)
- func (s *DbUtils) VerifyVkey(vkey string, id int) (res bool)
- type Flow
- func (s *Flow) Add(in, out int64)
- func (s *Flow) AddOver(in, out int64) (traffic, expired bool)
- func (s *Flow) MarkClient()
- func (s *Flow) MarshalJSON() ([]byte, error)
- func (s *Flow) Over() (traffic, expired bool)
- func (s *Flow) ResetTraffic()
- func (s *Flow) SetFlowLimit(limit int64)
- func (s *Flow) SetLimits(flowLimit int64, timeLimit time.Time)
- func (s *Flow) SetTimeLimit(limit time.Time)
- func (s *Flow) Values() FlowValues
- type FlowValues
- type Glob
- type Health
- type Host
- type JsonDb
- func (s *JsonDb) GetClient(id int) (c *Client, err error)
- func (s *JsonDb) GetClientId() int32
- func (s *JsonDb) GetHostId() int32
- func (s *JsonDb) GetTaskId() int32
- func (s *JsonDb) LoadClientFromJsonFile()
- func (s *JsonDb) LoadGlobalFromJsonFile()
- func (s *JsonDb) LoadHostFromJsonFile()
- func (s *JsonDb) LoadTaskFromJsonFile()
- func (s *JsonDb) StoreClientsToJsonFile() error
- func (s *JsonDb) StoreGlobalToJsonFile() error
- func (s *JsonDb) StoreHostToJsonFile() error
- func (s *JsonDb) StoreTasksToJsonFile() error
- type MultiAccount
- type RateLimits
- type SourceLists
- func (l *SourceLists) CompileSourceLists(scope acl.Scope, owner string)
- func (l *SourceLists) CopySourceLists(other *SourceLists)
- func (l *SourceLists) RefreshSourceLists(owner string)
- func (l *SourceLists) SetSourceLists(priority int, black, white []string, scope acl.Scope, owner string)
- func (l *SourceLists) SourceACL() *acl.Source
- type Target
- type Tunnel
- func (t *Tunnel) AllowsDestination(addr string) bool
- func (t *Tunnel) Clone() *Tunnel
- func (t *Tunnel) CompileDestACL()
- func (t *Tunnel) ControlGroup() *byterate.Group
- func (t *Tunnel) CurrentConnections() int32
- func (t *Tunnel) CutConn()
- func (t *Tunnel) GetConn() bool
- func (t *Tunnel) SetMaxConn(limit int)
- func (t *Tunnel) Update(other *Tunnel)
Constants ¶
const ( AclOff = 0 AclWhitelist = 1 AclBlacklist = 2 )
ACLMode: 0=off, 1=whitelist(deny-by-default), 2=blacklist(allow-by-default)
Variables ¶
var ( Db *DbUtils HostIndex = index.NewDomainIndex() Blake2bVkeyIndex = index.NewStringIDIndex() TaskPasswordIndex = index.NewStringIDIndex() )
var FlowLimitFactor atomic.Int64
FlowLimitFactor is what the flow limits of clients multiply the inbound and outbound traffic by: 1 to count the traffic, 2 to count the actual usage on both sides of the server.
Functions ¶
func ConfigTaskPorts ¶ added in v0.34.8
ConfigTaskPorts expands a task delivered by a client configuration into the server ports it is opened on and, for multi-port tcp/udp tasks, the matching target ports. The server reports one add status per returned port, or a single failure status when ok is false, so both sides use this to stay in step on the configuration connection.
func GetAccountMap ¶ added in v0.26.47
func GetAccountMap(multiAccount *MultiAccount) map[string]string
func GlobalConnections ¶ added in v0.34.10
func GlobalConnections() (active, limit int64)
GlobalConnections reports active forwarding slots and their configured limit.
func SetFlowLimitActual ¶ added in v0.34.9
func SetFlowLimitActual(actual bool)
SetFlowLimitActual makes the flow limits of clients count the actual usage, twice the traffic, or the traffic itself.
func SetGlobalMaxConn ¶ added in v0.34.10
func SetGlobalMaxConn(limit int)
SetGlobalMaxConn updates admission without disconnecting existing transfers.
func WatchAccessLists ¶ added in v0.34.9
WatchAccessLists checks the geodata files every interval until ctx ends and refreshes the access lists when one of them changed, so that updated geodata takes effect. Checking reads file metadata only.
Types ¶
type Client ¶
type Client struct {
Cnf *Config
Id int // id
VerifyKey string // verify key
Mode string // bridge mode
Addr string // client ip
LocalAddr string // client local ip
Remark string // remark
Status bool // allowed to connect
IsConnect bool // connected now
RateLimit int // rate limit (KB/s)
Flow *Flow // flow
ExportFlow int64 // outbound flow
InletFlow int64 // inbound flow
Rate *rate.Rate // rate limiter
NoStore bool // do not store to file
NoDisplay bool // do not display on web
MaxConn int // max concurrent connections
NowConn int32 // legacy JSON snapshot; use CurrentConnections at runtime
WebUserName string // web username
WebPassword string // web password
WebTotpSecret string // web totp secret
ConfigConnAllow bool // allowed by config file
MaxTunnelNum int
Version string
SourceLists
CreateTime string
LastOnlineTime string
ConnectionHistory *ConnectionHistory `json:",omitempty"`
// RegisterKey, if set, lets visitors register their address for the
// client on the registration page, as the verify key does.
RegisterKey string
// RegisterMaxHours bounds how long a registration made on the page
// lasts; 0 means the server default.
RegisterMaxHours int
sync.RWMutex
}
func (*Client) ControlGroup ¶ added in v0.34.10
ControlGroup is the client level of global -> client -> resource admission.
func (*Client) CurrentConnections ¶ added in v0.34.10
CurrentConnections includes all admitted connections of the client's resources.
func (*Client) CurrentRates ¶ added in v0.34.10
CurrentRates reports the client's visitor upload and download speeds. The existing RateLimit continues to limit their combined throughput.
func (*Client) EnsureWebPassword ¶ added in v0.29.30
func (s *Client) EnsureWebPassword()
func (*Client) GetTunnelNum ¶
func (*Client) LastSeenTime ¶ added in v0.34.10
LastSeenTime snapshots the legacy last-online value without changing its meaning.
func (*Client) MarkSeen ¶ added in v0.34.10
MarkSeen updates the legacy last-online value used by full and live lists.
func (*Client) RecordConnection ¶ added in v0.34.10
func (*Client) RegisterHoursLimit ¶ added in v0.34.9
RegisterHoursLimit returns how long a registration made on the page may last at most, in hours: the client's limit, or def when it sets none.
func (*Client) SetMaxConn ¶ added in v0.34.10
SetMaxConn updates the client configuration and its library cap together.
type ConnectionEvent ¶ added in v0.34.10
ConnectionEvent pairs one connection with its confirmed disconnection. OfflineTime remains empty while online or when shutdown prevented recording it.
type ConnectionHistory ¶ added in v0.34.10
type ConnectionHistory struct {
// contains filtered or unexported fields
}
ConnectionHistory keeps a bounded, concurrency-safe persisted session history.
func (*ConnectionHistory) MarshalJSON ¶ added in v0.34.10
func (h *ConnectionHistory) MarshalJSON() ([]byte, error)
func (*ConnectionHistory) UnmarshalJSON ¶ added in v0.34.10
func (h *ConnectionHistory) UnmarshalJSON(b []byte) error
type DbUtils ¶
type DbUtils struct {
JsonDb *JsonDb
}
func (*DbUtils) FindCertByHost ¶
FindCertByHost returns the host entry that terminates TLS for the server name host. An entry for the exact name beats a wildcard one, and a longer wildcard suffix a shorter one; among entries for the same name, one with TLS configured (a certificate or AutoSSL) wins, then the one with the shortest location, then the lowest id, so the choice never depends on map order. Names compare case-insensitively, ignoring a trailing dot.
func (*DbUtils) GetClientIdByBlake2bVkey ¶ added in v0.29.9
func (*DbUtils) GetClientIdByMd5Vkey ¶ added in v0.27.0
func (*DbUtils) GetClientList ¶
func (*DbUtils) GetIdByVerifyKey ¶
func (*DbUtils) GetInfoByHost ¶
GetInfoByHost get key by host from x
func (*DbUtils) GetTaskByMd5Password ¶
GetTaskByMd5Password md5 password
func (*DbUtils) GetTaskByMd5PasswordOld ¶ added in v0.29.9
func (*DbUtils) GlobalDenied ¶ added in v0.34.9
GlobalDenied reports whether the global access lists, the firewall in front of every listener of nps, deny addr, and logs a denial. what names the listener in the log.
func (*DbUtils) GlobalDeniedString ¶ added in v0.34.9
GlobalDeniedString is GlobalDenied for an address given as text.
func (*DbUtils) IsHostExist ¶
func (*DbUtils) IsHostModify ¶
func (*DbUtils) IsPubClient ¶
func (*DbUtils) NewTask ¶
NewTask adds t. A password explicitly set on a task created through the web or API that is already used by another task is rejected; for tasks delivered by a client configuration (NoStore) it is replaced by a random one, see reserveTaskPassword.
func (*DbUtils) RangeClientCertHosts ¶ added in v0.34.8
RangeClientCertHosts calls fn, until it returns false, for each host entry of the client clientID with a certificate configured.
func (*DbUtils) RefreshAccessLists ¶ added in v0.34.9
func (s *DbUtils) RefreshAccessLists()
RefreshAccessLists recompiles the access lists whose geodata changed.
func (*DbUtils) ReserveTaskPassword ¶ added in v0.34.8
ReserveTaskPassword claims t.Password for t.Id under the same rules as NewTask and UpdateTask. It is used before an existing task is updated in place from t.
func (*DbUtils) SaveGlobal ¶
func (*DbUtils) UpdateClient ¶
func (*DbUtils) UpdateTask ¶
UpdateTask stores t in place of the task with the same id. t may be the stored object itself or an edited copy; the password the task held before is tracked separately and released when it changes. A rejected password leaves the stored task and the password index unchanged.
func (*DbUtils) VerifyUserName ¶
type Flow ¶
type Flow struct {
ExportFlow int64 // outbound traffic
InletFlow int64 // inbound traffic
FlowLimit int64 // traffic limit
TimeLimit time.Time // expire time
sync.RWMutex
// contains filtered or unexported fields
}
func (*Flow) AddOver ¶ added in v0.34.8
AddOver adds like Add and reports, under the same lock, whether the flow is then over its traffic limit or past its time limit.
func (*Flow) MarkClient ¶ added in v0.34.9
func (s *Flow) MarkClient()
MarkClient marks f as the flow of a client, see FlowLimitFactor.
func (*Flow) MarshalJSON ¶ added in v0.34.10
MarshalJSON snapshots the persisted adapter fields while transfers update them.
func (*Flow) Over ¶ added in v0.34.8
Over reports whether the flow is over its traffic limit (FlowLimit MiB of inbound and outbound traffic together) or past its time limit. The fields are read under the lock, so that a 64-bit counter is never seen half written on 32-bit platforms.
func (*Flow) ResetTraffic ¶ added in v0.34.8
func (s *Flow) ResetTraffic()
ResetTraffic zeroes the inbound and outbound traffic.
func (*Flow) SetFlowLimit ¶ added in v0.34.8
SetFlowLimit sets the traffic limit in MiB, 0 for none.
func (*Flow) SetLimits ¶ added in v0.34.8
SetLimits sets the traffic limit in MiB and the time limit together.
func (*Flow) SetTimeLimit ¶ added in v0.34.8
SetTimeLimit sets the time limit, the zero time for none.
func (*Flow) Values ¶ added in v0.34.9
func (s *Flow) Values() FlowValues
Values returns a snapshot of the flow, taken under its lock; a nil flow gives zero values.
type FlowValues ¶ added in v0.34.9
FlowValues is a snapshot of a Flow. Used is the traffic its limit counts: the inbound and outbound traffic, scaled by FlowLimitFactor for a client.
type Glob ¶
type Glob struct {
SourceLists
MaxConn int // total concurrent forwarded connections; zero means unlimited
// RateLimit limits all forwarded traffic together, in KB/s; 0 means
// unlimited.
RateLimit int
// InletFlow and ExportFlow are the bytes forwarded in total, from
// visitors to clients and back, as of the last store; the running
// totals are kept by package rate.
InletFlow int64
ExportFlow int64
sync.RWMutex
}
func (*Glob) ApplyLimits ¶ added in v0.34.9
func (g *Glob) ApplyLimits()
ApplyLimits applies the global bandwidth and connection limits.
type Host ¶
type Host struct {
RateLimits
Id int
Host string // host
HeaderChange string // request header change
RespHeaderChange string // response header change
HostChange string // host change
Location string // url router
PathRewrite string // url rewrite
Remark string // remark
Scheme string // http/https/all
RedirectURL string // 307
HttpsJustProxy bool
TlsOffload bool
AutoSSL bool
CertType string
CertHash string
CertFile string
KeyFile string
NoStore bool
IsClose bool
AutoHttps bool
AutoCORS bool
CompatMode bool
Flow *Flow
NowConn int32
Client *Client
TargetIsHttps bool
Target *Target
UserAuth *MultiAccount
MultiAccount *MultiAccount
SourceLists
Health `json:"-"`
sync.RWMutex
}
func (*Host) ControlGroup ¶ added in v0.34.10
ControlGroup is a domain's leaf under its client; byte controls share it.
func (*Host) CurrentConnections ¶ added in v0.34.10
type JsonDb ¶
type JsonDb struct {
Tasks sync.Map
Hosts sync.Map
HostsTmp sync.Map
Clients sync.Map
Global *Glob
RunPath string
ClientIncreaseId int32 //client increased id
TaskIncreaseId int32 //task increased id
HostIncreaseId int32 //host increased id
TaskFilePath string //task file path
HostFilePath string //host file path
ClientFilePath string //client file path
GlobalFilePath string //global file path
}
func (*JsonDb) GetClientId ¶
func (*JsonDb) LoadClientFromJsonFile ¶
func (s *JsonDb) LoadClientFromJsonFile()
func (*JsonDb) LoadGlobalFromJsonFile ¶
func (s *JsonDb) LoadGlobalFromJsonFile()
func (*JsonDb) LoadHostFromJsonFile ¶
func (s *JsonDb) LoadHostFromJsonFile()
func (*JsonDb) LoadTaskFromJsonFile ¶
func (s *JsonDb) LoadTaskFromJsonFile()
func (*JsonDb) StoreClientsToJsonFile ¶
func (*JsonDb) StoreGlobalToJsonFile ¶
func (*JsonDb) StoreHostToJsonFile ¶
func (*JsonDb) StoreTasksToJsonFile ¶
type MultiAccount ¶
type RateLimits ¶ added in v0.34.10
type RateLimits struct {
UploadLimit int
DownloadLimit int
// contains filtered or unexported fields
}
RateLimits stores per-entry limits in KiB/s. Runtime buckets are never persisted. Upload runs from visitors to the backend; download runs in the other direction.
func (*RateLimits) ClearDownloadLimit ¶ added in v0.34.10
func (r *RateLimits) ClearDownloadLimit()
ClearDownloadLimit removes only the download limit.
func (*RateLimits) ClearUploadLimit ¶ added in v0.34.10
func (r *RateLimits) ClearUploadLimit()
ClearUploadLimit removes only the upload limit.
func (*RateLimits) CurrentRates ¶ added in v0.34.10
func (r *RateLimits) CurrentRates() (int64, int64)
CurrentRates returns measured upload and download speeds in bytes per second.
func (*RateLimits) RateLimiter ¶ added in v0.34.10
func (r *RateLimits) RateLimiter() *rate.Directional
RateLimiter returns the shared buckets, initializing loaded configuration lazily.
func (*RateLimits) SetRateLimits ¶ added in v0.34.10
func (r *RateLimits) SetRateLimits(upload, download int)
SetRateLimits applies configuration to existing connections as well as new ones.
type SourceLists ¶ added in v0.34.9
type SourceLists struct {
// IpListPriority decides an address on both lists or on neither when
// both are set: acl.DenyFirst (0) or acl.AllowFirst (1).
IpListPriority int
BlackIpList []string
WhiteIpList []string
// RegisteredOnly additionally requires the visitor address to be
// registered for the client, see package ipreg. The global settings do
// not use it.
RegisteredOnly bool
// contains filtered or unexported fields
}
SourceLists are the source access lists of a level, the global settings, a client, a tunnel or a host: which visitor addresses may connect. Every level has to allow an address.
func (*SourceLists) CompileSourceLists ¶ added in v0.34.9
func (l *SourceLists) CompileSourceLists(scope acl.Scope, owner string)
CompileSourceLists compiles the lists. Rules that cannot be used, such as geoip: rules without geodata, are logged and skipped.
func (*SourceLists) CopySourceLists ¶ added in v0.34.9
func (l *SourceLists) CopySourceLists(other *SourceLists)
CopySourceLists copies the lists of other and their compiled form.
func (*SourceLists) RefreshSourceLists ¶ added in v0.34.9
func (l *SourceLists) RefreshSourceLists(owner string)
RefreshSourceLists recompiles lists whose geodata changed.
func (*SourceLists) SetSourceLists ¶ added in v0.34.9
func (l *SourceLists) SetSourceLists(priority int, black, white []string, scope acl.Scope, owner string)
SetSourceLists replaces the lists and compiles them.
func (*SourceLists) SourceACL ¶ added in v0.34.9
func (l *SourceLists) SourceACL() *acl.Source
SourceACL returns the compiled lists, compiling them as user lists if that has not happened yet.
type Target ¶
type Target struct {
TargetStr string
LocalProxy bool
ProxyProtocol int // 0=off, 1=v1, 2=v2
sync.RWMutex
// contains filtered or unexported fields
}
func (*Target) GetRandomTarget ¶
func (*Target) HasTarget ¶ added in v0.34.8
HasTarget reports whether addr is one of the configured targets.
func (*Target) RemoveTarget ¶ added in v0.34.8
RemoveTarget takes addr out of the rotation and reports whether it was present.
func (*Target) RestoreTarget ¶ added in v0.34.8
RestoreTarget puts addr back into the rotation if it is a configured target that is currently out of it, and reports whether it was restored.
type Tunnel ¶
type Tunnel struct {
RateLimits
MaxConn int // concurrent connections; zero means unlimited
Id int
Port int
ServerIp string
Mode string
Status bool
RunStatus bool
Client *Client
Ports string
Flow *Flow
NowConn int32
Password string
Remark string
TargetAddr string
TargetType string
DestAclMode int // 0=off, 1=whitelist, 2=blacklist
DestAclRules string // one rule per line
SourceLists
NoStore bool
IsHttp bool
HttpProxy bool
Socks5Proxy bool
LocalPath string
StripPre string
ReadOnly bool
Target *Target
UserAuth *MultiAccount
MultiAccount *MultiAccount
Health
sync.RWMutex
// contains filtered or unexported fields
}
func NewTunnelByHost ¶ added in v0.33.9
func (*Tunnel) AllowsDestination ¶ added in v0.34.0
AllowsDestination reports whether the destination list lets the tunnel reach addr, a host or host:port.
func (*Tunnel) Clone ¶ added in v0.34.8
Clone returns a copy of t that can be edited and then stored with UpdateTask without touching t until the update is accepted. Flow and the health state are copied; Client, Target and the account settings are shared and are expected to be replaced rather than modified. Connection reservations are reattached on accepted updates, so an edit cannot forget active slots.
func (*Tunnel) CompileDestACL ¶ added in v0.34.0
func (t *Tunnel) CompileDestACL()
CompileDestACL compiles the destination list. Rules that cannot be used are logged and skipped.
func (*Tunnel) ControlGroup ¶ added in v0.34.10
ControlGroup is a tunnel's leaf under its client, shared across accepted edits.
func (*Tunnel) CurrentConnections ¶ added in v0.34.10
func (*Tunnel) SetMaxConn ¶ added in v0.34.10
SetMaxConn updates the library cap without reclaiming active slots.