Skip to content

Commit

Permalink
fix #703
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Mar 30, 2023
1 parent 898d4e7 commit e64931b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion trojan/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func InstallMysql() {
if choice < 0 {
return
} else if choice == 1 {
mysql = core.Mysql{ServerAddr: "127.0.0.1", ServerPort: util.RandomPort(), Password: util.RandString(5), Username: "root", Database: "trojan"}
mysql = core.Mysql{ServerAddr: "127.0.0.1", ServerPort: util.RandomPort(), Password: util.RandString(8, util.LETTER+util.DIGITS), Username: "root", Database: "trojan"}
InstallDocker()
fmt.Println(fmt.Sprintf(dbDockerRun, mysql.ServerPort, mysql.Password))
if util.CheckCommandExists("setenforce") {
Expand Down
4 changes: 2 additions & 2 deletions trojan/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func UserMenu() {

// AddUser 添加用户
func AddUser() {
randomUser := util.RandString(4)
randomPass := util.RandString(8)
randomUser := util.RandString(4, util.LETTER)
randomPass := util.RandString(8, util.LETTER+util.DIGITS)
inputUser := util.Input(fmt.Sprintf("生成随机用户名: %s, 使用直接回车, 否则输入自定义用户名: ", randomUser), randomUser)
if inputUser == "admin" {
fmt.Println(util.Yellow("不能新建用户名为'admin'的用户!"))
Expand Down
16 changes: 12 additions & 4 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ const (
WHITE = "\033[37m"
// RESET 重置颜色
RESET = "\033[0m"
// LETTER 大小写英文字母常量
LETTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// DIGITS 数字常量
DIGITS = "0123456789"
// SPECIALS 特殊字符常量
SPECIALS = "~=+%^*/()[]{}/!@#$?|"
// ALL 全部字符常量
ALL = LETTER + DIGITS + SPECIALS
)

// IsInteger 判断字符串是否为整数
Expand All @@ -36,12 +44,12 @@ func IsInteger(input string) bool {
}

// RandString 随机字符串
func RandString(length int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandString(length int, source string) string {
var runes = []rune(source)
b := make([]rune, length)
rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
b[i] = runes[rand.Intn(len(runes))]
}
return string(b)
}
Expand Down
14 changes: 12 additions & 2 deletions web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"time"
"trojan/core"
"trojan/util"
"trojan/web/controller"
)

Expand All @@ -21,10 +22,19 @@ type Login struct {
Password string `form:"password" json:"password" binding:"required"`
}

func getSecretKey() string {
sk, _ := core.GetValue("secretKey")
if sk == "" {
sk = util.RandString(15, util.ALL)
core.SetValue("secretKey", sk)
}
return sk
}

func jwtInit(timeout int) {
authMiddleware, err = jwt.New(&jwt.GinJWTMiddleware{
Realm: "k8s-manager",
Key: []byte("secret key"),
Realm: "trojan-manager",
Key: []byte(getSecretKey()),
Timeout: time.Minute * time.Duration(timeout),
MaxRefresh: time.Minute * time.Duration(timeout),
IdentityKey: identityKey,
Expand Down

0 comments on commit e64931b

Please sign in to comment.