This repository has been archived by the owner on Nov 26, 2019. It is now read-only.
/
stats.go
95 lines (90 loc) · 2.21 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package client
import (
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"sync/atomic"
"time"
)
func (cmd *Command) servSummary(w http.ResponseWriter, r *http.Request) {
var resp struct {
Status string
ErrCode string
Uptime int
BytesTX int
BytesRX int
}
cmd.stats.RLock()
defer cmd.stats.RUnlock()
w.Header().Add("Connection", "close")
resp.Status = cmd.stats.status
resp.Uptime = int(time.Now().Sub(cmd.stats.stTime).Seconds())
resp.BytesRX = int(cmd.stats.rxBytes)
resp.BytesTX = int(cmd.stats.txBytes)
w.Header().Add("content-type", "application/json")
bts, _ := json.MarshalIndent(&resp, "", " ")
w.Write(bts)
}
func (cmd *Command) servNetinfo(w http.ResponseWriter, r *http.Request) {
var resp struct {
Exit string
ExitCountry string
Entry string
Protocol string
ActiveTunns map[string]string
}
cmd.stats.RLock()
defer cmd.stats.RUnlock()
w.Header().Add("Connection", "close")
csn := cmd.stats.netinfo
resp.Exit = cmd.stats.netinfo.exit
if cmd.geodbloc != "" {
resp.ExitCountry = cmd.geodb.GetCountry(net.ParseIP(resp.Exit))
}
resp.Entry = csn.entry
resp.Protocol = csn.prot
resp.ActiveTunns = csn.tuns
w.Header().Add("content-type", "application/json")
bts, _ := json.MarshalIndent(&resp, "", " ")
w.Write(bts)
}
func (cmd *Command) servAccInfo(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Connection", "close")
var req struct {
Username string
}
req.Username = cmd.uname
bts, _ := json.Marshal(&req)
resp, err := cmd.proxclient.Post("https://binder.geph.io/user-status",
"application/json", bytes.NewReader(bts))
// If the network is borked, just die
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
// See if the status is 200
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
io.Copy(w, resp.Body)
} else {
w.WriteHeader(resp.StatusCode)
return
}
}
// io.Copy, except it increments a counter in real-time using atomic primitives
func ctrCopy(dest io.Writer, orig io.Reader, ctr *uint64) error {
buffer := make([]byte, 32768)
for {
n, err := orig.Read(buffer)
if err != nil {
return err
}
_, err = dest.Write(buffer[:n])
if err != nil {
return err
}
atomic.AddUint64(ctr, uint64(n))
}
}