194 lines
5.9 KiB
Go
194 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/IceWhaleTech/CasaOS-Common/model"
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
|
|
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/cache"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config/configfile"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
"github.com/IceWhaleTech/CasaOS/route"
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
"github.com/IceWhaleTech/CasaOS/types"
|
|
"github.com/coreos/go-systemd/daemon"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
_ "github.com/IceWhaleTech/CasaOS/drivers"
|
|
"github.com/robfig/cron"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const LOCALHOST = "127.0.0.1"
|
|
|
|
var sqliteDB *gorm.DB
|
|
|
|
var (
|
|
commit = "private build"
|
|
date = "private build"
|
|
|
|
configFlag = flag.String("c", "", "config address")
|
|
dbFlag = flag.String("db", "", "db path")
|
|
versionFlag = flag.Bool("v", false, "version")
|
|
)
|
|
|
|
func init() {
|
|
flag.Parse()
|
|
if *versionFlag {
|
|
fmt.Println("v" + types.CURRENTVERSION)
|
|
return
|
|
}
|
|
|
|
println("git commit:", commit)
|
|
println("build date:", date)
|
|
|
|
config.InitSetup(*configFlag)
|
|
|
|
logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)
|
|
if len(*dbFlag) == 0 {
|
|
*dbFlag = config.AppInfo.DBPath + "/db"
|
|
}
|
|
|
|
sqliteDB = sqlite.GetDb(*dbFlag)
|
|
// gredis.GetRedisConn(config.RedisInfo),
|
|
|
|
service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath, route.SocketIo())
|
|
|
|
service.Cache = cache.Init()
|
|
|
|
service.GetCPUThermalZone()
|
|
service.MyService.Storages().InitStorages()
|
|
|
|
route.InitFunction()
|
|
data := &configfile.Storage{}
|
|
e := data.Load()
|
|
fmt.Println(e)
|
|
fmt.Println(data.GetSectionList())
|
|
// fmt.Println(data.HasSection("google"))
|
|
// fmt.Println(data.GetKeyList("google"))
|
|
// fmt.Println(data.GetValue("google", "token"))
|
|
// data.SetValue("google", "type", "drive")
|
|
// data.SetValue("google", "client_id", "865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com")
|
|
// data.SetValue("google", "client_secret", "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")
|
|
// data.SetValue("google", "scope", "drive")
|
|
// data.SetValue("google", "token", `{"access_token":"ya29.a0AVvZVsqsy3vWjpjsl87mtxirrtkHpkyEXdvlORzZeIahObdEtDE47-Hzo1bIg8vJhfYKh-cdqgrUM305hiEJssFMcpkM-0IwPyxlpynMFWS0L5356AUvbv3DUd_RbV_MbKijyTThuDkfrXdLIiEOwxMOtYSXmDUaCgYKAbgSAQASFQGbdwaI6ae1NZbJARogHtpjitLGkg0166","token_type":"Bearer","refresh_token":"1//01CoIJ-aZDrUPCgYIARAAGAESNwF-L9IrNLyzp1Xzfa_sPPMouyrTgJrVchPX6uXqMizXjohTdycCpVgVcu402ND-Ikn2hArRGXA","expiry":"2023-01-28T19:26:50.198064816+08:00"}`)
|
|
//e = data.Save()
|
|
//fmt.Println(e)
|
|
}
|
|
|
|
// @title casaOS API
|
|
// @version 1.0.0
|
|
// @contact.name lauren.pan
|
|
// @contact.url https://www.zimaboard.com
|
|
// @contact.email lauren.pan@icewhale.org
|
|
// @description casaOS v1版本api
|
|
// @host 192.168.2.217:8089
|
|
// @securityDefinitions.apikey ApiKeyAuth
|
|
// @in header
|
|
// @name Authorization
|
|
// @BasePath /v1
|
|
func main() {
|
|
if *versionFlag {
|
|
return
|
|
}
|
|
// model.Setup()
|
|
// gredis.Setup()
|
|
|
|
r := route.InitRouter()
|
|
defer service.SocketServer.Close()
|
|
r.GET("/v1/socketio/*any", gin.WrapH(service.SocketServer))
|
|
r.POST("/v1/socketio/*any", gin.WrapH(service.SocketServer))
|
|
|
|
// service.SyncTask(sqliteDB)
|
|
cron2 := cron.New()
|
|
// every day execution
|
|
|
|
err := cron2.AddFunc("0/5 * * * * *", func() {
|
|
if service.ClientCount > 0 {
|
|
// route.SendNetINfoBySocket()
|
|
// route.SendCPUBySocket()
|
|
// route.SendMemBySocket()
|
|
// route.SendDiskBySocket()
|
|
// route.SendUSBBySocket()
|
|
route.SendAllHardwareStatusBySocket()
|
|
}
|
|
})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
cron2.Start()
|
|
|
|
defer cron2.Stop()
|
|
|
|
listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
routers := []string{"sys", "port", "file", "folder", "batch", "image", "samba", "notify", "socketio", "driver", "storage", "recover", "fs"}
|
|
for _, v := range routers {
|
|
err = service.MyService.Gateway().CreateRoute(&model.Route{
|
|
Path: "/v1/" + v,
|
|
Target: "http://" + listener.Addr().String(),
|
|
})
|
|
|
|
if err != nil {
|
|
fmt.Println("err", err)
|
|
panic(err)
|
|
}
|
|
}
|
|
go func() {
|
|
time.Sleep(time.Second * 2)
|
|
// v0.3.6
|
|
if config.ServerInfo.HttpPort != "" {
|
|
changePort := model.ChangePortRequest{}
|
|
changePort.Port = config.ServerInfo.HttpPort
|
|
err := service.MyService.Gateway().ChangePort(&changePort)
|
|
if err == nil {
|
|
config.Cfg.Section("server").Key("HttpPort").SetValue("")
|
|
config.Cfg.SaveTo(config.SystemConfigInfo.ConfigPath)
|
|
}
|
|
}
|
|
}()
|
|
|
|
urlFilePath := filepath.Join(config.CommonInfo.RuntimePath, "casaos.url")
|
|
if err := file.CreateFileAndWriteContent(urlFilePath, "http://"+listener.Addr().String()); err != nil {
|
|
logger.Error("error when creating address file", zap.Error(err),
|
|
zap.Any("address", listener.Addr().String()),
|
|
zap.Any("filepath", urlFilePath),
|
|
)
|
|
}
|
|
|
|
// run any script that needs to be executed
|
|
scriptDirectory := filepath.Join(constants.DefaultConfigPath, "start.d")
|
|
command.ExecuteScripts(scriptDirectory)
|
|
|
|
if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
|
|
logger.Error("Failed to notify systemd that casaos main service is ready", zap.Any("error", err))
|
|
} else if supported {
|
|
logger.Info("Notified systemd that casaos main service is ready")
|
|
} else {
|
|
logger.Info("This process is not running as a systemd service.")
|
|
}
|
|
|
|
s := &http.Server{
|
|
Handler: r,
|
|
ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
|
|
}
|
|
|
|
logger.Info("CasaOS main service is listening...", zap.Any("address", listener.Addr().String()))
|
|
|
|
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)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|