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

Print URLs in stats endpoint

tags/0.9
9seconds 8 лет назад
Родитель
Сommit
6abdc283ff
2 измененных файлов: 79 добавлений и 29 удалений
  1. 11
    26
      main.go
  2. 68
    3
      proxy/stats.go

+ 11
- 26
main.go Просмотреть файл

4
 
4
 
5
 import (
5
 import (
6
 	"encoding/hex"
6
 	"encoding/hex"
7
-	"fmt"
8
 	"io"
7
 	"io"
9
 	"io/ioutil"
8
 	"io/ioutil"
10
 	"net/http"
9
 	"net/http"
11
-	"net/url"
12
 	"os"
10
 	"os"
13
-	"strconv"
14
 	"strings"
11
 	"strings"
15
 
12
 
16
 	"github.com/9seconds/mtg/proxy"
13
 	"github.com/9seconds/mtg/proxy"
40
 			Envar("MTG_PORT").
37
 			Envar("MTG_PORT").
41
 			Default("3128").
38
 			Default("3128").
42
 			Uint16()
39
 			Uint16()
40
+	portToShow = app.Flag("show-bind-port",
41
+		"Which port to show in URL. Default is the value of bind-port").
42
+		Short('a').
43
+		Envar("MTG_SHOW_PORT").
44
+		Uint16()
43
 	statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
45
 	statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
44
 		Short('t').
46
 		Short('t').
45
 		Envar("MTG_STATS_IP").
47
 		Envar("MTG_STATS_IP").
78
 		usage("Secret has to be hexadecimal string.")
80
 		usage("Secret has to be hexadecimal string.")
79
 	}
81
 	}
80
 
82
 
83
+	if *portToShow == 0 {
84
+		*portToShow = *bindPort
85
+	}
86
+
81
 	if *serverName == "" {
87
 	if *serverName == "" {
82
 		resp, err := http.Get("https://api.ipify.org")
88
 		resp, err := http.Get("https://api.ipify.org")
83
 		if err != nil || resp.StatusCode != http.StatusOK {
89
 		if err != nil || resp.StatusCode != http.StatusOK {
107
 		atom,
113
 		atom,
108
 	)).Sugar()
114
 	)).Sugar()
109
 
115
 
110
-	printURLs()
111
-
112
-	stat := proxy.NewStats()
116
+	stat := proxy.NewStats(*serverName, *portToShow, *secret)
113
 	go stat.Serve(*statsIP, *statsPort)
117
 	go stat.Serve(*statsIP, *statsPort)
114
 
118
 
115
 	srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
119
 	srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
120
 }
124
 }
121
 
125
 
122
 func usage(msg string) {
126
 func usage(msg string) {
123
-	io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck
127
+	io.WriteString(os.Stderr, msg+"\n")
124
 	os.Exit(1)
128
 	os.Exit(1)
125
 }
129
 }
126
-
127
-func printURLs() {
128
-	values := url.Values{}
129
-	values.Set("server", *serverName)
130
-	values.Set("port", strconv.Itoa(int(*bindPort)))
131
-	values.Set("secret", *secret)
132
-
133
-	tgURL := url.URL{
134
-		Scheme:   "tg",
135
-		Host:     "proxy",
136
-		RawQuery: values.Encode(),
137
-	}
138
-	fmt.Println(tgURL.String())
139
-
140
-	tgURL.Scheme = "https"
141
-	tgURL.Host = "t.me"
142
-	tgURL.Path = "proxy"
143
-	fmt.Println(tgURL.String())
144
-}

+ 68
- 3
proxy/stats.go Просмотреть файл

4
 	"encoding/json"
4
 	"encoding/json"
5
 	"net"
5
 	"net"
6
 	"net/http"
6
 	"net/http"
7
+	"net/url"
7
 	"strconv"
8
 	"strconv"
8
 	"sync/atomic"
9
 	"sync/atomic"
9
 	"time"
10
 	"time"
23
 		Incoming uint64 `json:"incoming"`
24
 		Incoming uint64 `json:"incoming"`
24
 		Outgoing uint64 `json:"outgoing"`
25
 		Outgoing uint64 `json:"outgoing"`
25
 	} `json:"traffic"`
26
 	} `json:"traffic"`
27
+	URLs struct {
28
+		TG        string `json:"tg_url"`
29
+		TMe       string `json:"tme_url"`
30
+		TGQRCode  string `json:"tg_qrcode"`
31
+		TMeQRCode string `json:"tme_qrcode"`
32
+	} `json:"urls"`
26
 	Uptime statsUptime `json:"uptime"`
33
 	Uptime statsUptime `json:"uptime"`
27
 }
34
 }
28
 
35
 
46
 func (s *Stats) Serve(host net.IP, port uint16) {
53
 func (s *Stats) Serve(host net.IP, port uint16) {
47
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
54
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
48
 		w.Header().Set("Content-Type", "application/json")
55
 		w.Header().Set("Content-Type", "application/json")
49
-		json.NewEncoder(w).Encode(s)
56
+
57
+		encoder := json.NewEncoder(w)
58
+		encoder.SetEscapeHTML(false)
59
+		encoder.SetIndent("", "  ")
60
+		encoder.Encode(s)
50
 	})
61
 	})
51
 
62
 
52
 	addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
63
 	addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
53
 	http.ListenAndServe(addr, nil)
64
 	http.ListenAndServe(addr, nil)
54
 }
65
 }
55
 
66
 
56
-func NewStats() *Stats {
57
-	return &Stats{Uptime: statsUptime(time.Now())}
67
+func NewStats(serverName string, port uint16, secret string) *Stats {
68
+	urlQuery := makeURLQuery(serverName, port, secret)
69
+
70
+	stat := &Stats{Uptime: statsUptime(time.Now())}
71
+	stat.URLs.TG = makeTGURL(urlQuery)
72
+	stat.URLs.TMe = makeTMeURL(urlQuery)
73
+	stat.URLs.TGQRCode = makeQRCodeURL(stat.URLs.TG)
74
+	stat.URLs.TMeQRCode = makeQRCodeURL(stat.URLs.TMe)
75
+
76
+	return stat
77
+}
78
+
79
+func makeURLQuery(serverName string, port uint16, secret string) url.Values {
80
+	values := url.Values{}
81
+	values.Set("server", serverName)
82
+	values.Set("port", strconv.Itoa(int(port)))
83
+	values.Set("secret", secret)
84
+
85
+	return values
86
+}
87
+
88
+func makeTGURL(values url.Values) string {
89
+	tgURL := url.URL{
90
+		Scheme:   "tg",
91
+		Host:     "proxy",
92
+		RawQuery: values.Encode(),
93
+	}
94
+
95
+	return tgURL.String()
96
+}
97
+
98
+func makeTMeURL(values url.Values) string {
99
+	tMeURL := url.URL{
100
+		Scheme:   "https",
101
+		Host:     "t.me",
102
+		Path:     "proxy",
103
+		RawQuery: values.Encode(),
104
+	}
105
+
106
+	return tMeURL.String()
107
+}
108
+
109
+func makeQRCodeURL(data string) string {
110
+	QRURL := url.URL{
111
+		Scheme: "https",
112
+		Host:   "api.qrserver.com",
113
+		Path:   "v1/create-qr-code",
114
+	}
115
+
116
+	values := url.Values{}
117
+	values.Set("qzone", "4")
118
+	values.Set("format", "svg")
119
+	values.Set("data", data)
120
+	QRURL.RawQuery = values.Encode()
121
+
122
+	return QRURL.String()
58
 }
123
 }

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