代码拉取完成,页面将自动刷新
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"github.com/nu7hatch/gouuid"
"io"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
func home(res http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie("session-id")
// cookie is not set
if err != nil {
id, _ := uuid.NewV4()
code := getCode(id.String())
val := code + "|" + id.String()
cookie = &http.Cookie{
Name: "session-id",
Value: val,
}
}
values := strings.Split(cookie.Value, "|")
code := getCode(values[1])
cookieCode := values[0]
fmt.Fprintln(res, code)
fmt.Fprintln(res, cookieCode)
if code != cookieCode {
fmt.Fprintln(res, "Cookie monsters says: someone's had their hands in my cookies!")
cookie = &http.Cookie{
Name: "session-id",
Value: "0",
MaxAge: -1,
}
}
http.SetCookie(res, cookie)
}
func getCode(data string) string {
h := hmac.New(sha256.New, []byte("ourkey"))
io.WriteString(h, data)
return fmt.Sprintf("%x", h.Sum(nil))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。