init.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * @Author: LinkLeong link@icewhale.org
  3. * @Date: 2022-11-15 15:51:44
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-11-15 15:55:16
  6. * @FilePath: /CasaOS/route/init.go
  7. * @Description:
  8. * @Website: https://www.casaos.io
  9. * Copyright (c) 2022 by icewhale, All Rights Reserved.
  10. */
  11. package route
  12. import (
  13. "encoding/json"
  14. "fmt"
  15. "os"
  16. "strings"
  17. "time"
  18. file1 "github.com/IceWhaleTech/CasaOS-Common/utils/file"
  19. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  20. "github.com/IceWhaleTech/CasaOS/common"
  21. "github.com/IceWhaleTech/CasaOS/model"
  22. "github.com/IceWhaleTech/CasaOS/pkg/config"
  23. "github.com/IceWhaleTech/CasaOS/pkg/samba"
  24. "github.com/IceWhaleTech/CasaOS/pkg/utils/encryption"
  25. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  26. v1 "github.com/IceWhaleTech/CasaOS/route/v1"
  27. "github.com/IceWhaleTech/CasaOS/service"
  28. "go.uber.org/zap"
  29. )
  30. func InitFunction() {
  31. go InitNetworkMount()
  32. go InitInfo()
  33. go InitZerotier()
  34. }
  35. func InitInfo() {
  36. mb := model.BaseInfo{}
  37. if file.Exists(config.AppInfo.DBPath + "/baseinfo.conf") {
  38. err := json.Unmarshal(file.ReadFullFile(config.AppInfo.DBPath+"/baseinfo.conf"), &mb)
  39. if err != nil {
  40. logger.Error("baseinfo.conf", zap.String("error", err.Error()))
  41. }
  42. }
  43. if file.Exists("/etc/CHANNEL") {
  44. channel := file.ReadFullFile("/etc/CHANNEL")
  45. mb.Channel = string(channel)
  46. }
  47. mac, err := service.MyService.System().GetMacAddress()
  48. if err != nil {
  49. logger.Error("GetMacAddress", zap.String("error", err.Error()))
  50. }
  51. mb.Hash = encryption.GetMD5ByStr(mac)
  52. mb.Version = common.VERSION
  53. osRelease, _ := file1.ReadOSRelease()
  54. mb.DriveModel = osRelease["MODEL"]
  55. if len(mb.DriveModel) == 0 {
  56. mb.DriveModel = "Casa"
  57. }
  58. os.Remove(config.AppInfo.DBPath + "/baseinfo.conf")
  59. by, err := json.Marshal(mb)
  60. if err != nil {
  61. logger.Error("init info err", zap.Any("err", err))
  62. return
  63. }
  64. file.WriteToFullPath(by, config.AppInfo.DBPath+"/baseinfo.conf", 0o666)
  65. }
  66. func InitNetworkMount() {
  67. time.Sleep(time.Second * 10)
  68. connections := service.MyService.Connections().GetConnectionsList()
  69. for _, v := range connections {
  70. connection := service.MyService.Connections().GetConnectionByID(fmt.Sprint(v.ID))
  71. directories, err := samba.GetSambaSharesList(connection.Host, connection.Port, connection.Username, connection.Password)
  72. if err != nil {
  73. service.MyService.Connections().DeleteConnection(fmt.Sprint(connection.ID))
  74. logger.Error("mount samba err", zap.Any("err", err), zap.Any("info", connection))
  75. continue
  76. }
  77. baseHostPath := "/mnt/" + connection.Host
  78. mountPointList, err := service.MyService.System().GetDirPath(baseHostPath)
  79. if err != nil {
  80. logger.Error("get mount point err", zap.Any("err", err))
  81. continue
  82. }
  83. for _, v := range mountPointList {
  84. service.MyService.Connections().UnmountSmaba(v.Path)
  85. }
  86. os.RemoveAll(baseHostPath)
  87. file.IsNotExistMkDir(baseHostPath)
  88. for _, v := range directories {
  89. mountPoint := baseHostPath + "/" + v
  90. file.IsNotExistMkDir(mountPoint)
  91. service.MyService.Connections().MountSmaba(connection.Username, connection.Host, v, connection.Port, mountPoint, connection.Password)
  92. }
  93. connection.Directories = strings.Join(directories, ",")
  94. service.MyService.Connections().UpdateConnection(&connection)
  95. }
  96. err := service.MyService.Storage().CheckAndMountAll()
  97. if err != nil {
  98. logger.Error("mount storage err", zap.Any("err", err))
  99. }
  100. }
  101. func InitZerotier() {
  102. v1.CheckNetwork()
  103. }