Просмотр исходного кода

Show correct secrets

tags/0.10
9seconds 7 лет назад
Родитель
Сommit
1f74aabbe4
2 измененных файлов: 30 добавлений и 11 удалений
  1. 28
    8
      config/config.go
  2. 2
    3
      config/urls.go

+ 28
- 8
config/config.go Просмотреть файл

18
 
18
 
19
 // Config represents common configuration of mtg.
19
 // Config represents common configuration of mtg.
20
 type Config struct {
20
 type Config struct {
21
-	Debug   bool
22
-	Verbose bool
21
+	Debug      bool
22
+	Verbose    bool
23
+	SecureMode bool
23
 
24
 
24
 	BindPort       uint16
25
 	BindPort       uint16
25
 	PublicIPv4Port uint16
26
 	PublicIPv4Port uint16
45
 
46
 
46
 // IPURLs contains links to both ipv4 and ipv6 of the proxy.
47
 // IPURLs contains links to both ipv4 and ipv6 of the proxy.
47
 type IPURLs struct {
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
 // BindAddr returns connection for this server to bind to.
54
 // BindAddr returns connection for this server to bind to.
65
 	return len(c.AdTag) > 0
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
 // GetURLs returns configured IPURLs instance with links to this server.
82
 // GetURLs returns configured IPURLs instance with links to this server.
69
 func (c *Config) GetURLs() IPURLs {
83
 func (c *Config) GetURLs() IPURLs {
70
 	urls := IPURLs{}
84
 	urls := IPURLs{}
85
+	secret := c.SecretString()
71
 	if c.PublicIPv4 != nil {
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
 	if c.PublicIPv6 != nil {
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
 	return urls
94
 	return urls
79
 }
95
 }
91
 	publicIPv6 net.IP, publicIPv6Port uint16,
107
 	publicIPv6 net.IP, publicIPv6Port uint16,
92
 	statsIP net.IP, statsPort uint16,
108
 	statsIP net.IP, statsPort uint16,
93
 	secret, adtag string) (*Config, error) {
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
 		return nil, errors.New("Telegram demands secret of length 32")
115
 		return nil, errors.New("Telegram demands secret of length 32")
97
 	}
116
 	}
98
 	secretBytes, err := hex.DecodeString(secret)
117
 	secretBytes, err := hex.DecodeString(secret)
149
 		StatsPort:      statsPort,
168
 		StatsPort:      statsPort,
150
 		Secret:         secretBytes,
169
 		Secret:         secretBytes,
151
 		AdTag:          adTagBytes,
170
 		AdTag:          adTagBytes,
171
+		SecureMode:     secureMode,
152
 	}
172
 	}
153
 
173
 
154
 	return conf, nil
174
 	return conf, nil

+ 2
- 3
config/urls.go Просмотреть файл

1
 package config
1
 package config
2
 
2
 
3
 import (
3
 import (
4
-	"encoding/hex"
5
 	"net"
4
 	"net"
6
 	"net/url"
5
 	"net/url"
7
 	"strconv"
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
 	values := url.Values{}
10
 	values := url.Values{}
12
 	values.Set("server", addr.String())
11
 	values.Set("server", addr.String())
13
 	values.Set("port", strconv.Itoa(int(port)))
12
 	values.Set("port", strconv.Itoa(int(port)))
14
-	values.Set("secret", hex.EncodeToString(secret))
13
+	values.Set("secret", secret)
15
 
14
 
16
 	urls.TG = makeTGURL(values)
15
 	urls.TG = makeTGURL(values)
17
 	urls.TMe = makeTMeURL(values)
16
 	urls.TMe = makeTMeURL(values)

Загрузка…
Отмена
Сохранить