This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
stats.go
94 lines (84 loc) · 1.8 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"sync"
"time"
"github.com/geph-official/geph2/libs/bdclient"
"github.com/geph-official/geph2/libs/niaucchi4"
)
type stats struct {
Connected bool
PublicIP string
UpBytes uint64
DownBytes uint64
MinPing uint64
PingTime time.Time
Username string
Tier string
PayTxes []bdclient.PaymentTx
Expiry time.Time
LogLines []string
Bridges []niaucchi4.LinkInfo
bridgeThunk func() []niaucchi4.LinkInfo
lock sync.Mutex
}
var statsCollector = &stats{}
func useStats(f func(sc *stats)) {
statsCollector.lock.Lock()
defer statsCollector.lock.Unlock()
f(statsCollector)
}
func handleStats(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
var bts []byte
useStats(func(sc *stats) {
ll := sc.LogLines
sc.LogLines = nil
var err error
bts, err = json.Marshal(sc)
if err != nil {
panic(err)
}
if sc.bridgeThunk != nil {
sc.Bridges = sc.bridgeThunk()
}
sc.LogLines = ll
})
w.Header().Add("content-type", "application/json")
w.Write(bts)
}
func handleLogs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
var bts []byte
useStats(func(sc *stats) {
var err error
bts, err = json.Marshal(sc.LogLines)
if err != nil {
panic(err)
}
})
w.Header().Add("content-type", "application/json")
w.Write(bts)
}
func handleProxyPac(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(`function FindProxyForURL(url, host)
{
return "PROXY %v";
}
`, httpAddr)))
}
func handleStacktrace(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 8192)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = append(buf, buf...)
}
w.Write(buf)
}