mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-24 06:45:01 +03:00
fix(database): create the first panel user with random credentials
The first user is now created when the server starts, with a random username and password printed once to its log (docker logs, the journal), instead of a fixed pair seeded by InitDB. CLI calls that initialize the database are often piped away, as the Docker entrypoint's port lookup is, so only the server's own log reliably reaches the operator. `x-ui setting -show` keeps reporting hasDefaultCredential: true until a user exists, so install.sh still sets its own credentials on a fresh install. Existing databases are not changed. Verified on a built binary: fresh setting -show, install.sh-style credential set, first start, login and restart. The PostgreSQL lane was not run locally; the change adds no query.
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/sub"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/tunnelmonitor"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/sys"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/global"
|
||||
@@ -112,6 +111,15 @@ func runWebServer() {
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing database: %v", err)
|
||||
}
|
||||
// Created here, not in InitDB: a CLI call's output is often piped away, and
|
||||
// this log is the one place an operator reads the first login from.
|
||||
username, password, err := database.EnsureFirstUser()
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating the first panel user: %v", err)
|
||||
}
|
||||
if password != "" {
|
||||
log.Printf("No panel user existed, so one was generated. Username: %s Password: %s (shown once; change it in Panel Settings)", username, password)
|
||||
}
|
||||
|
||||
server := web.NewServer()
|
||||
global.SetWebServer(server)
|
||||
@@ -260,15 +268,11 @@ func showSetting(show bool) {
|
||||
}
|
||||
|
||||
userService := panel.UserService{}
|
||||
userModel, err := userService.GetFirstUser()
|
||||
hasDefaultCredential, err := userService.HasDefaultCredential()
|
||||
if err != nil {
|
||||
fmt.Println("get current user info failed, error info:", err)
|
||||
}
|
||||
|
||||
if userModel.Username == "" || userModel.Password == "" {
|
||||
fmt.Println("current username or password is empty")
|
||||
}
|
||||
|
||||
fmt.Println("current panel settings as follows:")
|
||||
if certFile == "" || keyFile == "" {
|
||||
fmt.Println("Warning: Panel is not secure with SSL")
|
||||
@@ -276,10 +280,6 @@ func showSetting(show bool) {
|
||||
fmt.Println("Panel is secure with SSL")
|
||||
}
|
||||
|
||||
hasDefaultCredential := func() bool {
|
||||
return userModel.Username == "admin" && crypto.CheckPasswordHash(userModel.Password, "admin")
|
||||
}()
|
||||
|
||||
fmt.Println("hasDefaultCredential:", hasDefaultCredential)
|
||||
fmt.Println("port:", port)
|
||||
fmt.Println("webBasePath:", webBasePath)
|
||||
|
||||
Reference in New Issue
Block a user