CasaOS/route/init.go
2023-06-14 11:32:04 +08:00

112 lines
3.3 KiB
Go

/*
* @Author: LinkLeong link@icewhale.org
* @Date: 2022-11-15 15:51:44
* @LastEditors: LinkLeong
* @LastEditTime: 2022-11-15 15:55:16
* @FilePath: /CasaOS/route/init.go
* @Description:
* @Website: https://www.casaos.io
* Copyright (c) 2022 by icewhale, All Rights Reserved.
*/
package route
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
file1 "github.com/IceWhaleTech/CasaOS-Common/utils/file"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS/common"
"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/samba"
"github.com/IceWhaleTech/CasaOS/pkg/utils/encryption"
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
v1 "github.com/IceWhaleTech/CasaOS/route/v1"
"github.com/IceWhaleTech/CasaOS/service"
"go.uber.org/zap"
)
func InitFunction() {
go InitNetworkMount()
go InitInfo()
go InitZerotier()
}
func InitInfo() {
mb := model.BaseInfo{}
if file.Exists(config.AppInfo.DBPath + "/baseinfo.conf") {
err := json.Unmarshal(file.ReadFullFile(config.AppInfo.DBPath+"/baseinfo.conf"), &mb)
if err != nil {
logger.Error("baseinfo.conf", zap.String("error", err.Error()))
}
}
if file.Exists("/etc/CHANNEL") {
channel := file.ReadFullFile("/etc/CHANNEL")
mb.Channel = string(channel)
}
mac, err := service.MyService.System().GetMacAddress()
if err != nil {
logger.Error("GetMacAddress", zap.String("error", err.Error()))
}
mb.Hash = encryption.GetMD5ByStr(mac)
mb.Version = common.VERSION
osRelease, _ := file1.ReadOSRelease()
mb.DriveModel = osRelease["MODEL"]
if len(mb.DriveModel) == 0 {
mb.DriveModel = "Casa"
}
os.Remove(config.AppInfo.DBPath + "/baseinfo.conf")
by, err := json.Marshal(mb)
if err != nil {
logger.Error("init info err", zap.Any("err", err))
return
}
file.WriteToFullPath(by, config.AppInfo.DBPath+"/baseinfo.conf", 0o666)
}
func InitNetworkMount() {
time.Sleep(time.Second * 10)
connections := service.MyService.Connections().GetConnectionsList()
for _, v := range connections {
connection := service.MyService.Connections().GetConnectionByID(fmt.Sprint(v.ID))
directories, err := samba.GetSambaSharesList(connection.Host, connection.Port, connection.Username, connection.Password)
if err != nil {
service.MyService.Connections().DeleteConnection(fmt.Sprint(connection.ID))
logger.Error("mount samba err", zap.Any("err", err), zap.Any("info", connection))
continue
}
baseHostPath := "/mnt/" + connection.Host
mountPointList, err := service.MyService.System().GetDirPath(baseHostPath)
if err != nil {
logger.Error("get mount point err", zap.Any("err", err))
continue
}
for _, v := range mountPointList {
service.MyService.Connections().UnmountSmaba(v.Path)
}
os.RemoveAll(baseHostPath)
file.IsNotExistMkDir(baseHostPath)
for _, v := range directories {
mountPoint := baseHostPath + "/" + v
file.IsNotExistMkDir(mountPoint)
service.MyService.Connections().MountSmaba(connection.Username, connection.Host, v, connection.Port, mountPoint, connection.Password)
}
connection.Directories = strings.Join(directories, ",")
service.MyService.Connections().UpdateConnection(&connection)
}
err := service.MyService.Storage().CheckAndMountAll()
if err != nil {
logger.Error("mount storage err", zap.Any("err", err))
}
}
func InitZerotier() {
v1.CheckNetwork()
}