nezha/cmd/dashboard/controller/controller.go

328 lines
7.3 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package controller
import (
"fmt"
"html/template"
2023-11-28 20:42:51 -05:00
"log"
"net/http"
2023-11-28 20:42:51 -05:00
"os"
"path/filepath"
2021-11-04 00:06:20 -04:00
"strconv"
2020-12-18 21:57:10 -05:00
"strings"
2019-12-08 03:59:58 -05:00
"time"
2019-12-09 05:14:31 -05:00
"code.cloudfoundry.org/bytefmt"
2021-05-10 06:04:38 -04:00
"github.com/gin-contrib/pprof"
2019-12-08 03:59:58 -05:00
"github.com/gin-gonic/gin"
2024-07-14 07:41:50 -04:00
"github.com/hashicorp/go-uuid"
2022-04-27 11:51:45 -04:00
"github.com/nicksnyder/go-i18n/v2/i18n"
2019-12-08 03:59:58 -05:00
2024-07-14 07:41:50 -04:00
"github.com/naiba/nezha/model"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/pkg/mygin"
2024-07-14 07:41:50 -04:00
"github.com/naiba/nezha/pkg/utils"
"github.com/naiba/nezha/proto"
2023-11-28 10:01:37 -05:00
"github.com/naiba/nezha/resource"
2024-07-14 07:41:50 -04:00
"github.com/naiba/nezha/service/rpc"
2022-01-08 22:54:14 -05:00
"github.com/naiba/nezha/service/singleton"
2019-12-08 03:59:58 -05:00
)
func ServeWeb(port uint) *http.Server {
2019-12-08 10:18:29 -05:00
gin.SetMode(gin.ReleaseMode)
2021-05-10 06:04:38 -04:00
r := gin.Default()
2024-07-14 07:41:50 -04:00
if singleton.Conf.Debug {
gin.SetMode(gin.DebugMode)
pprof.Register(r)
}
r.Use(natGateway)
tmpl := template.New("").Funcs(funcMap)
var err error
tmpl, err = tmpl.ParseFS(resource.TemplateFS, "template/**/*.html")
if err != nil {
panic(err)
2023-11-28 10:01:37 -05:00
}
tmpl = loadThirdPartyTemplates(tmpl)
r.SetHTMLTemplate(tmpl)
r.Use(mygin.RecordPath)
r.StaticFS("/static", http.FS(resource.StaticFS))
2019-12-08 03:59:58 -05:00
routers(r)
2021-08-10 08:13:17 -04:00
page404 := func(c *gin.Context) {
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusNotFound,
Title: "该页面不存在",
Msg: "该页面内容可能已着陆火星",
Link: "/",
Btn: "返回首页",
}, true)
}
r.NoRoute(page404)
r.NoMethod(page404)
srv := &http.Server{
2022-06-22 00:53:21 -04:00
Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: time.Second * 5,
Handler: r,
}
return srv
2019-12-08 03:59:58 -05:00
}
func routers(r *gin.Engine) {
// 通用页面
cp := commonPage{r: r}
2019-12-08 03:59:58 -05:00
cp.serve()
// 游客页面
gp := guestPage{r}
gp.serve()
// 会员页面
mp := &memberPage{r}
mp.serve()
// API
api := r.Group("api")
{
ma := &memberAPI{api}
ma.serve()
}
}
2023-11-28 20:42:51 -05:00
func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
var ret = tmpl
themes, err := os.ReadDir("resource/template")
if err != nil {
log.Printf("NEZHA>> Error reading themes folder: %v", err)
return ret
}
for _, theme := range themes {
if !theme.IsDir() {
continue
}
themeDir := theme.Name()
if !strings.HasPrefix(themeDir, "theme-") {
log.Printf("NEZHA>> Invalid theme name: %s", themeDir)
continue
}
descPath := filepath.Join("resource", "template", themeDir, "theme.json")
desc, err := os.ReadFile(filepath.Clean(descPath))
if err != nil {
log.Printf("NEZHA>> Error opening %s config: %v", themeDir, err)
continue
}
themeName, err := utils.GjsonGet(desc, "name")
if err != nil {
log.Printf("NEZHA>> Error opening %s config: not a valid description file", theme.Name())
continue
}
2023-11-28 20:42:51 -05:00
// load templates
templatePath := filepath.Join("resource", "template", themeDir, "*.html")
t, err := ret.ParseGlob(templatePath)
2023-11-28 20:42:51 -05:00
if err != nil {
log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err)
2023-11-28 20:42:51 -05:00
continue
}
themeKey := strings.TrimPrefix(themeDir, "theme-")
model.Themes[themeKey] = themeName.String()
2023-11-28 20:42:51 -05:00
ret = t
}
2023-11-28 20:42:51 -05:00
return ret
}
var funcMap = template.FuncMap{
"tr": func(id string, dataAndCount ...interface{}) string {
conf := i18n.LocalizeConfig{
MessageID: id,
}
if len(dataAndCount) > 0 {
conf.TemplateData = dataAndCount[0]
}
if len(dataAndCount) > 1 {
conf.PluralCount = dataAndCount[1]
}
return singleton.Localizer.MustLocalize(&conf)
},
"toValMap": func(val interface{}) map[string]interface{} {
return map[string]interface{}{
"Value": val,
}
},
"tf": func(t time.Time) string {
2022-04-30 09:30:58 -04:00
return t.In(singleton.Loc).Format("01/02/2006 15:04:05")
},
"len": func(slice []interface{}) string {
return strconv.Itoa(len(slice))
},
"safe": func(s string) template.HTML {
return template.HTML(s) // #nosec
},
"tag": func(s string) template.HTML {
return template.HTML(`<` + s + `>`) // #nosec
},
"stf": func(s uint64) string {
2022-04-30 09:30:58 -04:00
return time.Unix(int64(s), 0).In(singleton.Loc).Format("01/02/2006 15:04")
},
"sf": func(duration uint64) string {
return time.Duration(time.Duration(duration) * time.Second).String()
},
"sft": func(future time.Time) string {
return time.Until(future).Round(time.Second).String()
},
"bf": func(b uint64) string {
return bytefmt.ByteSize(b)
},
"ts": func(s string) string {
return strings.TrimSpace(s)
},
"float32f": func(f float32) string {
2022-04-30 09:30:58 -04:00
return fmt.Sprintf("%.3f", f)
},
"divU64": func(a, b uint64) float32 {
if b == 0 {
if a > 0 {
return 100
}
return 0
}
if a == 0 {
// 这是从未在线的情况
return 0.00001 / float32(b) * 100
}
return float32(a) / float32(b) * 100
},
"div": func(a, b int) float32 {
if b == 0 {
if a > 0 {
return 100
}
return 0
}
if a == 0 {
// 这是从未在线的情况
return 0.00001 / float32(b) * 100
}
return float32(a) / float32(b) * 100
},
"addU64": func(a, b uint64) uint64 {
return a + b
},
"add": func(a, b int) int {
return a + b
},
"TransLeftPercent": func(a, b float64) (n float64) {
n, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (100-(a/b)*100)), 64)
if n < 0 {
n = 0
}
return
},
"TransLeft": func(a, b uint64) string {
if a < b {
2022-05-31 03:12:15 -04:00
return "0B"
}
return bytefmt.ByteSize(a - b)
},
"TransClassName": func(a float64) string {
if a == 0 {
return "offline"
}
if a > 50 {
return "fine"
}
if a > 20 {
return "warning"
}
if a > 0 {
return "error"
}
return "offline"
},
"UintToFloat": func(a uint64) (n float64) {
n, _ = strconv.ParseFloat((strconv.FormatUint(a, 10)), 64)
return
},
"dayBefore": func(i int) string {
year, month, day := time.Now().Date()
2022-10-12 11:06:25 -04:00
today := time.Date(year, month, day, 0, 0, 0, 0, singleton.Loc)
2022-04-30 09:30:58 -04:00
return today.AddDate(0, 0, i-29).Format("01/02")
},
"className": func(percent float32) string {
if percent == 0 {
return ""
}
if percent > 95 {
return "good"
}
if percent > 80 {
return "warning"
}
return "danger"
},
"statusName": func(val float32) string {
return singleton.StatusCodeToString(singleton.GetStatusCode(val))
},
}
2024-07-14 07:41:50 -04:00
func natGateway(c *gin.Context) {
natConfig := singleton.GetNATConfigByDomain(c.Request.Host)
if natConfig == nil {
return
}
singleton.ServerLock.RLock()
server := singleton.ServerList[natConfig.ServerID]
singleton.ServerLock.RUnlock()
if server == nil || server.TaskStream == nil {
c.Writer.WriteString("server not found or not connected")
c.Abort()
return
}
streamId, err := uuid.GenerateUUID()
if err != nil {
c.Writer.WriteString(fmt.Sprintf("stream id error: %v", err))
c.Abort()
return
}
rpc.NezhaHandlerSingleton.CreateStream(streamId)
defer rpc.NezhaHandlerSingleton.CloseStream(streamId)
taskData, err := utils.Json.Marshal(model.TaskNAT{
2024-07-14 07:41:50 -04:00
StreamID: streamId,
Host: natConfig.Host,
})
if err != nil {
c.Writer.WriteString(fmt.Sprintf("task data error: %v", err))
c.Abort()
return
}
if err := server.TaskStream.Send(&proto.Task{
Type: model.TaskTypeNAT,
Data: string(taskData),
}); err != nil {
c.Writer.WriteString(fmt.Sprintf("send task error: %v", err))
c.Abort()
return
}
w, err := utils.NewRequestWrapper(c.Request, c.Writer)
if err != nil {
c.Writer.WriteString(fmt.Sprintf("request wrapper error: %v", err))
c.Abort()
return
}
if err := rpc.NezhaHandlerSingleton.UserConnected(streamId, w); err != nil {
c.Writer.WriteString(fmt.Sprintf("user connected error: %v", err))
c.Abort()
return
}
rpc.NezhaHandlerSingleton.StartStream(streamId, time.Second*10)
c.Abort()
}