-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use env to predefine admin user #214
- Loading branch information
Showing
9 changed files
with
165 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,111 @@ | ||
package kernal | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/internal/analytic" | ||
"github.com/0xJacky/Nginx-UI/internal/cert" | ||
"github.com/0xJacky/Nginx-UI/internal/logger" | ||
"github.com/0xJacky/Nginx-UI/internal/logrotate" | ||
"github.com/0xJacky/Nginx-UI/internal/validation" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/go-co-op/gocron" | ||
"github.com/google/uuid" | ||
"mime" | ||
"runtime" | ||
"time" | ||
"github.com/0xJacky/Nginx-UI/internal/analytic" | ||
"github.com/0xJacky/Nginx-UI/internal/cert" | ||
"github.com/0xJacky/Nginx-UI/internal/logger" | ||
"github.com/0xJacky/Nginx-UI/internal/logrotate" | ||
"github.com/0xJacky/Nginx-UI/internal/validation" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/go-co-op/gocron" | ||
"github.com/google/uuid" | ||
"mime" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
func Boot() { | ||
defer recovery() | ||
|
||
async := []func(){ | ||
InitJsExtensionType, | ||
InitDatabase, | ||
InitNodeSecret, | ||
validation.Init, | ||
} | ||
|
||
syncs := []func(){ | ||
analytic.RecordServerAnalytic, | ||
} | ||
|
||
for _, v := range async { | ||
v() | ||
} | ||
|
||
for _, v := range syncs { | ||
go v() | ||
} | ||
defer recovery() | ||
|
||
async := []func(){ | ||
InitJsExtensionType, | ||
InitDatabase, | ||
InitNodeSecret, | ||
validation.Init, | ||
} | ||
|
||
syncs := []func(){ | ||
analytic.RecordServerAnalytic, | ||
} | ||
|
||
for _, v := range async { | ||
v() | ||
} | ||
|
||
for _, v := range syncs { | ||
go v() | ||
} | ||
} | ||
|
||
func InitAfterDatabase() { | ||
syncs := []func(){ | ||
cert.InitRegister, | ||
InitCronJobs, | ||
analytic.RetrieveNodesStatus, | ||
} | ||
|
||
for _, v := range syncs { | ||
go v() | ||
} | ||
syncs := []func(){ | ||
registerPredefinedUser, | ||
cert.InitRegister, | ||
InitCronJobs, | ||
analytic.RetrieveNodesStatus, | ||
} | ||
|
||
for _, v := range syncs { | ||
go v() | ||
} | ||
} | ||
|
||
func recovery() { | ||
if err := recover(); err != nil { | ||
buf := make([]byte, 1024) | ||
runtime.Stack(buf, false) | ||
logger.Errorf("%s\n%s", err, buf) | ||
} | ||
if err := recover(); err != nil { | ||
buf := make([]byte, 1024) | ||
runtime.Stack(buf, false) | ||
logger.Errorf("%s\n%s", err, buf) | ||
} | ||
} | ||
|
||
func InitDatabase() { | ||
// Skip install | ||
if settings.ServerSettings.SkipInstallation { | ||
skipInstall() | ||
} | ||
|
||
// Skip installation | ||
if settings.ServerSettings.SkipInstallation && settings.ServerSettings.JwtSecret == "" { | ||
settings.ServerSettings.JwtSecret = uuid.New().String() | ||
err := settings.Save() | ||
if err != nil { | ||
logger.Error(err) | ||
} | ||
} | ||
|
||
if "" != settings.ServerSettings.JwtSecret { | ||
db := model.Init() | ||
query.Init(db) | ||
|
||
InitAfterDatabase() | ||
} | ||
if "" != settings.ServerSettings.JwtSecret { | ||
db := model.Init() | ||
query.Init(db) | ||
|
||
InitAfterDatabase() | ||
} | ||
} | ||
|
||
func InitNodeSecret() { | ||
if "" == settings.ServerSettings.NodeSecret { | ||
logger.Warn("NodeSecret is empty, generating...") | ||
settings.ServerSettings.NodeSecret = uuid.New().String() | ||
settings.ReflectFrom() | ||
|
||
err := settings.Save() | ||
if err != nil { | ||
logger.Error("Error save settings") | ||
} | ||
logger.Warn("Generated NodeSecret: ", settings.ServerSettings.NodeSecret) | ||
} | ||
if "" == settings.ServerSettings.NodeSecret { | ||
logger.Warn("NodeSecret is empty, generating...") | ||
settings.ServerSettings.NodeSecret = uuid.New().String() | ||
|
||
err := settings.Save() | ||
if err != nil { | ||
logger.Error("Error save settings") | ||
} | ||
logger.Warn("Generated NodeSecret: ", settings.ServerSettings.NodeSecret) | ||
} | ||
} | ||
|
||
func InitJsExtensionType() { | ||
// Hack: fix wrong Content Type of .js file on some OS platforms | ||
// See https://github.com/golang/go/issues/32350 | ||
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") | ||
// Hack: fix wrong Content Type of .js file on some OS platforms | ||
// See https://github.com/golang/go/issues/32350 | ||
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") | ||
} | ||
|
||
func InitCronJobs() { | ||
s := gocron.NewScheduler(time.UTC) | ||
job, err := s.Every(30).Minute().SingletonMode().Do(cert.AutoCert) | ||
s := gocron.NewScheduler(time.UTC) | ||
job, err := s.Every(30).Minute().SingletonMode().Do(cert.AutoCert) | ||
|
||
if err != nil { | ||
logger.Fatalf("AutoCert Job: %v, Err: %v\n", job, err) | ||
} | ||
if err != nil { | ||
logger.Fatalf("AutoCert Job: %v, Err: %v\n", job, err) | ||
} | ||
|
||
job, err = s.Every(settings.LogrotateSettings.Interval).Minute().SingletonMode().Do(logrotate.Exec) | ||
job, err = s.Every(settings.LogrotateSettings.Interval).Minute().SingletonMode().Do(logrotate.Exec) | ||
|
||
if err != nil { | ||
logger.Fatalf("LogRotate Job: %v, Err: %v\n", job, err) | ||
} | ||
if err != nil { | ||
logger.Fatalf("LogRotate Job: %v, Err: %v\n", job, err) | ||
} | ||
|
||
s.StartAsync() | ||
s.StartAsync() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package kernal | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/internal/logger" | ||
"github.com/0xJacky/Nginx-UI/model" | ||
"github.com/0xJacky/Nginx-UI/query" | ||
"github.com/0xJacky/Nginx-UI/settings" | ||
"github.com/caarlos0/env/v11" | ||
"github.com/google/uuid" | ||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/bcrypt" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type predefinedUser struct { | ||
Name string `json:"name"` | ||
Password string `json:"password"` | ||
} | ||
|
||
func skipInstall() { | ||
logger.Info("Skip installation mode enabled") | ||
|
||
if settings.ServerSettings.JwtSecret == "" { | ||
settings.ServerSettings.JwtSecret = uuid.New().String() | ||
} | ||
|
||
if settings.ServerSettings.NodeSecret == "" { | ||
settings.ServerSettings.NodeSecret = uuid.New().String() | ||
logger.Infof("NodeSecret: %s", settings.ServerSettings.NodeSecret) | ||
} | ||
|
||
err := settings.Save() | ||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
} | ||
|
||
func registerPredefinedUser() { | ||
// when skip installation mode is enabled, the predefined user will be created | ||
if !settings.ServerSettings.SkipInstallation { | ||
return | ||
} | ||
pUser := &predefinedUser{} | ||
|
||
err := env.ParseWithOptions(pUser, env.Options{ | ||
Prefix: "NGINX_UI_PREDEFINED_USER_", | ||
UseFieldNameByDefault: true, | ||
}) | ||
|
||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
u := query.Auth | ||
|
||
_, err = u.First() | ||
|
||
// Only effect when there is no user in the database | ||
if !errors.Is(err, gorm.ErrRecordNotFound) || pUser.Name == "" || pUser.Password == "" { | ||
return | ||
} | ||
|
||
// Create a new user with the predefined name and password | ||
pwd, _ := bcrypt.GenerateFromPassword([]byte(pUser.Password), bcrypt.DefaultCost) | ||
|
||
err = u.Create(&model.Auth{ | ||
Name: pUser.Name, | ||
Password: string(pwd), | ||
}) | ||
|
||
if err != nil { | ||
logger.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#!/bin/sh | ||
#!/command/with-contenv sh | ||
env | ||
nginx-ui --config /etc/nginx-ui/app.ini |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.