Skip to content

Commit

Permalink
chore: the time limit is set to 30 minutes (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 authored Jul 31, 2024
1 parent 997cdb8 commit bdeda6c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
4 changes: 0 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ func GetConfigCached() (conf Config, err error) {

// CompatibleConfig 兼容之前的配置文件
func (conf *Config) CompatibleConfig() {
// 如配置文件不为空, 兼容之前的语言为中文
if conf.Lang == "" {
conf.Lang = "zh"
}

// 如果之前密码不为空且不是bcrypt加密后的密码, 把密码加密并保存
if conf.Password != "" && !util.IsHashedPassword(conf.Password) {
Expand Down
4 changes: 2 additions & 2 deletions static/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const I18N_MAP = {
'Deny from WAN': 'Deny from WAN',
'NotAllowWanAccessHelp': 'Default enabled, can prohibit access to this page from the public network',
'Username': 'Username',
'accountHelp': 'Please enter to protect your information security',
'accountHelp': 'Username/Password is required',
'passwordHelp': 'If you need to change the password, please enter it here',
'Password': 'Password',
'WebhookURLHelp': `
Expand Down Expand Up @@ -257,7 +257,7 @@ const I18N_MAP = {
'Deny from WAN': '禁止公网访问',
'NotAllowWanAccessHelp': '默认启用, 可禁止从公网访问本页面',
'Username': '用户名',
'accountHelp': '为保护你的信息安全,建议输入',
'accountHelp': '必须输入用户名/密码',
'passwordHelp': '如需修改密码,请在此处输入新密码',
'Password': '密码',
'WebhookURLHelp': `
Expand Down
7 changes: 4 additions & 3 deletions util/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func init() {
message.SetString(language.English, "Callback调用失败, 异常信息: %s", "Webhook called failed! Exception: %s")

// save
message.SetString(language.English, "请在ddns-go启动后 10 分钟内完成初始化配置", "Please initialize configuration within 10 minutes after ddns-go starts")
message.SetString(language.English, "之前未设置帐号密码, 仅允许在ddns-go启动后 10 分钟内设置, 请重启ddns-go", "The username/password has not been set before, only allowed to set within 10 minutes after ddns-go starts, please restart ddns-go")
message.SetString(language.English, "必须输入登录用户名/密码", "Must enter login username/password")
message.SetString(language.English, "请在ddns-go启动后 %d 分钟内完成初始化配置", "Please initialize configuration within %d minutes after ddns-go starts")
message.SetString(language.English, "之前未设置帐号密码, 仅允许在ddns-go启动后 %d 分钟内设置, 请重启ddns-go", "The username/password has not been set before, only allowed to set within %d minutes after ddns-go starts, please restart ddns-go")
message.SetString(language.English, "必须输入用户名/密码", "Username/Password is required")
message.SetString(language.English, "密码不安全!尝试使用更复杂的密码", "Password is not secure! Try using a more complex password")
message.SetString(language.English, "数据解析失败, 请刷新页面重试", "Data parsing failed, please refresh the page and try again")
message.SetString(language.English, "第 %s 个配置未填写域名", "The %s config does not fill in the domain")
Expand Down Expand Up @@ -117,6 +117,7 @@ func init() {
message.SetString(language.English, "用户名或密码错误", "Username or password is incorrect")
message.SetString(language.English, "登录失败次数过多,请等待 %d 分钟后再试", "Too many login failures, please try again after %d minutes")
message.SetString(language.English, "用户名 %s 的密码已重置成功! 请重启ddns-go", "The password of username %s has been reset successfully! Please restart ddns-go")
message.SetString(language.English, "请在 %s 之前完成用户名密码设置", "Please complete the username and password setting before %s")

}

Expand Down
2 changes: 1 addition & 1 deletion web/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func AuthAssert(f ViewFunc) ViewFunc {

// 配置文件为空, 启动时间超过3小时禁止从公网访问
if err != nil &&
time.Now().Unix()-startTime > 3*60*60 && !util.IsPrivateNetwork(r.RemoteAddr) {
time.Since(startTime) > time.Duration(3*time.Hour) && !util.IsPrivateNetwork(r.RemoteAddr) {
w.WriteHeader(http.StatusForbidden)
util.Log("%q 配置文件为空, 超过3小时禁止从公网访问", util.GetRequestIPStr(r))
return
Expand Down
8 changes: 8 additions & 0 deletions web/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Login(writer http.ResponseWriter, request *http.Request) {

// LoginFunc login func
func LoginFunc(w http.ResponseWriter, r *http.Request) {
accept := r.Header.Get("Accept-Language")
util.InitLogLang(accept)

if ld.failedTimes >= 5 {
lockMinute := loginUnlock()
Expand Down Expand Up @@ -99,6 +101,12 @@ func LoginFunc(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, cookieInSystem)

util.Log("%q 登陆成功", util.GetRequestIPStr(r))

// 提示在服务启动时间内完成初始化配置
if conf.Username == "" && conf.Password == "" {
util.Log("请在 %s 之前完成用户名密码设置", startTime.Add(saveLimit).Format("2006-01-02 15:04:05"))
}

returnOK(w, util.LogStr("登陆成功"), cookieInSystem.Value)
return
}
Expand Down
2 changes: 1 addition & 1 deletion web/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ <h5 data-i18n="Login" class="portlet__head">Login</h5>
showMessage({
content: i18n({
"en": "You have not set up a user yet. Please click Login button directly and be sure to set up your username and password immediately after logging in.",
"zh-cn": "尚未设置用户, 请直接点击登录,登录后请立即设置用户名和密码",
"zh-cn": "尚未设置用户请直接点击登录,登录后请立即设置用户名和密码",
}),
type: "info",
duration: 300000,
Expand Down
18 changes: 11 additions & 7 deletions web/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
"github.com/jeessy2/ddns-go/v6/util"
)

var startTime = time.Now().Unix()
// 服务启动时间
var startTime = time.Now()

// 保存限制时间
var saveLimit = time.Duration(30 * time.Minute)

// Save 保存
func Save(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -53,15 +57,15 @@ func checkAndSave(request *http.Request) string {
accept := request.Header.Get("Accept-Language")
conf.Lang = util.InitLogLang(accept)

// 首次设置 && 必须在服务启动的 10 分钟内
if time.Now().Unix()-startTime > 10*60 {
// 首次设置 && 限制时间
if time.Since(startTime) > saveLimit {
if firstTime {
return util.LogStr("请在ddns-go启动后 10 分钟内完成初始化配置")
return util.LogStr("请在ddns-go启动后 %d 分钟内完成初始化配置", int(saveLimit.Minutes()))
}
// 之前未设置帐号密码 && 本次设置了帐号或密码 必须在10分钟内
// 之前未设置帐号密码 && 本次设置了帐号或密码 必须在30分钟内
if (conf.Username == "" && conf.Password == "") &&
(usernameNew != "" || passwordNew != "") {
return util.LogStr("之前未设置帐号密码, 仅允许在ddns-go启动后 10 分钟内设置, 请重启ddns-go")
return util.LogStr("之前未设置帐号密码, 仅允许在ddns-go启动后 %d 分钟内设置, 请重启ddns-go", int(saveLimit.Minutes()))
}
}

Expand All @@ -82,7 +86,7 @@ func checkAndSave(request *http.Request) string {

// 帐号密码不能为空
if conf.Username == "" || conf.Password == "" {
return util.LogStr("必须输入登录用户名/密码")
return util.LogStr("必须输入用户名/密码")
}

dnsConfFromJS := data.DnsConf
Expand Down

0 comments on commit bdeda6c

Please sign in to comment.