Browse Source

Show correct secrets

tags/0.10
9seconds 7 years ago
parent
commit
1f74aabbe4
2 changed files with 30 additions and 11 deletions
  1. 28
    8
      config/config.go
  2. 2
    3
      config/urls.go

+ 28
- 8
config/config.go View File

@@ -18,8 +18,9 @@ const (
18 18
 
19 19
 // Config represents common configuration of mtg.
20 20
 type Config struct {
21
-	Debug   bool
22
-	Verbose bool
21
+	Debug      bool
22
+	Verbose    bool
23
+	SecureMode bool
23 24
 
24 25
 	BindPort       uint16
25 26
 	PublicIPv4Port uint16
@@ -45,8 +46,9 @@ type URLs struct {
45 46
 
46 47
 // IPURLs contains links to both ipv4 and ipv6 of the proxy.
47 48
 type IPURLs struct {
48
-	IPv4 URLs `json:"ipv4"`
49
-	IPv6 URLs `json:"ipv6"`
49
+	IPv4      URLs   `json:"ipv4"`
50
+	IPv6      URLs   `json:"ipv6"`
51
+	BotSecret string `json:"secret_for_mtproxybot"`
50 52
 }
51 53
 
52 54
 // BindAddr returns connection for this server to bind to.
@@ -65,15 +67,29 @@ func (c *Config) UseMiddleProxy() bool {
65 67
 	return len(c.AdTag) > 0
66 68
 }
67 69
 
70
+func (c *Config) SecretString() string {
71
+	return hex.EncodeToString(c.Secret)
72
+}
73
+
74
+func (c *Config) BotSecretString() string {
75
+	secret := c.SecretString()
76
+	if c.SecureMode {
77
+		return "dd" + secret
78
+	}
79
+	return secret
80
+}
81
+
68 82
 // GetURLs returns configured IPURLs instance with links to this server.
69 83
 func (c *Config) GetURLs() IPURLs {
70 84
 	urls := IPURLs{}
85
+	secret := c.SecretString()
71 86
 	if c.PublicIPv4 != nil {
72
-		urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret)
87
+		urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, secret)
73 88
 	}
74 89
 	if c.PublicIPv6 != nil {
75
-		urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret)
90
+		urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, secret)
76 91
 	}
92
+	urls.BotSecret = c.BotSecretString()
77 93
 
78 94
 	return urls
79 95
 }
@@ -91,8 +107,11 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
91 107
 	publicIPv6 net.IP, publicIPv6Port uint16,
92 108
 	statsIP net.IP, statsPort uint16,
93 109
 	secret, adtag string) (*Config, error) {
94
-	secret = strings.TrimPrefix(secret, "dd")
95
-	if len(secret) != 32 {
110
+	secureMode := false
111
+	if strings.HasPrefix(secret, "dd") && len(secret) == 34 {
112
+		secureMode = true
113
+		secret = strings.TrimPrefix(secret, "dd")
114
+	} else if len(secret) != 32 {
96 115
 		return nil, errors.New("Telegram demands secret of length 32")
97 116
 	}
98 117
 	secretBytes, err := hex.DecodeString(secret)
@@ -149,6 +168,7 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
149 168
 		StatsPort:      statsPort,
150 169
 		Secret:         secretBytes,
151 170
 		AdTag:          adTagBytes,
171
+		SecureMode:     secureMode,
152 172
 	}
153 173
 
154 174
 	return conf, nil

+ 2
- 3
config/urls.go View File

@@ -1,17 +1,16 @@
1 1
 package config
2 2
 
3 3
 import (
4
-	"encoding/hex"
5 4
 	"net"
6 5
 	"net/url"
7 6
 	"strconv"
8 7
 )
9 8
 
10
-func getURLs(addr net.IP, port uint16, secret []byte) (urls URLs) {
9
+func getURLs(addr net.IP, port uint16, secret string) (urls URLs) {
11 10
 	values := url.Values{}
12 11
 	values.Set("server", addr.String())
13 12
 	values.Set("port", strconv.Itoa(int(port)))
14
-	values.Set("secret", hex.EncodeToString(secret))
13
+	values.Set("secret", secret)
15 14
 
16 15
 	urls.TG = makeTGURL(values)
17 16
 	urls.TMe = makeTMeURL(values)

Loading…
Cancel
Save