diff --git a/imaptool.exe b/imaptool.exe deleted file mode 100644 index aa95d37..0000000 Binary files a/imaptool.exe and /dev/null differ diff --git a/imaptool.go b/imaptool.go index e4ea4ed..216467f 100644 --- a/imaptool.go +++ b/imaptool.go @@ -104,11 +104,11 @@ func main() { log.Println("读取最新的几封信(all全部):") inLine = readLineFromInput() var maxNum uint32 - if inLine=="all"{ - maxNum=mbox.Messages - }else { + if inLine == "all" { + maxNum = mbox.Messages + } else { tempNum, _ := strconv.Atoi(inLine) - maxNum=uint32(tempNum) + maxNum = uint32(tempNum) } from := uint32(1) @@ -130,7 +130,7 @@ func main() { log.Printf("最新的 %d 封信:", maxNum) for msg := range messages { - log.Printf("* %d:%s\n" ,to,msg.Envelope.Subject) + log.Printf("* %d:%s\n", to, msg.Envelope.Subject) to-- } diff --git a/server.go b/server.go index 9733790..f4b2e42 100644 --- a/server.go +++ b/server.go @@ -1,42 +1,68 @@ package main import ( + "fmt" + "github.com/taoshihan1991/imaptool/tools" "html/template" "log" "net/http" ) -func main(){ +func main() { log.Println("listen on 8080...") - http.HandleFunc("/",index) + http.HandleFunc("/", index) //登陆界面 - http.HandleFunc("/login",login) + http.HandleFunc("/login", login) //监听端口 http.ListenAndServe(":8080", nil) } + //输出首页 -func index(w http.ResponseWriter, r *http.Request){ - auth:=getCookie(r,"auth") - if auth=="" { +func index(w http.ResponseWriter, r *http.Request) { + auth := getCookie(r, "auth") + if auth == "" { http.Redirect(w, r, "/login", 302) } - t,_:=template.ParseFiles("./tmpl/index.html") + t, _ := template.ParseFiles("./tmpl/index.html") t.Execute(w, nil) } + //登陆界面 -func login(w http.ResponseWriter, r *http.Request){ - t,_:=template.ParseFiles("./tmpl/login.html") - t.Execute(w, nil) +func login(w http.ResponseWriter, r *http.Request) { + email := r.PostFormValue("email") + server := r.PostFormValue("server") + password := r.PostFormValue("password") + var errStr string + if email != "" && server != "" && password != "" { + res := tools.CheckEmailPassword(server, email, password) + if !res { + errStr = "连接或验证失败" + t, _ := template.ParseFiles("./tmpl/login.html") + t.Execute(w, errStr) + } else { + auth := fmt.Sprintf("%s:%s:%s", server, email, password) + cookie := http.Cookie{ + Name: "auth", + Value: auth, + } + http.SetCookie(w, &cookie) + http.Redirect(w, r, "/", 302) + } + } else { + t, _ := template.ParseFiles("./tmpl/login.html") + t.Execute(w, nil) + } } + //加密cookie //func authCookie(){ // //} //获取cookie -func getCookie(r *http.Request,name string)string{ - cookies:=r.Cookies() - for _,cookie:=range cookies{ - if cookie.Name==name{ +func getCookie(r *http.Request, name string) string { + cookies := r.Cookies() + for _, cookie := range cookies { + if cookie.Name == name { return cookie.Value } } diff --git a/tmpl/login.html b/tmpl/login.html index 961dff9..ffc78b9 100644 --- a/tmpl/login.html +++ b/tmpl/login.html @@ -73,15 +73,15 @@ -
+

邮箱IMAP工具

- + - + - - + + {{if .}}
{{.}}
{{end}}

© 2020

diff --git a/tools/imap.go b/tools/imap.go new file mode 100644 index 0000000..a9de7ad --- /dev/null +++ b/tools/imap.go @@ -0,0 +1,39 @@ +package tools + +import ( + "fmt" + "github.com/emersion/go-imap/client" + "strconv" + "strings" +) + +func CheckEmailPassword(server string, email string, password string) bool { + if !strings.Contains(server, ":") { + return false + } + var c *client.Client + var err error + serverSlice := strings.Split(server, ":") + uri := serverSlice[0] + port, _ := strconv.Atoi(serverSlice[1]) + if port != 993 && port != 143 { + return false + } + if port == 993 { + c, err = client.DialTLS(fmt.Sprintf("%s:%d", uri, port), nil) + } else { + c, err = client.Dial(fmt.Sprintf("%s:%d", uri, port)) + } + if err != nil { + return false + } + + // 不要忘了退出 + defer c.Logout() + + // 登陆 + if err := c.Login(email, password); err != nil { + return false + } + return true +}