publish.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package proxy
  17. import (
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "net/http/httputil"
  22. "github.com/siyuan-note/logging"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. type PublishServiceTransport struct{}
  27. var (
  28. Host = "0.0.0.0"
  29. Port = "0"
  30. listener net.Listener
  31. transport = PublishServiceTransport{}
  32. proxy = &httputil.ReverseProxy{
  33. Rewrite: rewrite,
  34. Transport: transport,
  35. }
  36. )
  37. func InitPublishService() (uint16, error) {
  38. model.InitAccounts()
  39. if listener != nil {
  40. if !model.Conf.Publish.Enable {
  41. // 关闭发布服务
  42. closePublishListener()
  43. return 0, nil
  44. }
  45. if port, err := util.ParsePort(Port); err != nil {
  46. return 0, err
  47. } else if port != model.Conf.Publish.Port {
  48. // 关闭原端口的发布服务
  49. if err = closePublishListener(); err != nil {
  50. return 0, err
  51. }
  52. // 重新启动新端口的发布服务
  53. initPublishService()
  54. }
  55. } else {
  56. if !model.Conf.Publish.Enable {
  57. return 0, nil
  58. }
  59. // 启动新端口的发布服务
  60. initPublishService()
  61. }
  62. return util.ParsePort(Port)
  63. }
  64. func initPublishService() (err error) {
  65. if err = initPublishListener(); err == nil {
  66. go startPublishReverseProxyService()
  67. }
  68. return
  69. }
  70. func initPublishListener() (err error) {
  71. // Start new listener
  72. listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", Host, model.Conf.Publish.Port))
  73. if err != nil {
  74. logging.LogErrorf("start listener failed: %s", err)
  75. return
  76. }
  77. _, Port, err = net.SplitHostPort(listener.Addr().String())
  78. if err != nil {
  79. logging.LogErrorf("split host and port failed: %s", err)
  80. return
  81. }
  82. return
  83. }
  84. func closePublishListener() (err error) {
  85. listener_ := listener
  86. listener = nil
  87. if err = listener_.Close(); err != nil {
  88. logging.LogErrorf("close listener %s failed: %s", listener_.Addr().String(), err)
  89. listener = listener_
  90. }
  91. return
  92. }
  93. func startPublishReverseProxyService() {
  94. logging.LogInfof("publish service [%s:%s] is running", Host, Port)
  95. // 服务进行时一直阻塞
  96. if err := http.Serve(listener, proxy); err != nil {
  97. if listener != nil {
  98. logging.LogErrorf("boot publish service failed: %s", err)
  99. }
  100. }
  101. logging.LogInfof("publish service [%s:%s] is stopped", Host, Port)
  102. }
  103. func rewrite(r *httputil.ProxyRequest) {
  104. r.SetURL(util.ServerURL)
  105. r.SetXForwarded()
  106. // r.Out.Host = r.In.Host // if desired
  107. }
  108. func (PublishServiceTransport) RoundTrip(request *http.Request) (response *http.Response, err error) {
  109. if model.Conf.Publish.Auth.Enable {
  110. // Basic Auth
  111. username, password, ok := request.BasicAuth()
  112. account := model.GetBasicAuthAccount(username)
  113. if !ok ||
  114. account == nil ||
  115. account.Username == "" || // 匿名用户
  116. account.Password != password {
  117. return &http.Response{
  118. StatusCode: http.StatusUnauthorized,
  119. Status: http.StatusText(http.StatusUnauthorized),
  120. Proto: request.Proto,
  121. ProtoMajor: request.ProtoMajor,
  122. ProtoMinor: request.ProtoMinor,
  123. Request: request,
  124. Header: http.Header{
  125. model.BasicAuthHeaderKey: {model.BasicAuthHeaderValue},
  126. },
  127. Close: false,
  128. ContentLength: -1,
  129. }, nil
  130. } else {
  131. // set JWT
  132. request.Header.Set(model.XAuthTokenKey, account.Token)
  133. }
  134. } else {
  135. request.Header.Set(model.XAuthTokenKey, model.GetBasicAuthAccount("").Token)
  136. }
  137. response, err = http.DefaultTransport.RoundTrip(request)
  138. return
  139. }