Update docs

This commit is contained in:
9seconds
2022-08-04 18:39:00 +03:00
parent 008e17cdff
commit 6a19ded78e
26 changed files with 319 additions and 344 deletions
+32 -42
View File
@@ -1,20 +1,16 @@
// Network contains a default implementation of the network.
//
// Please see mtglib.Network interface to get some basic idea behind
// this abstraction.
// Please see [mtglib.Network] interface to get some basic idea behind this
// abstraction.
//
// Some notable feature of this implementation:
//
// 1. It detaches dialer from a network. Dialer is something which
// implements a real dialer and network completes it with more higher
// level details.
//
// 2. It uses only TCP connections. Even for DNS it uses DNS-Over-HTTPS
//
// 3. It has some simple implementation of DNS cache which is good
// enough for our purpose.
//
// 4. It sets uses SO_REUSEPORT port if applicable.
// 1. It detaches dialer from a network. Dialer is something which implements a
// real dialer and network completes it with more higher level details.
// 2. It uses only TCP connections. Even for DNS it uses DNS-Over-HTTPS
// 3. It has some simple implementation of DNS cache which is good enough for
// our purpose.
// 4. It sets uses SO_REUSEPORT port if applicable.
package network
import (
@@ -26,53 +22,47 @@ import (
)
const (
// DefaultTimeout is a default timeout for establishing TCP
// connection.
// DefaultTimeout is a default timeout for establishing TCP connection.
DefaultTimeout = 10 * time.Second
// DefaultHTTPTimeout defines a default timeout for making HTTP
// request.
// DefaultHTTPTimeout defines a default timeout for making HTTP request.
DefaultHTTPTimeout = 10 * time.Second
// Deprecated:
//
// DefaultBufferSize defines a TCP buffer size. Both read and write, so
// for real size, please multiply this number by 2.
// DefaultBufferSize defines a TCP buffer size. Both read and write, so for
// real size, please multiply this number by 2.
DefaultBufferSize = 16 * 1024 // 16 kib
// DefaultTCPKeepAlivePeriod defines a time period between 2
// consequitive probes.
// DefaultTCPKeepAlivePeriod defines a time period between 2 consequitive
// probes.
DefaultTCPKeepAlivePeriod = 10 * time.Second
// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
// only.
// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer only.
//
// This dialer uses circuit breaker with of 3 stages: OPEN,
// HALF_OPEN and CLOSED. If state is CLOSED, all requests go in
// a normal mode. If you get more that ProxyDialerOpenThreshold
// errors, circuit breaker goes into OPEN mode.
// This dialer uses circuit breaker with of 3 stages: OPEN, HALF_OPEN and
// CLOSED. If state is CLOSED, all requests go in a normal mode. If you get
// more that ProxyDialerOpenThreshold errors, circuit breaker goes into OPEN
// mode.
//
// When circuit breaker is in OPEN mode, it forbids all request to
// a given proxy. But after ProxyDialerHalfOpenTimeout it gives a
// second chance and opens an access for a SINGLE request. If this
// request success, then circuit breaker closes, otherwise opens
// again.
// When circuit breaker is in OPEN mode, it forbids all request to a given
// proxy. But after ProxyDialerHalfOpenTimeout it gives a second chance and
// opens an access for a SINGLE request. If this request success, then circuit
// breaker closes, otherwise opens again.
//
// When circuit breaker is closed, it clears an error states each
// ProxyDialerResetFailuresTimeout.
ProxyDialerOpenThreshold = 5
// ProxyDialerHalfOpenTimeout defines a halfopen timeout for circuit
// breaker.
// ProxyDialerHalfOpenTimeout defines a halfopen timeout for circuit breaker.
ProxyDialerHalfOpenTimeout = time.Minute
// ProxyDialerResetFailuresTimeout defines a timeout for resetting a
// failure.
// ProxyDialerResetFailuresTimeout defines a timeout for resetting a failure.
ProxyDialerResetFailuresTimeout = 10 * time.Second
// DefaultDOHHostname defines a default IP address for DOH host.
// Since mtg is simple, please pass IP address here. We do not
// have bootstrap servers here embedded.
// DefaultDOHHostname defines a default IP address for DOH host. Since mtg is
// simple, please pass IP address here. We do not have bootstrap servers here
// embedded.
DefaultDOHHostname = "9.9.9.9"
// DNSTimeout defines a timeout for DNS queries.
@@ -84,12 +74,12 @@ const (
)
var (
// ErrCircuitBreakerOpened is returned when proxy is being accessed
// but circuit breaker is opened.
// ErrCircuitBreakerOpened is returned when proxy is being accessed but
// circuit breaker is opened.
ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
// ErrCannotDialWithAllProxies is returned when load balancing
// client is trying to access proxies but all of them are failed.
// ErrCannotDialWithAllProxies is returned when load balancing client is
// trying to access proxies but all of them are failed.
ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
)
+6 -8
View File
@@ -33,16 +33,14 @@ func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, addr
return nil, ErrCannotDialWithAllProxies
}
// NewLoadBalancedSocks5Dialer builds a new load balancing SOCKS5
// dialer.
// NewLoadBalancedSocks5Dialer builds a new load balancing SOCKS5 dialer.
//
// The main difference from one which is made by NewSocks5Dialer is that
// we actually have a list of these proxies. When dial is requested,
// any proxy is picked and used. If proxy fails for some reason, we try
// another one.
// The main difference from one which is made by NewSocks5Dialer is that we
// actually have a list of these proxies. When dial is requested, any proxy is
// picked and used. If proxy fails for some reason, we try another one.
//
// So, it is mostly useful if you have some routes with proxies which
// are not always online or having buggy network.
// So, it is mostly useful if you have some routes with proxies which are not
// always online or having buggy network.
func NewLoadBalancedSocks5Dialer(baseDialer Dialer, proxyURLs []*url.URL) (Dialer, error) {
dialers := make([]Dialer, 0, len(proxyURLs))
+2 -2
View File
@@ -118,8 +118,8 @@ func (n *network) dnsResolve(protocol, address string) ([]string, error) {
return ips, nil
}
// NewNetwork assembles an mtglib.Network compatible structure
// based on a dialer and given params.
// NewNetwork assembles an mtglib.Network compatible structure based on a
// dialer and given params.
//
// It brings simple DNS cache and DNS-Over-HTTPS when necessary.
func NewNetwork(dialer Dialer,
+2 -2
View File
@@ -136,8 +136,8 @@ func (s socks5Dialer) connect(conn io.ReadWriter, address string) error {
return nil
}
// NewSocks5Dialer build a new dialer from a given one (so, in theory
// you can chain here). Proxy parameters are passed with URI in a form of:
// NewSocks5Dialer build a new dialer from a given one (so, in theory you can
// chain here). Proxy parameters are passed with URI in a form of:
//
// socks5://[user:[password]]@host:port
func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {