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

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

Binary file not shown.

@ -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--
}

@ -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
}
}

@ -73,15 +73,15 @@
</head>
<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>
<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>
<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>
<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>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p>
</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