config.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "log"
  11. "nginx-ui/server/models"
  12. "nginx-ui/server/utils"
  13. "os"
  14. "strings"
  15. )
  16. type DbConfig struct {
  17. Host string `json:"host"`
  18. Port string `json:"port"`
  19. User string `json:"user"`
  20. Pass string `json:"pass"`
  21. Name string `json:"name"`
  22. }
  23. type AppConfig struct {
  24. Port int
  25. BaseApi string
  26. DataDir string
  27. DBDir string
  28. ContextPath string
  29. NginxPath string
  30. NginxDir string
  31. ThirdSession bool
  32. ThirdSessionName string
  33. ThirdSessionCheckUrl string
  34. DbType string
  35. DbConfig DbConfig
  36. }
  37. type CompleteOauth2Config struct {
  38. *oauth2.Config
  39. Userinfo string
  40. Enable bool
  41. }
  42. var OauthConfig = &CompleteOauth2Config{
  43. Enable: false,
  44. Config: &oauth2.Config{
  45. ClientID: "",
  46. ClientSecret: "",
  47. Endpoint: oauth2.Endpoint{
  48. AuthStyle: oauth2.AuthStyleInParams,
  49. },
  50. RedirectURL: "",
  51. Scopes: []string{},
  52. },
  53. }
  54. var Config = &AppConfig{}
  55. func GetDataDir() string {
  56. return Config.DataDir
  57. }
  58. func init() {
  59. localConf := "./conf/app.local.conf"
  60. if utils.IsExist(localConf) {
  61. err := beego.LoadAppConfig("ini", localConf)
  62. if err != nil {
  63. log.Panic(err)
  64. }
  65. }
  66. beego.BConfig.CopyRequestBody = true
  67. mode := beego.AppConfig.DefaultString("runmode", "prod")
  68. beego.BConfig.RunMode = mode
  69. port := beego.AppConfig.DefaultInt("httpport", 38080)
  70. beego.BConfig.Listen.HTTPPort = port
  71. Config.Port = port
  72. // 需要和前端配置好
  73. baseApi := beego.AppConfig.DefaultString("baseApi", "/nginx-ui/api")
  74. baseApi = strings.TrimSuffix(baseApi, "/")
  75. Config.ContextPath = beego.AppConfig.DefaultString("contextpath", "/nginx-ui")
  76. Config.ContextPath = strings.TrimSuffix(Config.ContextPath, "/")
  77. Config.BaseApi = baseApi
  78. Config.DataDir = beego.AppConfig.DefaultString("datadir", "./data")
  79. // DB
  80. Config.DBDir = beego.AppConfig.DefaultString("dbdir", "./data/db")
  81. if exist := utils.IsExist(Config.DataDir); exist == false {
  82. err := os.MkdirAll(Config.DataDir, 0777)
  83. logs.Warn("create data dir fail", err)
  84. if err != nil {
  85. panic(err)
  86. }
  87. }
  88. dbType := beego.AppConfig.DefaultString("db_type", "sqlite")
  89. Config.DbType = dbType
  90. if dbType != "sqlite" {
  91. Config.DbConfig = DbConfig{
  92. Host: beego.AppConfig.DefaultString("db_host", "127.0.0.1"),
  93. Port: beego.AppConfig.DefaultString("db_port", "3306"),
  94. User: beego.AppConfig.DefaultString("db_user", "nginx"),
  95. Pass: beego.AppConfig.DefaultString("db_password", "nginx"),
  96. Name: beego.AppConfig.DefaultString("db_name", "nginx-ui"),
  97. }
  98. }
  99. Config.NginxPath = beego.AppConfig.DefaultString("nginxPath", "/usr/sbin/nginx")
  100. Config.NginxDir = beego.AppConfig.DefaultString("nginxDir", "/etc/nginx")
  101. Config.ThirdSession = beego.AppConfig.DefaultBool("thirdsessionenable", false)
  102. Config.ThirdSessionName = beego.AppConfig.DefaultString("thirdsessionname", "")
  103. Config.ThirdSessionCheckUrl = beego.AppConfig.DefaultString("thirdsessioncheckurl", "")
  104. if Config.ThirdSession && (len(Config.ThirdSessionName) == 0 || len(Config.ThirdSessionCheckUrl) == 0) {
  105. logs.Warn("please config thirdsessionname and thirdsessioncheckurl, third session will skip!")
  106. Config.ThirdSession = false
  107. }
  108. OauthConfig.ClientID = beego.AppConfig.DefaultString("oauth2_client_id", "")
  109. OauthConfig.ClientSecret = beego.AppConfig.DefaultString("oauth2_client_secret", "")
  110. authorizeEndpoint := beego.AppConfig.DefaultString("oauth2_authorize_endpoint", "")
  111. tokenEndpoint := beego.AppConfig.DefaultString("oauth2_token_endpoint", "")
  112. authStyle := beego.AppConfig.DefaultInt("oauth2_auth_style", 1)
  113. OauthConfig.Endpoint = oauth2.Endpoint{
  114. AuthURL: authorizeEndpoint,
  115. TokenURL: tokenEndpoint,
  116. AuthStyle: oauth2.AuthStyle(authStyle),
  117. }
  118. OauthConfig.RedirectURL = beego.AppConfig.DefaultString("oauth2_redirect_uri", "")
  119. OauthConfig.Scopes = beego.AppConfig.DefaultStrings("oauth2_scopes", []string{})
  120. OauthConfig.Userinfo = beego.AppConfig.DefaultString("oauth2_userinfo", "")
  121. OauthConfig.Enable = beego.AppConfig.DefaultBool("oauth2_enable", false)
  122. // session 相关的配置
  123. sessionon := beego.AppConfig.DefaultBool("sessionon", true)
  124. beego.BConfig.WebConfig.Session.SessionOn = sessionon
  125. sessionprovider := beego.AppConfig.DefaultString("sessionprovider", "file")
  126. beego.BConfig.WebConfig.Session.SessionProvider = sessionprovider
  127. sessionproviderconfig := beego.AppConfig.DefaultString("sessionproviderconfig", "./data/sessions")
  128. beego.BConfig.WebConfig.Session.SessionProviderConfig = sessionproviderconfig
  129. sessiongcmaxlifetime := beego.AppConfig.DefaultInt("sessiongcmaxlifetime", 7200)
  130. beego.BConfig.WebConfig.Session.SessionCookieLifeTime = sessiongcmaxlifetime
  131. }
  132. func InitAdmin() {
  133. logs.Info("InitAdmin")
  134. o := orm.NewOrm()
  135. reset := beego.AppConfig.DefaultBool("reset_admin_password", false)
  136. admin := models.User{Account: "admin"}
  137. err := o.Read(&admin, "Account")
  138. if admin.Id > 0 && !reset {
  139. logs.Info("admin has exist and reset is false, skip")
  140. return
  141. }
  142. password := beego.AppConfig.DefaultString("admin_password", randPassword(10))
  143. admin.Password = utils.GetSHA256HashCode(password)
  144. admin.Remark = "admin"
  145. admin.Roles = "ADMIN"
  146. if admin.Id > 0 {
  147. _, err = o.Update(&admin)
  148. } else {
  149. _, err = o.Insert(&admin)
  150. }
  151. if err != nil {
  152. logs.Warn("insert or update admin fail", err)
  153. } else {
  154. logs.Warn(fmt.Sprintf("admin password is: %s", password))
  155. }
  156. }
  157. func randPassword(n int) string {
  158. b := make([]byte, n)
  159. _, err := rand.Read(b)
  160. if err != nil {
  161. return "123456"
  162. }
  163. return base64.StdEncoding.EncodeToString(b)
  164. }