Преглед изворни кода

Correct secret parsing

tags/v2.0.0-rc1
9seconds пре 5 година
родитељ
комит
783c49db37
5 измењених фајлова са 107 додато и 82 уклоњено
  1. 83
    0
      config.go
  2. 1
    14
      main.go
  3. 7
    0
      mtglib/init.go
  4. 16
    5
      mtglib/secret.go
  5. 0
    63
      raw_config.go

+ 83
- 0
config.go Прегледај датотеку

@@ -0,0 +1,83 @@
1
+package main
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"fmt"
7
+	"io"
8
+
9
+	"github.com/9seconds/mtg/v2/mtglib"
10
+	"github.com/pelletier/go-toml"
11
+)
12
+
13
+type config struct {
14
+	Debug  bool          `json:"debug"`
15
+	Secret mtglib.Secret `json:"secret"`
16
+}
17
+
18
+type configRaw struct {
19
+	Debug     bool   `toml:"debug" json:"debug"`
20
+	Secret    string `toml:"secret" json:"secret"`
21
+	BindTo    string `toml:"bind-to" json:"bind-to"`
22
+	TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
23
+	PreferIP  string `toml:"prefer-ip" json:"prefer-ip"`
24
+	CloakPort uint   `toml:"cloak-port" json:"cloak-port"`
25
+	Probes    struct {
26
+		Time struct {
27
+			Enabled       bool   `toml:"enabled" json:"enabled"`
28
+			AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
29
+		} `toml:"time" json:"time"`
30
+		AntiReplay struct {
31
+			Enabled bool   `toml:"enabled" json:"enabled"`
32
+			MaxSize string `toml:"max-size" json:"max-size"`
33
+			TTL     string `toml:"ttl" json:"ttl"`
34
+		} `toml:"anti-replay" json:"anti-replay"`
35
+	} `toml:"probes" json:"probes"`
36
+	Network struct {
37
+		PublicIP struct {
38
+			IPv4 string `toml:"ipv4" json:"ipv4"`
39
+			IPv6 string `toml:"ipv6" json:"ipv6"`
40
+		} `toml:"public-ip" json:"public-ip"`
41
+		DOHHostname string   `toml:"doh-hostname" json:"doh-hostname"`
42
+		Proxies     []string `toml:"proxies" json:"proxies"`
43
+	} `toml:"network" json:"network"`
44
+	Stats struct {
45
+		StatsD struct {
46
+			Enabled      bool   `toml:"enabled" json:"enabled"`
47
+			Address      string `toml:"address" json:"address"`
48
+			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
49
+		} `toml:"statsd" json:"statsd"`
50
+		Prometheus struct {
51
+			Enabled      bool   `toml:"enabled" json:"enabled"`
52
+			BindTo       string `toml:"bind-to" json:"bind-to"`
53
+			HTTPPath     string `toml:"http-path" json:"http-path"`
54
+			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
55
+		} `toml:"prometheus" json:"prometheus"`
56
+	} `toml:"stats" json:"stats"`
57
+}
58
+
59
+func parseConfig(reader io.Reader) (*config, error) {
60
+	rawConf := &configRaw{}
61
+
62
+	if err := toml.NewDecoder(reader).Decode(rawConf); err != nil {
63
+		return nil, fmt.Errorf("cannot parse toml config: %w", err)
64
+	}
65
+
66
+	jsonBuf := &bytes.Buffer{}
67
+	jsonEncoder := json.NewEncoder(jsonBuf)
68
+
69
+	jsonEncoder.SetEscapeHTML(false)
70
+	jsonEncoder.SetIndent("", "")
71
+
72
+	if err := jsonEncoder.Encode(rawConf); err != nil {
73
+		return nil, fmt.Errorf("cannot dump into interim format: %w", err)
74
+	}
75
+
76
+	conf := &config{}
77
+
78
+	if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
79
+		return nil, fmt.Errorf("cannot parse final config: %w", err)
80
+	}
81
+
82
+	return conf, nil
83
+}

+ 1
- 14
main.go Прегледај датотеку

@@ -2,12 +2,9 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"io/ioutil"
6 5
 	"math/rand"
7 6
 	"os"
8 7
 	"time"
9
-
10
-	"github.com/9seconds/mtg/v2/mtglib/network"
11 8
 )
12 9
 
13 10
 var version = "dev" // has to be set by ldflags
@@ -17,15 +14,5 @@ func main() {
17 14
 
18 15
 	f, _ := os.Open("example.config.toml")
19 16
 
20
-	fmt.Println(parseRawConfig(f))
21
-
22
-	bd, _ := network.NewDefaultDialer(0, 0)
23
-	d, _ := network.NewNetwork(bd, "9.9.9.9", 0)
24
-
25
-	r, err := d.HTTP.Get("https://ifconfig.co")
26
-
27
-	fmt.Println(err)
28
-	body, _ := ioutil.ReadAll(r.Body)
29
-
30
-	fmt.Println(string(body))
17
+	fmt.Println(parseConfig(f))
31 18
 }

+ 7
- 0
mtglib/init.go Прегледај датотеку

