Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复邮件发送密码为密文密码的Bug #357

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/admin/api_uploaduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func UploadUser(file string) error {
if err := dbdata.AddBatch(user); err != nil {
return fmt.Errorf("请检查第%d行数据是否导入有重复用户", index)
}
user.PinCode = row[4]
if user.SendEmail {
if err := userAccountMail(user); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions server/admin/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/bjdgyc/anylink/base"
"github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/pkg/utils"
"github.com/bjdgyc/anylink/sessdata"
"github.com/skip2/go-qrcode"
mail "github.com/xhit/go-simple-mail/v2"
Expand Down Expand Up @@ -98,11 +99,17 @@ func UserSet(w http.ResponseWriter, r *http.Request) {
return
}

if len(data.PinCode) < 6 {
data.PinCode = utils.RandomRunes(8)
base.Info("用户", data.Username, "随机密码为:", data.PinCode)
}
plainpwd := data.PinCode
err = dbdata.SetUser(data)
if err != nil {
RespError(w, RespInternalErr, err)
return
}
data.PinCode = plainpwd

// 发送邮件
if data.SendEmail {
Expand Down
10 changes: 6 additions & 4 deletions server/base/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package base

import (
"fmt"
"github.com/bjdgyc/anylink/pkg/utils"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/bjdgyc/anylink/pkg/utils"
)

const (
Expand Down Expand Up @@ -85,9 +86,10 @@ type ServerConfig struct {
Compression bool `json:"compression"` // bool
NoCompressLimit int `json:"no_compress_limit"` // int

DisplayError bool `json:"display_error"`
ExcludeExportIp bool `json:"exclude_export_ip"`
AuthAloneOtp bool `json:"auth_alone_otp"`
DisplayError bool `json:"display_error"`
ExcludeExportIp bool `json:"exclude_export_ip"`
AuthAloneOtp bool `json:"auth_alone_otp"`
EncryptionPassword bool `json:"encryption_password"`

AntiBruteForce bool `json:"anti_brute_force"`
IPWhitelist string `json:"ip_whitelist"`
Expand Down
1 change: 1 addition & 0 deletions server/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var configs = []config{
{Typ: cfgBool, Name: "display_error", Usage: "客户端显示详细错误信息(线上环境慎开启)", ValBool: false},
{Typ: cfgBool, Name: "exclude_export_ip", Usage: "排除出口ip路由(出口ip不加密传输)", ValBool: true},
{Typ: cfgBool, Name: "auth_alone_otp", Usage: "登录单独验证OTP窗口", ValBool: false},
{Typ: cfgBool, Name: "encryption_password", Usage: "用户密码是否加密保存", ValBool: false},

{Typ: cfgBool, Name: "anti_brute_force", Usage: "是否开启防爆功能", ValBool: true},
{Typ: cfgStr, Name: "ip_whitelist", Usage: "全局IP白名单,多个用逗号分隔,支持单IP和CIDR范围", ValStr: "192.168.90.1,172.16.0.0/24"},
Expand Down
5 changes: 2 additions & 3 deletions server/conf/server-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ exclude_export_ip = true
#登录单独验证OTP窗口
auth_alone_otp = false

#加密保存用户密码
encryption_password = false

#防爆破全局开关
anti_brute_force = true
Expand Down Expand Up @@ -147,6 +149,3 @@ global_ip_lock_time = 300

#全局锁定状态的保存生命周期(秒),超过则删除记录
global_lock_state_expiration_time = 3600



14 changes: 8 additions & 6 deletions server/dbdata/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func checkLocalUser(name, pwd, group string, ext map[string]interface{}) error {
}

pinCode := pwd
if base.Cfg.AuthAloneOtp == false {
if !base.Cfg.AuthAloneOtp {
// 判断otp信息
if !v.DisableOtp {
pinCode = pwd[:pl-6]
Expand Down Expand Up @@ -207,16 +207,18 @@ func CheckOtp(name, otp, secret string) bool {

// 插入数据库前加密密码
func (u *User) BeforeInsert() {
hashedPassword, err := utils.PasswordHash(u.PinCode)
if err != nil {
base.Error(err)
if base.Cfg.EncryptionPassword {
hashedPassword, err := utils.PasswordHash(u.PinCode)
if err != nil {
base.Error(err)
}
u.PinCode = hashedPassword
}
u.PinCode = hashedPassword
}

// 更新数据库前加密密码
func (u *User) BeforeUpdate() {
if len(u.PinCode) != 60 {
if len(u.PinCode) != 60 && base.Cfg.EncryptionPassword {
hashedPassword, err := utils.PasswordHash(u.PinCode)
if err != nil {
base.Error(err)
Expand Down
121 changes: 0 additions & 121 deletions server/handler/antiBruteForce.go

This file was deleted.

6 changes: 0 additions & 6 deletions server/handler/link_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
ext := map[string]interface{}{"mac_addr": cr.MacAddressList.MacAddress}
err = dbdata.CheckUser(cr.Auth.Username, cr.Auth.Password, cr.GroupSelect, ext)
if err != nil {
// lockManager.LoginStatus.Store(loginStatusKey, false) // 记录登录失败状态
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
// hc.LoginStatus = false
lockManager.UpdateLoginStatus(cr.Auth.Username, r.RemoteAddr, false) // 记录登录失败状态

base.Warn(err, r.RemoteAddr)
Expand All @@ -131,9 +128,6 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
}
// 用户otp验证
if base.Cfg.AuthAloneOtp && !v.DisableOtp {
// lockManager.LoginStatus.Store(loginStatusKey, true) // 重置OTP验证计数
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
// hc.LoginStatus = true
lockManager.UpdateLoginStatus(cr.Auth.Username, r.RemoteAddr, true) // 重置OTP验证计数

sessionID, err := GenerateSessionID()
Expand Down
11 changes: 0 additions & 11 deletions server/handler/link_auth_otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func DeleteCookie(w http.ResponseWriter, name string) {
http.SetCookie(w, cookie)
}
func CreateSession(w http.ResponseWriter, r *http.Request, authSession *AuthSession) {
// lockManager.LoginStatus.Store(loginStatusKey, true) // 更新登录成功状态
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
// hc.LoginStatus = true
cr := authSession.ClientRequest
ua := authSession.UserActLog

Expand Down Expand Up @@ -208,14 +205,6 @@ func LinkAuth_otp(w http.ResponseWriter, r *http.Request) {

// 动态码错误
if !dbdata.CheckOtp(username, otp, otpSecret) {
// if sessionData.AddOtpErrCount(1) > maxOtpErrCount {
// SessStore.DeleteAuthSession(sessionID)
// http.Error(w, "TooManyError, please login again", http.StatusBadRequest)
// return
// }
// lockManager.LoginStatus.Store(loginStatusKey, false) // 记录登录失败状态
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
// hc.LoginStatus = false
lockManager.UpdateLoginStatus(username, r.RemoteAddr, false) // 记录登录失败状态

base.Warn("OTP 动态码错误", username, r.RemoteAddr)
Expand Down
Loading