Tiger Wang пре 2 година
родитељ
комит
182bc25343
4 измењених фајлова са 27 додато и 20 уклоњено
  1. 2 1
      build/sysroot/usr/lib/systemd/system/casaos.service
  2. 1 0
      go.mod
  3. 15 9
      main.go
  4. 9 10
      types/system.go

+ 2 - 1
build/sysroot/usr/lib/systemd/system/casaos.service

@@ -1,12 +1,13 @@
 [Unit]
 After=casaos-gateway.service
 ConditionFileNotEmpty=/etc/casaos/casaos.conf
-Description=CasaOS Service
+Description=CasaOS Main Service
 
 [Service]
 ExecStart=/usr/bin/casaos -c /etc/casaos/casaos.conf
 PIDFile=/var/run/casaos/casaos.pid
 Restart=always
+Type=notify
 
 [Install]
 WantedBy=multi-user.target

+ 1 - 0
go.mod

@@ -11,6 +11,7 @@ require (
 	github.com/ambelovsky/gosf v0.0.0-20201109201340-237aea4d6109
 	github.com/ambelovsky/gosf-socketio v0.0.0-20201109193639-add9d32f8b19 // indirect
 	github.com/containerd/containerd v1.5.7 // indirect
+	github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e
 	github.com/disintegration/imaging v1.6.2
 	github.com/docker/distribution v2.8.0+incompatible // indirect
 	github.com/docker/docker v20.10.7+incompatible

+ 15 - 9
main.go

@@ -18,6 +18,7 @@ import (
 	"github.com/IceWhaleTech/CasaOS/route"
 	"github.com/IceWhaleTech/CasaOS/service"
 	"github.com/IceWhaleTech/CasaOS/types"
+	"github.com/coreos/go-systemd/daemon"
 	"go.uber.org/zap"
 
 	"github.com/robfig/cron"
@@ -135,14 +136,6 @@ func main() {
 		}
 	}()
 
-	// s := &http.Server{
-	// 	Addr:           listener.Addr().String(), //fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
-	// 	Handler:        r,
-	// 	ReadTimeout:    60 * time.Second,
-	// 	WriteTimeout:   60 * time.Second,
-	// 	MaxHeaderBytes: 1 << 20,
-	// }
-	// s.ListenAndServe()
 	urlFilePath := filepath.Join(config.CommonInfo.RuntimePath, "casaos.url")
 	err = file.CreateFileAndWriteContent(urlFilePath, "http://"+listener.Addr().String())
 	if err != nil {
@@ -152,7 +145,20 @@ func main() {
 		)
 	}
 
-	err = http.Serve(listener, r)
+	if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
+		loger.Error("Failed to notify systemd that casaos main service is ready", zap.Any("error", err))
+	} else if supported {
+		loger.Info("Notified systemd that casaos main service is ready")
+	} else {
+		loger.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)
+	}
+
+	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)
 	}

+ 9 - 10
types/system.go

@@ -1,15 +1,14 @@
-/*
- * @Author: LinkLeong link@icewhale.com
- * @Date: 2022-02-17 18:53:22
- * @LastEditors: LinkLeong
- * @LastEditTime: 2022-09-06 14:27:42
- * @FilePath: /CasaOS/types/system.go
- * @Description:
- * @Website: https://www.casaos.io
- * Copyright (c) 2022 by icewhale, All Rights Reserved.
+/*@Author: LinkLeong link@icewhale.com
+ *@Date: 2022-02-17 18:53:22
+ *@LastEditors: LinkLeong
+ *@LastEditTime: 2022-09-06 14:27:42
+ *@FilePath: /CasaOS/types/system.go
+ *@Description:
+ *@Website: https://www.casaos.io
+ *Copyright (c) 2022 by icewhale, All Rights Reserved.
  */
 package types
 
-const CURRENTVERSION = "0.3.6"
+const CURRENTVERSION = "0.3.7"
 
 const BODY = " "