@@ -0,0 +1,7 @@
1
+package mtglib
2
+
3
+import "errors"
4
+
5
+var (
6
+	ErrSecretEmpty = errors.New("secret is empty")
7
+)

+ 16
- 5
mtglib/secret.go Прегледај датотеку

@@ -8,7 +8,7 @@ import (
8 8
 	"strings"
9 9
 )
10 10
 
11
-const SecretKeyLength = 32
11
+const SecretKeyLength = 16
12 12
 
13 13
 type Secret struct {
14 14
 	Key  []byte
@@ -30,11 +30,19 @@ func (s *Secret) UnmarshalText(data []byte) error {
30 30
 		return ErrSecretEmpty
31 31
 	}
32 32
 
33
-	decoded, err := base64.RawStdEncoding.DecodeString(text)
34
-	if err != nil && strings.HasPrefix(text, "ee") {
33
+	var (
34
+		decoded []byte
35
+		err     error
36
+	)
37
+
38
+	if strings.HasPrefix(text, "ee") {
35 39
 		decoded, err = hex.DecodeString(strings.TrimPrefix(text, "ee"))
36 40
 	}
37 41
 
42
+	if err != nil || len(decoded) <= SecretKeyLength {
43
+		decoded, err = base64.RawURLEncoding.DecodeString(text)
44
+	}
45
+
38 46
 	if err != nil {
39 47
 		return fmt.Errorf("incorrect secret format: %w", err)
40 48
 	}
@@ -50,7 +58,10 @@ func (s *Secret) UnmarshalText(data []byte) error {
50 58
 }
51 59
 
52 60
 func (s Secret) Base64() string {
53
-	return base64.StdEncoding.EncodeToString(append(s.Key[:], s.Host...))
61
+    data := append([]byte{238}, s.Key...) // 238 = hex ee
62
+    data = append(data, s.Host...)
63
+
64
+    return base64.RawURLEncoding.EncodeToString(data)
54 65
 }
55 66
 
56 67
 func (s Secret) String() string {
@@ -58,7 +69,7 @@ func (s Secret) String() string {
58 69
 }
59 70
 
60 71
 func (s Secret) EE() string {
61
-	return "ee" + hex.EncodeToString(append(s.Key[:], s.Host...))
72
+	return "ee" + hex.EncodeToString(append(s.Key, s.Host...))
62 73
 }
63 74
 
64 75
 func GenerateSecret(hostname string) Secret {

+ 0
- 63
raw_config.go Прегледај датотеку

@@ -1,63 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"fmt"
5
-	"io"
6
-
7
-	"github.com/pelletier/go-toml"
8
-)
9
-
10
-type rawConfig struct {
11
-	Debug     bool   `toml:"debug"`
12
-	Secret    string `toml:"secret"`
13
-	BindTo    string `toml:"bind-to"`
14
-	TCPBuffer string `toml:"tcp-buffer"`
15
-	PreferIP  string `toml:"prefer-ip"`
16
-	CloakPort uint   `toml:"cloak-port"`
17
-	Probes    struct {
18
-		Time struct {
19
-			Enabled       bool   `toml:"enabled"`
20
-			AllowSkewness string `toml:"allow-skewness"`
21
-		} `toml:"time"`
22
-		AntiReplay struct {
23
-			Enabled bool   `toml:"enabled"`
24
-			MaxSize string `toml:"max-size"`
25
-			TTL     string `toml:"ttl"`
26
-		} `toml:"anti-replay"`
27
-	} `toml:"probes"`
28
-	Network struct {
29
-		PublicIP struct {
30
-			IPv4 string `toml:"ipv4"`
31
-			IPv6 string `toml:"ipv6"`
32
-		} `toml:"public-ip"`
33
-		Dialers struct {
34
-			Telegram string `toml:"telegram"`
35
-			Default  string `toml:"default"`
36
-		} `toml:"dialers"`
37
-		DOHHostname string   `toml:"doh-hostname"`
38
-		Proxies     []string `toml:"proxies"`
39
-	} `toml:"network"`
40
-	Stats struct {
41
-		StatsD struct {
42
-			Enabled      bool   `toml:"enabled"`
43
-			Address      string `toml:"address"`
44
-			MetricPrefix string `toml:"metric-prefix"`
45
-		} `toml:"statsd"`
46
-		Prometheus struct {
47
-			Enabled      bool   `toml:"enabled"`
48
-			BindTo       string `toml:"bind-to"`
49
-			HttpPath     string `toml:"http-path"`
50
-			MetricPrefix string `toml:"metric-prefix"`
51
-		} `toml:"prometheus"`
52
-	} `toml:"stats"`
53
-}
54
-
55
-func parseRawConfig(reader io.Reader) (*rawConfig, error) {
56
-	conf := &rawConfig{}
57
-
58
-	if err := toml.NewDecoder(reader).Decode(conf); err != nil {
59
-		return nil, fmt.Errorf("cannot parse config: %w", err)
60
-	}
61
-
62
-	return conf, nil
63
-}

Loading…
Откажи
Сачувај