mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 15:24:01 +03:00
Use custom cli classes
This commit is contained in:
+22
-14
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -31,38 +32,43 @@ type runAccessResponseURLs struct {
|
|||||||
TmeQrCode string `json:"tme_qrcode"`
|
TmeQrCode string `json:"tme_qrcode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAccess(cli *CLI) {
|
type cliCommandAccess struct {
|
||||||
|
ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
|
||||||
|
Hex bool `help:"Print secret in hex encoding."`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cliCommandAccess) Run(cli *CLI) error {
|
||||||
filefp, err := os.Open(cli.Access.ConfigPath)
|
filefp, err := os.Open(cli.Access.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
return fmt.Errorf("cannot open config file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer filefp.Close()
|
defer filefp.Close()
|
||||||
|
|
||||||
conf, err := parseConfig(filefp)
|
conf, err := parseConfig(filefp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
return fmt.Errorf("cannot parse config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ntw, err := makeNetwork(conf)
|
ntw, err := makeNetwork(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit(err)
|
return fmt.Errorf("cannot build a network: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
|
ipv4 := conf.Network.PublicIP.IPv4.Value(nil)
|
||||||
ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
|
ipv6 := conf.Network.PublicIP.IPv6.Value(nil)
|
||||||
|
|
||||||
if ipv4 == nil {
|
if ipv4 == nil {
|
||||||
ipv4 = runAccessGetIP(ntw, "tcp4")
|
ipv4 = c.getIP(ntw, "tcp4")
|
||||||
}
|
}
|
||||||
|
|
||||||
if ipv6 == nil {
|
if ipv6 == nil {
|
||||||
ipv6 = runAccessGetIP(ntw, "tcp6")
|
ipv6 = c.getIP(ntw, "tcp6")
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := runAccessResponse{
|
resp := runAccessResponse{
|
||||||
IPv4: runMakeAccessResponseURLs(ipv4, conf, cli),
|
IPv4: c.makeResponseURLs(ipv4, conf, cli),
|
||||||
IPv6: runMakeAccessResponseURLs(ipv6, conf, cli),
|
IPv6: c.makeResponseURLs(ipv6, conf, cli),
|
||||||
}
|
}
|
||||||
resp.Secret.Base64 = conf.Secret.Base64()
|
resp.Secret.Base64 = conf.Secret.Base64()
|
||||||
resp.Secret.Hex = conf.Secret.Hex()
|
resp.Secret.Hex = conf.Secret.Hex()
|
||||||
@@ -73,11 +79,13 @@ func runAccess(cli *CLI) {
|
|||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
|
|
||||||
if err := encoder.Encode(resp); err != nil {
|
if err := encoder.Encode(resp); err != nil {
|
||||||
exit(err)
|
return fmt.Errorf("cannot dump access json: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
|
func (c *cliCommandAccess) getIP(ntw *network.Network, protocol string) net.IP {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: ntw.HTTP.Timeout,
|
Timeout: ntw.HTTP.Timeout,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
@@ -106,7 +114,7 @@ func runAccessGetIP(ntw *network.Network, protocol string) net.IP {
|
|||||||
return net.ParseIP(strings.TrimSpace(string(data)))
|
return net.ParseIP(strings.TrimSpace(string(data)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
func (c *cliCommandAccess) makeResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResponseURLs {
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -139,13 +147,13 @@ func runMakeAccessResponseURLs(ip net.IP, conf *config, cli *CLI) *runAccessResp
|
|||||||
}).String(),
|
}).String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
rv.TgQrCode = runMakeAccessResponseURLsQRCode(rv.TgURL)
|
rv.TgQrCode = c.makeResponseQRCode(rv.TgURL)
|
||||||
rv.TmeQrCode = runMakeAccessResponseURLsQRCode(rv.TmeURL)
|
rv.TmeQrCode = c.makeResponseQRCode(rv.TmeURL)
|
||||||
|
|
||||||
return rv
|
return rv
|
||||||
}
|
}
|
||||||
|
|
||||||
func runMakeAccessResponseURLsQRCode(data string) string {
|
func (c *cliCommandAccess) makeResponseQRCode(data string) string {
|
||||||
values := url.Values{}
|
values := url.Values{}
|
||||||
|
|
||||||
values.Set("qzone", "4")
|
values.Set("qzone", "4")
|
||||||
|
|||||||
@@ -6,7 +6,12 @@ import (
|
|||||||
"github.com/9seconds/mtg/v2/mtglib"
|
"github.com/9seconds/mtg/v2/mtglib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runGenerateSecret(cli *CLI) {
|
type cliCommandGenerateSecret struct {
|
||||||
|
HostName string `arg optional help:"Hostname to use for domain fronting. Default is '${domain_front}'." name:"hostname" default:"${domain_front}"` // nolint: lll, govet
|
||||||
|
Hex bool `help:"Print secret in hex encoding."`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cliCommandGenerateSecret) Run(cli *CLI) error { // nolint: unparam
|
||||||
secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
|
secret := mtglib.GenerateSecret(cli.GenerateSecret.HostName)
|
||||||
|
|
||||||
if cli.GenerateSecret.Hex {
|
if cli.GenerateSecret.Hex {
|
||||||
@@ -14,4 +19,6 @@ func runGenerateSecret(cli *CLI) {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Println(secret.Base64()) // nolint: forbidigo
|
fmt.Println(secret.Base64()) // nolint: forbidigo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,9 @@ import (
|
|||||||
var version = "dev" // has to be set by ldflags
|
var version = "dev" // has to be set by ldflags
|
||||||
|
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
GenerateSecret struct { // nolint: govet
|
GenerateSecret cliCommandGenerateSecret `cmd help:"Generate new proxy secret"` // nolint: govet
|
||||||
HostName string `arg optional help:"Hostname to use for domain fronting. Default is '${domain_front}'." name:"hostname" default:"${domain_front}"` // nolint: lll, govet
|
Access cliCommandAccess `cmd help:"Print access information."` // nolint: govet
|
||||||
Hex bool `help:"Print secret in hex encoding."`
|
Version kong.VersionFlag `help:"Print version."`
|
||||||
} `cmd help:"Generate new proxy secret."`
|
|
||||||
Access struct { // nolint: govet
|
|
||||||
ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
|
|
||||||
Hex bool `help:"Print secret in hex encoding."`
|
|
||||||
} `cmd help:"Print access information."`
|
|
||||||
Run struct { // nolint: govet
|
|
||||||
ConfigPath string `arg required type:"existingfile" help:"Path to the configuration file." name:"config-path"` // nolint: lll, govet
|
|
||||||
} `cmd help:"Run proxy."`
|
|
||||||
Version kong.VersionFlag `help:"Print version."`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -30,18 +21,8 @@ func main() {
|
|||||||
cli := &CLI{}
|
cli := &CLI{}
|
||||||
ctx := kong.Parse(cli, kong.Vars{
|
ctx := kong.Parse(cli, kong.Vars{
|
||||||
"domain_front": "amazonaws.com",
|
"domain_front": "amazonaws.com",
|
||||||
"config_path": "/etc/mtg.toml",
|
|
||||||
"version": version,
|
"version": version,
|
||||||
})
|
})
|
||||||
|
|
||||||
switch ctx.Command() {
|
ctx.FatalIfErrorf(ctx.Run(cli))
|
||||||
case "generate-secret":
|
|
||||||
runGenerateSecret(cli)
|
|
||||||
case "access <config-path>":
|
|
||||||
runAccess(cli)
|
|
||||||
case "run <config-path>":
|
|
||||||
panic("not implemented yet")
|
|
||||||
default:
|
|
||||||
panic(ctx.Command())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,10 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/9seconds/mtg/v2/mtglib/network"
|
"github.com/9seconds/mtg/v2/mtglib/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
func exit(err error) {
|
|
||||||
fmt.Fprintln(os.Stderr, err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeNetwork(conf *config) (*network.Network, error) {
|
func makeNetwork(conf *config) (*network.Network, error) {
|
||||||
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
|
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
|
||||||
httpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultHTTPTimeout)
|
httpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultHTTPTimeout)
|
||||||
|
|||||||
Reference in New Issue
Block a user