You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package admin
  5. import (
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. system_model "code.gitea.io/gitea/models/system"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/json"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/setting/config"
  17. "code.gitea.io/gitea/modules/util"
  18. "code.gitea.io/gitea/services/context"
  19. "code.gitea.io/gitea/services/mailer"
  20. "gitea.com/go-chi/session"
  21. )
  22. const (
  23. tplConfig base.TplName = "admin/config"
  24. tplConfigSettings base.TplName = "admin/config_settings"
  25. )
  26. // SendTestMail send test mail to confirm mail service is OK
  27. func SendTestMail(ctx *context.Context) {
  28. email := ctx.FormString("email")
  29. // Send a test email to the user's email address and redirect back to Config
  30. if err := mailer.SendTestMail(email); err != nil {
  31. ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
  32. } else {
  33. ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
  34. }
  35. ctx.Redirect(setting.AppSubURL + "/admin/config")
  36. }
  37. func shadowPasswordKV(cfgItem, splitter string) string {
  38. fields := strings.Split(cfgItem, splitter)
  39. for i := 0; i < len(fields); i++ {
  40. if strings.HasPrefix(fields[i], "password=") {
  41. fields[i] = "password=******"
  42. break
  43. }
  44. }
  45. return strings.Join(fields, splitter)
  46. }
  47. func shadowURL(provider, cfgItem string) string {
  48. u, err := url.Parse(cfgItem)
  49. if err != nil {
  50. log.Error("Shadowing Password for %v failed: %v", provider, err)
  51. return cfgItem
  52. }
  53. if u.User != nil {
  54. atIdx := strings.Index(cfgItem, "@")
  55. if atIdx > 0 {
  56. colonIdx := strings.LastIndex(cfgItem[:atIdx], ":")
  57. if colonIdx > 0 {
  58. return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
  59. }
  60. }
  61. }
  62. return cfgItem
  63. }
  64. func shadowPassword(provider, cfgItem string) string {
  65. switch provider {
  66. case "redis":
  67. return shadowPasswordKV(cfgItem, ",")
  68. case "mysql":
  69. // root:@tcp(localhost:3306)/macaron?charset=utf8
  70. atIdx := strings.Index(cfgItem, "@")
  71. if atIdx > 0 {
  72. colonIdx := strings.Index(cfgItem[:atIdx], ":")
  73. if colonIdx > 0 {
  74. return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
  75. }
  76. }
  77. return cfgItem
  78. case "postgres":
  79. // user=jiahuachen dbname=macaron port=5432 sslmode=disable
  80. if !strings.HasPrefix(cfgItem, "postgres://") {
  81. return shadowPasswordKV(cfgItem, " ")
  82. }
  83. fallthrough
  84. case "couchbase":
  85. return shadowURL(provider, cfgItem)
  86. // postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full
  87. // Notice: use shadowURL
  88. }
  89. return cfgItem
  90. }
  91. // Config show admin config page
  92. func Config(ctx *context.Context) {
  93. ctx.Data["Title"] = ctx.Tr("admin.config_summary")
  94. ctx.Data["PageIsAdminConfig"] = true
  95. ctx.Data["PageIsAdminConfigSummary"] = true
  96. ctx.Data["CustomConf"] = setting.CustomConf
  97. ctx.Data["AppUrl"] = setting.AppURL
  98. ctx.Data["AppBuiltWith"] = setting.AppBuiltWith
  99. ctx.Data["Domain"] = setting.Domain
  100. ctx.Data["OfflineMode"] = setting.OfflineMode
  101. ctx.Data["RunUser"] = setting.RunUser
  102. ctx.Data["RunMode"] = util.ToTitleCase(setting.RunMode)
  103. ctx.Data["GitVersion"] = git.DefaultFeatures().VersionInfo()
  104. ctx.Data["AppDataPath"] = setting.AppDataPath
  105. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  106. ctx.Data["CustomRootPath"] = setting.CustomPath
  107. ctx.Data["LogRootPath"] = setting.Log.RootPath
  108. ctx.Data["ScriptType"] = setting.ScriptType
  109. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  110. ctx.Data["ReverseProxyAuthEmail"] = setting.ReverseProxyAuthEmail
  111. ctx.Data["SSH"] = setting.SSH
  112. ctx.Data["LFS"] = setting.LFS
  113. ctx.Data["Service"] = setting.Service
  114. ctx.Data["DbCfg"] = setting.Database
  115. ctx.Data["Webhook"] = setting.Webhook
  116. ctx.Data["MailerEnabled"] = false
  117. if setting.MailService != nil {
  118. ctx.Data["MailerEnabled"] = true
  119. ctx.Data["Mailer"] = setting.MailService
  120. }
  121. ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
  122. ctx.Data["CacheInterval"] = setting.CacheService.Interval
  123. ctx.Data["CacheConn"] = shadowPassword(setting.CacheService.Adapter, setting.CacheService.Conn)
  124. ctx.Data["CacheItemTTL"] = setting.CacheService.TTL
  125. sessionCfg := setting.SessionConfig
  126. if sessionCfg.Provider == "VirtualSession" {
  127. var realSession session.Options
  128. if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil {
  129. log.Error("Unable to unmarshall session config for virtual provider config: %s\nError: %v", sessionCfg.ProviderConfig, err)
  130. }
  131. sessionCfg.Provider = realSession.Provider
  132. sessionCfg.ProviderConfig = realSession.ProviderConfig
  133. sessionCfg.CookieName = realSession.CookieName
  134. sessionCfg.CookiePath = realSession.CookiePath
  135. sessionCfg.Gclifetime = realSession.Gclifetime
  136. sessionCfg.Maxlifetime = realSession.Maxlifetime
  137. sessionCfg.Secure = realSession.Secure
  138. sessionCfg.Domain = realSession.Domain
  139. }
  140. sessionCfg.ProviderConfig = shadowPassword(sessionCfg.Provider, sessionCfg.ProviderConfig)
  141. ctx.Data["SessionConfig"] = sessionCfg
  142. ctx.Data["Git"] = setting.Git
  143. ctx.Data["AccessLogTemplate"] = setting.Log.AccessLogTemplate
  144. ctx.Data["LogSQL"] = setting.Database.LogSQL
  145. ctx.Data["Loggers"] = log.GetManager().DumpLoggers()
  146. config.GetDynGetter().InvalidateCache()
  147. prepareStartupProblemsAlert(ctx)
  148. ctx.HTML(http.StatusOK, tplConfig)
  149. }
  150. func ConfigSettings(ctx *context.Context) {
  151. ctx.Data["Title"] = ctx.Tr("admin.config_settings")
  152. ctx.Data["PageIsAdminConfig"] = true
  153. ctx.Data["PageIsAdminConfigSettings"] = true
  154. ctx.Data["DefaultOpenWithEditorAppsString"] = setting.DefaultOpenWithEditorApps().ToTextareaString()
  155. ctx.HTML(http.StatusOK, tplConfigSettings)
  156. }
  157. func ChangeConfig(ctx *context.Context) {
  158. key := strings.TrimSpace(ctx.FormString("key"))
  159. value := ctx.FormString("value")
  160. cfg := setting.Config()
  161. marshalBool := func(v string) (string, error) {
  162. if b, _ := strconv.ParseBool(v); b {
  163. return "true", nil
  164. }
  165. return "false", nil
  166. }
  167. marshalOpenWithApps := func(value string) (string, error) {
  168. lines := strings.Split(value, "\n")
  169. var openWithEditorApps setting.OpenWithEditorAppsType
  170. for _, line := range lines {
  171. line = strings.TrimSpace(line)
  172. if line == "" {
  173. continue
  174. }
  175. displayName, openURL, ok := strings.Cut(line, "=")
  176. displayName, openURL = strings.TrimSpace(displayName), strings.TrimSpace(openURL)
  177. if !ok || displayName == "" || openURL == "" {
  178. continue
  179. }
  180. openWithEditorApps = append(openWithEditorApps, setting.OpenWithEditorApp{
  181. DisplayName: strings.TrimSpace(displayName),
  182. OpenURL: strings.TrimSpace(openURL),
  183. })
  184. }
  185. b, err := json.Marshal(openWithEditorApps)
  186. if err != nil {
  187. return "", err
  188. }
  189. return string(b), nil
  190. }
  191. marshallers := map[string]func(string) (string, error){
  192. cfg.Picture.DisableGravatar.DynKey(): marshalBool,
  193. cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
  194. cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
  195. }
  196. marshaller, hasMarshaller := marshallers[key]
  197. if !hasMarshaller {
  198. ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
  199. return
  200. }
  201. marshaledValue, err := marshaller(value)
  202. if err != nil {
  203. ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
  204. return
  205. }
  206. if err = system_model.SetSettings(ctx, map[string]string{key: marshaledValue}); err != nil {
  207. ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
  208. return
  209. }
  210. config.GetDynGetter().InvalidateCache()
  211. ctx.JSONOK()
  212. }