Sfoglia il codice sorgente

Print URLs in stats endpoint

tags/0.9
9seconds 8 anni fa
parent
commit
6abdc283ff
2 ha cambiato i file con 79 aggiunte e 29 eliminazioni
  1. 11
    26
      main.go
  2. 68
    3
      proxy/stats.go

+ 11
- 26
main.go Vedi File

@@ -4,13 +4,10 @@ package main
4 4
 
5 5
 import (
6 6
 	"encoding/hex"
7
-	"fmt"
8 7
 	"io"
9 8
 	"io/ioutil"
10 9
 	"net/http"
11
-	"net/url"
12 10
 	"os"
13
-	"strconv"
14 11
 	"strings"
15 12
 
16 13
 	"github.com/9seconds/mtg/proxy"
@@ -40,6 +37,11 @@ var (
40 37
 			Envar("MTG_PORT").
41 38
 			Default("3128").
42 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 45
 	statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
44 46
 		Short('t').
45 47
 		Envar("MTG_STATS_IP").
@@ -78,6 +80,10 @@ func main() {
78 80
 		usage("Secret has to be hexadecimal string.")
79 81
 	}
80 82
 
83
+	if *portToShow == 0 {
84
+		*portToShow = *bindPort
85
+	}
86
+
81 87
 	if *serverName == "" {
82 88
 		resp, err := http.Get("https://api.ipify.org")
83 89
 		if err != nil || resp.StatusCode != http.StatusOK {
@@ -107,9 +113,7 @@ func main() {
107 113
 		atom,
108 114
 	)).Sugar()
109 115
 
110
-	printURLs()
111
-
112
-	stat := proxy.NewStats()
116
+	stat := proxy.NewStats(*serverName, *portToShow, *secret)
113 117
 	go stat.Serve(*statsIP, *statsPort)
114 118
 
115 119
 	srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
@@ -120,25 +124,6 @@ func main() {
120 124
 }
121 125
 
122 126
 func usage(msg string) {
123
-	io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck
127
+	io.WriteString(os.Stderr, msg+"\n")
124 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 Vedi File

@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"net"
6 6
 	"net/http"
7
+	"net/url"
7 8
 	"strconv"
8 9
 	"sync/atomic"
9 10
 	"time"
@@ -23,6 +24,12 @@ type Stats struct {
23 24
 		Incoming uint64 `json:"incoming"`
24 25
 		Outgoing uint64 `json:"outgoing"`
25 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 33
 	Uptime statsUptime `json:"uptime"`
27 34
 }
28 35
 
@@ -46,13 +53,71 @@ func (s *Stats) addOutgoingTraffic(n int) {
46 53
 func (s *Stats) Serve(host net.IP, port uint16) {
47 54
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
48 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 63
 	addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
53 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
 }

Loading…
Annulla
Salva