|
|
@@ -1,28 +1,25 @@
|
|
1
|
1
|
package config
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
|
4
|
+ "bytes"
|
|
4
|
5
|
"encoding/hex"
|
|
5
|
6
|
"fmt"
|
|
6
|
7
|
"net"
|
|
7
|
8
|
"strconv"
|
|
8
|
|
- "strings"
|
|
9
|
9
|
|
|
10
|
10
|
"github.com/juju/errors"
|
|
11
|
11
|
statsd "gopkg.in/alexcesaro/statsd.v2"
|
|
12
|
12
|
)
|
|
13
|
13
|
|
|
14
|
|
-// Buffer sizes define internal socket buffer sizes.
|
|
15
|
|
-const (
|
|
16
|
|
- BufferWriteSize = 32 * 1024
|
|
17
|
|
- BufferReadSize = 32 * 1024
|
|
18
|
|
-)
|
|
19
|
|
-
|
|
20
|
14
|
// Config represents common configuration of mtg.
|
|
21
|
15
|
type Config struct {
|
|
22
|
16
|
Debug bool
|
|
23
|
17
|
Verbose bool
|
|
24
|
18
|
SecureMode bool
|
|
25
|
19
|
|
|
|
20
|
+ ReadBufferSize int
|
|
|
21
|
+ WriteBufferSize int
|
|
|
22
|
+
|
|
26
|
23
|
BindPort uint16
|
|
27
|
24
|
PublicIPv4Port uint16
|
|
28
|
25
|
PublicIPv6Port uint16
|
|
|
@@ -114,30 +111,21 @@ func getAddr(host fmt.Stringer, port uint16) string {
|
|
114
|
111
|
// fetches data from external sources. Parameters passed to this
|
|
115
|
112
|
// function, should come from command line arguments.
|
|
116
|
113
|
func NewConfig(debug, verbose bool, // nolint: gocyclo
|
|
|
114
|
+ writeBufferSize, readBufferSize uint32,
|
|
117
|
115
|
bindIP, publicIPv4, publicIPv6, statsIP net.IP,
|
|
118
|
116
|
bindPort, publicIPv4Port, publicIPv6Port, statsPort, statsdPort uint16,
|
|
119
|
|
- secret, adtag, statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
|
|
120
|
|
- statsdTags map[string]string) (*Config, error) {
|
|
|
117
|
+ statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
|
|
|
118
|
+ statsdTags map[string]string,
|
|
|
119
|
+ secret, adtag []byte) (*Config, error) {
|
|
121
|
120
|
secureMode := false
|
|
122
|
|
- if strings.HasPrefix(secret, "dd") && len(secret) == 34 {
|
|
|
121
|
+ if bytes.HasPrefix(secret, []byte{0xdd}) && len(secret) == 17 {
|
|
123
|
122
|
secureMode = true
|
|
124
|
|
- secret = strings.TrimPrefix(secret, "dd")
|
|
125
|
|
- } else if len(secret) != 32 {
|
|
|
123
|
+ secret = bytes.TrimPrefix(secret, []byte{0xdd})
|
|
|
124
|
+ } else if len(secret) != 16 {
|
|
126
|
125
|
return nil, errors.New("Telegram demands secret of length 32")
|
|
127
|
126
|
}
|
|
128
|
|
- secretBytes, err := hex.DecodeString(secret)
|
|
129
|
|
- if err != nil {
|
|
130
|
|
- return nil, errors.Annotate(err, "Cannot create config")
|
|
131
|
|
- }
|
|
132
|
|
-
|
|
133
|
|
- var adTagBytes []byte
|
|
134
|
|
- if len(adtag) != 0 {
|
|
135
|
|
- adTagBytes, err = hex.DecodeString(adtag)
|
|
136
|
|
- if err != nil {
|
|
137
|
|
- return nil, errors.Annotate(err, "Cannot create config")
|
|
138
|
|
- }
|
|
139
|
|
- }
|
|
140
|
127
|
|
|
|
128
|
+ var err error
|
|
141
|
129
|
if publicIPv4 == nil {
|
|
142
|
130
|
publicIPv4, err = getGlobalIPv4()
|
|
143
|
131
|
if err != nil {
|
|
|
@@ -167,19 +155,21 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
|
|
167
|
155
|
}
|
|
168
|
156
|
|
|
169
|
157
|
conf := &Config{
|
|
170
|
|
- Debug: debug,
|
|
171
|
|
- Verbose: verbose,
|
|
172
|
|
- BindIP: bindIP,
|
|
173
|
|
- BindPort: bindPort,
|
|
174
|
|
- PublicIPv4: publicIPv4,
|
|
175
|
|
- PublicIPv4Port: publicIPv4Port,
|
|
176
|
|
- PublicIPv6: publicIPv6,
|
|
177
|
|
- PublicIPv6Port: publicIPv6Port,
|
|
178
|
|
- StatsIP: statsIP,
|
|
179
|
|
- StatsPort: statsPort,
|
|
180
|
|
- Secret: secretBytes,
|
|
181
|
|
- AdTag: adTagBytes,
|
|
182
|
|
- SecureMode: secureMode,
|
|
|
158
|
+ Debug: debug,
|
|
|
159
|
+ Verbose: verbose,
|
|
|
160
|
+ BindIP: bindIP,
|
|
|
161
|
+ BindPort: bindPort,
|
|
|
162
|
+ PublicIPv4: publicIPv4,
|
|
|
163
|
+ PublicIPv4Port: publicIPv4Port,
|
|
|
164
|
+ PublicIPv6: publicIPv6,
|
|
|
165
|
+ PublicIPv6Port: publicIPv6Port,
|
|
|
166
|
+ StatsIP: statsIP,
|
|
|
167
|
+ StatsPort: statsPort,
|
|
|
168
|
+ Secret: secret,
|
|
|
169
|
+ AdTag: adtag,
|
|
|
170
|
+ SecureMode: secureMode,
|
|
|
171
|
+ ReadBufferSize: int(readBufferSize),
|
|
|
172
|
+ WriteBufferSize: int(writeBufferSize),
|
|
183
|
173
|
}
|
|
184
|
174
|
|
|
185
|
175
|
if statsdIP != "" {
|
|
|
@@ -187,7 +177,10 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
|
|
187
|
177
|
conf.StatsD.Prefix = statsdPrefix
|
|
188
|
178
|
conf.StatsD.Tags = statsdTags
|
|
189
|
179
|
|
|
190
|
|
- var addr net.Addr
|
|
|
180
|
+ var (
|
|
|
181
|
+ addr net.Addr
|
|
|
182
|
+ err error
|
|
|
183
|
+ )
|
|
191
|
184
|
hostPort := net.JoinHostPort(statsdIP, strconv.Itoa(int(statsdPort)))
|
|
192
|
185
|
switch statsdNetwork {
|
|
193
|
186
|
case "tcp":
|