Skip to content

Commit

Permalink
(refactor): move scrap code
Browse files Browse the repository at this point in the history
  • Loading branch information
cookieY committed Jul 25, 2024
1 parent dc9763c commit e072c1e
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/handler/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func checkMeta(s, database, flag string) string {
}

func Highlight(s *model.CoreDataSource, isField string, dbName string) []map[string]string {
ps := enc.Decrypt(model.JWT, s.Password)
ps := enc.Decrypt(model.C.General.SecretKey, s.Password)
var list []map[string]string
db, err := model.NewDBSub(model.DSN{
Username: s.Username,
Expand Down
2 changes: 1 addition & 1 deletion src/handler/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func FetchSQLTest(c yee.Context) (err error) {
IP: s.IP,
Username: s.Username,
Port: s.Port,
Password: enc.Decrypt(model.JWT, s.Password),
Password: enc.Decrypt(model.C.General.SecretKey, s.Password),
CA: s.CAFile,
Cert: s.Cert,
Key: s.KeyFile,
Expand Down
4 changes: 2 additions & 2 deletions src/handler/login/oidcLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func oidcAuthUrl(c yee.Context) (oidcAuthUrl string, state string) {
// 生成 state,state 格式为 hs265签名.当前分钟数
func generateState() string {
curMinutes := strconv.FormatInt(time.Now().Unix()/60, 10)
sign, err := jwt.GetSigningMethod("HS256").Sign(curMinutes, []byte(model.JWT))
sign, err := jwt.GetSigningMethod("HS256").Sign(curMinutes, []byte(model.C.General.SecretKey))
if err != nil {
return ""
}
Expand All @@ -77,7 +77,7 @@ func validState(state string) error {
sign := split[0]
curMinutes := time.Now().Unix() / 60
for i := 0; i < 3; i++ {
err := jwt.GetSigningMethod("HS256").Verify(strconv.FormatInt(curMinutes-int64(i), 10), sign, []byte(model.JWT))
err := jwt.GetSigningMethod("HS256").Verify(strconv.FormatInt(curMinutes-int64(i), 10), sign, []byte(model.C.General.SecretKey))
if err == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions src/handler/manage/db/dbmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func ManageDBCreateOrEdit(c yee.Context) (err error) {
case "create":
return c.JSON(http.StatusOK, SuperCreateSource(&u.DB))
case "test":
if u.DB.Password != "" && enc.Decrypt(model.JWT, u.DB.Password) != "" {
u.DB.Password = enc.Decrypt(model.JWT, u.DB.Password)
if u.DB.Password != "" && enc.Decrypt(model.C.General.SecretKey, u.DB.Password) != "" {
u.DB.Password = enc.Decrypt(model.C.General.SecretKey, u.DB.Password)
}
return c.JSON(http.StatusOK, SuperTestDBConnect(&u.DB))
}
Expand Down
6 changes: 3 additions & 3 deletions src/handler/manage/db/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func ConnTest(u *model.CoreDataSource) error {

func SuperEditSource(source *model.CoreDataSource) common.Resp {

if source.Password != "" && enc.Decrypt(model.JWT, source.Password) == "" {
model.DB().Model(&model.CoreDataSource{}).Where("source_id =?", source.SourceId).Updates(&model.CoreDataSource{Password: enc.Encrypt(model.JWT, source.Password)})
if source.Password != "" && enc.Decrypt(model.C.General.SecretKey, source.Password) == "" {
model.DB().Model(&model.CoreDataSource{}).Where("source_id =?", source.SourceId).Updates(&model.CoreDataSource{Password: enc.Encrypt(model.C.General.SecretKey, source.Password)})
}
model.DB().Model(&model.CoreDataSource{}).Where("source_id =?", source.SourceId).Updates(map[string]interface{}{
"id_c": source.IDC,
Expand Down Expand Up @@ -89,7 +89,7 @@ func SuperEditSource(source *model.CoreDataSource) common.Resp {
}

func SuperCreateSource(source *model.CoreDataSource) common.Resp {
source.Password = enc.Encrypt(model.JWT, source.Password)
source.Password = enc.Encrypt(model.C.General.SecretKey, source.Password)
if source.Password != "" {
source.SourceId = uuid.New().String()
model.DB().Create(source)
Expand Down
2 changes: 1 addition & 1 deletion src/handler/order/audit/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ExecuteOrder(u *Confirm, user string) common.Resp {
IP: source.IP,
Port: source.Port,
Username: source.Username,
Password: enc.Decrypt(model.JWT, source.Password),
Password: enc.Decrypt(model.C.General.SecretKey, source.Password),
CA: source.CAFile,
Cert: source.Cert,
Key: source.KeyFile,
Expand Down
2 changes: 1 addition & 1 deletion src/handler/personal/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func SocketQueryResults(c yee.Context) (err error) {
core := new(queryCore)
var u model.CoreDataSource
model.DB().Where("source_id =?", args.SourceId).First(&u)
core.db, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4", u.Username, enc.Decrypt(model.JWT, u.Password), u.IP, u.Port))
core.db, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4", u.Username, enc.Decrypt(model.C.General.SecretKey, u.Password), u.IP, u.Port))
if err != nil {
c.Logger().Error(err)
_ = websocket.Message.Send(ws, lib.ToMsg(queryResults{Error: err.Error()}))
Expand Down
2 changes: 1 addition & 1 deletion src/handler/personal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CallAutoTask(order *model.CoreSqlOrder, length int) {
IP: source.IP,
Port: source.Port,
Username: source.Username,
Password: enc.Decrypt(model.JWT, source.Password),
Password: enc.Decrypt(model.C.General.SecretKey, source.Password),
Message: model.GloMessage,
MaxAffectRows: autoTask.Affectrow,
}, &isCall); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/jwtAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func JwtAuth(h Token) (t string, err error) {
claims["real_name"] = h.RealName
claims["is_record"] = h.IsRecord
claims["exp"] = time.Now().Add(time.Hour * 8).Unix()
t, err = token.SignedString([]byte(model.JWT))
t, err = token.SignedString([]byte(model.C.General.SecretKey))
if err != nil {
return "", errors.New("JWT Generate Failure")
}
Expand All @@ -52,13 +52,13 @@ func (h *Token) JwtParse(c yee.Context) *Token {

func WSTokenIsValid(token string) (bool, error) {
t, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte(model.JWT), nil
return []byte(model.C.General.SecretKey), nil
})
return t.Valid, err
}

func WsTokenParse(token string) (*jwt.Token, error) {
return jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte(model.JWT), nil
return []byte(model.C.General.SecretKey), nil
})
}
2 changes: 0 additions & 2 deletions src/model/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ var C Config

var DefaultLogger logger.Logger

var JWT = ""

var GloPer CoreGlobalConfiguration

var GloLdap Ldap
Expand Down
2 changes: 1 addition & 1 deletion src/model/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (l *ALdap) LdapConnect(user string, pass string, isTest bool) (isOk bool, e
}

func (s *CoreDataSource) ConnectDB(schema string) (*gorm.DB, error) {
ps := enc.Decrypt(JWT, s.Password)
ps := enc.Decrypt(C.General.SecretKey, s.Password)
if ps == "" {
return nil, errors.New("连接失败,密码解析错误!")
}
Expand Down
2 changes: 1 addition & 1 deletion src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func AddRouter(e *yee.Core) {
e.GET("/oidc/_token-login", login.OidcLogin)
e.GET("/oidc/state", login.OidcState)

r := e.Group("/api/v2", middleware.JWTWithConfig(middleware.JwtConfig{SigningKey: []byte(model.JWT), TokenLookup: []string{yee.HeaderAuthorization, yee.HeaderSecWebSocketProtocol}}))
r := e.Group("/api/v2", middleware.JWTWithConfig(middleware.JwtConfig{SigningKey: []byte(model.C.General.SecretKey), TokenLookup: []string{yee.HeaderAuthorization, yee.HeaderSecWebSocketProtocol}}))
r.POST("/chat", fetch.AiChat)
r.Restful("/common/:tp", personal.PersonalRestFulAPis())
r.Restful("/dash/:tp", apis.YearningDashApis())
Expand Down

0 comments on commit e072c1e

Please sign in to comment.