common.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package admin
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "time"
  6. "github.com/bjdgyc/anylink/base"
  7. "github.com/bjdgyc/anylink/dbdata"
  8. "github.com/dgrijalva/jwt-go"
  9. mail "github.com/xhit/go-simple-mail/v2"
  10. // "github.com/mojocn/base64Captcha"
  11. )
  12. func SetJwtData(data map[string]interface{}, expiresAt int64) (string, error) {
  13. jwtData := jwt.MapClaims{"exp": expiresAt}
  14. for k, v := range data {
  15. jwtData[k] = v
  16. }
  17. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtData)
  18. // Sign and get the complete encoded token as a string using the secret
  19. tokenString, err := token.SignedString([]byte(base.Cfg.JwtSecret))
  20. return tokenString, err
  21. }
  22. func GetJwtData(jwtToken string) (map[string]interface{}, error) {
  23. token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
  24. // since we only use the one private key to sign the tokens,
  25. // we also only use its public counter part to verify
  26. return []byte(base.Cfg.JwtSecret), nil
  27. })
  28. if err != nil || !token.Valid {
  29. return nil, err
  30. }
  31. claims, ok := token.Claims.(jwt.MapClaims)
  32. if !ok {
  33. return nil, errors.New("data is parse err")
  34. }
  35. return claims, nil
  36. }
  37. func SendMail(subject, to, htmlBody string) error {
  38. dataSmtp := &dbdata.SettingSmtp{}
  39. err := dbdata.SettingGet(dataSmtp)
  40. if err != nil {
  41. base.Error(err)
  42. return err
  43. }
  44. server := mail.NewSMTPClient()
  45. // SMTP Server
  46. server.Host = dataSmtp.Host
  47. server.Port = dataSmtp.Port
  48. server.Username = dataSmtp.Username
  49. server.Password = dataSmtp.Password
  50. switch dataSmtp.Encryption {
  51. case "SSLTLS":
  52. server.Encryption = mail.EncryptionSSLTLS
  53. case "STARTTLS":
  54. server.Encryption = mail.EncryptionSTARTTLS
  55. default:
  56. server.Encryption = mail.EncryptionNone
  57. }
  58. // Since v2.3.0 you can specified authentication type:
  59. // - PLAIN (default)
  60. // - LOGIN
  61. // - CRAM-MD5
  62. server.Authentication = mail.AuthPlain
  63. // Variable to keep alive connection
  64. server.KeepAlive = false
  65. // Timeout for connect to SMTP Server
  66. server.ConnectTimeout = 10 * time.Second
  67. // Timeout for send the data and wait respond
  68. server.SendTimeout = 10 * time.Second
  69. // Set TLSConfig to provide custom TLS configuration. For example,
  70. // to skip TLS verification (useful for testing):
  71. server.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  72. // SMTP client
  73. smtpClient, err := server.Connect()
  74. if err != nil {
  75. base.Error(err)
  76. return err
  77. }
  78. // New email simple html with inline and CC
  79. email := mail.NewMSG()
  80. email.SetFrom(dataSmtp.From).
  81. AddTo(to).
  82. SetSubject(subject)
  83. email.SetBody(mail.TextHTML, htmlBody)
  84. // Call Send and pass the client
  85. err = email.Send(smtpClient)
  86. if err != nil {
  87. base.Error(err)
  88. }
  89. return err
  90. }