增加验证邮箱密码和cookie存储判断

pull/30/head
unknown 5 years ago
parent 8ee456bf46
commit c105ef7f1f

Binary file not shown.

@ -1,6 +1,8 @@
package main package main
import ( import (
"fmt"
"github.com/taoshihan1991/imaptool/tools"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -14,6 +16,7 @@ func main(){
//监听端口 //监听端口
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
//输出首页 //输出首页
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
auth := getCookie(r, "auth") auth := getCookie(r, "auth")
@ -23,11 +26,34 @@ func index(w http.ResponseWriter, r *http.Request){
t, _ := template.ParseFiles("./tmpl/index.html") t, _ := template.ParseFiles("./tmpl/index.html")
t.Execute(w, nil) t.Execute(w, nil)
} }
//登陆界面 //登陆界面
func login(w http.ResponseWriter, r *http.Request) { 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, _ := template.ParseFiles("./tmpl/login.html")
t.Execute(w, nil) t.Execute(w, nil)
} }
}
//加密cookie //加密cookie
//func authCookie(){ //func authCookie(){
// //

@ -73,15 +73,15 @@
</head> </head>
<body class="text-center"> <body class="text-center">
<form class="form-signin"> <form class="form-signin" action="/login" method="post">
<h1 class="h3 mb-3 font-weight-normal">邮箱IMAP工具</h1> <h1 class="h3 mb-3 font-weight-normal">邮箱IMAP工具</h1>
<label for="inputServer" class="sr-only">IMAP服务器:</label> <label for="inputServer" class="sr-only">IMAP服务器:</label>
<input type="email" id="inputServer" class="form-control" placeholder="IMAP服务器" required autofocus> <input type="text" name="server" id="inputServer" class="form-control" placeholder="IMAP服务器" required autofocus>
<label for="inputEmail" class="sr-only">邮箱地址:</label> <label for="inputEmail" class="sr-only">邮箱地址:</label>
<input type="email" id="inputEmail" class="form-control" placeholder="邮箱地址" required autofocus> <input type="email" id="inputEmail" name="email" class="form-control" placeholder="邮箱地址" required autofocus>
<label for="inputPassword" class="sr-only">密码:</label> <label for="inputPassword" class="sr-only">密码:</label>
<input type="password" id="inputPassword" class="form-control" placeholder="密码" required> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="密码" required>
{{if .}}<div><code>{{.}}</code></div>{{end}}
<button class="btn btn-lg btn-primary btn-block" type="submit">登陆</button> <button class="btn btn-lg btn-primary btn-block" type="submit">登陆</button>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p> <p class="mt-5 mb-3 text-muted">&copy; 2020</p>
</form> </form>

@ -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
}
Loading…
Cancel
Save