2022-11-15 07:56:08 +00:00
|
|
|
/*
|
|
|
|
* @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.
|
|
|
|
*/
|
2021-11-09 10:57:50 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2022-11-04 07:41:09 +00:00
|
|
|
"encoding/json"
|
2022-08-15 03:37:21 +00:00
|
|
|
"fmt"
|
2022-06-29 03:09:58 +00:00
|
|
|
"os"
|
2022-05-13 10:12:26 +00:00
|
|
|
"strings"
|
2022-08-18 03:18:27 +00:00
|
|
|
"time"
|
2021-11-09 10:57:50 +00:00
|
|
|
|
2022-11-29 17:17:14 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
2023-02-06 07:47:29 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/common"
|
2022-11-04 07:41:09 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
2022-08-15 03:37:21 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/samba"
|
2022-11-04 07:41:09 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/encryption"
|
2021-11-09 10:57:50 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
2023-05-25 04:14:32 +00:00
|
|
|
v1 "github.com/IceWhaleTech/CasaOS/route/v1"
|
2021-11-09 10:57:50 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
2022-08-18 03:18:27 +00:00
|
|
|
"go.uber.org/zap"
|
2021-11-09 10:57:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitFunction() {
|
2022-08-18 03:18:27 +00:00
|
|
|
go InitNetworkMount()
|
2022-11-04 07:41:09 +00:00
|
|
|
go InitInfo()
|
2023-05-25 04:14:32 +00:00
|
|
|
go InitZerotier()
|
2021-11-09 10:57:50 +00:00
|
|
|
}
|
2022-11-04 07:41:09 +00:00
|
|
|
|
|
|
|
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 {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("baseinfo.conf", zap.String("error", err.Error()))
|
2022-11-04 07:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if file.Exists("/etc/CHANNEL") {
|
|
|
|
channel := file.ReadFullFile("/etc/CHANNEL")
|
|
|
|
mb.Channel = string(channel)
|
|
|
|
}
|
|
|
|
mac, err := service.MyService.System().GetMacAddress()
|
|
|
|
if err != nil {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("GetMacAddress", zap.String("error", err.Error()))
|
2022-11-04 07:41:09 +00:00
|
|
|
}
|
|
|
|
mb.Hash = encryption.GetMD5ByStr(mac)
|
2023-02-06 07:47:29 +00:00
|
|
|
mb.Version = common.VERSION
|
2022-11-04 07:41:09 +00:00
|
|
|
os.Remove(config.AppInfo.DBPath + "/baseinfo.conf")
|
|
|
|
by, err := json.Marshal(mb)
|
|
|
|
if err != nil {
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("init info err", zap.Any("err", err))
|
2022-11-04 07:41:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
file.WriteToFullPath(by, config.AppInfo.DBPath+"/baseinfo.conf", 0o666)
|
|
|
|
}
|
|
|
|
|
2022-08-15 03:37:21 +00:00
|
|
|
func InitNetworkMount() {
|
2022-08-18 03:18:27 +00:00
|
|
|
time.Sleep(time.Second * 10)
|
2022-08-15 03:37:21 +00:00
|
|
|
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))
|
2022-11-29 17:17:14 +00:00
|
|
|
logger.Error("mount samba err", zap.Any("err", err), zap.Any("info", connection))
|
2022-08-15 03:37:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
baseHostPath := "/mnt/" + connection.Host
|
|
|
|
|
2023-02-07 06:38:21 +00:00
|
|
|
mountPointList, err := service.MyService.System().GetDirPath(baseHostPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("get mount point err", zap.Any("err", err))
|
|
|
|
continue
|
|
|
|
}
|
2022-08-15 03:37:21 +00:00
|
|
|
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)
|
2023-04-12 05:53:27 +00:00
|
|
|
}
|
|
|
|
err := service.MyService.Storage().CheckAndMountAll()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("mount storage err", zap.Any("err", err))
|
2022-08-15 03:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-25 04:14:32 +00:00
|
|
|
func InitZerotier() {
|
|
|
|
v1.CheckNetwork()
|
|
|
|
}
|