mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +03:00
Change network to accept DialFunc
This commit is contained in:
+7
-17
@@ -38,8 +38,8 @@ type Access struct {
|
||||
Hex bool `help:"Print secret in hex encoding."`
|
||||
}
|
||||
|
||||
func (c *Access) Run(cli *CLI) error {
|
||||
if err := c.ReadConfig(cli.Access.ConfigPath); err != nil {
|
||||
func (c *Access) Run(cli *CLI, version string) error {
|
||||
if err := c.ReadConfig(cli.Access.ConfigPath, version); err != nil {
|
||||
return fmt.Errorf("cannot init config: %w", err)
|
||||
}
|
||||
|
||||
@@ -74,21 +74,11 @@ func (c *Access) Run(cli *CLI) error {
|
||||
}
|
||||
|
||||
func (c *Access) getIP(protocol string) net.IP {
|
||||
client := c.network.MakeHTTPClient(0)
|
||||
client.Transport = &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return c.network.DialContext(ctx, protocol, address)
|
||||
},
|
||||
}
|
||||
client := c.network.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return c.network.DialContext(ctx, protocol, address)
|
||||
})
|
||||
|
||||
c.network.PrepareHTTPClient(client)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := client.Get("https://ifconfig.co") // nolint: noctx
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -98,7 +88,7 @@ func (c *Access) getIP(protocol string) net.IP {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
|
||||
resp.Body.Close()
|
||||
}()
|
||||
|
||||
|
||||
+7
-6
@@ -15,7 +15,7 @@ type base struct {
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func (b *base) ReadConfig(path string) error {
|
||||
func (b *base) ReadConfig(path, version string) error {
|
||||
content, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot read config file: %w", err)
|
||||
@@ -26,7 +26,7 @@ func (b *base) ReadConfig(path string) error {
|
||||
return fmt.Errorf("cannot parse config: %w", err)
|
||||
}
|
||||
|
||||
ntw, err := b.makeNetwork(conf)
|
||||
ntw, err := b.makeNetwork(conf, version)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot build a network: %w", err)
|
||||
}
|
||||
@@ -37,11 +37,12 @@ func (b *base) ReadConfig(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *base) makeNetwork(conf *config.Config) (network.Network, error) {
|
||||
func (b *base) makeNetwork(conf *config.Config, version string) (network.Network, error) {
|
||||
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
|
||||
idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
|
||||
dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
|
||||
bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
|
||||
userAgent := "mtg/" + version
|
||||
|
||||
baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
|
||||
if err != nil {
|
||||
@@ -58,14 +59,14 @@ func (b *base) makeNetwork(conf *config.Config) (network.Network, error) {
|
||||
|
||||
switch len(proxyURLs) {
|
||||
case 0:
|
||||
return network.NewNetwork(baseDialer, dohIP, idleTimeout)
|
||||
return network.NewNetwork(baseDialer, userAgent, dohIP, idleTimeout)
|
||||
case 1:
|
||||
socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
||||
}
|
||||
|
||||
return network.NewNetwork(socksDialer, dohIP, idleTimeout)
|
||||
return network.NewNetwork(socksDialer, userAgent, dohIP, idleTimeout)
|
||||
}
|
||||
|
||||
socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
|
||||
@@ -73,5 +74,5 @@ func (b *base) makeNetwork(conf *config.Config) (network.Network, error) {
|
||||
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
||||
}
|
||||
|
||||
return network.NewNetwork(socksDialer, dohIP, idleTimeout)
|
||||
return network.NewNetwork(socksDialer, userAgent, dohIP, idleTimeout)
|
||||
}
|
||||
|
||||
+1
-3
@@ -1,8 +1,6 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/alecthomas/kong"
|
||||
)
|
||||
import "github.com/alecthomas/kong"
|
||||
|
||||
type CLI struct {
|
||||
GenerateSecret GenerateSecret `cmd help:"Generate new proxy secret"` // nolint: govet
|
||||
|
||||
@@ -13,7 +13,7 @@ type GenerateSecret struct {
|
||||
Hex bool `help:"Print secret in hex encoding."`
|
||||
}
|
||||
|
||||
func (c *GenerateSecret) Run(cli *CLI) error { // nolint: unparam
|
||||
func (c *GenerateSecret) Run(cli *CLI, _ string) error {
|
||||
secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
|
||||
|
||||
if cli.GenerateSecret.Hex {
|
||||
|
||||
Reference in New Issue
Block a user