config.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package config
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/astaxie/beego"
  7. "github.com/astaxie/beego/logs"
  8. "github.com/astaxie/beego/orm"
  9. "golang.org/x/oauth2"
  10. "nginx-ui/server/models"
  11. "nginx-ui/server/utils"
  12. "os"
  13. "strings"
  14. )
  15. type AppConfig struct {
  16. Port int
  17. BaseApi string
  18. DataDir string
  19. DBDir string
  20. ContextPath string
  21. NginxPath string
  22. NginxDir string
  23. ThirdSession bool
  24. ThirdSessionName string
  25. ThirdSessionCheckUrl string
  26. }
  27. type CompleteOauth2Config struct {
  28. *oauth2.Config
  29. Userinfo string
  30. Enable bool
  31. }
  32. var OauthConfig = &CompleteOauth2Config{
  33. Enable: false,
  34. Config: &oauth2.Config{
  35. ClientID: "",
  36. ClientSecret: "",
  37. Endpoint: oauth2.Endpoint{
  38. AuthStyle: oauth2.AuthStyleInParams,
  39. },
  40. RedirectURL: "",
  41. Scopes: []string{},
  42. },
  43. }
  44. var Config = &AppConfig{}
  45. func GetDataDir() string {
  46. return Config.DataDir
  47. }
  48. func init() {
  49. beego.BConfig.CopyRequestBody = true
  50. mode := beego.AppConfig.DefaultString("runmode", "prod")
  51. beego.BConfig.RunMode = mode
  52. port := beego.AppConfig.DefaultInt("httpport", 38080)
  53. beego.BConfig.Listen.HTTPPort = port
  54. Config.Port = port
  55. // 需要和前端配置好
  56. baseApi := beego.AppConfig.DefaultString("baseApi", "/nginx-ui/api")
  57. baseApi = strings.TrimSuffix(baseApi, "/")
  58. Config.ContextPath = beego.AppConfig.DefaultString("contextpath", "/nginx-ui")
  59. Config.ContextPath = strings.TrimSuffix(Config.ContextPath, "/")
  60. Config.BaseApi = baseApi
  61. Config.DataDir = beego.AppConfig.DefaultString("datadir", "./data")
  62. Config.DBDir = beego.AppConfig.DefaultString("dbdir", "./data/db")
  63. if exist := utils.IsExist(Config.DataDir); exist == false {
  64. err := os.MkdirAll(Config.DataDir, 0777)
  65. logs.Warn("create data dir fail", err)
  66. if err != nil {
  67. panic(err)
  68. }
  69. }
  70. Config.NginxPath = beego.AppConfig.DefaultString("nginxPath", "/usr/sbin/nginx")
  71. Config.NginxDir = beego.AppConfig.DefaultString("nginxDir", "/etc/nginx")
  72. Config.ThirdSession = beego.AppConfig.DefaultBool("thirdsessionenable", false)
  73. Config.ThirdSessionName = beego.AppConfig.DefaultString("thirdsessionname", "")
  74. Config.ThirdSessionCheckUrl = beego.AppConfig.DefaultString("thirdsessioncheckurl", "")
  75. if Config.ThirdSession && (len(Config.ThirdSessionName) == 0 || len(Config.ThirdSessionCheckUrl) == 0) {
  76. logs.Warn("please config thirdsessionname and thirdsessioncheckurl, third session will skip!")
  77. Config.ThirdSession = false
  78. }
  79. OauthConfig.ClientID = beego.AppConfig.DefaultString("oauth2_client_id", "")
  80. OauthConfig.ClientSecret = beego.AppConfig.DefaultString("oauth2_client_secret", "")
  81. authorizeEndpoint := beego.AppConfig.DefaultString("oauth2_authorize_endpoint", "")
  82. tokenEndpoint := beego.AppConfig.DefaultString("oauth2_token_endpoint", "")
  83. authStyle := beego.AppConfig.DefaultInt("oauth2_auth_style", 1)
  84. OauthConfig.Endpoint = oauth2.Endpoint{
  85. AuthURL: authorizeEndpoint,
  86. TokenURL: tokenEndpoint,
  87. AuthStyle: oauth2.AuthStyle(authStyle),
  88. }
  89. OauthConfig.RedirectURL = beego.AppConfig.DefaultString("oauth2_redirect_uri", "")
  90. OauthConfig.Scopes = beego.AppConfig.DefaultStrings("oauth2_scopes", []string{})
  91. OauthConfig.Userinfo = beego.AppConfig.DefaultString("oauth2_userinfo", "")
  92. OauthConfig.Enable = beego.AppConfig.DefaultBool("oauth2_enable", false)
  93. // session 相关的配置
  94. sessionon := beego.AppConfig.DefaultBool("sessionon", true)
  95. beego.BConfig.WebConfig.Session.SessionOn = sessionon
  96. sessionprovider := beego.AppConfig.DefaultString("sessionprovider", "file")
  97. beego.BConfig.WebConfig.Session.SessionProvider = sessionprovider
  98. sessionproviderconfig := beego.AppConfig.DefaultString("sessionproviderconfig", "./data/sessions")
  99. beego.BConfig.WebConfig.Session.SessionProviderConfig = sessionproviderconfig
  100. sessiongcmaxlifetime := beego.AppConfig.DefaultInt("sessiongcmaxlifetime", 7200)
  101. beego.BConfig.WebConfig.Session.SessionCookieLifeTime = sessiongcmaxlifetime
  102. }
  103. func InitAdmin() {
  104. logs.Info("InitAdmin")
  105. o := orm.NewOrm()
  106. reset := beego.AppConfig.DefaultBool("reset_admin_password", false)
  107. admin := models.User{Account: "admin"}
  108. err := o.Read(&admin, "Account")
  109. if admin.Id > 0 && !reset {
  110. logs.Info("admin has exist and reset is false, skip")
  111. return
  112. }
  113. password := beego.AppConfig.DefaultString("admin_password", randPassword(10))
  114. admin.Password = utils.GetSHA256HashCode(password)
  115. admin.Remark = "admin"
  116. admin.Roles = "ADMIN"
  117. if admin.Id > 0 {
  118. _, err = o.Update(&admin)
  119. } else {
  120. _, err = o.Insert(&admin)
  121. }
  122. if err != nil {
  123. logs.Warn("insert or update admin fail", err)
  124. } else {
  125. logs.Warn(fmt.Sprintf("admin password is: %s", password))
  126. }
  127. }
  128. func randPassword(n int) string {
  129. b := make([]byte, n)
  130. _, err := rand.Read(b)
  131. if err != nil {
  132. return "123456"
  133. }
  134. return base64.StdEncoding.EncodeToString(b)
  135. }