mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 15:34:01 +03:00
Introduce explicit config
This commit is contained in:
@@ -3,18 +3,16 @@ package main
|
||||
//go:generate scripts/generate_version.sh
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/9seconds/mtg/proxy"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/proxy"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -28,8 +26,9 @@ var (
|
||||
Short('v').
|
||||
Envar("MTG_VERBOSE").
|
||||
Bool()
|
||||
|
||||
bindIP = app.Flag("bind-ip", "Which IP to bind to.").
|
||||
Short('i').
|
||||
Short('b').
|
||||
Envar("MTG_IP").
|
||||
Default("127.0.0.1").
|
||||
IP()
|
||||
@@ -38,11 +37,23 @@ var (
|
||||
Envar("MTG_PORT").
|
||||
Default("3128").
|
||||
Uint16()
|
||||
portToShow = app.Flag("show-bind-port",
|
||||
"Which port to show in URL. Default is the value of bind-port").
|
||||
Short('a').
|
||||
Envar("MTG_SHOW_PORT").
|
||||
Uint16()
|
||||
|
||||
publicIPv4 = app.Flag("public-ipv4", "Which IPv4 address is public.").
|
||||
Short('4').
|
||||
Envar("MTG_IPV4").
|
||||
IP()
|
||||
publicIPv4Port = app.Flag("public-ipv4-port", "Which IPv4 port is public. Default is 'bind-port' value.").
|
||||
Envar("MTG_IPV4_PORT").
|
||||
Uint16()
|
||||
|
||||
publicIPv6 = app.Flag("public-ipv6", "Which IPv6 address is public.").
|
||||
Short('6').
|
||||
Envar("MTG_IPV6").
|
||||
IP()
|
||||
publicIPv6Port = app.Flag("public-ipv6-port", "Which IPv6 port is public. Default is 'bind-port' value.").
|
||||
Envar("MTG_IPV6_PORT").
|
||||
Uint16()
|
||||
|
||||
statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
|
||||
Short('t').
|
||||
Envar("MTG_STATS_IP").
|
||||
@@ -53,6 +64,7 @@ var (
|
||||
Envar("MTG_STATS_PORT").
|
||||
Default("3129").
|
||||
Uint16()
|
||||
|
||||
readTimeout = app.Flag("read-timeout", "Socket read timeout.").
|
||||
Short('r').
|
||||
Envar("MTG_READ_TIMEOUT").
|
||||
@@ -63,15 +75,6 @@ var (
|
||||
Envar("MTG_WRITE_TIMEOUT").
|
||||
Default("30s").
|
||||
Duration()
|
||||
serverName = app.Flag("server-name",
|
||||
"Which server name to use. Default is IP address resolved by ipify.").
|
||||
Short('s').
|
||||
Envar("MTG_SERVER").
|
||||
String()
|
||||
preferIPv6 = app.Flag("prefer-ipv6", "Use IPv6").
|
||||
Short('6').
|
||||
Envar("MTG_USE_IPV6").
|
||||
Bool()
|
||||
|
||||
secret = app.Arg("secret", "Secret of this proxy.").Required().String()
|
||||
)
|
||||
@@ -80,33 +83,22 @@ func main() {
|
||||
app.Version(version)
|
||||
kingpin.MustParse(app.Parse(os.Args[1:]))
|
||||
|
||||
secretBytes, err := hex.DecodeString(*secret)
|
||||
conf, err := config.NewConfig(*debug, *verbose,
|
||||
*bindIP, *bindPort,
|
||||
*publicIPv4, *publicIPv4Port,
|
||||
*publicIPv6, *publicIPv6Port,
|
||||
*statsIP, *statsPort,
|
||||
*readTimeout, *writeTimeout,
|
||||
*secret,
|
||||
)
|
||||
if err != nil {
|
||||
usage("Secret has to be hexadecimal string.")
|
||||
}
|
||||
|
||||
if *portToShow == 0 {
|
||||
*portToShow = *bindPort
|
||||
}
|
||||
|
||||
if *serverName == "" {
|
||||
resp, err := http.Get("https://api.ipify.org")
|
||||
if err != nil || resp.StatusCode != http.StatusOK {
|
||||
usage("Cannot get local IP address.")
|
||||
}
|
||||
myIPBytes, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close() // nolint: errcheck
|
||||
|
||||
if err != nil {
|
||||
usage("Cannot get local IP address.")
|
||||
}
|
||||
*serverName = strings.TrimSpace(string(myIPBytes))
|
||||
usage(err.Error())
|
||||
}
|
||||
|
||||
atom := zap.NewAtomicLevel()
|
||||
if *debug {
|
||||
if conf.Debug {
|
||||
atom.SetLevel(zapcore.DebugLevel)
|
||||
} else if *verbose {
|
||||
} else if conf.Verbose {
|
||||
atom.SetLevel(zapcore.InfoLevel)
|
||||
} else {
|
||||
atom.SetLevel(zapcore.ErrorLevel)
|
||||
@@ -118,12 +110,12 @@ func main() {
|
||||
atom,
|
||||
)).Sugar()
|
||||
|
||||
stat := proxy.NewStats(*serverName, *portToShow, *secret)
|
||||
go stat.Serve(*statsIP, *statsPort)
|
||||
printURLs(stat.URLs)
|
||||
stat := proxy.NewStats(conf)
|
||||
go stat.Serve()
|
||||
|
||||
srv := proxy.NewServer(conf, logger, stat)
|
||||
printURLs(conf.GetURLs())
|
||||
|
||||
srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
|
||||
*readTimeout, *writeTimeout, *preferIPv6, stat)
|
||||
if err := srv.Serve(); err != nil {
|
||||
logger.Fatal(err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user