Print URLs in stats endpoint

This commit is contained in:
9seconds
2018-05-31 09:54:20 +03:00
parent a160ac4ac4
commit 6abdc283ff
2 changed files with 79 additions and 29 deletions
+11 -26
View File
@@ -4,13 +4,10 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
"strconv"
"strings" "strings"
"github.com/9seconds/mtg/proxy" "github.com/9seconds/mtg/proxy"
@@ -40,6 +37,11 @@ var (
Envar("MTG_PORT"). Envar("MTG_PORT").
Default("3128"). Default("3128").
Uint16() 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"). statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
Short('t'). Short('t').
Envar("MTG_STATS_IP"). Envar("MTG_STATS_IP").
@@ -78,6 +80,10 @@ func main() {
usage("Secret has to be hexadecimal string.") usage("Secret has to be hexadecimal string.")
} }
if *portToShow == 0 {
*portToShow = *bindPort
}
if *serverName == "" { if *serverName == "" {
resp, err := http.Get("https://api.ipify.org") resp, err := http.Get("https://api.ipify.org")
if err != nil || resp.StatusCode != http.StatusOK { if err != nil || resp.StatusCode != http.StatusOK {
@@ -107,9 +113,7 @@ func main() {
atom, atom,
)).Sugar() )).Sugar()
printURLs() stat := proxy.NewStats(*serverName, *portToShow, *secret)
stat := proxy.NewStats()
go stat.Serve(*statsIP, *statsPort) go stat.Serve(*statsIP, *statsPort)
srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger, srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
@@ -120,25 +124,6 @@ func main() {
} }
func usage(msg string) { func usage(msg string) {
io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck io.WriteString(os.Stderr, msg+"\n")
os.Exit(1) 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())
}
+68 -3
View File
@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"net" "net"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -23,6 +24,12 @@ type Stats struct {
Incoming uint64 `json:"incoming"` Incoming uint64 `json:"incoming"`
Outgoing uint64 `json:"outgoing"` Outgoing uint64 `json:"outgoing"`
} `json:"traffic"` } `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"` Uptime statsUptime `json:"uptime"`
} }
@@ -46,13 +53,71 @@ func (s *Stats) addOutgoingTraffic(n int) {
func (s *Stats) Serve(host net.IP, port uint16) { func (s *Stats) Serve(host net.IP, port uint16) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") 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))) addr := net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
http.ListenAndServe(addr, nil) http.ListenAndServe(addr, nil)
} }
func NewStats() *Stats { func NewStats(serverName string, port uint16, secret string) *Stats {
return &Stats{Uptime: statsUptime(time.Now())} 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()
} }