Documentation
¶
Index ¶
- Variables
- func AppendForwardedFor(h http.Header, remoteAddr string)
- func DropClientForwarding(h http.Header)
- func HandleTrans(c *conn.Conn, s *TunnelModeServer) error
- func KeepClientForwarding(pr *httputil.ProxyRequest, trusted bool)
- func ProcessHttp(c net.Conn, s *TunnelModeServer) error
- func ProcessMix(c *conn.Conn, s *TunnelModeServer) error
- func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error
- func SecretDenied(src net.Addr, t *model.Tunnel, target string)
- func SourceDenied(addr net.Addr, g *acl.Source, client *model.Client, entry model.SourceView, ...) bool
- func SourceDeniedString(addr string, g *acl.Source, client *model.Client, entry model.SourceView, ...) bool
- type BaseServer
- func (s *BaseServer) AcquireForwarding(client *model.Client, group *byterate.Group) (func(), error)
- func (s *BaseServer) Auth(r *http.Request, c *conn.Conn, u, p string, ...) error
- func (s *BaseServer) BindLocked(bind func() error) error
- func (s *BaseServer) DealClient(c *conn.Conn, client *model.Client, addr string, rb []byte, tp string, ...) error
- func (s *BaseServer) DealTarget(c *conn.Conn, client *model.Client, rb []byte, tp string, f func(), ...) error
- func (s *BaseServer) FlowAdd(in, out int64)
- func (s *BaseServer) FlowAddHost(host *model.Host, in, out int64)
- func (s *BaseServer) MarkClosed(snapshot func())
- type Binder
- type NetBridge
- type SecretPolicy
- type SecretRoute
- type SecretServer
- type Service
- type TunnelModeServer
- type UdpModeServer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSecretLinkRefused is a link of a network the policy does not know. ErrSecretLinkRefused = errors.New("secret link refused by the task's policy") // ErrSecretDestDenied is a destination or network outside the scope. ErrSecretDestDenied = errors.New("destination outside the secret task's access scope") )
var ErrForwardingRefused = errors.New("forwarding refused")
ErrForwardingRefused matches the errors of AcquireForwarding: the quota, the expiry or the connection limits refuse the transfer.
var ErrServerClosed = errors.New("server closed")
ErrServerClosed is returned by Bind and Start once the server was closed.
Functions ¶
func AppendForwardedFor ¶
AppendForwardedFor appends the client IP taken from remoteAddr to the X-Forwarded-For header of h, creating the header when it is absent.
func DropClientForwarding ¶ added in v0.35.3
DropClientForwarding removes the forwarding headers a client sent from h.
func HandleTrans ¶
func HandleTrans(c *conn.Conn, s *TunnelModeServer) error
HandleTrans forwards a connection redirected to the transparent proxy, by iptables REDIRECT or TPROXY on Linux or pf on macOS and FreeBSD, to its original destination.
func KeepClientForwarding ¶
func KeepClientForwarding(pr *httputil.ProxyRequest, trusted bool)
KeepClientForwarding undoes the clean-up ReverseProxy applies before Rewrite, so the client's request reaches the rewrite logic as sent: the query string keeps parameters ReverseProxy cannot parse and, when trusted says the client is a trusted proxy, the forwarding headers come back unless it listed them in Connection. An untrusted client's forwarding headers are all dropped, so the backend sees only those set from what was observed.
func ProcessHttp ¶
func ProcessHttp(c net.Conn, s *TunnelModeServer) error
ProcessHttp serves the HTTP proxy requests of c.
func ProcessMix ¶
func ProcessMix(c *conn.Conn, s *TunnelModeServer) error
func ProcessTunnel ¶
func ProcessTunnel(c *conn.Conn, s *TunnelModeServer) error
ProcessTunnel tcp proxy
func SecretDenied ¶
SecretDenied writes the tunnel_denied line of a link of a visitor from src to t whose destination target the scope refuses.
func SourceDenied ¶
func SourceDenied(addr net.Addr, g *acl.Source, client *model.Client, entry model.SourceView, entryName string) bool
SourceDenied reports whether the access lists of the global settings, the client and the entry deny the visitor addr, and logs a denial. g, the global lists, is nil where there are no global lists, as on npc. entryName names the entry in the log, for example "tunnel 3".
Types ¶
type BaseServer ¶
type BaseServer struct {
Id int
Bridge NetBridge
Task *model.Tunnel
ErrorContent []byte
AllowLocalProxy bool
sync.Mutex
// contains filtered or unexported fields
}
BaseServer struct
func NewBaseServer ¶
func NewBaseServer(bridge NetBridge, task *model.Tunnel, allowLocalProxy bool) *BaseServer
func (*BaseServer) AcquireForwarding ¶
AcquireForwarding admits once at a leaf. byterate reserves every ancestor atomically and rolls all slots back if any cap rejects the transfer.
func (*BaseServer) Auth ¶
func (s *BaseServer) Auth(r *http.Request, c *conn.Conn, u, p string, multiAccount, userAuth *model.MultiAccount) error
Auth check
func (*BaseServer) BindLocked ¶
func (s *BaseServer) BindLocked(bind func() error) error
BindLocked runs bind under the lifecycle lock unless the server is closed.
func (*BaseServer) DealClient ¶
func (*BaseServer) DealTarget ¶
func (s *BaseServer) DealTarget(c *conn.Conn, client *model.Client, rb []byte, tp string, f func(), flows []conn.Flow, task *model.Tunnel) error
DealTarget is DealClient along a route of task, to one of the addresses of its target, retrying the next address if the server fails to dial one itself. It returns model.ErrNoTarget, leaving c open, if no address can be tried.
func (*BaseServer) FlowAddHost ¶
func (s *BaseServer) FlowAddHost(host *model.Host, in, out int64)
FlowAddHost change the flow
func (*BaseServer) MarkClosed ¶
func (s *BaseServer) MarkClosed(snapshot func())
MarkClosed marks the server closed and runs snapshot under the lifecycle lock, so snapshot observes every resource a concurrent Bind opened. The resources should be released after MarkClosed returns.
type Binder ¶
type Binder interface {
Bind() error
}
Binder is implemented by services that own a listening socket. Bind opens it synchronously so that the caller learns about address errors before the service is published, after which Start serves on it. Bind is idempotent and fails with ErrServerClosed once Close has been called, so a Close racing with Bind or Start never leaves a socket open.
type NetBridge ¶
type NetBridge interface {
// Session returns the way to open links to client clientId.
Session(clientId int) conn.ClientSession
IsServer() bool
// LinkCert is the certificate this side presents on the encrypted links
// it opens (conn.LinkConn).
LinkCert() *tls.Certificate
// GlobalSource returns the global source access lists of the server,
// nil if there are none, as on npc.
GlobalSource() *acl.Source
CliProcess(c *conn.Conn, tunnelType string)
}
type SecretPolicy ¶
type SecretPolicy struct {
// Targets are the task's target addresses, one per line, used when the
// visitor names none.
Targets string `json:"targets,omitempty"`
// TargetType is the network the default targets are used for: tcp, udp
// or all. A destination the visitor names is not bound by it.
TargetType string `json:"target_type,omitempty"`
// NameDest lets the visitor name destinations, within the destination
// list when the task has one. nps always sets it (SecretPolicy above);
// false on the wire leaves only the targets.
NameDest bool `json:"name_dest,omitempty"`
// AllowLocal lets a visitor choose, link by link, that nps reaches the
// destination itself (local_proxy): the tunnel has "proxy to local" on
// (Target.LocalProxy) and the server allows it (allow_local_proxy). A
// visitor asking for it without that is served by the provider as if it
// had not asked.
AllowLocal bool `json:"allow_local,omitempty"`
// LocalProxy is whether nps always reaches the target itself, whatever
// the visitor asks: then no direct path can serve the link.
LocalProxy bool `json:"local_proxy,omitempty"`
// DestAclMode and DestAclRules are the task's destination list, applied
// to destinations the visitor names; mode off with NameDest is no limit.
DestAclMode int `json:"dest_acl_mode,omitempty"`
DestAclRules string `json:"dest_acl_rules,omitempty"`
// contains filtered or unexported fields
}
SecretPolicy is what a visitor of a secret task may reach, its access scope. nps resolves it once per grant (an attached visitor) and applies it to the links it relays (SecretServer); the invitation of a direct path hands it to the provider npc, which applies it to every link the visitor sends over that path: both paths follow the same rules.
The task's targets are only the default of a link naming no destination; the limit is the task's destination list. Without a list the visitor may name any destination, on any network; with one, those the list allows. A link naming no destination on a task without targets fails. TargetType says which network the default target serves and limits nothing else.
func NewSecretPolicy ¶
NewSecretPolicy is the policy of task t reaching target, with the server's allow_local_proxy.
func (*SecretPolicy) Admit ¶
func (p *SecretPolicy) Admit(lk *conn.Link) (SecretRoute, error)
Admit decides what lk reaches; lk nil is a visitor that sent no link information and gets the task's target. A destination outside the scope is ErrSecretDestDenied, with the route's Host naming it: it is never replaced by the task's target.
func (*SecretPolicy) Compile ¶
func (p *SecretPolicy) Compile() error
Compile prepares a policy received on the wire for Admit. A destination list with rules this side cannot apply (geodata it lacks) is an error: the direct path must not allow what nps would refuse.
func (*SecretPolicy) Direct ¶
func (p *SecretPolicy) Direct() bool
Direct reports whether a direct path can serve some link: not when nps must reach the target itself.
type SecretRoute ¶
SecretRoute is what an admitted link reaches: Host empty means one of the policy's targets.
type SecretServer ¶
type SecretServer struct {
*BaseServer
// contains filtered or unexported fields
}
SecretServer relays the links of one grant (an attached visitor) by the grant's policy, resolved once.
func NewSecretServer ¶
func NewSecretServer(bridge NetBridge, task *model.Tunnel, policy *SecretPolicy, allowLocalProxy bool) *SecretServer
func (*SecretServer) Serve ¶
func (s *SecretServer) Serve(src net.Conn, lk *conn.Link, ack func(net.Conn, time.Duration) error) error
Serve relays src, a visitor's stream that carried lk, until either side ends; ack answers a link that asks for an acknowledgement, once the target is reached. src is closed when Serve returns.
type TunnelModeServer ¶
type TunnelModeServer struct {
*BaseServer
// contains filtered or unexported fields
}
func NewTunnelModeServer ¶
func NewTunnelModeServer(process process, bridge NetBridge, task *model.Tunnel, allowLocalProxy bool) *TunnelModeServer
NewTunnelModeServer tcp|host|mixproxy
func (*TunnelModeServer) Bind ¶
func (s *TunnelModeServer) Bind() error
Bind opens the TCP listener and, for SOCKS5, the UDP associate socket.
func (*TunnelModeServer) Close ¶
func (s *TunnelModeServer) Close() error
func (*TunnelModeServer) DialVirtual ¶
func (s *TunnelModeServer) DialVirtual(rAddr string) (net.Conn, error)
func (*TunnelModeServer) ServeVirtual ¶
func (s *TunnelModeServer) ServeVirtual(c net.Conn)
func (*TunnelModeServer) Start ¶
func (s *TunnelModeServer) Start() error
type UdpModeServer ¶
type UdpModeServer struct {
*BaseServer
// contains filtered or unexported fields
}
func NewUdpModeServer ¶
func NewUdpModeServer(bridge NetBridge, task *model.Tunnel, allowLocalProxy bool) *UdpModeServer
func (*UdpModeServer) Bind ¶
func (s *UdpModeServer) Bind() error
Bind opens the UDP socket. Each visitor address becomes a session; the datagrams of all of them are limited by the task's rates, dropping those over the limits.
func (*UdpModeServer) Close ¶
func (s *UdpModeServer) Close() error
func (*UdpModeServer) Start ¶
func (s *UdpModeServer) Start() error