fix: display stats by html.

This commit is contained in:
Daniel Ding
2024-07-04 12:10:12 +08:00
parent f3b05bc4f2
commit 83db0f096f
2 changed files with 56 additions and 45 deletions

View File

@@ -23,12 +23,12 @@ import (
type HttpRecord struct { type HttpRecord struct {
Count int Count int
LastAt time.Time LastAt string
} }
func (r *HttpRecord) Update() { func (r *HttpRecord) Update() {
r.Count += 1 r.Count += 1
r.LastAt = time.Now() r.LastAt = time.Now().Local().String()
} }
type HttpProxy struct { type HttpProxy struct {
@@ -389,57 +389,71 @@ func (t *HttpProxy) Start() {
var httpTmpl = map[string]string{ var httpTmpl = map[string]string{
"stats": ` "stats": `
<table> <!DOCTYPE html>
<tr> <html lang="en">
<td style="border: 1px solid black;">Configuration:</td> <head>
<td style="border: 1px solid black;"><a href="/config">json</a></td> <meta charset="UTF-8">
</tr> <title>OpenLAN Proxy</title>
<tr> <style>
<td style="border: 1px solid black;">StartAt:</td> body {
<td style="border: 1px solid black;">{{ .StartAt }}</td> background-color: #121212;
</tr> color: #e0e0e0;
<tr> }
<td style="border: 1px solid black;">Total:</td> table, a {
<td style="border: 1px solid black;">{{ .Total }}</td> color: #e0e0e0;
</tr> }
</table> td, th {
border: 1px solid #9E9E9E;;
<table> border-radius: 2px;
<tr> text-align: center;
<th style="border: 1px solid black;">Domain</th> }
<th style="border: 1px solid black;">Count</th> </style>
<th style="border: 1px solid black;">LastAt</th> </head>
</tr> <body>
{{- range $k, $v := .Requests }} <table>
<tr> <tr>
<td style="border: 1px solid black;">{{ $k }}</td> <td>Configuration:</td><td><a href="/config">display</a></td>
<td style="border: 1px solid black;">{{ $v.Count }}</td> </tr>
<td style="border: 1px solid black;">{{ $v.LastAt }}</td> <tr>
</tr> <td>StartAt:</td><td>{{ .StartAt }}</td>
{{- end }} </tr>
</table>`, <tr>
<td>Total:</td><td>{{ .Total }}</td>
</tr>
</table>
<table>
<tr>
<th>Domain</th><th>Count</th><th>LastAt</th>
</tr>
{{- range $k, $v := .Requests }}
<tr>
<td>{{ $k }}</td><td>{{ $v.Count }}</td><td>{{ $v.LastAt }}</td>
</tr>
{{- end }}
</table>
<body>
</html>`,
"pac": ` "pac": `
function FindProxyForURL(url, host) { function FindProxyForURL(url, host) {
if (isPlainHostName(host)) if (isPlainHostName(host))
return "DIRECT"; return "DIRECT";
{{- range .Rules }} {{- range .Rules }}
if (shExpMatch(host, "(^|*\.){{ . }}")) if (shExpMatch(host, "(^|*\.){{ . }}"))
return "PROXY {{ $.Local }}"; return "PROXY {{ $.Local }}";
{{- end }} {{- end }}
return "DIRECT"; return "DIRECT";
}`, }`,
} }
func (t *HttpProxy) GetStats(w http.ResponseWriter, r *http.Request) { func (t *HttpProxy) GetStats(w http.ResponseWriter, r *http.Request) {
data := &struct { data := &struct {
StartAt time.Time StartAt string
Total int Total int
Requests map[string]*HttpRecord Requests map[string]*HttpRecord
}{ }{
Total: len(t.requests), Total: len(t.requests),
Requests: t.requests, Requests: t.requests,
StartAt: t.startat, StartAt: t.startat.Local().String(),
} }
if tmpl, err := template.New("main").Parse(httpTmpl["stats"]); err != nil { if tmpl, err := template.New("main").Parse(httpTmpl["stats"]); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@@ -69,8 +69,7 @@ conn {{ .Name }}-c2
rightid=@c1.{{ .RightId }}.{{ .Transport }} rightid=@c1.{{ .RightId }}.{{ .Transport }}
{{- end }} {{- end }}
leftprotoport=udp leftprotoport=udp
rightprotoport=udp/8472 rightprotoport=udp/8472`,
`,
"gre": ` "gre": `
conn {{ .Name }}-c1 conn {{ .Name }}-c1
auto=add auto=add
@@ -92,11 +91,9 @@ conn {{ .Name }}-c1
{{- end }} {{- end }}
authby=secret authby=secret
leftprotoport=gre leftprotoport=gre
rightprotoport=gre rightprotoport=gre`,
`,
"secret": ` "secret": `
%any @{{ .RightId }}.{{ .Transport }} : PSK "{{ .Secret }}" %any @{{ .RightId }}.{{ .Transport }} : PSK "{{ .Secret }}"`,
`,
} }
func (w *IPSecWorker) Initialize() { func (w *IPSecWorker) Initialize() {