Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.3.6 #519

Merged
merged 18 commits into from
Sep 6, 2022
Merged

0.3.6 #519

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add registration route
  • Loading branch information
LinkLeong committed Aug 19, 2022
commit 81011076ee5f13084267f13fa1520d3e59bcf3ce
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,4 @@ gen
__debug_bin
main
CasaOS
github.com
.all-contributorsrc
build
.all-contributorsrc
1 change: 0 additions & 1 deletion conf/conf.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ UserDataPath = /var/lib/casaos/conf
TempPath = /var/lib/casaos/temp

[server]
HttpPort = 80
RunMode = release
ServerApi = https://api.casaos.io/casaos-api
Handshake = socket.casaos.io
Expand Down
30 changes: 7 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
github.com/IceWhaleTech/CasaOS-Gateway v0.3.6-alpha1
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/Microsoft/hcsshim v0.8.22 // indirect
github.com/ambelovsky/go-structs v1.1.0 // indirect
Expand All @@ -18,13 +19,11 @@ require (
github.com/docker/go-connections v0.4.0
github.com/dsoprea/go-exif/v3 v3.0.0-20210625224831-a6301f85c82b
github.com/dsoprea/go-logging v0.0.0-20200710184922-b02d349568dd // indirect
github.com/gin-contrib/gzip v0.0.2
github.com/gin-gonic/gin v1.7.2
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/go-ini/ini v1.62.0
github.com/go-playground/validator/v10 v10.6.1 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/golang-jwt/jwt/v4 v4.4.1
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang-jwt/jwt/v4 v4.4.2
github.com/golang/mock v1.6.0
github.com/gomodule/redigo v1.8.5
github.com/google/go-github/v36 v36.0.0
Expand All @@ -34,13 +33,8 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/hirochachacha/go-smb2 v1.1.0
github.com/jinzhu/copier v0.3.2
github.com/json-iterator/go v1.1.11 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lucas-clemente/quic-go v0.25.0
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.11 // indirect
github.com/mholt/archiver/v3 v3.5.1
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
Expand All @@ -58,22 +52,12 @@ require (
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.8.0
github.com/tidwall/gjson v1.10.2
github.com/ugorji/go v1.2.6 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/zap v1.10.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/mod v0.5.0 // indirect
golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210924002016-3dee208752a0 // indirect
google.golang.org/grpc v1.41.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/driver/sqlite v1.2.6
gorm.io/gorm v1.22.5
Expand Down
440 changes: 391 additions & 49 deletions go.sum

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"flag"
"fmt"
"net"
"net/http"
"time"

"github.com/IceWhaleTech/CasaOS-Gateway/common"
"github.com/IceWhaleTech/CasaOS/model/notify"
"github.com/IceWhaleTech/CasaOS/pkg/cache"
"github.com/IceWhaleTech/CasaOS/pkg/config"
Expand All @@ -21,6 +23,8 @@ import (
"gorm.io/gorm"
)

const LOCALHOST = "127.0.0.1"

var sqliteDB *gorm.DB

var configFlag = flag.String("c", "", "config address")
Expand All @@ -46,7 +50,7 @@ func init() {
sqliteDB = sqlite.GetDb(*dbFlag)
//gredis.GetRedisConn(config.RedisInfo),

service.MyService = service.NewService(sqliteDB)
service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath)

service.Cache = cache.Init()

Expand Down Expand Up @@ -98,6 +102,7 @@ func main() {
go route.MonitoryUSB()
//model.Setup()
//gredis.Setup()

r := route.InitRouter()
//service.SyncTask(sqliteDB)
cron2 := cron.New()
Expand All @@ -117,9 +122,27 @@ func main() {
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", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "disks", "storage", "samba"}
for _, v := range routers {
err = service.MyService.Gateway().CreateRoute(&common.Route{
Path: "/v1/" + v,
Target: "http://" + listener.Addr().String(),
})

if err != nil {
panic(err)
}
}

s := &http.Server{
Addr: fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
Addr: listener.Addr().String(), //fmt.Sprintf(":%v", config.ServerInfo.HttpPort),
Handler: r,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
Expand Down
5 changes: 4 additions & 1 deletion model/sys_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-05-13 18:15:46
* @LastEditors: LinkLeong
* @LastEditTime: 2022-07-14 11:02:06
* @LastEditTime: 2022-08-18 14:32:16
* @FilePath: /CasaOS/model/sys_common.go
* @Description:
* @Website: https://www.casaos.io
Expand Down Expand Up @@ -56,6 +56,9 @@ type APPModel struct {
ShellPath string
TempPath string
}
type CommonModel struct {
RuntimePath string
}

//公共返回模型
type Result struct {
Expand Down
6 changes: 5 additions & 1 deletion pkg/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-05-13 18:15:46
* @LastEditors: LinkLeong
* @LastEditTime: 2022-07-14 10:58:45
* @LastEditTime: 2022-08-18 14:31:14
* @FilePath: /CasaOS/pkg/config/init.go
* @Description:
* @Website: https://www.casaos.io
Expand Down Expand Up @@ -32,6 +32,10 @@ var UserInfo = &model.UserModel{}
//用户相关
var AppInfo = &model.APPModel{}

var CommonInfo = &model.CommonModel{
RuntimePath: "/var/run/casaos",
}

//var RedisInfo = &model.RedisModel{}

//server相关
Expand Down
4 changes: 3 additions & 1 deletion route/periodical.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-07-01 15:11:36
* @LastEditors: LinkLeong
* @LastEditTime: 2022-08-15 11:50:04
* @LastEditTime: 2022-08-18 16:58:00
* @FilePath: /CasaOS/route/periodical.go
* @Description:
* @Website: https://www.casaos.io
Expand Down Expand Up @@ -180,6 +180,8 @@ func SendAllHardwareStatusBySocket() {
cpuData := make(map[string]interface{})
cpuData["percent"] = cpu
cpuData["num"] = num
cpuData["temperature"] = service.MyService.System().GetCPUTemperature()
cpuData["power"] = service.MyService.System().GetCPUPower()

list := service.MyService.Disk().LSBLK(true)

Expand Down
1 change: 1 addition & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func InitRouter() *gin.Engine {
v1SysGroup.GET("/apps-state", v1.GetSystemAppsStatus)
v1SysGroup.GET("/port", v1.GetCasaOSPort)
v1SysGroup.PUT("/port", v1.PutCasaOSPort)
v1SysGroup.GET("/proxy", v1.GetSystemProxy)
}
v1PortGroup := v1Group.Group("/port")
v1PortGroup.Use()
Expand Down
13 changes: 13 additions & 0 deletions route/v1/system.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v1

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -525,3 +527,14 @@ func GetSystemNetInfo(c *gin.Context) {

c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: newNet})
}

func GetSystemProxy(c *gin.Context) {
url := c.Query("url")
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
rda, _ := ioutil.ReadAll(resp.Body)
json.NewEncoder(c.Writer).Encode(json.RawMessage(string(rda)))
}
16 changes: 14 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-07-12 09:48:56
* @LastEditors: LinkLeong
* @LastEditTime: 2022-07-27 10:28:48
* @LastEditTime: 2022-08-18 14:26:00
* @FilePath: /CasaOS/service/service.go
* @Description:
* @Website: https://www.casaos.io
Expand All @@ -11,6 +11,7 @@
package service

import (
gateway "github.com/IceWhaleTech/CasaOS-Gateway/common"
"github.com/gorilla/websocket"
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
Expand All @@ -35,10 +36,17 @@ type Repository interface {
System() SystemService
Shares() SharesService
Connections() ConnectionsService
Gateway() gateway.ManagementService
}

func NewService(db *gorm.DB) Repository {
func NewService(db *gorm.DB, RuntimePath string) Repository {
gatewayManagement, err := gateway.NewManagementService(RuntimePath)
if err != nil {
panic(err)
}

return &store{
gateway: gatewayManagement,
app: NewAppService(db),
user: NewUserService(db),
docker: NewDockerService(),
Expand All @@ -64,8 +72,12 @@ type store struct {
system SystemService
shares SharesService
connections ConnectionsService
gateway gateway.ManagementService
}

func (c *store) Gateway() gateway.ManagementService {
return c.gateway
}
func (s *store) Connections() ConnectionsService {
return s.connections
}
Expand Down
29 changes: 29 additions & 0 deletions service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type SystemService interface {
RenameFile(oldF, newF string) (int, error)
MkdirAll(path string) (int, error)
IsServiceRunning(name string) bool
GetCPUTemperature() int
GetCPUPower() map[string]string
}
type systemService struct {
}
Expand Down Expand Up @@ -294,6 +296,33 @@ func (s *systemService) IsServiceRunning(name string) bool {
return strings.TrimSpace(status) == "running"

}
func (s *systemService) GetCPUTemperature() int {
outPut := ""
if file.Exists("/sys/class/thermal/thermal_zone0/temp") {
outPut = string(file.ReadFullFile("/sys/class/thermal/thermal_zone0/temp"))
} else if file.Exists("/sys/class/hwmon/hwmon0/temp1_input") {
outPut = string(file.ReadFullFile("/sys/class/hwmon/hwmon0/temp1_input"))
} else {
outPut = "0"
}

celsius, _ := strconv.Atoi(outPut)

if celsius > 1000 {
celsius = celsius / 1000
}
return celsius
}
func (s *systemService) GetCPUPower() map[string]string {
data := make(map[string]string, 2)
data["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
if file.Exists(`/sys/class/powercap/intel-rapl/intel-rapl\:0/energy_uj`) {
data["value"] = string(file.ReadFullFile(`/sys/class/powercap/intel-rapl/intel-rapl\:0/energy_uj`))
} else {
data["value"] = "0"
}
return data
}
func NewSystemService() SystemService {
return &systemService{}
}
38 changes: 38 additions & 0 deletions shell/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,41 @@ UDEVILUmount(){
$sudo_cmd udevil umount -f $1
}

GetTemp(){
if(file_exists("/sys/class/thermal/thermal_zone0/temp"))
{
$output = rtrim(file_get_contents("/sys/class/thermal/thermal_zone0/temp"));
}
elseif (file_exists("/sys/class/hwmon/hwmon0/temp1_input"))
{
$output = rtrim(file_get_contents("/sys/class/hwmon/hwmon0/temp1_input"));
}
else
{
$output = "";
}

if(is_numeric($output))
{
$celsius = intval($output);

if($celsius > 1000) {
$celsius *= 1e-3;
}

if(isset($setupVars['TEMPERATURE_LIMIT']))
{
$temperaturelimit = intval($setupVars['TEMPERATURE_LIMIT']);
}
else
{
$temperaturelimit = 60;
}
}
else
{
// Nothing can be colder than -273.15 degree Celsius (= 0 Kelvin)
// This is the minimum temperature possible (AKA absolute zero)
$celsius = -273.16;
}
}