mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:54:02 +03:00
Liniting for network
This commit is contained in:
@@ -21,7 +21,7 @@ func (d *defaultDialer) Dial(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":
|
||||
case "tcp", "tcp4", "tcp6": // nolint: goconst
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported network %s", network)
|
||||
}
|
||||
@@ -35,21 +35,25 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -61,7 +65,7 @@ func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
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)
|
||||
return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
|
||||
}
|
||||
|
||||
if timeout == 0 {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
doh "github.com/babolivier/go-doh-client"
|
||||
@@ -50,26 +51,48 @@ func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
||||
return []string{address}, nil
|
||||
}
|
||||
|
||||
var ips []string
|
||||
ips := []string{}
|
||||
wg := &sync.WaitGroup{}
|
||||
mutex := &sync.Mutex{}
|
||||
|
||||
switch network {
|
||||
case "tcp", "tcp4":
|
||||
if recs, _, err := d.DNS.LookupA(address); err == nil {
|
||||
for _, v := range recs {
|
||||
ips = append(ips, v.IP4)
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if recs, _, err := d.DNS.LookupA(address); err == nil {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
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)
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
for _, v := range recs {
|
||||
ips = append(ips, v.IP6)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if len(ips) == 0 {
|
||||
return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
shadowsocks "github.com/shadowsocks/go-shadowsocks2/core"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
type shadowsocksDialer struct {
|
||||
@@ -18,19 +19,23 @@ type shadowsocksDialer struct {
|
||||
cipher shadowsocks.StreamConnCipher
|
||||
}
|
||||
|
||||
func (s *shadowsocksDialer) Dial(network, address string) (net.Conn, error) {
|
||||
return s.DialContext(context.Background(), network, address)
|
||||
}
|
||||
|
||||
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 nil, err // nolint: wrapcheck
|
||||
}
|
||||
|
||||
return s.cipher.StreamConn(conn), nil
|
||||
}
|
||||
|
||||
func NewShadowsocksDialer(proxyUrl *url.URL,
|
||||
func NewShadowsocksDialer(proxyURL *url.URL,
|
||||
timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
username := proxyUrl.User.Username()
|
||||
username := proxyURL.User.Username()
|
||||
|
||||
decoded, err := base64.RawURLEncoding.DecodeString(username)
|
||||
if err != nil {
|
||||
@@ -47,14 +52,24 @@ func NewShadowsocksDialer(proxyUrl *url.URL,
|
||||
return nil, fmt.Errorf("cannot initialize shadowsocks cipher: %w", err)
|
||||
}
|
||||
|
||||
socks5URL := *proxyURL
|
||||
socks5URL.Scheme = "socks5"
|
||||
socks5URL.User = nil
|
||||
|
||||
dialer, err := NewDefaultDialer(timeout, bufferSize)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize a base dialer: %w", err)
|
||||
|
||||
}
|
||||
|
||||
return &shadowsocksDialer{
|
||||
ssDialer := &shadowsocksDialer{
|
||||
Dialer: dialer,
|
||||
cipher: cipher,
|
||||
}, nil
|
||||
}
|
||||
|
||||
rv, err := proxy.FromURL(&socks5URL, ssDialer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize ss proxy dialer: %w", err)
|
||||
}
|
||||
|
||||
return rv.(Dialer), nil
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
func NewSocks5Dialer(proxyUrl *url.URL, timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||
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))
|
||||
rv, err := proxy.FromURL(proxyURL, dialer.(*defaultDialer))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user