增加验证jwt

pull/30/head
taoshihan1991 4 years ago
parent df43420cd1
commit 750fbdf96d

@ -9,48 +9,48 @@ import (
) )
const Dir = "config/" const Dir = "config/"
const AccountConf = Dir +"account.json" const AccountConf = Dir + "account.json"
func GetAccount()map[string]string{ func GetAccount() map[string]string {
var account map[string]string var account map[string]string
isExist,_:=tools.IsFileExist(AccountConf) isExist, _ := tools.IsFileExist(AccountConf)
if !isExist{ if !isExist {
return account return account
} }
info,err:=ioutil.ReadFile(AccountConf) info, err := ioutil.ReadFile(AccountConf)
if err!=nil{ if err != nil {
return account return account
} }
err=json.Unmarshal(info,&account) err = json.Unmarshal(info, &account)
return account return account
} }
func GetUserInfo(uid string)map[string]string{ func GetUserInfo(uid string) map[string]string {
var userInfo map[string]string var userInfo map[string]string
userFile:=Dir+"sess_"+uid+".json" userFile := Dir + "sess_" + uid + ".json"
isExist,_:=tools.IsFileExist(userFile) isExist, _ := tools.IsFileExist(userFile)
if !isExist{ if !isExist {
return userInfo return userInfo
} }
info,err:=ioutil.ReadFile(userFile) info, err := ioutil.ReadFile(userFile)
if err!=nil{ if err != nil {
return userInfo return userInfo
} }
err=json.Unmarshal(info,&userInfo) err = json.Unmarshal(info, &userInfo)
return userInfo return userInfo
} }
func SetUserInfo(uid string,info map[string]string){ func SetUserInfo(uid string, info map[string]string) {
userFile:=Dir+"sess_"+uid+".json" userFile := Dir + "sess_" + uid + ".json"
isExist,_:=tools.IsFileExist(Dir) isExist, _ := tools.IsFileExist(Dir)
if !isExist{ if !isExist {
os.Mkdir(Dir,os.ModePerm) os.Mkdir(Dir, os.ModePerm)
} }
file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm) file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm)
str:="{\r\n" str := "{\r\n"
for k,v:=range info{ for k, v := range info {
str+=fmt.Sprintf(`"%s":"%s",`,k,v) str += fmt.Sprintf(`"%s":"%s",`, k, v)
} }
str+=fmt.Sprintf(`"session_id":"%s"%s}`,uid,"\r\n") str += fmt.Sprintf(`"session_id":"%s"%s}`, uid, "\r\n")
file.WriteString(str) file.WriteString(str)
} }

