1
0

sub.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package sub
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "io"
  6. "net"
  7. "net/http"
  8. "strconv"
  9. "x-ui/config"
  10. "x-ui/logger"
  11. "x-ui/util/common"
  12. "x-ui/web/middleware"
  13. "x-ui/web/network"
  14. "x-ui/web/service"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type Server struct {
  18. httpServer *http.Server
  19. listener net.Listener
  20. sub *SUBController
  21. settingService service.SettingService
  22. ctx context.Context
  23. cancel context.CancelFunc
  24. }
  25. func NewServer() *Server {
  26. ctx, cancel := context.WithCancel(context.Background())
  27. return &Server{
  28. ctx: ctx,
  29. cancel: cancel,
  30. }
  31. }
  32. func (s *Server) initRouter() (*gin.Engine, error) {
  33. if config.IsDebug() {
  34. gin.SetMode(gin.DebugMode)
  35. } else {
  36. gin.DefaultWriter = io.Discard
  37. gin.DefaultErrorWriter = io.Discard
  38. gin.SetMode(gin.ReleaseMode)
  39. }
  40. engine := gin.Default()
  41. subDomain, err := s.settingService.GetSubDomain()
  42. if err != nil {
  43. return nil, err
  44. }
  45. if subDomain != "" {
  46. engine.Use(middleware.DomainValidatorMiddleware(subDomain))
  47. }
  48. LinksPath, err := s.settingService.GetSubPath()
  49. if err != nil {
  50. return nil, err
  51. }
  52. JsonPath, err := s.settingService.GetSubJsonPath()
  53. if err != nil {
  54. return nil, err
  55. }
  56. Encrypt, err := s.settingService.GetSubEncrypt()
  57. if err != nil {
  58. return nil, err
  59. }
  60. ShowInfo, err := s.settingService.GetSubShowInfo()
  61. if err != nil {
  62. return nil, err
  63. }
  64. RemarkModel, err := s.settingService.GetRemarkModel()
  65. if err != nil {
  66. RemarkModel = "-ieo"
  67. }
  68. SubUpdates, err := s.settingService.GetSubUpdates()
  69. if err != nil {
  70. SubUpdates = "10"
  71. }
  72. SubJsonFragment, err := s.settingService.GetSubJsonFragment()
  73. if err != nil {
  74. SubJsonFragment = ""
  75. }
  76. SubJsonNoise, err := s.settingService.GetSubJsonNoise()
  77. if err != nil {
  78. SubJsonNoise = ""
  79. }
  80. SubJsonMux, err := s.settingService.GetSubJsonMux()
  81. if err != nil {
  82. SubJsonMux = ""
  83. }
  84. SubJsonRules, err := s.settingService.GetSubJsonRules()
  85. if err != nil {
  86. SubJsonRules = ""
  87. }
  88. g := engine.Group("/")
  89. s.sub = NewSUBController(
  90. g, LinksPath, JsonPath, Encrypt, ShowInfo, RemarkModel, SubUpdates,
  91. SubJsonFragment, SubJsonNoise, SubJsonMux, SubJsonRules)
  92. return engine, nil
  93. }
  94. func (s *Server) Start() (err error) {
  95. // This is an anonymous function, no function name
  96. defer func() {
  97. if err != nil {
  98. s.Stop()
  99. }
  100. }()
  101. subEnable, err := s.settingService.GetSubEnable()
  102. if err != nil {
  103. return err
  104. }
  105. if !subEnable {
  106. return nil
  107. }
  108. engine, err := s.initRouter()
  109. if err != nil {
  110. return err
  111. }
  112. certFile, err := s.settingService.GetSubCertFile()
  113. if err != nil {
  114. return err
  115. }
  116. keyFile, err := s.settingService.GetSubKeyFile()
  117. if err != nil {
  118. return err
  119. }
  120. listen, err := s.settingService.GetSubListen()
  121. if err != nil {
  122. return err
  123. }
  124. port, err := s.settingService.GetSubPort()
  125. if err != nil {
  126. return err
  127. }
  128. listenAddr := net.JoinHostPort(listen, strconv.Itoa(port))
  129. listener, err := net.Listen("tcp", listenAddr)
  130. if err != nil {
  131. return err
  132. }
  133. if certFile != "" || keyFile != "" {
  134. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  135. if err == nil {
  136. c := &tls.Config{
  137. Certificates: []tls.Certificate{cert},
  138. }
  139. listener = network.NewAutoHttpsListener(listener)
  140. listener = tls.NewListener(listener, c)
  141. logger.Info("Sub server running HTTPS on", listener.Addr())
  142. } else {
  143. logger.Error("Error loading certificates:", err)
  144. logger.Info("Sub server running HTTP on", listener.Addr())
  145. }
  146. } else {
  147. logger.Info("Sub server running HTTP on", listener.Addr())
  148. }
  149. s.listener = listener
  150. s.httpServer = &http.Server{
  151. Handler: engine,
  152. }
  153. go func() {
  154. s.httpServer.Serve(listener)
  155. }()
  156. return nil
  157. }
  158. func (s *Server) Stop() error {
  159. s.cancel()
  160. var err1 error
  161. var err2 error
  162. if s.httpServer != nil {
  163. err1 = s.httpServer.Shutdown(s.ctx)
  164. }
  165. if s.listener != nil {
  166. err2 = s.listener.Close()
  167. }
  168. return common.Combine(err1, err2)
  169. }
  170. func (s *Server) GetCtx() context.Context {
  171. return s.ctx
  172. }