-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into stable
- Loading branch information
Showing
125 changed files
with
5,130 additions
and
3,677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
package antireplay | ||
|
||
import ( | ||
"github.com/allegro/bigcache" | ||
"github.com/juju/errors" | ||
import "github.com/VictoriaMetrics/fastcache" | ||
|
||
"github.com/9seconds/mtg/config" | ||
var ( | ||
prefixObfuscated2 = []byte{0x00} | ||
prefixTLS = []byte{0x01} | ||
) | ||
|
||
// Cache defines storage for obfuscated2 handshake frames. | ||
type Cache struct { | ||
cache *bigcache.BigCache | ||
type cache struct { | ||
data *fastcache.Cache | ||
} | ||
|
||
func (a Cache) Add(frame []byte) { | ||
a.cache.Set(string(frame), nil) // nolint: errcheck | ||
func (c *cache) AddObfuscated2(data []byte) { | ||
c.data.Set(keyObfuscated2(data), nil) | ||
} | ||
|
||
func (a Cache) Has(frame []byte) bool { | ||
_, err := a.cache.Get(string(frame)) | ||
func (c *cache) AddTLS(data []byte) { | ||
c.data.Set(keyTLS(data), nil) | ||
} | ||
|
||
func (c *cache) HasObfuscated2(data []byte) bool { | ||
return c.data.Has(keyObfuscated2(data)) | ||
} | ||
|
||
func (c *cache) HasTLS(data []byte) bool { | ||
return c.data.Has(keyTLS(data)) | ||
} | ||
|
||
return err == nil | ||
func keyObfuscated2(data []byte) []byte { | ||
return append(prefixObfuscated2, data...) | ||
} | ||
|
||
func NewCache(config *config.Config) (Cache, error) { | ||
cache, err := bigcache.NewBigCache(bigcache.Config{ | ||
Shards: 1024, | ||
LifeWindow: config.AntiReplayEvictionTime, | ||
Hasher: hasher{}, | ||
HardMaxCacheSize: config.AntiReplayMaxSize, | ||
}) | ||
if err != nil { | ||
return Cache{}, errors.Annotate(err, "Cannot make cache") | ||
} | ||
|
||
return Cache{cache}, nil | ||
func keyTLS(data []byte) []byte { | ||
return append(prefixTLS, data...) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package antireplay | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/VictoriaMetrics/fastcache" | ||
|
||
"mtg/config" | ||
) | ||
|
||
var ( | ||
Cache cache | ||
initOnce sync.Once | ||
) | ||
|
||
func Init() { | ||
initOnce.Do(func() { | ||
Cache.data = fastcache.New(config.C.AntiReplayMaxSize) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cli | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/hex" | ||
|
||
"mtg/config" | ||
) | ||
|
||
func Generate(secretType, hostname 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: | ||
PrintStdout("ee" + secret + hex.EncodeToString([]byte(hostname))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package cli | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"mtg/antireplay" | ||
"mtg/config" | ||
"mtg/faketls" | ||
"mtg/hub" | ||
"mtg/ntp" | ||
"mtg/obfuscated2" | ||
"mtg/proxy" | ||
"mtg/stats" | ||
"mtg/telegram" | ||
"mtg/utils" | ||
) | ||
|
||
func Proxy() error { // nolint: funlen | ||
ctx := utils.GetSignalContext() | ||
|
||
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(ctx); err != nil { | ||
Fatal(err) | ||
} | ||
|
||
zap.S().Debugw("Configuration", "config", config.Printable()) | ||
|
||
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 := stats.Init(ctx); err != nil { | ||
Fatal(err) | ||
} | ||
|
||
antireplay.Init() | ||
telegram.Init() | ||
hub.Init(ctx) | ||
|
||
proxyListener, err := net.Listen("tcp", config.C.Bind.String()) | ||
if err != nil { | ||
Fatal(err) | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
proxyListener.Close() | ||
}() | ||
|
||
app := &proxy.Proxy{ | ||
Logger: zap.S().Named("proxy"), | ||
Context: ctx, | ||
ClientProtocolMaker: obfuscated2.MakeClientProtocol, | ||
} | ||
if config.C.SecretMode == config.SecretModeTLS { | ||
app.ClientProtocolMaker = faketls.MakeClientProtocol | ||
} | ||
|
||
app.Serve(proxyListener) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
func Fatal(arg interface{}) { | ||
if value, ok := arg.(error); ok { | ||
arg = fmt.Errorf("fatal error: %+v", value) | ||
} | ||
|
||
PrintStderr(arg) | ||
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.