Direct proxy works

This commit is contained in:
9seconds
2019-09-04 10:19:01 +03:00
parent 07985cf418
commit 2492a47d0a
87 changed files with 1883 additions and 1447 deletions
+25
View File
@@ -0,0 +1,25 @@
package cli
import (
"crypto/rand"
"encoding/hex"
"github.com/9seconds/mtg/config"
)
func Generate(secretType string) {
data := make([]byte, config.SimpleSecretLength)
if _, err := rand.Read(data); err != nil {
panic(err)
}
secret := hex.EncodeToString(data)
switch secretType {
case "simple":
PrintStdout(secret)
case "secured":
PrintStdout("dd" + secret)
default:
Fatal("Unknown secret type " + secret)
}
}
+86
View File
@@ -0,0 +1,86 @@
package cli
import (
"net"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/9seconds/mtg/antireplay"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/ntp"
"github.com/9seconds/mtg/obfuscated2"
"github.com/9seconds/mtg/proxy"
"github.com/9seconds/mtg/stats"
"github.com/9seconds/mtg/telegram"
)
func Proxy() error {
atom := zap.NewAtomicLevel()
switch {
case config.C.Debug:
atom.SetLevel(zapcore.DebugLevel)
case config.C.Verbose:
atom.SetLevel(zapcore.InfoLevel)
default:
atom.SetLevel(zapcore.ErrorLevel)
}
encoderCfg := zap.NewProductionEncoderConfig()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stderr),
atom,
))
zap.ReplaceGlobals(logger)
defer logger.Sync() // nolint: errcheck
if err := config.InitPublicAddress(); err != nil {
Fatal(err.Error())
}
zap.S().Debugw("Configuration", "config", config.C)
if len(config.C.AdTag) > 0 {
zap.S().Infow("Use middle proxy connection to Telegram")
diff, err := ntp.Fetch()
if err != nil {
Fatal("Cannot fetch time data from NTP")
}
if diff > time.Second {
Fatal("Your local time is skewed and drift is bigger than a second. Please sync your time.")
}
go ntp.AutoUpdate()
} else {
zap.S().Infow("Use direct connection to Telegram")
}
PrintJSONStdout(config.GetURLs())
if err := antireplay.Init(); err != nil {
Fatal(err.Error())
}
if err := stats.Init(); err != nil {
Fatal(err.Error())
}
proxyListener, err := net.Listen("tcp", config.C.ListenAddr.String())
if err != nil {
Fatal(err.Error())
}
app := &proxy.Proxy{
Logger: zap.S().Named("proxy"),
}
if len(config.C.AdTag) == 0 {
app.TelegramProtocolMaker = obfuscated2.MakeTelegramProtocol
app.TelegramDialer = telegram.NewDirectTelegram()
}
if config.C.SecretMode != config.SecretModeTLS {
app.ClientProtocolMaker = obfuscated2.MakeClientProtocol
}
app.Serve(proxyListener)
return nil
}
+39
View File
@@ -0,0 +1,39 @@
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
)
func Fatal(args ...interface{}) {
PrintStderr(args...)
os.Exit(1)
}
func PrintStderr(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
}
func PrintStdout(args ...interface{}) {
fmt.Println(args...)
}
func PrintJSONStderr(data interface{}) {
printJSON(os.Stderr, data)
}
func PrintJSONStdout(data interface{}) {
printJSON(os.Stdout, data)
}
func printJSON(writer io.Writer, data interface{}) {
encoder := json.NewEncoder(writer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
panic(err)
}
}