mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 18:35:51 +02:00
Merge branch 'dev' into feat/cli
This commit is contained in:
commit
8a7ed08619
69 changed files with 1233 additions and 623 deletions
|
@ -3,10 +3,11 @@ package cert
|
|||
import (
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
|
@ -39,8 +40,19 @@ func GetCertInfo(sslCertificatePath string) (info *Info, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// for wildcard certificate, the subject name is the first DNS name
|
||||
subjectName := cert.Subject.CommonName
|
||||
if subjectName == "" {
|
||||
for _, name := range cert.DNSNames {
|
||||
if name != "" {
|
||||
subjectName = name
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info = &Info{
|
||||
SubjectName: cert.Subject.CommonName,
|
||||
SubjectName: subjectName,
|
||||
IssuerName: cert.Issuer.CommonName,
|
||||
NotAfter: cert.NotAfter,
|
||||
NotBefore: cert.NotBefore,
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package cert
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
)
|
||||
|
||||
type Content struct {
|
||||
|
@ -33,25 +34,25 @@ func (c *Content) WriteFile() (err error) {
|
|||
// The permission bits perm (before umask) are used for all directories that MkdirAll creates.
|
||||
// If path is already a directory, MkdirAll does nothing and returns nil.
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(c.SSLCertificatePath), 0644)
|
||||
err = os.MkdirAll(filepath.Dir(c.SSLCertificatePath), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(c.SSLCertificateKeyPath), 0644)
|
||||
err = os.MkdirAll(filepath.Dir(c.SSLCertificateKeyPath), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if c.SSLCertificate != "" {
|
||||
err = os.WriteFile(c.SSLCertificatePath, []byte(c.SSLCertificate), 0644)
|
||||
err = os.WriteFile(c.SSLCertificatePath, []byte(c.SSLCertificate), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if c.SSLCertificateKey != "" {
|
||||
err = os.WriteFile(c.SSLCertificateKeyPath, []byte(c.SSLCertificateKey), 0644)
|
||||
err = os.WriteFile(c.SSLCertificateKeyPath, []byte(c.SSLCertificateKey), 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func colorLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
||||
colorLevel := color.New()
|
||||
|
||||
switch l {
|
||||
case zapcore.DebugLevel:
|
||||
colorLevel.Add(color.FgCyan)
|
||||
case zapcore.InfoLevel:
|
||||
colorLevel.Add(color.FgGreen)
|
||||
case zapcore.WarnLevel:
|
||||
colorLevel.Add(color.FgYellow)
|
||||
case zapcore.ErrorLevel, zapcore.DPanicLevel:
|
||||
colorLevel.Add(color.FgHiRed)
|
||||
case zapcore.PanicLevel, zapcore.FatalLevel:
|
||||
colorLevel.Add(color.FgRed)
|
||||
default:
|
||||
colorLevel.Add(color.Reset)
|
||||
}
|
||||
|
||||
enc.AppendString(colorLevel.Sprint(l.CapitalString()))
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"os"
|
||||
)
|
||||
|
||||
var logger *zap.SugaredLogger
|
||||
|
||||
func init() {
|
||||
// First, define our level-handling logic.
|
||||
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
||||
return lvl >= zapcore.ErrorLevel
|
||||
})
|
||||
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
||||
switch settings.ServerSettings.RunMode {
|
||||
case gin.ReleaseMode:
|
||||
return lvl >= zapcore.InfoLevel && lvl < zapcore.ErrorLevel
|
||||
default:
|
||||
fallthrough
|
||||
case gin.DebugMode:
|
||||
return lvl < zapcore.ErrorLevel
|
||||
}
|
||||
})
|
||||
|
||||
// Directly output to stdout and stderr, and add caller information.
|
||||
consoleDebugging := zapcore.Lock(os.Stdout)
|
||||
consoleErrors := zapcore.Lock(os.Stderr)
|
||||
encoderConfig := zap.NewDevelopmentEncoderConfig()
|
||||
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
|
||||
encoderConfig.ConsoleSeparator = "\t"
|
||||
encoderConfig.EncodeLevel = colorLevelEncoder
|
||||
consoleEncoder := zapcore.NewConsoleEncoder(encoderConfig)
|
||||
|
||||
// Join the outputs, encoders, and level-handling functions into
|
||||
// zapcore.Cores, then tee the two cores together.
|
||||
core := zapcore.NewTee(
|
||||
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
|
||||
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
|
||||
)
|
||||
|
||||
// From a zapcore.Core, it's easy to construct a Logger.
|
||||
logger = zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(1)).Sugar()
|
||||
}
|
||||
|
||||
func Sync() {
|
||||
_ = logger.Sync()
|
||||
}
|
||||
|
||||
func GetLogger() *zap.SugaredLogger {
|
||||
return logger
|
||||
}
|
||||
|
||||
func Info(args ...interface{}) {
|
||||
logger.Infoln(args...)
|
||||
}
|
||||
|
||||
func Error(args ...interface{}) {
|
||||
logger.Errorln(args...)
|
||||
}
|
||||
|
||||
func Fatal(args ...interface{}) {
|
||||
logger.Fatalln(args...)
|
||||
}
|
||||
|
||||
func Warn(args ...interface{}) {
|
||||
logger.Warnln(args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
logger.Debugln(args...)
|
||||
}
|
||||
|
||||
func DPanic(args ...interface{}) {
|
||||
logger.DPanic(args...)
|
||||
}
|
||||
|
||||
func Infof(format string, args ...interface{}) {
|
||||
logger.Infof(format, args...)
|
||||
}
|
||||
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
logger.Errorf(format, args...)
|
||||
}
|
||||
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
logger.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
func DPanicf(format string, args ...interface{}) {
|
||||
logger.DPanicf(format, args...)
|
||||
}
|
||||
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
logger.Warnf(format, args...)
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
logger.Debugf(format, args...)
|
||||
}
|
29
internal/middleware/embed.go
Normal file
29
internal/middleware/embed.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
//go:build !unembed
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/app"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
func MustFs(dir string) (serverFileSystem static.ServeFileSystem) {
|
||||
|
||||
sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
serverFileSystem = ServerFileSystemType{
|
||||
http.FS(sub),
|
||||
}
|
||||
|
||||
return
|
||||
}
|
|
@ -2,16 +2,14 @@ package middleware
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"github.com/0xJacky/Nginx-UI/app"
|
||||
"github.com/0xJacky/Nginx-UI/internal/user"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/user"
|
||||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
func AuthRequired() gin.HandlerFunc {
|
||||
|
@ -72,22 +70,6 @@ func (f ServerFileSystemType) Exists(prefix string, _path string) bool {
|
|||
return err == nil
|
||||
}
|
||||
|
||||
func MustFs(dir string) (serverFileSystem static.ServeFileSystem) {
|
||||
|
||||
sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
serverFileSystem = ServerFileSystemType{
|
||||
http.FS(sub),
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CacheJs() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if strings.Contains(c.Request.URL.String(), "js") {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue