Rename dialers into network

This commit is contained in:
9seconds
2021-03-05 14:12:38 +03:00
parent 4689479745
commit d5147f1935
8 changed files with 60 additions and 69 deletions
+9
View File
@@ -0,0 +1,9 @@
package network
import "time"
const (
DefaultTimeout = 10 * time.Second
DefaultHTTPTimeout = DefaultTimeout
DefaultBufferSize = 4096
)
+82
View File
@@ -0,0 +1,82 @@
package network
import (
"context"
"fmt"
"net"
"time"
"github.com/libp2p/go-reuseport"
)
type defaultDialer struct {
net.Dialer
bufferSize int
}
func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return nil, fmt.Errorf("unsupported network %s", network)
}
conn, err := d.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, fmt.Errorf("cannot dial to %s: %w", address, err)
}
tcpConn := conn.(*net.TCPConn)
if err := tcpConn.SetNoDelay(true); err != nil {
conn.Close()
return nil, fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
}
if err := tcpConn.SetReadBuffer(d.bufferSize); err != nil {
tcpConn.Close()
return nil, fmt.Errorf("cannot set read buffer size: %w", err)
}
if err := tcpConn.SetWriteBuffer(d.bufferSize); err != nil {
tcpConn.Close()
return nil, fmt.Errorf("cannot set write buffer size: %w", err)
}
if err := tcpConn.SetKeepAlive(true); err != nil {
tcpConn.Close()
return nil, fmt.Errorf("cannot enable keep-alive: %w", err)
}
return tcpConn, nil
}
func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
switch {
case timeout < 0:
return nil, fmt.Errorf("timeout %v should be positive number", timeout)
case bufferSize < 0:
return nil, fmt.Errorf("buffer size %s should be positive number", bufferSize)
}
if timeout == 0 {
timeout = DefaultTimeout
}
if bufferSize == 0 {
bufferSize = DefaultBufferSize
}
return &defaultDialer{
Dialer: net.Dialer{
Timeout: timeout,
Control: reuseport.Control,
},
bufferSize: bufferSize,
}, nil
}
+10
View File
@@ -0,0 +1,10 @@
package network
import (
"context"
"net"
)
type Dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}
+112
View File
@@ -0,0 +1,112 @@
package network
import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
"time"
doh "github.com/babolivier/go-doh-client"
)
type Network struct {
HTTP http.Client
DNS doh.Resolver
dialer Dialer
}
func (d *Network) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *Network) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
host, port, _ := net.SplitHostPort(address)
ips, err := d.resolveIPs(network, host)
if err != nil {
return nil, fmt.Errorf("cannot resolve dns names: %w", err)
}
rand.Shuffle(len(ips), func(i, j int) {
ips[i], ips[j] = ips[j], ips[i]
})
for _, v := range ips {
if conn, err := d.dialer.DialContext(ctx, network, net.JoinHostPort(v, port)); err == nil {
return conn, nil
}
}
return nil, fmt.Errorf("cannot dial to %s:%s", network, address)
}
func (d *Network) resolveIPs(network, address string) ([]string, error) {
if net.ParseIP(address) != nil {
return []string{address}, nil
}
var ips []string
switch network {
case "tcp", "tcp4":
if recs, _, err := d.DNS.LookupA(address); err == nil {
for _, v := range recs {
ips = append(ips, v.IP4)
}
}
}
switch network {
case "tcp", "tcp6":
if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
for _, v := range recs {
ips = append(ips, v.IP6)
}
}
}
if len(ips) == 0 {
return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
}
return ips, nil
}
func NewNetwork(dialer Dialer, dohHostname string, httpTimeout time.Duration) (*Network, error) {
switch {
case httpTimeout < 0:
return nil, fmt.Errorf("timeout should be positive number %v", httpTimeout)
case httpTimeout == 0:
httpTimeout = DefaultHTTPTimeout
}
if net.ParseIP(dohHostname) == nil {
return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
}
dohHTTPClient := &http.Client{
Timeout: httpTimeout,
Transport: &http.Transport{
DialContext: dialer.DialContext,
},
}
network := &Network{
dialer: dialer,
DNS: doh.Resolver{
Host: dohHostname,
Class: doh.IN,
HTTPClient: dohHTTPClient,
},
}
network.HTTP = http.Client{
Timeout: httpTimeout,
Transport: &http.Transport{
DialContext: network.DialContext,
},
}
return network, nil
}
+60
View File
@@ -0,0 +1,60 @@
package network
import (
"context"
"encoding/base64"
"fmt"
"net"
"net/url"
"strings"
"time"
shadowsocks "github.com/shadowsocks/go-shadowsocks2/core"
)
type shadowsocksDialer struct {
Dialer
cipher shadowsocks.StreamConnCipher
}
func (s *shadowsocksDialer) DialContext(ctx context.Context,
network, address string) (net.Conn, error) {
conn, err := s.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, err
}
return s.cipher.StreamConn(conn), nil
}
func NewShadowsocksDialer(proxyUrl *url.URL,
timeout time.Duration, bufferSize int) (Dialer, error) {
username := proxyUrl.User.Username()
decoded, err := base64.RawURLEncoding.DecodeString(username)
if err != nil {
return nil, fmt.Errorf("cannot decode payload: %w", err)
}
chunks := strings.SplitN(string(decoded), ":", 2)
if len(chunks) != 2 {
return nil, fmt.Errorf("incorrect payload %s", username)
}
cipher, err := shadowsocks.PickCipher(chunks[0], nil, chunks[1])
if err != nil {
return nil, fmt.Errorf("cannot initialize shadowsocks cipher: %w", err)
}
dialer, err := NewDefaultDialer(timeout, bufferSize)
if err != nil {
return nil, fmt.Errorf("cannot initialize a base dialer: %w", err)
}
return &shadowsocksDialer{
Dialer: dialer,
cipher: cipher,
}, nil
}
+23
View File
@@ -0,0 +1,23 @@
package network
import (
"fmt"
"net/url"
"time"
"golang.org/x/net/proxy"
)
func NewSocks5Dialer(proxyUrl *url.URL, timeout time.Duration, bufferSize int) (Dialer, error) {
dialer, err := NewDefaultDialer(timeout, bufferSize)
if err != nil {
return nil, fmt.Errorf("cannot initialize base dialer: %w", err)
}
rv, err := proxy.FromURL(proxyUrl, dialer.(*defaultDialer))
if err != nil {
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
}
return rv.(Dialer), nil
}