mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 17:34:01 +03:00
Rename dialers into network
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package dialers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
func NewSocks5BaseDialer(proxyUrl *url.URL, timeout time.Duration, bufferSize int) (BaseDialer, error) {
|
||||
baseDialer, err := NewDefaultBaseDialer(timeout, bufferSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize base dialer: %w", err)
|
||||
}
|
||||
|
||||
rv, err := proxy.FromURL(proxyUrl, baseDialer.(*defaultBaseDialer))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
|
||||
}
|
||||
|
||||
return rv.(BaseDialer), nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package dialers
|
||||
package network
|
||||
|
||||
import "time"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dialers
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
"github.com/libp2p/go-reuseport"
|
||||
)
|
||||
|
||||
type defaultBaseDialer struct {
|
||||
type defaultDialer struct {
|
||||
net.Dialer
|
||||
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (d *defaultBaseDialer) Dial(network, address string) (net.Conn, error) {
|
||||
func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
|
||||
return d.DialContext(context.Background(), network, address)
|
||||
}
|
||||
|
||||
func (d *defaultBaseDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
default:
|
||||
@@ -56,7 +56,7 @@ func (d *defaultBaseDialer) DialContext(ctx context.Context, network, address st
|
||||
return tcpConn, nil
|
||||
}
|
||||
|
||||
func NewDefaultBaseDialer(timeout time.Duration, bufferSize int) (BaseDialer, error) {
|
||||
func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
switch {
|
||||
case timeout < 0:
|
||||
return nil, fmt.Errorf("timeout %v should be positive number", timeout)
|
||||
@@ -72,7 +72,7 @@ func NewDefaultBaseDialer(timeout time.Duration, bufferSize int) (BaseDialer, er
|
||||
bufferSize = DefaultBufferSize
|
||||
}
|
||||
|
||||
return &defaultBaseDialer{
|
||||
return &defaultDialer{
|
||||
Dialer: net.Dialer{
|
||||
Timeout: timeout,
|
||||
Control: reuseport.Control,
|
||||
@@ -1,11 +1,10 @@
|
||||
package dialers
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
type BaseDialer interface {
|
||||
Dial(network, address string) (net.Conn, error)
|
||||
type Dialer interface {
|
||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package dialers
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,18 +11,18 @@ import (
|
||||
doh "github.com/babolivier/go-doh-client"
|
||||
)
|
||||
|
||||
type Dialer struct {
|
||||
type Network struct {
|
||||
HTTP http.Client
|
||||
DNS doh.Resolver
|
||||
|
||||
baseDialer BaseDialer
|
||||
dialer Dialer
|
||||
}
|
||||
|
||||
func (d *Dialer) Dial(network, address string) (net.Conn, error) {
|
||||
func (d *Network) Dial(network, address string) (net.Conn, error) {
|
||||
return d.DialContext(context.Background(), network, address)
|
||||
}
|
||||
|
||||
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
func (d *Network) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
host, port, _ := net.SplitHostPort(address)
|
||||
|
||||
ips, err := d.resolveIPs(network, host)
|
||||
@@ -35,7 +35,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.
|
||||
})
|
||||
|
||||
for _, v := range ips {
|
||||
if conn, err := d.baseDialer.DialContext(ctx, network, net.JoinHostPort(v, port)); err == nil {
|
||||
if conn, err := d.dialer.DialContext(ctx, network, net.JoinHostPort(v, port)); err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.
|
||||
return nil, fmt.Errorf("cannot dial to %s:%s", network, address)
|
||||
}
|
||||
|
||||
func (d *Dialer) resolveIPs(network, address string) ([]string, error) {
|
||||
func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
||||
if net.ParseIP(address) != nil {
|
||||
return []string{address}, nil
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func (d *Dialer) resolveIPs(network, address string) ([]string, error) {
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
func MakeDialer(base BaseDialer, dohHostname string, httpTimeout time.Duration) (*Dialer, error) {
|
||||
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)
|
||||
@@ -90,23 +90,23 @@ func MakeDialer(base BaseDialer, dohHostname string, httpTimeout time.Duration)
|
||||
dohHTTPClient := &http.Client{
|
||||
Timeout: httpTimeout,
|
||||
Transport: &http.Transport{
|
||||
DialContext: base.DialContext,
|
||||
DialContext: dialer.DialContext,
|
||||
},
|
||||
}
|
||||
rv := &Dialer{
|
||||
baseDialer: base,
|
||||
network := &Network{
|
||||
dialer: dialer,
|
||||
DNS: doh.Resolver{
|
||||
Host: dohHostname,
|
||||
Class: doh.IN,
|
||||
HTTPClient: dohHTTPClient,
|
||||
},
|
||||
}
|
||||
rv.HTTP = http.Client{
|
||||
network.HTTP = http.Client{
|
||||
Timeout: httpTimeout,
|
||||
Transport: &http.Transport{
|
||||
DialContext: rv.DialContext,
|
||||
DialContext: network.DialContext,
|
||||
},
|
||||
}
|
||||
|
||||
return rv, nil
|
||||
return network, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package dialers
|
||||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -12,23 +12,15 @@ import (
|
||||
shadowsocks "github.com/shadowsocks/go-shadowsocks2/core"
|
||||
)
|
||||
|
||||
type shadowsocksBaseDialer struct {
|
||||
base BaseDialer
|
||||
type shadowsocksDialer struct {
|
||||
Dialer
|
||||
|
||||
cipher shadowsocks.StreamConnCipher
|
||||
}
|
||||
|
||||
func (s *shadowsocksBaseDialer) Dial(network, address string) (net.Conn, error) {
|
||||
conn, err := s.base.Dial(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.cipher.StreamConn(conn), nil
|
||||
}
|
||||
|
||||
func (s *shadowsocksBaseDialer) DialContext(ctx context.Context,
|
||||
func (s *shadowsocksDialer) DialContext(ctx context.Context,
|
||||
network, address string) (net.Conn, error) {
|
||||
conn, err := s.base.DialContext(ctx, network, address)
|
||||
conn, err := s.Dialer.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -36,8 +28,8 @@ func (s *shadowsocksBaseDialer) DialContext(ctx context.Context,
|
||||
return s.cipher.StreamConn(conn), nil
|
||||
}
|
||||
|
||||
func NewShadowsocksBaseDialer(proxyUrl *url.URL,
|
||||
timeout time.Duration, bufferSize int) (BaseDialer, error) {
|
||||
func NewShadowsocksDialer(proxyUrl *url.URL,
|
||||
timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
username := proxyUrl.User.Username()
|
||||
|
||||
decoded, err := base64.RawURLEncoding.DecodeString(username)
|
||||
@@ -55,14 +47,14 @@ func NewShadowsocksBaseDialer(proxyUrl *url.URL,
|
||||
return nil, fmt.Errorf("cannot initialize shadowsocks cipher: %w", err)
|
||||
}
|
||||
|
||||
baseDialer, err := NewDefaultBaseDialer(timeout, bufferSize)
|
||||
dialer, err := NewDefaultDialer(timeout, bufferSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize a base dialer: %w", err)
|
||||
|
||||
}
|
||||
|
||||
return &shadowsocksBaseDialer{
|
||||
base: baseDialer,
|
||||
return &shadowsocksDialer{
|
||||
Dialer: dialer,
|
||||
cipher: cipher,
|
||||
}, nil
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user