-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add a login page * feat: Modify save rules, more secure * remove remoteAddr == "localhost" * "登录失败次数过多,请等待 %d 分钟后再试 * cookie remove secure * set cookie expires time by `NotAllowWanAccess` * prettier * fix: rename * feat: auto login if unfilled * feat: auto login if there is no username/password * auto login if no username/password
- Loading branch information
Showing
14 changed files
with
457 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function toggleTheme(write = false) { | ||
const docEle = document.documentElement; | ||
if (docEle.getAttribute("data-theme") === "dark") { | ||
docEle.removeAttribute("data-theme"); | ||
write && localStorage.setItem("theme", "light"); | ||
} else { | ||
docEle.setAttribute("data-theme", "dark"); | ||
write && localStorage.setItem("theme", "dark"); | ||
} | ||
} | ||
|
||
const theme = localStorage.getItem("theme") ?? | ||
(window.matchMedia("(prefers-color-scheme: dark)").matches | ||
? "dark" | ||
: "light"); | ||
|
||
if (theme === "dark") { | ||
toggleTheme(); | ||
} | ||
|
||
// 主题切换 | ||
document.getElementById("themeButton").addEventListener('click', () => toggleTheme(true)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package util | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// GenerateToken 生成Token | ||
func GenerateToken(username string) string { | ||
key := []byte(generateRandomKey()) | ||
h := hmac.New(sha256.New, key) | ||
msg := fmt.Sprintf("%s%d", username, time.Now().Unix()) | ||
h.Write([]byte(msg)) | ||
return base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
// generateRandomKey 生成随机密钥 | ||
func generateRandomKey() string { | ||
// 设置随机种子 | ||
source := rand.NewSource(time.Now().UnixNano()) | ||
random := rand.New(source) | ||
|
||
// 生成随机的64位整数 | ||
randomNumber := random.Uint64() | ||
|
||
return fmt.Sprint(randomNumber) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/jeessy2/ddns-go/v6/config" | ||
"github.com/jeessy2/ddns-go/v6/util" | ||
) | ||
|
||
// ViewFunc func | ||
type ViewFunc func(http.ResponseWriter, *http.Request) | ||
|
||
// Auth 验证Token是否已经通过 | ||
func Auth(f ViewFunc) ViewFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
tokenInCookie, err := r.Cookie("token") | ||
if err != nil { | ||
http.Redirect(w, r, "./login", http.StatusTemporaryRedirect) | ||
return | ||
} | ||
|
||
conf, _ := config.GetConfigCached() | ||
|
||
// 禁止公网访问 | ||
if conf.NotAllowWanAccess { | ||
if !util.IsPrivateNetwork(r.RemoteAddr) { | ||
w.WriteHeader(http.StatusForbidden) | ||
util.Log("%q 被禁止从公网访问", util.GetRequestIPStr(r)) | ||
return | ||
} | ||
} | ||
|
||
// 验证token | ||
if tokenInSystem != "" && tokenInSystem == tokenInCookie.Value { | ||
f(w, r) // 执行被装饰的函数 | ||
return | ||
} | ||
|
||
http.Redirect(w, r, "./login", http.StatusTemporaryRedirect) | ||
} | ||
} | ||
|
||
// AuthAssert 保护静态等文件不被公网访问 | ||
func AuthAssert(f ViewFunc) ViewFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
|
||
conf, err := config.GetConfigCached() | ||
|
||
// 配置文件为空, 启动时间超过3小时禁止从公网访问 | ||
if err != nil && | ||
time.Now().Unix()-startTime > 3*60*60 && !util.IsPrivateNetwork(r.RemoteAddr) { | ||
w.WriteHeader(http.StatusForbidden) | ||
util.Log("%q 配置文件为空, 超过3小时禁止从公网访问", util.GetRequestIPStr(r)) | ||
return | ||
} | ||
|
||
// 禁止公网访问 | ||
if conf.NotAllowWanAccess { | ||
if !util.IsPrivateNetwork(r.RemoteAddr) { | ||
w.WriteHeader(http.StatusForbidden) | ||
util.Log("%q 被禁止从公网访问", util.GetRequestIPStr(r)) | ||
return | ||
} | ||
} | ||
|
||
f(w, r) // 执行被装饰的函数 | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.