Show correct secrets

This commit is contained in:
9seconds
2018-07-12 18:12:12 +03:00
parent 6c0954004e
commit 1f74aabbe4
2 changed files with 30 additions and 11 deletions
+28 -8
View File
@@ -18,8 +18,9 @@ const (
// Config represents common configuration of mtg.
type Config struct {
Debug bool
Verbose bool
Debug bool
Verbose bool
SecureMode bool
BindPort uint16
PublicIPv4Port uint16
@@ -45,8 +46,9 @@ type URLs struct {
// IPURLs contains links to both ipv4 and ipv6 of the proxy.
type IPURLs struct {
IPv4 URLs `json:"ipv4"`
IPv6 URLs `json:"ipv6"`
IPv4 URLs `json:"ipv4"`
IPv6 URLs `json:"ipv6"`
BotSecret string `json:"secret_for_mtproxybot"`
}
// BindAddr returns connection for this server to bind to.
@@ -65,15 +67,29 @@ func (c *Config) UseMiddleProxy() bool {
return len(c.AdTag) > 0
}
func (c *Config) SecretString() string {
return hex.EncodeToString(c.Secret)
}
func (c *Config) BotSecretString() string {
secret := c.SecretString()
if c.SecureMode {
return "dd" + secret
}
return secret
}
// GetURLs returns configured IPURLs instance with links to this server.
func (c *Config) GetURLs() IPURLs {
urls := IPURLs{}
secret := c.SecretString()
if c.PublicIPv4 != nil {
urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret)
urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, secret)
}
if c.PublicIPv6 != nil {
urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret)
urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, secret)
}
urls.BotSecret = c.BotSecretString()
return urls
}
@@ -91,8 +107,11 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
publicIPv6 net.IP, publicIPv6Port uint16,
statsIP net.IP, statsPort uint16,
secret, adtag string) (*Config, error) {
secret = strings.TrimPrefix(secret, "dd")
if len(secret) != 32 {
secureMode := false
if strings.HasPrefix(secret, "dd") && len(secret) == 34 {
secureMode = true
secret = strings.TrimPrefix(secret, "dd")
} else if len(secret) != 32 {
return nil, errors.New("Telegram demands secret of length 32")
}
secretBytes, err := hex.DecodeString(secret)
@@ -149,6 +168,7 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
StatsPort: statsPort,
Secret: secretBytes,
AdTag: adTagBytes,
SecureMode: secureMode,
}
return conf, nil
+2 -3
View File
@@ -1,17 +1,16 @@
package config
import (
"encoding/hex"
"net"
"net/url"
"strconv"
)
func getURLs(addr net.IP, port uint16, secret []byte) (urls URLs) {
func getURLs(addr net.IP, port uint16, secret string) (urls URLs) {
values := url.Values{}
values.Set("server", addr.String())
values.Set("port", strconv.Itoa(int(port)))
values.Set("secret", hex.EncodeToString(secret))
values.Set("secret", secret)
urls.TG = makeTGURL(values)
urls.TMe = makeTMeURL(values)