main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "path/filepath"
  8. "time"
  9. "github.com/IceWhaleTech/CasaOS-Gateway/common"
  10. "github.com/IceWhaleTech/CasaOS/model/notify"
  11. "github.com/IceWhaleTech/CasaOS/pkg/cache"
  12. "github.com/IceWhaleTech/CasaOS/pkg/config"
  13. "github.com/IceWhaleTech/CasaOS/pkg/sqlite"
  14. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  15. "github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
  16. "github.com/IceWhaleTech/CasaOS/route"
  17. "github.com/IceWhaleTech/CasaOS/service"
  18. "github.com/IceWhaleTech/CasaOS/types"
  19. "github.com/coreos/go-systemd/daemon"
  20. "go.uber.org/zap"
  21. "github.com/robfig/cron"
  22. "gorm.io/gorm"
  23. )
  24. const LOCALHOST = "127.0.0.1"
  25. var sqliteDB *gorm.DB
  26. var (
  27. configFlag = flag.String("c", "", "config address")
  28. dbFlag = flag.String("db", "", "db path")
  29. versionFlag = flag.Bool("v", false, "version")
  30. )
  31. func init() {
  32. flag.Parse()
  33. if *versionFlag {
  34. fmt.Println("v" + types.CURRENTVERSION)
  35. return
  36. }
  37. config.InitSetup(*configFlag)
  38. loger.LogInit()
  39. if len(*dbFlag) == 0 {
  40. *dbFlag = config.AppInfo.DBPath + "/db"
  41. }
  42. sqliteDB = sqlite.GetDb(*dbFlag)
  43. // gredis.GetRedisConn(config.RedisInfo),
  44. service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath)
  45. service.Cache = cache.Init()
  46. service.GetToken()
  47. service.NewVersionApp = make(map[string]string)
  48. route.InitFunction()
  49. // go service.LoopFriend()
  50. // go service.MyService.App().CheckNewImage()
  51. }
  52. // @title casaOS API
  53. // @version 1.0.0
  54. // @contact.name lauren.pan
  55. // @contact.url https://www.zimaboard.com
  56. // @contact.email lauren.pan@icewhale.org
  57. // @description casaOS v1版本api
  58. // @host 192.168.2.217:8089
  59. // @securityDefinitions.apikey ApiKeyAuth
  60. // @in header
  61. // @name Authorization
  62. // @BasePath /v1
  63. func main() {
  64. service.NotifyMsg = make(chan notify.Message, 10)
  65. if *versionFlag {
  66. return
  67. }
  68. go route.SocketInit(service.NotifyMsg)
  69. // model.Setup()
  70. // gredis.Setup()
  71. r := route.InitRouter()
  72. // service.SyncTask(sqliteDB)
  73. cron2 := cron.New()
  74. // every day execution
  75. err := cron2.AddFunc("0/5 * * * * *", func() {
  76. if service.ClientCount > 0 {
  77. // route.SendNetINfoBySocket()
  78. // route.SendCPUBySocket()
  79. // route.SendMemBySocket()
  80. // route.SendDiskBySocket()
  81. // route.SendUSBBySocket()
  82. route.SendAllHardwareStatusBySocket()
  83. }
  84. })
  85. if err != nil {
  86. fmt.Println(err)
  87. }
  88. cron2.Start()
  89. defer cron2.Stop()
  90. listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0"))
  91. if err != nil {
  92. panic(err)
  93. }
  94. routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "samba", "notify"}
  95. for _, v := range routers {
  96. err = service.MyService.Gateway().CreateRoute(&common.Route{
  97. Path: "/v1/" + v,
  98. Target: "http://" + listener.Addr().String(),
  99. })
  100. if err != nil {
  101. fmt.Println("err", err)
  102. panic(err)
  103. }
  104. }
  105. go func() {
  106. time.Sleep(time.Second * 2)
  107. // v0.3.6
  108. if config.ServerInfo.HttpPort != "" {
  109. changePort := common.ChangePortRequest{}
  110. changePort.Port = config.ServerInfo.HttpPort
  111. err := service.MyService.Gateway().ChangePort(&changePort)
  112. if err == nil {
  113. config.Cfg.Section("server").Key("HttpPort").SetValue("")
  114. config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
  115. }
  116. }
  117. }()
  118. urlFilePath := filepath.Join(config.CommonInfo.RuntimePath, "casaos.url")
  119. err = file.CreateFileAndWriteContent(urlFilePath, "http://"+listener.Addr().String())
  120. if err != nil {
  121. loger.Error("Management service is listening...",
  122. zap.Any("address", listener.Addr().String()),
  123. zap.Any("filepath", urlFilePath),
  124. )
  125. }
  126. if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
  127. loger.Error("Failed to notify systemd that casaos main service is ready", zap.Any("error", err))
  128. } else if supported {
  129. loger.Info("Notified systemd that casaos main service is ready")
  130. } else {
  131. loger.Info("This process is not running as a systemd service.")
  132. }
  133. s := &http.Server{
  134. Handler: r,
  135. ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
  136. }
  137. err = s.Serve(listener) // not using http.serve() to fix G114: Use of net/http serve function that has no support for setting timeouts (see https://github.com/securego/gosec)
  138. if err != nil {
  139. panic(err)
  140. }
  141. }