Просмотр исходного кода

Add simple-run command

tags/v2.1.0^2
9seconds 4 лет назад
Родитель
Сommit
c85c88efd6
6 измененных файлов: 107 добавлений и 19 удалений
  1. 12
    12
      internal/cli/access.go
  2. 2
    1
      internal/cli/cli.go
  3. 2
    2
      internal/cli/generate_secret.go
  4. 3
    3
      internal/cli/run.go
  5. 84
    0
      internal/cli/simple_run.go
  6. 4
    1
      mtglib/secret.go

+ 12
- 12
internal/cli/access.go Просмотреть файл

44
 	Hex        bool   `kong:"help='Print secret in hex encoding.',short='x'"`
44
 	Hex        bool   `kong:"help='Print secret in hex encoding.',short='x'"`
45
 }
45
 }
46
 
46
 
47
-func (c *Access) Run(cli *CLI, version string) error {
48
-	conf, err := utils.ReadConfig(c.ConfigPath)
47
+func (a *Access) Run(cli *CLI, version string) error {
48
+	conf, err := utils.ReadConfig(a.ConfigPath)
49
 	if err != nil {
49
 	if err != nil {
50
 		return fmt.Errorf("cannot init config: %w", err)
50
 		return fmt.Errorf("cannot init config: %w", err)
51
 	}
51
 	}
65
 	go func() {
65
 	go func() {
66
 		defer wg.Done()
66
 		defer wg.Done()
67
 
67
 
68
-		ip := c.PublicIPv4
68
+		ip := a.PublicIPv4
69
 		if ip == nil {
69
 		if ip == nil {
70
-			ip = c.getIP(ntw, "tcp4")
70
+			ip = a.getIP(ntw, "tcp4")
71
 		}
71
 		}
72
 
72
 
73
 		if ip != nil {
73
 		if ip != nil {
74
 			ip = ip.To4()
74
 			ip = ip.To4()
75
 		}
75
 		}
76
 
76
 
77
-		resp.IPv4 = c.makeURLs(conf, ip)
77
+		resp.IPv4 = a.makeURLs(conf, ip)
78
 	}()
78
 	}()
79
 
79
 
80
 	go func() {
80
 	go func() {
81
 		defer wg.Done()
81
 		defer wg.Done()
82
 
82
 
83
-		ip := c.PublicIPv6
83
+		ip := a.PublicIPv6
84
 		if ip == nil {
84
 		if ip == nil {
85
-			ip = c.getIP(ntw, "tcp6")
85
+			ip = a.getIP(ntw, "tcp6")
86
 		}
86
 		}
87
 
87
 
88
 		if ip != nil {
88
 		if ip != nil {
89
 			ip = ip.To16()
89
 			ip = ip.To16()
90
 		}
90
 		}
91
 
91
 
92
-		resp.IPv6 = c.makeURLs(conf, ip)
92
+		resp.IPv6 = a.makeURLs(conf, ip)
93
 	}()
93
 	}()
94
 
94
 
95
 	wg.Wait()
95
 	wg.Wait()
105
 	return nil
105
 	return nil
106
 }
106
 }
107
 
107
 
108
-func (c *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
108
+func (a *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
109
 	client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
109
 	client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
110
 		return ntw.DialContext(ctx, protocol, address) // nolint: wrapcheck
110
 		return ntw.DialContext(ctx, protocol, address) // nolint: wrapcheck
111
 	})
111
 	})
139
 	return net.ParseIP(strings.TrimSpace(string(data)))
139
 	return net.ParseIP(strings.TrimSpace(string(data)))
140
 }
140
 }
141
 
141
 
