From f14bdd646ea412686662a5fedb6baf2e7146929f Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Thu, 15 Apr 2021 13:12:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/install.go | 6 +- cmd/version.go | 4 +- common/common.go | 11 ++++ common/config.go | 29 +++++++++ config/config.go | 146 ------------------------------------------ config/config.json | 4 -- controller/auth.go | 46 ------------- controller/ip.go | 6 +- controller/main.go | 13 ---- controller/message.go | 12 ++-- controller/visitor.go | 6 +- models/models.go | 4 +- start.bat | 2 + ws/visitor.go | 4 +- 14 files changed, 62 insertions(+), 231 deletions(-) create mode 100644 common/common.go create mode 100644 common/config.go delete mode 100644 config/config.go delete mode 100644 config/config.json create mode 100644 start.bat diff --git a/cmd/install.go b/cmd/install.go index f418805..d06c586 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" "github.com/spf13/cobra" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "io/ioutil" @@ -21,8 +21,8 @@ var installCmd = &cobra.Command{ } func install() { - sqlFile := config.Dir + "go-fly.sql" - isExit, _ := tools.IsFileExist(config.MysqlConf) + sqlFile := common.Dir + "go-fly.sql" + isExit, _ := tools.IsFileExist(common.MysqlConf) dataExit, _ := tools.IsFileExist(sqlFile) if !isExit || !dataExit { fmt.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在") diff --git a/cmd/version.go b/cmd/version.go index 02be9b3..f21f515 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,13 +3,13 @@ package cmd import ( "fmt" "github.com/spf13/cobra" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" ) var versionCmd = &cobra.Command{ Use: "version", Short: "example:go-fly version", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("go-fly " + config.Version) + fmt.Println("go-fly " + common.Version) }, } diff --git a/common/common.go b/common/common.go new file mode 100644 index 0000000..1b220a6 --- /dev/null +++ b/common/common.go @@ -0,0 +1,11 @@ +package common + +var ( + PageSize uint = 10 + VisitorPageSize uint = 8 + Version string = "0.3.9" + VisitorExpire float64 = 600 + Upload string = "static/upload/" + Dir string = "config/" + MysqlConf string = Dir + "mysql.json" +) diff --git a/common/config.go b/common/config.go new file mode 100644 index 0000000..36f9573 --- /dev/null +++ b/common/config.go @@ -0,0 +1,29 @@ +package common + +import ( + "encoding/json" + "github.com/taoshihan1991/imaptool/tools" + "io/ioutil" +) + +type Mysql struct { + Server string + Port string + Database string + Username string + Password string +} + +func GetMysqlConf() *Mysql { + var mysql = &Mysql{} + isExist, _ := tools.IsFileExist(MysqlConf) + if !isExist { + return mysql + } + info, err := ioutil.ReadFile(MysqlConf) + if err != nil { + return mysql + } + err = json.Unmarshal(info, mysql) + return mysql +} diff --git a/config/config.go b/config/config.go deleted file mode 100644 index edcf5ae..0000000 --- a/config/config.go +++ /dev/null @@ -1,146 +0,0 @@ -package config - -import ( - "encoding/json" - "fmt" - "github.com/taoshihan1991/imaptool/tools" - "io/ioutil" - "os" -) - -var ( - PageSize uint = 10 - VisitorPageSize uint = 8 - Version = "0.1.2" - VisitorExpire float64 = 600 - GoflyConfig *Config -) - -const Dir = "config/" -const AccountConf = Dir + "account.json" -const MysqlConf = Dir + "mysql.json" -const MailConf = Dir + "mail.json" -const MainConf = Dir + "config.json" - -func init() { - //配置文件 - GoflyConfig = CreateConfig() -} - -type Mysql struct { - Server string - Port string - Database string - Username string - Password string -} -type MailServer struct { - Server, Email, Password string -} -type Config struct { - Upload string - NoticeServerJiang bool -} - -func CreateConfig() *Config { - var configObj Config - c := &Config{ - Upload: "static/upload/", - NoticeServerJiang: false, - } - isExist, _ := tools.IsFileExist(MainConf) - if !isExist { - return c - } - info, err := ioutil.ReadFile(MainConf) - if err != nil { - return c - } - err = json.Unmarshal(info, &configObj) - return &configObj -} -func CreateMailServer() *MailServer { - var imap MailServer - isExist, _ := tools.IsFileExist(MailConf) - if !isExist { - return &imap - } - info, err := ioutil.ReadFile(MailConf) - if err != nil { - return &imap - } - - err = json.Unmarshal(info, &imap) - return &imap -} -func CreateMysql() *Mysql { - var mysql Mysql - isExist, _ := tools.IsFileExist(MysqlConf) - if !isExist { - return &mysql - } - info, err := ioutil.ReadFile(MysqlConf) - if err != nil { - return &mysql - } - - err = json.Unmarshal(info, &mysql) - return &mysql -} -func GetMysql() map[string]string { - var mysql map[string]string - isExist, _ := tools.IsFileExist(MysqlConf) - if !isExist { - return mysql - } - info, err := ioutil.ReadFile(MysqlConf) - if err != nil { - return mysql - } - - err = json.Unmarshal(info, &mysql) - return mysql -} -func GetAccount() map[string]string { - var account map[string]string - isExist, _ := tools.IsFileExist(AccountConf) - if !isExist { - return account - } - info, err := ioutil.ReadFile(AccountConf) - if err != nil { - return account - } - - err = json.Unmarshal(info, &account) - return account -} -func GetUserInfo(uid string) map[string]string { - var userInfo map[string]string - userFile := Dir + "sess_" + uid + ".json" - isExist, _ := tools.IsFileExist(userFile) - if !isExist { - return userInfo - } - info, err := ioutil.ReadFile(userFile) - if err != nil { - return userInfo - } - - err = json.Unmarshal(info, &userInfo) - return userInfo -} -func SetUserInfo(uid string, info map[string]string) { - userFile := Dir + "sess_" + uid + ".json" - isExist, _ := tools.IsFileExist(Dir) - if !isExist { - os.Mkdir(Dir, os.ModePerm) - } - file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm) - str := "{\r\n" - for k, v := range info { - str += fmt.Sprintf(`"%s":"%s",`, k, v) - } - str += fmt.Sprintf(`"session_id":"%s"%s}`, uid, "\r\n") - file.WriteString(str) -} diff --git a/config/config.json b/config/config.json deleted file mode 100644 index 1e7afdb..0000000 --- a/config/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "Upload":"static/upload/", - "NoticeServerJiang": false -} diff --git a/controller/auth.go b/controller/auth.go index f8a93b3..d0874b6 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -1,30 +1,10 @@ package controller import ( - "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" ) -func CheckPass(username string, password string) string { - account := config.GetAccount() - if account == nil { - account = make(map[string]string) - } - if account["Username"] == "" && account["Password"] == "" { - account["Username"] = "admin" - account["Password"] = "admin123" - } - if username == account["Username"] && password == account["Password"] { - - sessionId := tools.Md5(username) - info := make(map[string]string) - info["username"] = username - config.SetUserInfo(sessionId, info) - return sessionId - } - return "" -} func CheckKefuPass(username string, password string) (models.User, models.User_role, bool) { info := models.FindUser(username) var uRole models.User_role @@ -35,29 +15,3 @@ func CheckKefuPass(username string, password string) (models.User, models.User_r return info, uRole, true } -func AuthLocal(username string, password string) string { - account := config.GetAccount() - if account == nil { - account = make(map[string]string) - } - if account["Username"] == "" && account["Password"] == "" { - account["Username"] = "admin" - account["Password"] = "admin123" - } - if username == account["Username"] && password == account["Password"] { - - sessionId := tools.Md5(username) - info := make(map[string]string) - info["username"] = username - config.SetUserInfo(sessionId, info) - return sessionId - } - return "" -} - -//验证是否已经登录 -func AuthCheck(uid string) map[string]string { - info := config.GetUserInfo(uid) - - return info -} diff --git a/controller/ip.go b/controller/ip.go index 2330344..979cd07 100644 --- a/controller/ip.go +++ b/controller/ip.go @@ -2,7 +2,7 @@ package controller import ( "github.com/gin-gonic/gin" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" "strconv" ) @@ -44,14 +44,14 @@ func GetIpblacks(c *gin.Context) { page = 1 } count := models.CountIps(nil, nil) - list := models.FindIps(nil, nil, uint(page), config.VisitorPageSize) + list := models.FindIps(nil, nil, uint(page), common.VisitorPageSize) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ "list": list, "count": count, - "pagesize": config.PageSize, + "pagesize": common.PageSize, }, }) } diff --git a/controller/main.go b/controller/main.go index 92354dd..ef6fb76 100644 --- a/controller/main.go +++ b/controller/main.go @@ -3,22 +3,9 @@ package controller import ( "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/models" - "github.com/taoshihan1991/imaptool/tmpl" - "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" - "net/http" ) -func ActionMain(w http.ResponseWriter, r *http.Request) { - sessionId := tools.GetCookie(r, "session_id") - info := AuthCheck(sessionId) - if len(info) == 0 { - http.Redirect(w, r, "/login", 302) - return - } - render := tmpl.NewRender(w) - render.Display("main", render) -} func MainCheckAuth(c *gin.Context) { id, _ := c.Get("kefu_id") userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id) diff --git a/controller/message.go b/controller/message.go index ac51df9..36a85fc 100644 --- a/controller/message.go +++ b/controller/message.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" @@ -174,7 +174,6 @@ func SendCloseMessageV2(c *gin.Context) { }) } func UploadImg(c *gin.Context) { - config := config.CreateConfig() f, err := c.FormFile("imgfile") if err != nil { c.JSON(200, gin.H{ @@ -192,12 +191,12 @@ func UploadImg(c *gin.Context) { }) return } - isMainUploadExist, _ := tools.IsFileExist(config.Upload) + isMainUploadExist, _ := tools.IsFileExist(common.Upload) if !isMainUploadExist { - os.Mkdir(config.Upload, os.ModePerm) + os.Mkdir(common.Upload, os.ModePerm) } fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String())) - fildDir := fmt.Sprintf("%s%d%s/", config.Upload, time.Now().Year(), time.Now().Month().String()) + fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String()) isExist, _ := tools.IsFileExist(fildDir) if !isExist { os.Mkdir(fildDir, os.ModePerm) @@ -222,7 +221,6 @@ func UploadFile(c *gin.Context) { }) return } - config := config.CreateConfig() f, err := c.FormFile("realfile") if err != nil { c.JSON(200, gin.H{ @@ -242,7 +240,7 @@ func UploadFile(c *gin.Context) { } fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String())) - fildDir := fmt.Sprintf("%s%d%s/", config.Upload, time.Now().Year(), time.Now().Month().String()) + fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String()) isExist, _ := tools.IsFileExist(fildDir) if !isExist { os.Mkdir(fildDir, os.ModePerm) diff --git a/controller/visitor.go b/controller/visitor.go index 0e68fbb..7aea8a4 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "github.com/gin-gonic/gin" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" @@ -159,7 +159,7 @@ func GetVisitors(c *gin.Context) { page, _ := strconv.Atoi(c.Query("page")) pagesize, _ := strconv.Atoi(c.Query("pagesize")) if pagesize == 0 { - pagesize = int(config.VisitorPageSize) + pagesize = int(common.VisitorPageSize) } kefuId, _ := c.Get("kefu_name") vistors := models.FindVisitorsByKefuId(uint(page), uint(pagesize), kefuId.(string)) @@ -170,7 +170,7 @@ func GetVisitors(c *gin.Context) { "result": gin.H{ "list": vistors, "count": count, - "pagesize": config.PageSize, + "pagesize": common.PageSize, }, }) } diff --git a/models/models.go b/models/models.go index e9afaea..175ac8e 100644 --- a/models/models.go +++ b/models/models.go @@ -3,7 +3,7 @@ package models import ( "fmt" "github.com/jinzhu/gorm" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/tools" "log" "time" @@ -19,7 +19,7 @@ type Model struct { } func init() { - mysql := config.CreateMysql() + mysql := common.GetMysqlConf() dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database) var err error DB, err = gorm.Open("mysql", dsn) diff --git a/start.bat b/start.bat new file mode 100644 index 0000000..2271c68 --- /dev/null +++ b/start.bat @@ -0,0 +1,2 @@ +go-fly.exe server +pause \ No newline at end of file diff --git a/ws/visitor.go b/ws/visitor.go index 46fe4cb..9690d54 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" "log" "time" @@ -185,7 +185,7 @@ func cleanVisitorExpire() { for { for _, user := range ClientList { diff := time.Now().Sub(user.UpdateTime).Seconds() - if diff >= config.VisitorExpire { + if diff >= common.VisitorExpire { msg := TypeMessage{ Type: "auto_close", Data: user.Id,