This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
stats.go
145 lines (133 loc) · 3.01 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"archive/zip"
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
"runtime/pprof"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/geph-official/geph2/libs/bdclient"
log "github.com/sirupsen/logrus"
)
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 map[string]int
//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 handleKill(w http.ResponseWriter, r *http.Request) {
log.Println("dying on command")
go func() {
time.Sleep(time.Millisecond * 100)
os.Exit(0)
}()
}
func handleStats(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
var bts []byte
useStats(func(sc *stats) {
sc.Bridges = make(map[string]int)
// bridges
if !direct {
trackerMap.Range(func(key, value interface{}) bool {
v := int(atomic.LoadInt64(value.(*int64)))
if v > 0 {
sc.Bridges[key.(string)] = v
}
return true
})
}
ll := sc.LogLines
sc.LogLines = nil
var err error
bts, err = json.Marshal(sc)
if err != nil {
panic(err)
}
sc.LogLines = ll
})
w.Header().Add("content-type", "application/json")
w.Write(bts)
}
func handleDebugPack(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"geph-logs-%v-%v.zip\"", username, time.Now().Format(time.RFC3339)))
w.Header().Add("content-type", "application/zip")
zwriter := zip.NewWriter(w)
defer zwriter.Close()
logFile, err := zwriter.Create("logs.txt")
if err != nil {
return
}
useStats(func(sc *stats) {
for _, line := range sc.LogLines {
fmt.Fprintln(logFile, line)
}
})
straceFile, err := zwriter.Create("stacktrace.txt")
if err != nil {
return
}
buf := make([]byte, 8192)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = append(buf, buf...)
}
straceFile.Write(buf)
heapprofFile, err := zwriter.Create("heap.pprof")
pprof.WriteHeapProfile(heapprofFile)
}
func handleLogs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
var bts []byte
useStats(func(sc *stats) {
for _, line := range sc.LogLines {
fmt.Fprintln(w, line)
}
})
w.Header().Add("content-type", "text/plain")
w.Write(bts)
}
func handleProxyPac(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(`function FindProxyForURL(url, host)
{
return "PROXY %v";
}
`, strings.Replace(httpAddr, strings.Split(httpAddr, ":")[0], "localhost", -1))))
}
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)
}