142
-func (c *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
142
+func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
143
 	if ip == nil {
143
 	if ip == nil {
144
 		return nil
144
 		return nil
145
 	}
145
 	}
146
 
146
 
147
-	portNo := c.Port
147
+	portNo := a.Port
148
 	if portNo == 0 {
148
 	if portNo == 0 {
149
 		portNo = conf.BindTo.Port
149
 		portNo = conf.BindTo.Port
150
 	}
150
 	}
153
 	values.Set("server", ip.String())
153
 	values.Set("server", ip.String())
154
 	values.Set("port", strconv.Itoa(int(portNo)))
154
 	values.Set("port", strconv.Itoa(int(portNo)))
155
 
155
 
156
-	if c.Hex {
156
+	if a.Hex {
157
 		values.Set("secret", conf.Secret.Hex())
157
 		values.Set("secret", conf.Secret.Hex())
158
 	} else {
158
 	} else {
159
 		values.Set("secret", conf.Secret.Base64())
159
 		values.Set("secret", conf.Secret.Base64())

+ 2
- 1
internal/cli/cli.go Просмотреть файл

5
 type CLI struct {
5
 type CLI struct {
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
8
-	Run            Proxy            `kong:"cmd,help='Run proxy.'"`
8
+	Run            Run              `kong:"cmd,help='Run proxy.'"`
9
+	SimpleRun      SimpleRun        `kong:"cmd,help='Run proxy without config file.'"`
9
 	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
10
 	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
10
 }
11
 }

+ 2
- 2
internal/cli/generate_secret.go Просмотреть файл

11
 	Hex      bool   `kong:"help='Print secret in hex encoding.',short='x'"`
11
 	Hex      bool   `kong:"help='Print secret in hex encoding.',short='x'"`
12
 }
12
 }
13
 
13
 
14
-func (c *GenerateSecret) Run(cli *CLI, _ string) error {
14
+func (g *GenerateSecret) Run(cli *CLI, _ string) error {
15
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
15
 	secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
16
 
16
 
17
-	if cli.GenerateSecret.Hex {
17
+	if g.Hex {
18
 		fmt.Println(secret.Hex()) // nolint: forbidigo
18
 		fmt.Println(secret.Hex()) // nolint: forbidigo
19
 	} else {
19
 	} else {
20
 		fmt.Println(secret.Base64()) // nolint: forbidigo
20
 		fmt.Println(secret.Base64()) // nolint: forbidigo

internal/cli/proxy.go → internal/cli/run.go Просмотреть файл

6
 	"github.com/9seconds/mtg/v2/internal/utils"
6
 	"github.com/9seconds/mtg/v2/internal/utils"
7
 )
7
 )
8
 
8
 
9
-type Proxy struct {
9
+type Run struct {
10
 	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` // nolint: lll
10
 	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` // nolint: lll
11
 }
11
 }
12
 
12
 
13
-func (c *Proxy) Run(cli *CLI, version string) error {
14
-	conf, err := utils.ReadConfig(c.ConfigPath)
13
+func (r *Run) Run(cli *CLI, version string) error {
14
+	conf, err := utils.ReadConfig(r.ConfigPath)
15
 	if err != nil {
15
 	if err != nil {
16
 		return fmt.Errorf("cannot init config: %w", err)
16
 		return fmt.Errorf("cannot init config: %w", err)
17
 	}
17
 	}

+ 84
- 0
internal/cli/simple_run.go Просмотреть файл

1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"strconv"
7
+	"time"
8
+
9
+	"github.com/9seconds/mtg/v2/internal/config"
10
+)
11
+
12
+type SimpleRun struct {
13
+	BindTo string `kong:"arg,required,name='bind-to',help='A host:port to bind proxy to.'"`
14
+	Secret string `kong:"arg,required,name='secret',help='Proxy secret.'"`
15
+
16
+	Debug               bool          `kong:"name='debug',short='d',help='Run in debug mode.'"`
17
+	Concurrency         uint64        `kong:"name='concurrency',short='c',default='8192',help='Max number of concurrent connection to proxy.'"`
18
+	TCPBuffer           string        `kong:"name='tcp-buffer',short='b',default='4KB',help='Size of TCP buffer to use.'"`
19
+	PreferIP            string        `kong:"name='prefer-ip',short='i',default='prefer-ipv6',help='IP preference. By default we prefer IPv6 with fallback to IPv4.'"`
20
+	DomainFrontingPort  uint64        `kong:"name='domain-fronting-port',short='p',default='443',help='A port to access for domain fronting.'"`
21
+	DOHIP               net.IP        `kong:"name='doh-ip',short='d',default='9.9.9.9',help='IP address of DNS-over-HTTP to use.'"`
22
+	Timeout             time.Duration `kong:"name='timeout',short='t',default='10s',help='Network timeout to use'"`
23
+	AntiReplayCacheSize string        `kong:"name='antireplay-cache-size',short='a',default='1MB',help='A size of anti-replay cache to use.'"`
24
+}
25
+
26
+func (s *SimpleRun) Run(cli *CLI, version string) error {
27
+	conf := &config.Config{}
28
+
29
+	if err := conf.BindTo.Set(s.BindTo); err != nil {
30
+		return fmt.Errorf("incorrect bind-to parameter: %w", err)
31
+	}
32
+
33
+	if err := conf.Secret.Set(s.Secret); err != nil {
34
+		return fmt.Errorf("incorrect secret: %w", err)
35
+	}
36
+
37
+	if err := conf.Concurrency.Set(strconv.FormatUint(s.Concurrency, 10)); err != nil {
38
+		return fmt.Errorf("incorrect concurrency: %w", err)
39
+	}
40
+
41
+	if err := conf.TCPBuffer.Set(s.TCPBuffer); err != nil {
42
+		return fmt.Errorf("incorrect tcp-buffer: %w", err)
43
+	}
44
+
45
+	if err := conf.PreferIP.Set(s.PreferIP); err != nil {
46
+		return fmt.Errorf("incorrect prefer-ip: %w", err)
47
+	}
48
+
49
+	if err := conf.DomainFrontingPort.Set(strconv.FormatUint(s.DomainFrontingPort, 10)); err != nil {
50
+		return fmt.Errorf("incorrect domain-fronting-port: %w", err)
51
+	}
52
+
53
+	if err := conf.Network.DOHIP.Set(s.DOHIP.String()); err != nil {
54
+		return fmt.Errorf("incorrect doh-ip: %w", err)
55
+	}
56
+
57
+	if err := conf.Network.Timeout.TCP.Set(s.Timeout.String()); err != nil {
58
+		return fmt.Errorf("incorrect timeout: %w", err)
59
+	}
60
+
61
+	if err := conf.Network.Timeout.HTTP.Set(s.Timeout.String()); err != nil {
62
+		return fmt.Errorf("incorrect timeout: %w", err)
63
+	}
64
+
65
+	if err := conf.Network.Timeout.Idle.Set(s.Timeout.String()); err != nil {
66
+		return fmt.Errorf("incorrect timeout: %w", err)
67
+	}
68
+
69
+	if err := conf.Defense.AntiReplay.MaxSize.Set(s.AntiReplayCacheSize); err != nil {
70
+		return fmt.Errorf("incorrect antireplay-cache-size: %w", err)
71
+	}
72
+
73
+	conf.Debug.Value = s.Debug
74
+	conf.Defense.AntiReplay.Enabled.Value = true
75
+	conf.Defense.Blocklist.Enabled.Value = false
76
+	conf.Stats.StatsD.Enabled.Value = false
77
+	conf.Stats.Prometheus.Enabled.Value = false
78
+
79
+	if err := conf.Validate(); err != nil {
80
+		return fmt.Errorf("invalid result configuration: %w", err)
81
+	}
82
+
83
+	return runProxy(conf, version)
84
+}

+ 4
- 1
mtglib/secret.go Просмотреть файл

58
 
58
 
59
 // UnmarshalText is to support text.Unmarshaller interface.
59
 // UnmarshalText is to support text.Unmarshaller interface.
60
 func (s *Secret) UnmarshalText(data []byte) error {
60
 func (s *Secret) UnmarshalText(data []byte) error {
61
-	text := string(data)
61
+	return s.Set(string(data))
62
+}
63
+
64
+func (s *Secret) Set(text string) error {
62
 	if text == "" {
65
 	if text == "" {
63
 		return ErrSecretEmpty
66
 		return ErrSecretEmpty
64
 	}
67
 	}

Загрузка…
Отмена
Сохранить