@ -3,30 +3,59 @@ package controller
import ( import (
"github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"log"
) )
func AuthLocal(username string,password string)string{
account:=config.GetAccount() func CheckPass(username string, password string) string {
if account==nil{ account := config.GetAccount()
account=make(map[string]string) 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 CheckAuth(token string) bool {
userinfo := tools.ParseToken(token)
log.Println(userinfo)
if userinfo == nil {
return false
}
return true
}
func AuthLocal(username string, password string) string {
account := config.GetAccount()
if account == nil {
account = make(map[string]string)
} }
if account["Username"]=="" && account["Password"]==""{ if account["Username"] == "" && account["Password"] == "" {
account["Username"]="admin" account["Username"] = "admin"
account["Password"]="admin123" account["Password"] = "admin123"
} }
if username==account["Username"] && password==account["Password"]{ if username == account["Username"] && password == account["Password"] {
sessionId:=tools.Md5(username) sessionId := tools.Md5(username)
info:=make(map[string]string) info := make(map[string]string)
info["username"]=username info["username"] = username
config.SetUserInfo(sessionId,info) config.SetUserInfo(sessionId, info)
return sessionId return sessionId
} }
return "" return ""
} }
//验证是否已经登录 //验证是否已经登录
func AuthCheck(uid string)map[string]string{ func AuthCheck(uid string) map[string]string {
info:=config.GetUserInfo(uid) info := config.GetUserInfo(uid)
return info return info
} }

@ -16,17 +16,20 @@ func ActionChatMain(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w) render := tmpl.NewRender(w)
render.Display("chat_main", nil) render.Display("chat_main", nil)
} }
//聊天客户端界面 //聊天客户端界面
func ActionChatPage(w http.ResponseWriter, r *http.Request){ func ActionChatPage(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewRender(w) render := tmpl.NewRender(w)
render.Display("chat_page", nil) render.Display("chat_page", nil)
} }
//咨询界面 //咨询界面
func PageChat(c *gin.Context){ func PageChat(c *gin.Context) {
html := tools.FileGetContent("html/chat_page.html") html := tools.FileGetContent("html/chat_page.html")
c.Header("Content-Type", "text/html; charset=utf-8") c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, html) c.String(200, html)
} }
//获取在线用户 //获取在线用户
func ChatUsers(w http.ResponseWriter, r *http.Request) { func ChatUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
@ -34,7 +37,7 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) {
for uid, _ := range clientList { for uid, _ := range clientList {
userInfo := make(map[string]string) userInfo := make(map[string]string)
userInfo["uid"] = uid userInfo["uid"] = uid
userInfo["username"]=clientNameList[uid] userInfo["username"] = clientNameList[uid]
result = append(result, userInfo) result = append(result, userInfo)
} }
msg, _ := json.Marshal(tools.JsonListResult{ msg, _ := json.Marshal(tools.JsonListResult{
@ -43,6 +46,7 @@ func ChatUsers(w http.ResponseWriter, r *http.Request) {
}) })
w.Write(msg) w.Write(msg)
} }
type NoticeMessage struct { type NoticeMessage struct {
Type interface{} `json:"type"` Type interface{} `json:"type"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
@ -52,13 +56,13 @@ type TypeMessage struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type KfMessage struct { type KfMessage struct {
Kf_name string `json:"kf_name"` Kf_name string `json:"kf_name"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
Kf_id string `json:"kf_id"` Kf_id string `json:"kf_id"`
Kf_group string `json:"kf_group"` Kf_group string `json:"kf_group"`
Time string `json:"time"` Time string `json:"time"`
Guest_id string `json:"guest_id"` Guest_id string `json:"guest_id"`
Content string `json:"content"` Content string `json:"content"`
} }
type UserMessage struct { type UserMessage struct {
From_avatar string `json:"from_avatar"` From_avatar string `json:"from_avatar"`
@ -66,9 +70,10 @@ type UserMessage struct {
From_name string `json:"from_name"` From_name string `json:"from_name"`
To_id string `json:"to_id"` To_id string `json:"to_id"`
To_name string `json:"to_name"` To_name string `json:"to_name"`
Time string `json:"time"` Time string `json:"time"`
Content string `json:"content"` Content string `json:"content"`
} }
//定时检测客户端是否在线 //定时检测客户端是否在线
func init() { func init() {
sendPingToClient() sendPingToClient()
@ -91,11 +96,11 @@ func ChatServer(w *websocket.Conn) {
var kfMsg KfMessage var kfMsg KfMessage
var userMsg UserMessage var userMsg UserMessage
json.Unmarshal([]byte(receive), &typeMsg) json.Unmarshal([]byte(receive), &typeMsg)
if typeMsg.Type==nil||typeMsg.Data==nil{ if typeMsg.Type == nil || typeMsg.Data == nil {
break break
} }
msgType:=typeMsg.Type.(string) msgType := typeMsg.Type.(string)
msgData, _ :=json.Marshal(typeMsg.Data) msgData, _ := json.Marshal(typeMsg.Data)
switch msgType { switch msgType {
//获取当前在线的所有用户 //获取当前在线的所有用户
@ -103,39 +108,39 @@ func ChatServer(w *websocket.Conn) {
getOnlineUser(w) getOnlineUser(w)
//用户上线 //用户上线
case "userInit": case "userInit":
json.Unmarshal(msgData,&userMsg) json.Unmarshal(msgData, &userMsg)
//用户id对应的连接 //用户id对应的连接
clientList[userMsg.From_id] = w clientList[userMsg.From_id] = w
clientNameList[userMsg.From_id]=userMsg.From_name clientNameList[userMsg.From_id] = userMsg.From_name
SendUserAllNotice() SendUserAllNotice()
//客服上线 //客服上线
case "kfOnline": case "kfOnline":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData, &kfMsg)
//客服id对应的连接 //客服id对应的连接
kefuList[kfMsg.Kf_id] = w kefuList[kfMsg.Kf_id] = w
//发送给客户 //发送给客户
if len(clientList)==0{ if len(clientList) == 0 {
break break
} }
for _, conn := range clientList { for _, conn := range clientList {
SendKefuOnline(kfMsg,conn) SendKefuOnline(kfMsg, conn)
} }
//发送给客服通知 //发送给客服通知
//SendOnekfuAllNotice(w) //SendOnekfuAllNotice(w)
//客服接手 //客服接手
case "kfConnect": case "kfConnect":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData, &kfMsg)
kefuList[kfMsg.Kf_id] = w kefuList[kfMsg.Kf_id] = w
SendKefuOnline(kfMsg,clientList[kfMsg.Guest_id]) SendKefuOnline(kfMsg, clientList[kfMsg.Guest_id])
case "kfChatMessage": case "kfChatMessage":
json.Unmarshal(msgData,&kfMsg) json.Unmarshal(msgData, &kfMsg)
conn:=clientList[kfMsg.Guest_id] conn := clientList[kfMsg.Guest_id]
if kfMsg.Guest_id=="" ||conn==nil{ if kfMsg.Guest_id == "" || conn == nil {
return return
} }
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "kfChatMessage", Type: "kfChatMessage",
Data:KfMessage{ Data: KfMessage{
Kf_name: kfMsg.Kf_name, Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar, Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id, Kf_id: kfMsg.Kf_id,
@ -144,110 +149,122 @@ func ChatServer(w *websocket.Conn) {
Content: kfMsg.Content, Content: kfMsg.Content,
}, },
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
websocket.Message.Send(conn,sendStr) sendStr := string(str)
websocket.Message.Send(conn, sendStr)
case "chatMessage": case "chatMessage":
json.Unmarshal(msgData,&userMsg) json.Unmarshal(msgData, &userMsg)
conn:=kefuList[userMsg.To_id] conn := kefuList[userMsg.To_id]
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "chatMessage", Type: "chatMessage",
Data:UserMessage{ Data: UserMessage{
From_avatar: userMsg.From_avatar, From_avatar: userMsg.From_avatar,
From_id: userMsg.From_id, From_id: userMsg.From_id,
From_name: userMsg.From_name, From_name: userMsg.From_name,
To_id: userMsg.To_id, To_id: userMsg.To_id,
To_name: userMsg.To_name, To_name: userMsg.To_name,
Content: userMsg.Content, Content: userMsg.Content,
Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
}, },
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
websocket.Message.Send(conn,sendStr) sendStr := string(str)
websocket.Message.Send(conn, sendStr)
} }
} }
} }
//发送给所有客服客户上线 //发送给所有客服客户上线
func SendUserAllNotice(){ func SendUserAllNotice() {
if len(kefuList)!=0{ if len(kefuList) != 0 {
//发送给客服通知 //发送给客服通知
for _, conn := range kefuList { for _, conn := range kefuList {
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "notice", Type: "notice",
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
websocket.Message.Send(conn,sendStr) sendStr := string(str)
websocket.Message.Send(conn, sendStr)
} }
} }
} }
//发送给客户客服上线 //发送给客户客服上线
func SendKefuOnline(kfMsg KfMessage,conn *websocket.Conn){ func SendKefuOnline(kfMsg KfMessage, conn *websocket.Conn) {
sendMsg := TypeMessage{ sendMsg := TypeMessage{
Type: "kfOnline", Type: "kfOnline",
Data: KfMessage{ Data: KfMessage{
Kf_name: kfMsg.Kf_name, Kf_name: kfMsg.Kf_name,
Avatar: kfMsg.Avatar, Avatar: kfMsg.Avatar,
Kf_id: kfMsg.Kf_id, Kf_id: kfMsg.Kf_id,
Kf_group: kfMsg.Kf_group, Kf_group: kfMsg.Kf_group,
Time: time.Now().Format("2006-01-02 15:04:05"), Time: time.Now().Format("2006-01-02 15:04:05"),
Content: "客服上线", Content: "客服上线",
}, },
} }
jsonStrByte, _ := json.Marshal(sendMsg) jsonStrByte, _ := json.Marshal(sendMsg)
websocket.Message.Send(conn, string(jsonStrByte)) websocket.Message.Send(conn, string(jsonStrByte))
} }
//发送给所有客服客户上线 //发送给所有客服客户上线
func SendOnekfuAllNotice(conn *websocket.Conn){ func SendOnekfuAllNotice(conn *websocket.Conn) {
result := make([]map[string]string, 0) result := make([]map[string]string, 0)
for uid, _ := range clientList { for uid, _ := range clientList {
userInfo := make(map[string]string) userInfo := make(map[string]string)
userInfo["uid"] = uid userInfo["uid"] = uid
userInfo["username"]=clientNameList[uid] userInfo["username"] = clientNameList[uid]
result = append(result, userInfo) result = append(result, userInfo)
} }
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "notice", Type: "notice",
Data:result, Data: result,
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
websocket.Message.Send(conn,sendStr) sendStr := string(str)
websocket.Message.Send(conn, sendStr)
} }
//获取当前的在线用户 //获取当前的在线用户
func getOnlineUser(w *websocket.Conn){ func getOnlineUser(w *websocket.Conn) {
result := make([]map[string]string, 0) result := make([]map[string]string, 0)
for uid, _ := range clientList { for uid, _ := range clientList {
userInfo := make(map[string]string) userInfo := make(map[string]string)
userInfo["uid"] = uid userInfo["uid"] = uid
userInfo["username"]=clientNameList[uid] userInfo["username"] = clientNameList[uid]
result = append(result, userInfo) result = append(result, userInfo)
} }
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "getOnlineUsers", Type: "getOnlineUsers",
Data:result, Data: result,
} }
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
websocket.Message.Send(w,sendStr) sendStr := string(str)
websocket.Message.Send(w, sendStr)
} }
//定时给客户端发送消息判断客户端是否在线 //定时给客户端发送消息判断客户端是否在线
func sendPingToClient(){ func sendPingToClient() {
msg:=NoticeMessage{ msg := NoticeMessage{
Type: "ping", Type: "ping",
} }
go func() { go func() {
for{ for {
log.Println("check online users...") log.Println("check online users...")
str,_:=json.Marshal(msg);sendStr:=string(str) str, _ := json.Marshal(msg)
sendStr := string(str)
for uid, conn := range clientList { for uid, conn := range clientList {
err:=websocket.Message.Send(conn,sendStr) err := websocket.Message.Send(conn, sendStr)
if err!=nil{ if err != nil {
delete(clientList,uid) delete(clientList, uid)
SendUserAllNotice() SendUserAllNotice()
} }
} }
time.Sleep(10*time.Second) time.Sleep(10 * time.Second)
} }
}() }()
} }
var clientList = make(map[string]*websocket.Conn) var clientList = make(map[string]*websocket.Conn)
var clientNameList = make(map[string]string) var clientNameList = make(map[string]string)
var kefuList = make(map[string]*websocket.Conn) var kefuList = make(map[string]*websocket.Conn)

