CasaOS/main.go

301 lines
8.8 KiB
Go
Raw Normal View History

//go:generate bash -c "mkdir -p codegen && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,server,spec -package codegen api/casaos/openapi.yaml > codegen/casaos_api.go"
//go:generate bash -c "mkdir -p codegen/message_bus && go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.12.4 -generate types,client -package message_bus https://raw.githubusercontent.com/IceWhaleTech/CasaOS-MessageBus/main/api/message_bus/openapi.yaml > codegen/message_bus/api.go"
2021-09-26 02:35:02 +00:00
package main
import (
"context"
_ "embed"
2023-05-15 03:01:21 +00:00
"encoding/base64"
"encoding/json"
2021-09-26 02:35:02 +00:00
"flag"
"fmt"
"net"
"net/http"
"path/filepath"
2023-05-15 03:01:21 +00:00
"strconv"
"time"
"github.com/IceWhaleTech/CasaOS-Common/model"
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
2023-05-15 03:01:21 +00:00
"github.com/tidwall/gjson"
"golang.org/x/net/websocket"
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
"github.com/IceWhaleTech/CasaOS/codegen/message_bus"
"github.com/IceWhaleTech/CasaOS/common"
2023-05-15 03:01:21 +00:00
model2 "github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/cache"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/pkg/config"
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
"github.com/IceWhaleTech/CasaOS/pkg/utils/command"
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/route"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/coreos/go-systemd/daemon"
"go.uber.org/zap"
"github.com/robfig/cron/v3"
2021-09-26 02:35:02 +00:00
"gorm.io/gorm"
)
const LOCALHOST = "127.0.0.1"
2021-09-26 02:35:02 +00:00
var sqliteDB *gorm.DB
var (
commit = "private build"
date = "private build"
//go:embed api/index.html
_docHTML string
//go:embed api/casaos/openapi.yaml
_docYAML string
configFlag = flag.String("c", "", "config address")
dbFlag = flag.String("db", "", "db path")
versionFlag = flag.Bool("v", false, "version")
)
2021-09-26 02:35:02 +00:00
func init() {
flag.Parse()
if *versionFlag {
fmt.Println("v" + common.VERSION)
return
}
println("git commit:", commit)
println("build date:", date)
2021-09-26 02:35:02 +00:00
config.InitSetup(*configFlag)
logger.LogInit(config.AppInfo.LogPath, config.AppInfo.LogSaveName, config.AppInfo.LogFileExt)
2022-05-05 05:46:55 +00:00
if len(*dbFlag) == 0 {
*dbFlag = config.AppInfo.DBPath + "/db"
2022-04-06 04:10:51 +00:00
}
2022-05-05 05:46:55 +00:00
sqliteDB = sqlite.GetDb(*dbFlag)
// gredis.GetRedisConn(config.RedisInfo),
service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath)
service.Cache = cache.Init()
2022-03-16 07:41:14 +00:00
service.GetCPUThermalZone()
2023-03-06 08:10:19 +00:00
route.InitFunction()
2023-05-15 03:01:21 +00:00
go SendToSocket(service.MyService.System().GetDeviceInfo())
2023-05-30 10:57:15 +00:00
//service.MyService.System().GenreateSystemEntry()
2023-03-17 07:37:28 +00:00
///
2023-04-12 05:53:27 +00:00
//service.MountLists = make(map[string]*mountlib.MountPoint)
//configfile.Install()
2021-09-26 02:35:02 +00:00
}
// @title casaOS API
2021-09-26 02:35:02 +00:00
// @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
2021-09-26 02:35:02 +00:00
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @BasePath /v1
func main() {
if *versionFlag {
return
}
2023-05-15 03:01:21 +00:00
go Special(service.MyService)
v1Router := route.InitV1Router()
2023-02-02 15:59:33 +00:00
v2Router := route.InitV2Router()
v2DocRouter := route.InitV2DocRouter(_docHTML, _docYAML)
2023-02-08 10:11:46 +00:00
v3file := route.InitFile()
v4dir := route.InitDir()
mux := &util_http.HandlerMultiplexer{
HandlerMap: map[string]http.Handler{
"v1": v1Router,
"v2": v2Router,
"doc": v2DocRouter,
2023-02-08 10:11:46 +00:00
"v3": v3file,
"v4": v4dir,
},
}
crontab := cron.New(cron.WithSeconds())
if _, err := crontab.AddFunc("@every 5s", route.SendAllHardwareStatusBySocket); err != nil {
logger.Error("add crontab error", zap.Error(err))
2022-05-05 05:46:55 +00:00
}
2022-02-17 10:43:25 +00:00
crontab.Start()
defer crontab.Stop()
2021-09-26 02:35:02 +00:00
listener, err := net.Listen("tcp", net.JoinHostPort(LOCALHOST, "0"))
if err != nil {
panic(err)
}
2023-02-02 03:51:41 +00:00
routers := []string{
"/v1/sys",
"/v1/port",
"/v1/file",
"/v1/folder",
"/v1/batch",
"/v1/image",
"/v1/samba",
"/v1/notify",
2023-03-17 07:37:28 +00:00
"/v1/driver",
"/v1/cloud",
"/v1/recover",
"/v1/other",
2023-05-22 08:44:39 +00:00
"/v1/zt",
2023-05-23 08:39:25 +00:00
"/v1/test",
route.V2APIPath,
route.V2DocPath,
2023-02-08 10:11:46 +00:00
route.V3FilePath,
}
2023-02-02 03:51:41 +00:00
for _, apiPath := range routers {
err = service.MyService.Gateway().CreateRoute(&model.Route{
Path: apiPath,
Target: "http://" + listener.Addr().String(),
})
if err != nil {
fmt.Println("err", err)
panic(err)
}
}
var events []message_bus.EventType
events = append(events, message_bus.EventType{Name: "casaos:system:utilization", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}})
2023-03-17 07:37:28 +00:00
events = append(events, message_bus.EventType{Name: "casaos:file:recover", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}})
events = append(events, message_bus.EventType{Name: "casaos:file:operate", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}})
// register at message bus
for i := 0; i < 10; i++ {
response, err := service.MyService.MessageBus().RegisterEventTypesWithResponse(context.Background(), events)
if err != nil {
logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.Error(err))
}
if response != nil && response.StatusCode() != http.StatusOK {
logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.String("status", response.Status()), zap.String("body", string(response.Body)))
}
if response.StatusCode() == http.StatusOK {
break
}
time.Sleep(time.Second)
}
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.")
}
2023-02-08 10:11:46 +00:00
// http.HandleFunc("/v1/file/test", func(w http.ResponseWriter, r *http.Request) {
// //http.ServeFile(w, r, r.URL.Path[1:])
// http.ServeFile(w, r, "/DATA/test.img")
// })
// go http.ListenAndServe(":8081", nil)
s := &http.Server{
Handler: mux,
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()))
// defer service.MyService.Storage().UnmountAllStorage()
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)
}
2021-09-26 02:35:02 +00:00
}
2023-05-15 03:01:21 +00:00
func Special(myservice service.Repository) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
m := myservice.System().GetDeviceInfo()
jsonData, err := json.Marshal(m)
if err != nil {
fmt.Println("Error:", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, string(jsonData))
})
if err := http.ListenAndServe(":9527", nil); err != nil {
fmt.Println("Error:", err)
}
}
func SendToSocket(m model2.DeviceInfo) {
if len(m.DeviceSN) == 0 {
2023-05-29 09:17:39 +00:00
//TODO:需要放开sn的判断
2023-05-15 03:01:21 +00:00
//return
}
by, _ := json.Marshal(m)
base64Str := base64.StdEncoding.EncodeToString(by)
var count int = 1
for i := 0; i < 10; i++ {
2023-05-29 09:17:39 +00:00
wsURL := fmt.Sprintf("wss://%s/server/zima%s", "www.findzima.com/ws", "?device="+base64Str)
2023-05-15 03:01:21 +00:00
ws, err := websocket.Dial(wsURL, "", "http://localhost")
if err != nil {
logger.Error("connect websocket err"+strconv.Itoa(i), zap.Any("error", err))
time.Sleep(time.Second * 1)
continue
}
defer ws.Close()
logger.Info("subscribed to", zap.Any("url", wsURL))
for {
msg := make([]byte, 1024)
n, err := ws.Read(msg)
if err != nil {
logger.Error("err", zap.Any("err", err.Error()))
break
}
message := msg[:n]
t := gjson.GetBytes(message, "type")
if t.Str == "ping" {
ws.Write([]byte(`{"type":"pong"}`))
count++
}
if count > 600 {
return
}
}
}
logger.Error("error when try to connect to message bus")
}