Skip to content

Commit e64931b

Browse files
committed
fix #703
1 parent 898d4e7 commit e64931b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

trojan/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func InstallMysql() {
163163
if choice < 0 {
164164
return
165165
} else if choice == 1 {
166-
mysql = core.Mysql{ServerAddr: "127.0.0.1", ServerPort: util.RandomPort(), Password: util.RandString(5), Username: "root", Database: "trojan"}
166+
mysql = core.Mysql{ServerAddr: "127.0.0.1", ServerPort: util.RandomPort(), Password: util.RandString(8, util.LETTER+util.DIGITS), Username: "root", Database: "trojan"}
167167
InstallDocker()
168168
fmt.Println(fmt.Sprintf(dbDockerRun, mysql.ServerPort, mysql.Password))
169169
if util.CheckCommandExists("setenforce") {

trojan/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func UserMenu() {
3232

3333
// AddUser 添加用户
3434
func AddUser() {
35-
randomUser := util.RandString(4)
36-
randomPass := util.RandString(8)
35+
randomUser := util.RandString(4, util.LETTER)
36+
randomPass := util.RandString(8, util.LETTER+util.DIGITS)
3737
inputUser := util.Input(fmt.Sprintf("生成随机用户名: %s, 使用直接回车, 否则输入自定义用户名: ", randomUser), randomUser)
3838
if inputUser == "admin" {
3939
fmt.Println(util.Yellow("不能新建用户名为'admin'的用户!"))

util/string.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ const (
2727
WHITE = "\033[37m"
2828
// RESET 重置颜色
2929
RESET = "\033[0m"
30+
// LETTER 大小写英文字母常量
31+
LETTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
32+
// DIGITS 数字常量
33+
DIGITS = "0123456789"
34+
// SPECIALS 特殊字符常量
35+
SPECIALS = "~=+%^*/()[]{}/!@#$?|"
36+
// ALL 全部字符常量
37+
ALL = LETTER + DIGITS + SPECIALS
3038
)
3139

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

3846
// RandString 随机字符串
39-
func RandString(length int) string {
40-
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
47+
func RandString(length int, source string) string {
48+
var runes = []rune(source)
4149
b := make([]rune, length)
42-
rand.Seed(time.Now().UnixNano())
50+
rand.New(rand.NewSource(time.Now().UnixNano()))
4351
for i := range b {
44-
b[i] = letterRunes[rand.Intn(len(letterRunes))]
52+
b[i] = runes[rand.Intn(len(runes))]
4553
}
4654
return string(b)
4755
}

web/auth.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/gin-gonic/gin"
77
"time"
88
"trojan/core"
9+
"trojan/util"
910
"trojan/web/controller"
1011
)
1112

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

25+
func getSecretKey() string {
26+
sk, _ := core.GetValue("secretKey")
27+
if sk == "" {
28+
sk = util.RandString(15, util.ALL)
29+
core.SetValue("secretKey", sk)
30+
}
31+
return sk
32+
}
33+
2434
func jwtInit(timeout int) {
2535
authMiddleware, err = jwt.New(&jwt.GinJWTMiddleware{
26-
Realm: "k8s-manager",
27-
Key: []byte("secret key"),
36+
Realm: "trojan-manager",
37+
Key: []byte(getSecretKey()),
2838
Timeout: time.Minute * time.Duration(timeout),
2939
MaxRefresh: time.Minute * time.Duration(timeout),
3040
IdentityKey: identityKey,

0 commit comments

Comments
 (0)