@ -9,11 +9,13 @@ import (
"strconv" "strconv"
"sync" "sync"
) )
const PageSize = 20 const PageSize = 20
//输出列表 //输出列表
func ActionFolder(w http.ResponseWriter, r *http.Request){ func ActionFolder(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid") fid := tools.GetUrlArg(r, "fid")
currentPage, _ :=strconv.Atoi(tools.GetUrlArg(r,"page")) currentPage, _ := strconv.Atoi(tools.GetUrlArg(r, "page"))
if fid == "" { if fid == "" {
fid = "INBOX" fid = "INBOX"
} }
@ -23,28 +25,31 @@ func ActionFolder(w http.ResponseWriter, r *http.Request){
render := tmpl.NewFolderHtml(w) render := tmpl.NewFolderHtml(w)
render.CurrentPage = currentPage render.CurrentPage = currentPage
render.Fid = fid render.Fid = fid
render.Display("list",render) render.Display("list", render)
} }
//写信界面 //写信界面
func ActionWrite(w http.ResponseWriter, r *http.Request){ func ActionWrite(w http.ResponseWriter, r *http.Request) {
render:=tmpl.NewRender(w) render := tmpl.NewRender(w)
render.SetLeft("mail_left") render.SetLeft("mail_left")
render.Display("write",nil) render.Display("write", nil)
} }
//读信界面 //读信界面
func ActionDetail(w http.ResponseWriter, r *http.Request){ func ActionDetail(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid") fid := tools.GetUrlArg(r, "fid")
id, _ :=strconv.Atoi(tools.GetUrlArg(r,"id")) id, _ := strconv.Atoi(tools.GetUrlArg(r, "id"))
render:=tmpl.NewDetailHtml(w) render := tmpl.NewDetailHtml(w)
render.SetLeft("mail_left") render.SetLeft("mail_left")
render.Fid=fid render.Fid = fid
render.Id=uint32(id) render.Id = uint32(id)
render.Display("mail_detail",render) render.Display("mail_detail", render)
} }
//获取邮件夹接口 //获取邮件夹接口
func FolderDir(w http.ResponseWriter, r *http.Request){ func FolderDir(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid") fid := tools.GetUrlArg(r, "fid")
if fid == "" { if fid == "" {
fid = "INBOX" fid = "INBOX"
@ -69,10 +74,11 @@ func FolderDir(w http.ResponseWriter, r *http.Request){
}) })
w.Write(msg) w.Write(msg)
} }
//邮件夹接口 //邮件夹接口
func FoldersList(w http.ResponseWriter, r *http.Request) { func FoldersList(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid") fid := tools.GetUrlArg(r, "fid")
currentPage, _ :=strconv.Atoi(tools.GetUrlArg(r,"page")) currentPage, _ := strconv.Atoi(tools.GetUrlArg(r, "page"))
if fid == "" { if fid == "" {
fid = "INBOX" fid = "INBOX"
@ -90,7 +96,6 @@ func FoldersList(w http.ResponseWriter, r *http.Request) {
return return
} }
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
result := make(map[string]interface{}) result := make(map[string]interface{})
@ -115,10 +120,11 @@ func FoldersList(w http.ResponseWriter, r *http.Request) {
}) })
w.Write(msg) w.Write(msg)
} }
//邮件接口 //邮件接口
func FolderMail(w http.ResponseWriter, r *http.Request) { func FolderMail(w http.ResponseWriter, r *http.Request) {
fid:=tools.GetUrlArg(r,"fid") fid := tools.GetUrlArg(r, "fid")
id, _ :=strconv.Atoi(tools.GetUrlArg(r,"id")) id, _ := strconv.Atoi(tools.GetUrlArg(r, "id"))
mailServer := tools.GetMailServerFromCookie(r) mailServer := tools.GetMailServerFromCookie(r)
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
@ -154,8 +160,9 @@ func FolderMail(w http.ResponseWriter, r *http.Request) {
}) })
w.Write(msg) w.Write(msg)
} }
//发送邮件接口 //发送邮件接口
func FolderSend(w http.ResponseWriter, r *http.Request){ func FolderSend(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
mailServer := tools.GetMailServerFromCookie(r) mailServer := tools.GetMailServerFromCookie(r)
@ -165,28 +172,28 @@ func FolderSend(w http.ResponseWriter, r *http.Request){
return return
} }
bodyBytes,err:=ioutil.ReadAll(r.Body) bodyBytes, err := ioutil.ReadAll(r.Body)
if err!=nil{ if err != nil {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败,"+err.Error()}) msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败," + err.Error()})
w.Write(msg) w.Write(msg)
return return
} }
var sendData tools.SmtpBody var sendData tools.SmtpBody
err = json.Unmarshal(bodyBytes, &sendData) err = json.Unmarshal(bodyBytes, &sendData)
if err!=nil{ if err != nil {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败,"+err.Error()}) msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败," + err.Error()})
w.Write(msg) w.Write(msg)
return return
} }
smtpServer:=sendData.Smtp smtpServer := sendData.Smtp
smtpFrom:=mailServer.Email smtpFrom := mailServer.Email
smtpTo:=sendData.To smtpTo := sendData.To
smtpBody:=sendData.Body smtpBody := sendData.Body
smtpPass:=mailServer.Password smtpPass := mailServer.Password
smtpSubject:=sendData.Subject smtpSubject := sendData.Subject
err=tools.Send(smtpServer,smtpFrom,smtpPass,smtpTo,smtpSubject,smtpBody) err = tools.Send(smtpServer, smtpFrom, smtpPass, smtpTo, smtpSubject, smtpBody)
if err!=nil{ if err != nil {
msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: err.Error()}) msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: err.Error()})
w.Write(msg) w.Write(msg)
return return

