fix: review const var.

This commit is contained in:
Daniel Ding
2024-07-04 09:52:48 +08:00
parent 65eb936210
commit f3b05bc4f2
2 changed files with 29 additions and 27 deletions

View File

@@ -387,7 +387,8 @@ func (t *HttpProxy) Start() {
}) })
} }
var httpStatsTmpl = ` var httpTmpl = map[string]string{
"stats": `
<table> <table>
<tr> <tr>
<td style="border: 1px solid black;">Configuration:</td> <td style="border: 1px solid black;">Configuration:</td>
@@ -416,7 +417,19 @@ var httpStatsTmpl = `
<td style="border: 1px solid black;">{{ $v.LastAt }}</td> <td style="border: 1px solid black;">{{ $v.LastAt }}</td>
</tr> </tr>
{{- end }} {{- end }}
</table>` </table>`,
"pac": `
function FindProxyForURL(url, host) {
if (isPlainHostName(host))
return "DIRECT";
{{- range .Rules }}
if (shExpMatch(host, "(^|*\.){{ . }}"))
return "PROXY {{ $.Local }}";
{{- end }}
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 {
@@ -428,7 +441,7 @@ func (t *HttpProxy) GetStats(w http.ResponseWriter, r *http.Request) {
Requests: t.requests, Requests: t.requests,
StartAt: t.startat, StartAt: t.startat,
} }
if tmpl, err := template.New("main").Parse(httpStatsTmpl); 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)
} else { } else {
if err := tmpl.Execute(w, data); err != nil { if err := tmpl.Execute(w, data); err != nil {
@@ -441,17 +454,6 @@ func (t *HttpProxy) GetConfig(w http.ResponseWriter, r *http.Request) {
encodeJson(w, t.cfg) encodeJson(w, t.cfg)
} }
var httpPacTmpl = `
function FindProxyForURL(url, host) {
if (isPlainHostName(host))
return "DIRECT";
{{- range .Rules }}
if (shExpMatch(host, "(^|*\.){{ . }}"))
return "PROXY {{ $.Local }}";
{{- end }}
return "DIRECT";
}`
func (t *HttpProxy) GetPac(w http.ResponseWriter, r *http.Request) { func (t *HttpProxy) GetPac(w http.ResponseWriter, r *http.Request) {
data := &struct { data := &struct {
Local string Local string
@@ -466,7 +468,7 @@ func (t *HttpProxy) GetPac(w http.ResponseWriter, r *http.Request) {
} }
} }
if tmpl, err := template.New("main").Parse(httpPacTmpl); err != nil { if tmpl, err := template.New("main").Parse(httpTmpl["pac"]); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} else { } else {
expired := time.Now().Add(1 * time.Minute) expired := time.Now().Add(1 * time.Minute)

View File

@@ -31,8 +31,8 @@ func NewIPSecWorker(c *co.Network) *IPSecWorker {
return w return w
} }
const ( var ipsecTmpl = map[string]string{
vxlanTmpl = ` "vxlan": `
conn {{ .Name }} conn {{ .Name }}
keyexchange=ike keyexchange=ike
ikev2=no ikev2=no
@@ -70,8 +70,8 @@ conn {{ .Name }}-c2
{{- end }} {{- end }}
leftprotoport=udp leftprotoport=udp
rightprotoport=udp/8472 rightprotoport=udp/8472
` `,
greTmpl = ` "gre": `
conn {{ .Name }}-c1 conn {{ .Name }}-c1
auto=add auto=add
ikev2=no ikev2=no
@@ -93,11 +93,11 @@ conn {{ .Name }}-c1
authby=secret authby=secret
leftprotoport=gre leftprotoport=gre
rightprotoport=gre rightprotoport=gre
` `,
secretTmpl = ` "secret": `
%any @{{ .RightId }}.{{ .Transport }} : PSK "{{ .Secret }}" %any @{{ .RightId }}.{{ .Transport }} : PSK "{{ .Secret }}"
` `,
) }
func (w *IPSecWorker) Initialize() { func (w *IPSecWorker) Initialize() {
w.out.Info("IPSecWorker.Initialize") w.out.Info("IPSecWorker.Initialize")
@@ -159,11 +159,11 @@ func (w *IPSecWorker) addTunnel(tun *co.IPSecTunnel) error {
name := tun.Name name := tun.Name
if tun.Transport == "vxlan" { if tun.Transport == "vxlan" {
connTmpl = vxlanTmpl connTmpl = ipsecTmpl["vxlan"]
secTmpl = secretTmpl secTmpl = ipsecTmpl["secret"]
} else if tun.Transport == "gre" { } else if tun.Transport == "gre" {
connTmpl = greTmpl connTmpl = ipsecTmpl["gre"]
secTmpl = secretTmpl secTmpl = ipsecTmpl["secret"]
} }
if secTmpl != "" { if secTmpl != "" {