config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package config
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "crypto/x509"
  7. "encoding/json"
  8. "encoding/pem"
  9. "os"
  10. )
  11. var IsInit bool
  12. type Config struct {
  13. LogLevel string `json:"logLevel"` // 日志级别
  14. Domain string `json:"domain"`
  15. Domains []string `json:"domains"` //多域名设置,把所有收信域名都填进去
  16. WebDomain string `json:"webDomain"`
  17. DkimPrivateKeyPath string `json:"dkimPrivateKeyPath"`
  18. SSLType string `json:"sslType"` // 0表示自动生成证书,HTTP挑战模式,1表示用户上传证书,2表示自动-DNS挑战模式
  19. SSLPrivateKeyPath string `json:"SSLPrivateKeyPath"`
  20. SSLPublicKeyPath string `json:"SSLPublicKeyPath"`
  21. DbDSN string `json:"dbDSN"`
  22. DbType string `json:"dbType"`
  23. HttpsEnabled int `json:"httpsEnabled"` //后台页面是否启用https,0默认(启用),1启用,2不启用
  24. SpamFilterLevel int `json:"spamFilterLevel"` //垃圾邮件过滤级别,0不过滤、1 spf dkim 校验均失败时过滤,2 spf校验不通过时过滤
  25. HttpPort int `json:"httpPort"` //http服务端口设置,默认80
  26. HttpsPort int `json:"httpsPort"` //https服务端口,默认443
  27. WeChatPushAppId string `json:"weChatPushAppId"`
  28. WeChatPushSecret string `json:"weChatPushSecret"`
  29. WeChatPushTemplateId string `json:"weChatPushTemplateId"`
  30. WeChatPushUserId string `json:"weChatPushUserId"`
  31. TgBotToken string `json:"tgBotToken"`
  32. TgChatId string `json:"tgChatId"`
  33. IsInit bool `json:"isInit"`
  34. WebPushUrl string `json:"webPushUrl"`
  35. WebPushToken string `json:"webPushToken"`
  36. Tables map[string]string `json:"-"`
  37. TablesInitData map[string]string `json:"-"`
  38. setupPort int // 初始化阶段端口
  39. }
  40. func (c *Config) GetSetupPort() int {
  41. return c.setupPort
  42. }
  43. func (c *Config) SetSetupPort(setupPort int) {
  44. c.setupPort = setupPort
  45. }
  46. const DBTypeMySQL = "mysql"
  47. const DBTypeSQLite = "sqlite"
  48. const DBTypePostgres = "postgres"
  49. const SSLTypeAutoHTTP = "0" //自动生成证书
  50. const SSLTypeAutoDNS = "2" //自动生成证书,DNS api验证
  51. const SSLTypeUser = "1" //用户上传证书
  52. var DBTypes []string = []string{DBTypeMySQL, DBTypeSQLite, DBTypePostgres}
  53. var Instance *Config = &Config{}
  54. func Init() {
  55. var cfgData []byte
  56. var err error
  57. args := os.Args
  58. if len(args) >= 2 && args[len(args)-1] == "dev" {
  59. cfgData, err = os.ReadFile("./config/config.dev.json")
  60. if err != nil {
  61. return
  62. }
  63. } else {
  64. cfgData, err = os.ReadFile("./config/config.json")
  65. if err != nil {
  66. return
  67. }
  68. }
  69. err = json.Unmarshal(cfgData, &Instance)
  70. if err != nil {
  71. return
  72. }
  73. if len(Instance.Domains) == 0 && Instance.Domain != "" {
  74. Instance.Domains = []string{Instance.Domain}
  75. }
  76. if Instance.Domain != "" && Instance.IsInit {
  77. IsInit = true
  78. }
  79. }
  80. func ReadPrivateKey() (*ecdsa.PrivateKey, bool) {
  81. key, err := os.ReadFile("./config/ssl/account_private.pem")
  82. if err != nil {
  83. return createNewPrivateKey(), true
  84. }
  85. block, _ := pem.Decode(key)
  86. x509Encoded := block.Bytes
  87. privateKey, _ := x509.ParseECPrivateKey(x509Encoded)
  88. return privateKey, false
  89. }
  90. func createNewPrivateKey() *ecdsa.PrivateKey {
  91. // Create a user. New accounts need an email and private key to start.
  92. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  93. if err != nil {
  94. panic(err)
  95. }
  96. x509Encoded, _ := x509.MarshalECPrivateKey(privateKey)
  97. // 将ec 密钥写入到 pem文件里
  98. keypem, _ := os.OpenFile("./config/ssl/account_private.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
  99. err = pem.Encode(keypem, &pem.Block{Type: "EC PRIVATE KEY", Bytes: x509Encoded})
  100. if err != nil {
  101. panic(err)
  102. }
  103. return privateKey
  104. }