@ -4,8 +4,9 @@ import (
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"net/http" "net/http"
) )
//首页跳转 //首页跳转
func ActionIndex(w http.ResponseWriter, r *http.Request){ func ActionIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() == "/favicon.ico" { if r.URL.RequestURI() == "/favicon.ico" {
return return
} }

@ -6,16 +6,44 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"html/template" "html/template"
"log"
"net/http" "net/http"
) )
//验证接口 //验证接口
func LoginCheckPass(c *gin.Context) { func LoginCheckPass(c *gin.Context) {
authType := c.PostForm("type")
password := c.PostForm("password")
username := c.PostForm("username")
switch authType {
case "local":
sessionId := CheckPass(username, password)
userinfo := make(map[string]interface{})
userinfo["name"] = username
token, err := tools.MakeToken(userinfo)
log.Println(err)
if sessionId != "" {
c.JSON(200, gin.H{
"code": 200,
"msg": "验证成功,正在跳转",
"result": gin.H{
"token": token,
},
})
return
}
c.JSON(200, gin.H{
"code": 400,
"msg": "验证失败",
})
}
} }
func ActionLogin(w http.ResponseWriter, r *http.Request){ func ActionLogin(w http.ResponseWriter, r *http.Request) {
html := tools.FileGetContent("html/login.html") html := tools.FileGetContent("html/login.html")
t, _ := template.New("login").Parse(html) t, _ := template.New("login").Parse(html)
t.Execute(w, nil) t.Execute(w, nil)
} }
//验证接口 //验证接口
func LoginCheck(w http.ResponseWriter, r *http.Request) { func LoginCheck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
@ -25,8 +53,8 @@ func LoginCheck(w http.ResponseWriter, r *http.Request) {
switch authType { switch authType {
case "local": case "local":
username := r.PostFormValue("username") username := r.PostFormValue("username")
sessionId:=AuthLocal(username,password) sessionId := AuthLocal(username, password)
if sessionId!=""{ if sessionId != "" {
tools.SetCookie("session_id", sessionId, &w) tools.SetCookie("session_id", sessionId, &w)
msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."}) msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: "验证成功,正在跳转..."})
w.Write(msg) w.Write(msg)

@ -1,17 +1,34 @@
package controller package controller
import ( import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"net/http" "net/http"
) )
func ActionMain(w http.ResponseWriter, r *http.Request){
sessionId:=tools.GetCookie(r,"session_id") func ActionMain(w http.ResponseWriter, r *http.Request) {
info:=AuthCheck(sessionId) sessionId := tools.GetCookie(r, "session_id")
if len(info)==0{ info := AuthCheck(sessionId)
if len(info) == 0 {
http.Redirect(w, r, "/login", 302) http.Redirect(w, r, "/login", 302)
return return
} }
render:=tmpl.NewRender(w) render := tmpl.NewRender(w)
render.Display("main",render) render.Display("main", render)
}
func MainCheckAuth(c *gin.Context) {
token := c.Query("token")
r := CheckAuth(token)
if !r {
c.JSON(200, gin.H{
"code": 400,
"msg": "验证失败",
})
} else {
c.JSON(200, gin.H{
"code": 200,
"msg": "验证成功",
})
}
} }

@ -5,9 +5,9 @@ import (
"net/http" "net/http"
) )
func ActionMysqlSet(w http.ResponseWriter, r *http.Request){ func ActionMysqlSet(w http.ResponseWriter, r *http.Request) {
render:=tmpl.NewSettingHtml(w) render := tmpl.NewSettingHtml(w)
render.SetLeft("setting_left") render.SetLeft("setting_left")
render.SetBottom("setting_bottom") render.SetBottom("setting_bottom")
render.Display("mysql_setting",render) render.Display("mysql_setting", render)
} }

@ -7,10 +7,12 @@ import (
"log" "log"
"net/http" "net/http"
) )
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
var oldFolders map[string]int var oldFolders map[string]int
//推送新邮件到达 //推送新邮件到达
func PushMailServer(w http.ResponseWriter, r *http.Request){ func PushMailServer(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil) c, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Print("upgrade:", err) log.Print("upgrade:", err)
@ -28,26 +30,26 @@ func PushMailServer(w http.ResponseWriter, r *http.Request){
var msg []byte var msg []byte
if mailServer == nil { if mailServer == nil {
msg, _ = json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) msg, _ = json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
err = c.WriteMessage(mt,msg) err = c.WriteMessage(mt, msg)
if err != nil { if err != nil {
log.Println("write:", err) log.Println("write:", err)
break break
} }
}else{ } else {
folders:=tools.GetMailNum(mailServer.Server, mailServer.Email, mailServer.Password) folders := tools.GetMailNum(mailServer.Server, mailServer.Email, mailServer.Password)
for name,num:=range folders{ for name, num := range folders {
if oldFolders[name]!=num{ if oldFolders[name] != num {
result := make(map[string]interface{}) result := make(map[string]interface{})
result["folder_name"] = name result["folder_name"] = name
result["new_num"] = num-oldFolders[name] result["new_num"] = num - oldFolders[name]
msg, _ := json.Marshal(tools.JsonListResult{ msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result, Result: result,
}) })
c.WriteMessage(mt,msg) c.WriteMessage(mt, msg)
} }
} }
oldFolders=folders oldFolders = folders
} }
} }
} }

