Skip to content

Commit

Permalink
命令行增加重置web管理员密码
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Mar 23, 2020
1 parent 85f7721 commit 5f3d6ee
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exit:
fmt.Println()
fmt.Println(util.Cyan("欢迎使用trojan管理程序"))
fmt.Println()
menuList := []string{"trojan管理", "用户管理", "安装管理", "查看配置", "生成客户端配置文件"}
menuList := []string{"trojan管理", "用户管理", "安装管理", "web管理", "查看配置", "生成客户端配置文件"}
for i := 0; i < len(menuList); i++ {
if i%2 == 0 {
fmt.Printf("%d.%-15s\t", i+1, menuList[i])
Expand All @@ -61,8 +61,10 @@ exit:
case 3:
trojan.InstallMenu()
case 4:
trojan.UserList()
trojan.WebMenu()
case 5:
trojan.UserList()
case 6:
trojan.GenClientJson()
default:
break exit
Expand Down
6 changes: 0 additions & 6 deletions cmd/web.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"trojan/web"
)
Expand All @@ -16,11 +15,6 @@ var webCmd = &cobra.Command{
Use: "web",
Short: "以web方式启动",
Run: func(cmd *cobra.Command, args []string) {
if ssl && port == 80 {
fmt.Println("启动web服务失败!")
fmt.Println("以https方式运行必须传参-p来指定https的运行端口(不能为80)")
return
}
web.Start(port, ssl)
},
}
Expand Down
4 changes: 4 additions & 0 deletions trojan/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func AddUser() {
randomUser := util.RandString(4)
randomPass := util.RandString(8)
inputUser := util.Input(fmt.Sprintf("生成随机用户名: %s, 使用直接回车, 否则输入自定义用户名: ", randomUser), randomUser)
if inputUser == "admin" {
fmt.Println(util.Yellow("不能新建用户名为'admin'的用户!"))
return
}
inputPass := util.Input(fmt.Sprintf("生成随机密码: %s, 使用直接回车, 否则输入自定义密码: ", randomPass), randomPass)
mysql := core.GetMysql()
if mysql.CreateUser(inputUser, inputPass) == nil {
Expand Down
34 changes: 34 additions & 0 deletions trojan/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package trojan

import (
"crypto/sha256"
"fmt"
"trojan/core"
"trojan/util"
)

// WebMenu web管理菜单
func WebMenu() {
fmt.Println()
menu := []string{"重置web管理员密码"}
switch util.LoopInput("请选择: ", menu, true) {
case 1:
ResetAdminPass()
}
}

// ResetAdminPass 重置管理员密码
func ResetAdminPass() {
inputPass := util.Input("请输入admin用户密码: ", "")
if inputPass == "" {
fmt.Println("撤销更改!")
} else {
encryPass := sha256.Sum224([]byte(inputPass))
err := core.SetValue("admin_pass", fmt.Sprintf("%x", encryPass))
if err == nil {
fmt.Println(util.Green("重置admin密码成功!"))
} else {
fmt.Println(err)
}
}
}
4 changes: 4 additions & 0 deletions web/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func UserList() *ResponseBody {
func CreateUser(username string, password string) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
if username == "admin" {
responseBody.Msg = "不能创建用户名为admin的用户!"
return &responseBody
}
mysql := core.GetMysql()
pass, err := base64.StdEncoding.DecodeString(password)
if err != nil {
Expand Down
26 changes: 0 additions & 26 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/gobuffalo/packr/v2"
"github.com/unrolled/secure"
"net/http"
"strconv"
"trojan/core"
Expand Down Expand Up @@ -73,32 +72,9 @@ func staticRouter(router *gin.Engine) {
})
}

func sslRouter(router *gin.Engine, port int) {
domain, _ := core.GetValue("domain")
secureFunc := func() gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: fmt.Sprintf("%s:%d", domain, port),
})
err := secureMiddleware.Process(c.Writer, c.Request)
// If there was an error, do not continue.
if err != nil {
return
}

c.Next()
}
}()
router.Use(secureFunc)
}

// Start web启动入口
func Start(port int, isSSL bool) {
router := gin.Default()
if isSSL {
sslRouter(router, port)
}
router.Use(gzip.Gzip(gzip.DefaultCompression))
staticRouter(router)
router.Use(Auth(router).MiddlewareFunc())
Expand All @@ -109,8 +85,6 @@ func Start(port int, isSSL bool) {
if isSSL {
config := core.Load("")
ssl := &config.SSl
util.OpenPort(80)
go router.Run(":80")
router.RunTLS(fmt.Sprintf(":%d", port), ssl.Cert, ssl.Key)
} else {
router.Run(fmt.Sprintf(":%d", port))
Expand Down

0 comments on commit 5f3d6ee

Please sign in to comment.