From 6abdc283ff378e7cd77846ff9fa03ec64485be73 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Thu, 31 May 2018 08:24:57 +0300 Subject: [PATCH] Print URLs in stats endpoint --- main.go | 37 ++++++++------------------ proxy/stats.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 6f8d278..2935d89 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,10 @@ package main import ( "encoding/hex" - "fmt" "io" "io/ioutil" "net/http" - "net/url" "os" - "strconv" "strings" "github.com/9seconds/mtg/proxy" @@ -40,6 +37,11 @@ var ( Envar("MTG_PORT"). Default("3128"). Uint16() + portToShow = app.Flag("show-bind-port", + "Which port to show in URL. Default is the value of bind-port"). + Short('a'). + Envar("MTG_SHOW_PORT"). + Uint16() statsIP = app.Flag("stats-ip", "Which IP bind stats server to"). Short('t'). Envar("MTG_STATS_IP"). @@ -78,6 +80,10 @@ func main() { usage("Secret has to be hexadecimal string.") } + if *portToShow == 0 { + *portToShow = *bindPort + } + if *serverName == "" { resp, err := http.Get("https://api.ipify.org") if err != nil || resp.StatusCode != http.StatusOK { @@ -107,9 +113,7 @@ func main() { atom, )).Sugar() - printURLs() - - stat := proxy.NewStats() + stat := proxy.NewStats(*serverName, *portToShow, *secret) go stat.Serve(*statsIP, *statsPort) srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger, @@ -120,25 +124,6 @@ func main() { } func usage(msg string) { - io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck + io.WriteString(os.Stderr, msg+"\n") os.Exit(1) } - -func printURLs() { - values := url.Values{} - values.Set("server", *serverName) - values.Set("port", strconv.Itoa(int(*bindPort))) - values.Set("secret", *secret) - - tgURL := url.URL{ - Scheme: "tg", - Host: "proxy", - RawQuery: values.Encode(), - } - fmt.Println(tgURL.String()) - - tgURL.Scheme = "https" - tgURL.Host = "t.me" - tgURL.Path = "proxy" - fmt.Println(tgURL.String()) -} diff --git a/proxy/stats.go b/proxy/stats.go index 02016d1..a7375e2 100644 --- a/proxy/stats.go +++ b/proxy/stats.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net" "net/http" + "net/url" "strconv" "sync/atomic" "time" @@ -23,6 +24,12 @@ type Stats struct { Incoming uint64 `json:"incoming"` Outgoing uint64 `json:"outgoing"` } `json:"traffic"` + URLs struct { + TG string `json:"tg_url"` + TMe string `json:"tme_url"` + TGQRCode string `json:"tg_qrcode"` + TMeQRCode string `json:"tme_qrcode"` + } `json:"urls"` Uptime statsUptime `json:"uptime"` } @@ -46,13 +53,71 @@ func (s *Stats) addOutgoingTraffic(n int) { func (s *Stats) Serve(host net.IP, port uint16) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(s) + + encoder := json.NewEncoder(w) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + encoder.Encode(s) }) addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port))) http.ListenAndServe(addr, nil) } -func NewStats() *Stats { - return &Stats{Uptime: statsUptime(time.Now())} +func NewStats(serverName string, port uint16, secret string) *Stats { + urlQuery := makeURLQuery(serverName, port, secret) + + stat := &Stats{Uptime: statsUptime(time.Now())} + stat.URLs.TG = makeTGURL(urlQuery) + stat.URLs.TMe = makeTMeURL(urlQuery) + stat.URLs.TGQRCode = makeQRCodeURL(stat.URLs.TG) + stat.URLs.TMeQRCode = makeQRCodeURL(stat.URLs.TMe) + + return stat +} + +func makeURLQuery(serverName string, port uint16, secret string) url.Values { + values := url.Values{} + values.Set("server", serverName) + values.Set("port", strconv.Itoa(int(port))) + values.Set("secret", secret) + + return values +} + +func makeTGURL(values url.Values) string { + tgURL := url.URL{ + Scheme: "tg", + Host: "proxy", + RawQuery: values.Encode(), + } + + return tgURL.String() +} + +func makeTMeURL(values url.Values) string { + tMeURL := url.URL{ + Scheme: "https", + Host: "t.me", + Path: "proxy", + RawQuery: values.Encode(), + } + + return tMeURL.String() +} + +func makeQRCodeURL(data string) string { + QRURL := url.URL{ + Scheme: "https", + Host: "api.qrserver.com", + Path: "v1/create-qr-code", + } + + values := url.Values{} + values.Set("qzone", "4") + values.Set("format", "svg") + values.Set("data", data) + QRURL.RawQuery = values.Encode() + + return QRURL.String() }