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.

302 lines
7.3 KiB

Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package mailer
  6. import (
  7. "bytes"
  8. "crypto/tls"
  9. "fmt"
  10. "io"
  11. "net"
  12. "net/smtp"
  13. "os"
  14. "os/exec"
  15. "strings"
  16. "time"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/setting"
  20. "github.com/jaytaylor/html2text"
  21. "gopkg.in/gomail.v2"
  22. )
  23. // Message mail body and log info
  24. type Message struct {
  25. Info string // Message information for log purpose.
  26. *gomail.Message
  27. }
  28. // NewMessageFrom creates new mail message object with custom From header.
  29. func NewMessageFrom(to []string, fromDisplayName, fromAddress, subject, body string) *Message {
  30. log.Trace("NewMessageFrom (body):\n%s", body)
  31. msg := gomail.NewMessage()
  32. msg.SetAddressHeader("From", fromAddress, fromDisplayName)
  33. msg.SetHeader("To", to...)
  34. msg.SetHeader("Subject", subject)
  35. msg.SetDateHeader("Date", time.Now())
  36. msg.SetHeader("X-Auto-Response-Suppress", "All")
  37. plainBody, err := html2text.FromString(body)
  38. if err != nil || setting.MailService.SendAsPlainText {
  39. if strings.Contains(base.TruncateString(body, 100), "<html>") {
  40. log.Warn("Mail contains HTML but configured to send as plain text.")
  41. }
  42. msg.SetBody("text/plain", plainBody)
  43. } else {
  44. msg.SetBody("text/plain", plainBody)
  45. msg.AddAlternative("text/html", body)
  46. }
  47. return &Message{
  48. Message: msg,
  49. }
  50. }
  51. // NewMessage creates new mail message object with default From header.
  52. func NewMessage(to []string, subject, body string) *Message {
  53. return NewMessageFrom(to, setting.MailService.FromName, setting.MailService.FromEmail, subject, body)
  54. }
  55. type loginAuth struct {
  56. username, password string
  57. }
  58. // LoginAuth SMTP AUTH LOGIN Auth Handler
  59. func LoginAuth(username, password string) smtp.Auth {
  60. return &loginAuth{username, password}
  61. }
  62. // Start start SMTP login auth
  63. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  64. return "LOGIN", []byte{}, nil
  65. }
  66. // Next next step of SMTP login auth
  67. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  68. if more {
  69. switch string(fromServer) {
  70. case "Username:":
  71. return []byte(a.username), nil
  72. case "Password:":
  73. return []byte(a.password), nil
  74. default:
  75. return nil, fmt.Errorf("unknown fromServer: %s", string(fromServer))
  76. }
  77. }
  78. return nil, nil
  79. }
  80. // Sender SMTP mail sender
  81. type smtpSender struct {
  82. }
  83. // Send send email
  84. func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
  85. opts := setting.MailService
  86. host, port, err := net.SplitHostPort(opts.Host)
  87. if err != nil {
  88. return err
  89. }
  90. tlsconfig := &tls.Config{
  91. InsecureSkipVerify: opts.SkipVerify,
  92. ServerName: host,
  93. }
  94. if opts.UseCertificate {
  95. cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile)
  96. if err != nil {
  97. return err
  98. }
  99. tlsconfig.Certificates = []tls.Certificate{cert}
  100. }
  101. conn, err := net.Dial("tcp", net.JoinHostPort(host, port))
  102. if err != nil {
  103. return err
  104. }
  105. defer conn.Close()
  106. isSecureConn := opts.IsTLSEnabled || (strings.HasSuffix(port, "465"))
  107. // Start TLS directly if the port ends with 465 (SMTPS protocol)
  108. if isSecureConn {
  109. conn = tls.Client(conn, tlsconfig)
  110. }
  111. client, err := smtp.NewClient(conn, host)
  112. if err != nil {
  113. return fmt.Errorf("NewClient: %v", err)
  114. }
  115. if !opts.DisableHelo {
  116. hostname := opts.HeloHostname
  117. if len(hostname) == 0 {
  118. hostname, err = os.Hostname()
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. if err = client.Hello(hostname); err != nil {
  124. return fmt.Errorf("Hello: %v", err)
  125. }
  126. }
  127. // If not using SMTPS, always use STARTTLS if available
  128. hasStartTLS, _ := client.Extension("STARTTLS")
  129. if !isSecureConn && hasStartTLS {
  130. if err = client.StartTLS(tlsconfig); err != nil {
  131. return fmt.Errorf("StartTLS: %v", err)
  132. }
  133. }
  134. canAuth, options := client.Extension("AUTH")
  135. if canAuth && len(opts.User) > 0 {
  136. var auth smtp.Auth
  137. if strings.Contains(options, "CRAM-MD5") {
  138. auth = smtp.CRAMMD5Auth(opts.User, opts.Passwd)
  139. } else if strings.Contains(options, "PLAIN") {
  140. auth = smtp.PlainAuth("", opts.User, opts.Passwd, host)
  141. } else if strings.Contains(options, "LOGIN") {
  142. // Patch for AUTH LOGIN
  143. auth = LoginAuth(opts.User, opts.Passwd)
  144. }
  145. if auth != nil {
  146. if err = client.Auth(auth); err != nil {
  147. return fmt.Errorf("Auth: %v", err)
  148. }
  149. }
  150. }
  151. if err = client.Mail(from); err != nil {
  152. return fmt.Errorf("Mail: %v", err)
  153. }
  154. for _, rec := range to {
  155. if err = client.Rcpt(rec); err != nil {
  156. return fmt.Errorf("Rcpt: %v", err)
  157. }
  158. }
  159. w, err := client.Data()
  160. if err != nil {
  161. return fmt.Errorf("Data: %v", err)
  162. } else if _, err = msg.WriteTo(w); err != nil {
  163. return fmt.Errorf("WriteTo: %v", err)
  164. } else if err = w.Close(); err != nil {
  165. return fmt.Errorf("Close: %v", err)
  166. }
  167. return client.Quit()
  168. }
  169. // Sender sendmail mail sender
  170. type sendmailSender struct {
  171. }
  172. // Send send email
  173. func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
  174. var err error
  175. var closeError error
  176. var waitError error
  177. args := []string{"-F", from, "-i"}
  178. args = append(args, setting.MailService.SendmailArgs...)
  179. args = append(args, to...)
  180. log.Trace("Sending with: %s %v", setting.MailService.SendmailPath, args)
  181. cmd := exec.Command(setting.MailService.SendmailPath, args...)
  182. pipe, err := cmd.StdinPipe()
  183. if err != nil {
  184. return err
  185. }
  186. if err = cmd.Start(); err != nil {
  187. return err
  188. }
  189. _, err = msg.WriteTo(pipe)
  190. // we MUST close the pipe or sendmail will hang waiting for more of the message
  191. // Also we should wait on our sendmail command even if something fails
  192. closeError = pipe.Close()
  193. waitError = cmd.Wait()
  194. if err != nil {
  195. return err
  196. } else if closeError != nil {
  197. return closeError
  198. } else {
  199. return waitError
  200. }
  201. }
  202. // Sender sendmail mail sender
  203. type dummySender struct {
  204. }
  205. // Send send email
  206. func (s *dummySender) Send(from string, to []string, msg io.WriterTo) error {
  207. buf := bytes.Buffer{}
  208. if _, err := msg.WriteTo(&buf); err != nil {
  209. return err
  210. }
  211. log.Info("Mail From: %s To: %v Body: %s", from, to, buf.String())
  212. return nil
  213. }
  214. func processMailQueue() {
  215. for {
  216. select {
  217. case msg := <-mailQueue:
  218. log.Trace("New e-mail sending request %s: %s", msg.GetHeader("To"), msg.Info)
  219. if err := gomail.Send(Sender, msg.Message); err != nil {
  220. log.Error("Failed to send emails %s: %s - %v", msg.GetHeader("To"), msg.Info, err)
  221. } else {
  222. log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info)
  223. }
  224. }
  225. }
  226. }
  227. var mailQueue chan *Message
  228. // Sender sender for sending mail synchronously
  229. var Sender gomail.Sender
  230. // NewContext start mail queue service
  231. func NewContext() {
  232. // Need to check if mailQueue is nil because in during reinstall (user had installed
  233. // before but swithed install lock off), this function will be called again
  234. // while mail queue is already processing tasks, and produces a race condition.
  235. if setting.MailService == nil || mailQueue != nil {
  236. return
  237. }
  238. switch setting.MailService.MailerType {
  239. case "smtp":
  240. Sender = &smtpSender{}
  241. case "sendmail":
  242. Sender = &sendmailSender{}
  243. case "dummy":
  244. Sender = &dummySender{}
  245. }
  246. mailQueue = make(chan *Message, setting.MailService.QueueLength)
  247. go processMailQueue()
  248. }
  249. // SendAsync send mail asynchronous
  250. func SendAsync(msg *Message) {
  251. go func() {
  252. mailQueue <- msg
  253. }()
  254. }