@ -10,41 +10,40 @@ import (
"os" "os"
) )
func ActionSetting(w http.ResponseWriter, r *http.Request) {
func ActionSetting(w http.ResponseWriter, r *http.Request){ render := tmpl.NewSettingHtml(w)
render:=tmpl.NewSettingHtml(w)
render.SetLeft("setting_left") render.SetLeft("setting_left")
render.SetBottom("setting_bottom") render.SetBottom("setting_bottom")
account:=config.GetAccount() account := config.GetAccount()
render.Username=account["Username"] render.Username = account["Username"]
render.Password=account["Password"] render.Password = account["Password"]
render.Display("setting",render) render.Display("setting", render)
} }
func SettingAccount(w http.ResponseWriter, r *http.Request){ func SettingAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
username:=r.PostFormValue("username") username := r.PostFormValue("username")
password:=r.PostFormValue("password") password := r.PostFormValue("password")
isExist,_:=tools.IsFileExist(config.Dir) isExist, _ := tools.IsFileExist(config.Dir)
if !isExist{ if !isExist {
os.Mkdir(config.Dir,os.ModePerm) os.Mkdir(config.Dir, os.ModePerm)
} }
fileConfig:=config.AccountConf fileConfig := config.AccountConf
file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm) file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm)
format:=`{ format := `{
"Username":"%s", "Username":"%s",
"Password":"%s" "Password":"%s"
} }
` `
data := fmt.Sprintf(format,username,password) data := fmt.Sprintf(format, username, password)
file.WriteString(data) file.WriteString(data)
msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "操作成功!"}) msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "操作成功!"})
w.Write(msg) w.Write(msg)
} }
func SettingGetAccount(w http.ResponseWriter, r *http.Request){ func SettingGetAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;") w.Header().Set("content-type", "text/json;charset=utf-8;")
mailServer := tools.GetMailServerFromCookie(r) mailServer := tools.GetMailServerFromCookie(r)
@ -53,7 +52,7 @@ func SettingGetAccount(w http.ResponseWriter, r *http.Request){
w.Write(msg) w.Write(msg)
return return
} }
result:=config.GetAccount() result := config.GetAccount()
msg, _ := json.Marshal(tools.JsonListResult{ msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result, Result: result,

@ -10,25 +10,28 @@ import (
"syscall" "syscall"
"time" "time"
) )
var osType = runtime.GOOS var osType = runtime.GOOS
const expireTime=30*60
const expireTime = 30 * 60
//检测权限文件是否过期,超过30分钟删除掉 //检测权限文件是否过期,超过30分钟删除掉
func TimerSessFile(){ func TimerSessFile() {
go func() { go func() {
for { for {
time.Sleep(time.Second*10) time.Sleep(time.Second * 10)
files,_:=filepath.Glob(config.Dir+"sess_*") files, _ := filepath.Glob(config.Dir + "sess_*")
for _,file:=range files{ for _, file := range files {
fileInfo, _ := os.Stat(file) fileInfo, _ := os.Stat(file)
var createTime int64 var createTime int64
now:=time.Now().Unix() now := time.Now().Unix()
if osType == "windows" { if osType == "windows" {
wFileSys := fileInfo.Sys().(*syscall.Win32FileAttributeData) wFileSys := fileInfo.Sys().(*syscall.Win32FileAttributeData)
tNanSeconds := wFileSys.CreationTime.Nanoseconds() /// 返回的是纳秒 tNanSeconds := wFileSys.CreationTime.Nanoseconds() /// 返回的是纳秒
createTime = tNanSeconds/1e9 ///秒 createTime = tNanSeconds / 1e9 ///秒
} }
diffTime:=now-createTime diffTime := now - createTime
if diffTime>expireTime{ if diffTime > expireTime {
os.Remove(file) os.Remove(file)
} }
} }

@ -8,24 +8,27 @@ import (
"runtime" "runtime"
"time" "time"
) )
var osType = runtime.GOOS var osType = runtime.GOOS
const expireTime=30*60
const expireTime = 30 * 60
//检测权限文件是否过期,超过30分钟删除掉 //检测权限文件是否过期,超过30分钟删除掉
func TimerSessFile(){ func TimerSessFile() {
go func() { go func() {
for { for {
time.Sleep(time.Second*10) time.Sleep(time.Second * 10)
files,_:=filepath.Glob(config.Dir+"sess_*") files, _ := filepath.Glob(config.Dir + "sess_*")
for _,file:=range files{ for _, file := range files {
fileInfo, _ := os.Stat(file) fileInfo, _ := os.Stat(file)
var createTime int64 var createTime int64
now:=time.Now().Unix() now := time.Now().Unix()
if osType == "linux" { if osType == "linux" {
stat_t := fileInfo.Sys().(*syscall.Stat_t) stat_t := fileInfo.Sys().(*syscall.Stat_t)
createTime = int64(stat_t.Ctim.Sec) createTime = int64(stat_t.Ctim.Sec)
} }
diffTime:=now-createTime diffTime := now - createTime
if diffTime>expireTime{ if diffTime > expireTime {
os.Remove(file) os.Remove(file)
} }
} }

@ -6,6 +6,7 @@ require github.com/emersion/go-imap v1.0.4
require ( require (
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/emersion/go-message v0.11.2 github.com/emersion/go-message v0.11.2
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.13.0 github.com/emersion/go-smtp v0.13.0

@ -9,6 +9,8 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/emersion/go-imap v1.0.4 h1:uiCAIHM6Z5Jwkma1zdNDWWXxSCqb+/xHBkHflD7XBro= github.com/emersion/go-imap v1.0.4 h1:uiCAIHM6Z5Jwkma1zdNDWWXxSCqb+/xHBkHflD7XBro=
github.com/emersion/go-imap v1.0.4/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO7GAmvicU= github.com/emersion/go-imap v1.0.4/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO7GAmvicU=
github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY= github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY=

@ -1,27 +1,34 @@
package main package main
import ( import (
"github.com/taoshihan1991/imaptool/controller"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/controller"
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"log" "log"
"net/http" "net/http"
"time" "time"
) )
func main() { func main() {
baseServer:="127.0.0.1:8080" baseServer := "127.0.0.1:8080"
log.Println("start server...\r\ngohttp://"+baseServer) log.Println("start server...\r\ngohttp://" + baseServer)
engine := gin.Default() engine := gin.Default()
engine.LoadHTMLGlob("static/html/*")
//登陆界面 //登陆界面
engine.GET("/login",tmpl.PageLogin) engine.GET("/login", tmpl.PageLogin)
//咨询界面 //咨询界面
engine.GET("/chat_page",tmpl.PageChat) engine.GET("/chat_page", tmpl.PageChat)
//登陆验证 //登陆验证
engine.POST("/check",controller.LoginCheckPass) engine.POST("/check", controller.LoginCheckPass)
//框架界面
engine.GET("/main", tmpl.PageMain)
//框架界面
engine.GET("/chat_main", tmpl.PageChatMain)
//验证权限
engine.GET("/check_auth", controller.MainCheckAuth)
//------------------old code----------------------------- //------------------old code-----------------------------
mux:=&http.ServeMux{} mux := &http.ServeMux{}
//根路径 //根路径
mux.HandleFunc("/", controller.ActionIndex) mux.HandleFunc("/", controller.ActionIndex)
//邮件夹 //邮件夹
@ -53,13 +60,13 @@ func main() {
//新邮件提醒服务 //新邮件提醒服务
mux.HandleFunc("/push_mail", controller.PushMailServer) mux.HandleFunc("/push_mail", controller.PushMailServer)
//聊天界面 //聊天界面
mux.HandleFunc("/chat_page",controller.ActionChatPage) mux.HandleFunc("/chat_page", controller.ActionChatPage)
//聊天服务 //聊天服务
mux.Handle("/chat_server",websocket.Handler(controller.ChatServer)) mux.Handle("/chat_server", websocket.Handler(controller.ChatServer))
//获取在线用户 //获取在线用户
mux.HandleFunc("/chat_users", controller.ChatUsers) mux.HandleFunc("/chat_users", controller.ChatUsers)
//设置mysql //设置mysql
mux.HandleFunc("/setting_mysql",controller.ActionMysqlSet) mux.HandleFunc("/setting_mysql", controller.ActionMysqlSet)
//后台任务 //后台任务
controller.TimerSessFile() controller.TimerSessFile()
//监听端口 //监听端口

@ -160,7 +160,7 @@
message: data.msg, message: data.msg,
type: 'success' type: 'success'
}); });
window.location.href="/"; //window.location.href="/";
}else{ }else{
_this.$message({ _this.$message({
message: data.msg, message: data.msg,
@ -195,7 +195,7 @@
message: data.msg, message: data.msg,
type: 'success' type: 'success'
}); });
window.location.href="/main"; //window.location.href="/main";
}else{ }else{
_this.$message({ _this.$message({
message: data.msg, message: data.msg,

@ -1,40 +1,55 @@
package tmpl package tmpl
import ( import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"html/template" "html/template"
"net/http" "net/http"
) )
type CommonHtml struct{ type CommonHtml struct {
Header template.HTML Header template.HTML
Nav template.HTML Nav template.HTML
Left template.HTML Left template.HTML
Bottom template.HTML Bottom template.HTML
Rw http.ResponseWriter Rw http.ResponseWriter
} }
func NewRender(rw http.ResponseWriter)*CommonHtml{
obj:=new(CommonHtml) func NewRender(rw http.ResponseWriter) *CommonHtml {
obj.Rw=rw obj := new(CommonHtml)
obj.Rw = rw
header := tools.FileGetContent("html/header.html") header := tools.FileGetContent("html/header.html")
nav := tools.FileGetContent("html/nav.html") nav := tools.FileGetContent("html/nav.html")
obj.Header=template.HTML(header) obj.Header = template.HTML(header)
obj.Nav=template.HTML(nav) obj.Nav = template.HTML(nav)
return obj return obj
} }
func (obj *CommonHtml)SetLeft(file string){ func (obj *CommonHtml) SetLeft(file string) {
leftStr := tools.FileGetContent("html/"+file+".html") leftStr := tools.FileGetContent("html/" + file + ".html")
obj.Left=template.HTML(leftStr) obj.Left = template.HTML(leftStr)
} }
func (obj *CommonHtml)SetBottom(file string){ func (obj *CommonHtml) SetBottom(file string) {
str := tools.FileGetContent("html/"+file+".html") str := tools.FileGetContent("html/" + file + ".html")
obj.Bottom=template.HTML(str) obj.Bottom = template.HTML(str)
} }
func (obj *CommonHtml)Display(file string,data interface{}){ func (obj *CommonHtml) Display(file string, data interface{}) {
if data==nil{ if data == nil {
data=obj data = obj
} }
main := tools.FileGetContent("html/"+file+".html") main := tools.FileGetContent("html/" + file + ".html")
t, _ := template.New(file).Parse(main) t, _ := template.New(file).Parse(main)
t.Execute(obj.Rw, data) t.Execute(obj.Rw, data)
} }
//登陆界面
func PageMain(c *gin.Context) {
nav := tools.FileGetContent("html/nav.html")
c.HTML(http.StatusOK, "main.html", gin.H{
"Nav": template.HTML(nav),
})
}
//客服界面
func PageChatMain(c *gin.Context) {
c.HTML(http.StatusOK, "chat_main.html", nil)
}

@ -5,11 +5,12 @@ import "net/http"
type DetailHtml struct { type DetailHtml struct {
*CommonHtml *CommonHtml
Fid string Fid string
Id uint32 Id uint32
} }
func NewDetailHtml(w http.ResponseWriter)*DetailHtml{
obj:=new(DetailHtml) func NewDetailHtml(w http.ResponseWriter) *DetailHtml {
parent:=NewRender(w) obj := new(DetailHtml)
obj.CommonHtml=parent parent := NewRender(w)
obj.CommonHtml = parent
return obj return obj
} }

@ -1,15 +1,16 @@
package tmpl package tmpl
import "net/http" import "net/http"
type FolderHtml struct { type FolderHtml struct {
*CommonHtml *CommonHtml
CurrentPage int CurrentPage int
Fid string Fid string
} }
func NewFolderHtml(w http.ResponseWriter)*FolderHtml{
obj:=new(FolderHtml) func NewFolderHtml(w http.ResponseWriter) *FolderHtml {
parent:=NewRender(w) obj := new(FolderHtml)
obj.CommonHtml=parent parent := NewRender(w)
obj.CommonHtml = parent
return obj return obj
} }

@ -6,17 +6,15 @@ import (
"html/template" "html/template"
"net/http" "net/http"
) )
//登陆界面 //登陆界面
func PageLogin(c *gin.Context){ func PageLogin(c *gin.Context) {
html := tools.FileGetContent("html/login.html") c.HTML(http.StatusOK, "login.html", nil)
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, html)
} }
//咨询界面 //咨询界面
func PageChat(c *gin.Context){ func PageChat(c *gin.Context) {
html := tools.FileGetContent("html/chat_page.html") c.HTML(http.StatusOK, "chat_page.html", nil)
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, html)
} }
func RenderLogin(w http.ResponseWriter, render interface{}) { func RenderLogin(w http.ResponseWriter, render interface{}) {
html := tools.FileGetContent("html/login.html") html := tools.FileGetContent("html/login.html")

@ -4,11 +4,12 @@ import "net/http"
type SettingHtml struct { type SettingHtml struct {
*CommonHtml *CommonHtml
Username,Password string Username, Password string
} }
func NewSettingHtml(w http.ResponseWriter)*SettingHtml{
obj:=new(SettingHtml) func NewSettingHtml(w http.ResponseWriter) *SettingHtml {
parent:=NewRender(w) obj := new(SettingHtml)
obj.CommonHtml=parent parent := NewRender(w)
obj.CommonHtml = parent
return obj return obj
} }

@ -63,6 +63,7 @@ func connect(server string, email string, password string) *client.Client {
} }
return c return c
} }
//获取邮件总数 //获取邮件总数
func GetMailNum(server string, email string, password string) map[string]int { func GetMailNum(server string, email string, password string) map[string]int {
var c *client.Client var c *client.Client
@ -90,6 +91,7 @@ func GetMailNum(server string, email string, password string) map[string]int {
} }
return folders return folders
} }
//获取邮件夹 //获取邮件夹
func GetFolders(server string, email string, password string, folder string) map[string]int { func GetFolders(server string, email string, password string, folder string) map[string]int {
var c *client.Client var c *client.Client
@ -143,9 +145,9 @@ func GetFolderMail(server string, email string, password string, folder string,
messages := make(chan *imap.Message, pagesize) messages := make(chan *imap.Message, pagesize)
done := make(chan error, 1) done := make(chan error, 1)
fetchItem:=imap.FetchItem(imap.FetchEnvelope) fetchItem := imap.FetchItem(imap.FetchEnvelope)
items := make([]imap.FetchItem,0) items := make([]imap.FetchItem, 0)
items=append(items,fetchItem) items = append(items, fetchItem)
go func() { go func() {
done <- c.Fetch(seqset, items, messages) done <- c.Fetch(seqset, items, messages)
}() }()
@ -196,7 +198,7 @@ func GetMessage(server string, email string, password string, folder string, id
seqSet.AddNum(id) seqSet.AddNum(id)
// Get the whole message body // Get the whole message body
section:= &imap.BodySectionName{} section := &imap.BodySectionName{}
items := []imap.FetchItem{section.FetchItem()} items := []imap.FetchItem{section.FetchItem()}
messages := make(chan *imap.Message, 1) messages := make(chan *imap.Message, 1)
@ -221,7 +223,6 @@ func GetMessage(server string, email string, password string, folder string, id
// Create a new mail reader // Create a new mail reader
mr, _ := mail.CreateReader(r) mr, _ := mail.CreateReader(r)
// Print some info about the message // Print some info about the message
header := mr.Header header := mr.Header
date, _ := header.Date() date, _ := header.Date()

@ -0,0 +1,25 @@
package tools
import (
"github.com/dgrijalva/jwt-go"
"time"
)
const SECRET = "taoshihan"
func MakeToken(obj map[string]interface{}) (string, error) {
obj["time"] = time.Now().Unix()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(obj))
tokenString, err := token.SignedString([]byte(SECRET))
return tokenString, err
}
func ParseToken(tokenStr string) map[string]interface{} {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (i interface{}, e error) {
return []byte(SECRET), nil
})
if err != nil {
return nil
}
finToken := token.Claims.(jwt.MapClaims)
return finToken
}

@ -1,8 +1,8 @@
package session package session
func Set(name string,value string){ func Set(name string, value string) {
} }
func Get(name string){ func Get(name string) {
} }

@ -6,17 +6,17 @@ import (
"strings" "strings"
) )
func Send(server string,from string,password string,to []string,subject string,body string)error{ func Send(server string, from string, password string, to []string, subject string, body string) error {
auth := sasl.NewPlainClient("", from, password) auth := sasl.NewPlainClient("", from, password)
msg := strings.NewReader( msg := strings.NewReader(
"From: "+from+"\r\n"+ "From: " + from + "\r\n" +
"To: "+strings.Join(to,",")+"\r\n"+ "To: " + strings.Join(to, ",") + "\r\n" +
"Subject: "+subject+"\r\n" + "Subject: " + subject + "\r\n" +
"\r\n" + "\r\n" +
body+"\r\n") body + "\r\n")
err := smtp.SendMail(server, auth, from, to, msg) err := smtp.SendMail(server, auth, from, to, msg)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }

@ -13,13 +13,15 @@ import (
"net/http" "net/http"
"strings" "strings"
) )
//获取URL的GET参数 //获取URL的GET参数
func GetUrlArg(r *http.Request,name string)string{ func GetUrlArg(r *http.Request, name string) string {
var arg string var arg string
values := r.URL.Query() values := r.URL.Query()
arg=values.Get(name) arg = values.Get(name)
return arg return arg
} }
// Reverse 将其实参字符串以符文为单位左右反转。 // Reverse 将其实参字符串以符文为单位左右反转。
func Reverse(s string) string { func Reverse(s string) string {
r := []rune(s) r := []rune(s)
@ -47,20 +49,22 @@ func DetermineEncoding(html string) (encoding.Encoding, string) {
e, name, _ := charset.DetermineEncoding([]byte(html), "") e, name, _ := charset.DetermineEncoding([]byte(html), "")
return e, name return e, name
} }
//获取文件内容,可以打包到二进制 //获取文件内容,可以打包到二进制
func FileGetContent(file string) string { func FileGetContent(file string) string {
str := "" str := ""
box := packr.New("tmpl","../static") box := packr.New("tmpl", "../static")
content, err := box.FindString(file) content, err := box.FindString(file)
if err != nil { if err != nil {
return str return str
} }
return content return content
} }
//md5加密 //md5加密
func Md5(src string)string{ func Md5(src string) string {
m:=md5.New() m := md5.New()
m.Write([]byte(src)) m.Write([]byte(src))
res:=hex.EncodeToString(m.Sum(nil)) res := hex.EncodeToString(m.Sum(nil))
return res return res
} }

@ -5,9 +5,9 @@ import "html/template"
type MailServer struct { type MailServer struct {
Server, Email, Password string Server, Email, Password string
} }
type ViewHtml struct{ type ViewHtml struct {
Header template.HTML Header template.HTML
Nav template.HTML Nav template.HTML
} }
type IndexData struct { type IndexData struct {
ViewHtml ViewHtml
@ -44,11 +44,11 @@ type JsonListResult struct {
JsonResult JsonResult
Result interface{} `json:"result"` Result interface{} `json:"result"`
} }
type SmtpBody struct{ type SmtpBody struct {
Smtp string Smtp string
From string From string
To []string To []string
Password string Password string
Subject string Subject string
Body string Body string
} }

Loading…
Cancel
Save