Skip to content

Commit

Permalink
feat: 同步修改
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 12, 2024
1 parent d18bf93 commit 99cd473
Show file tree
Hide file tree
Showing 38 changed files with 387 additions and 550 deletions.
8 changes: 7 additions & 1 deletion internal/apps/mysql/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"fmt"
"net/http"
"os"
"regexp"

"github.com/spf13/cast"
Expand Down Expand Up @@ -80,11 +81,16 @@ func (s *Service) Load(w http.ResponseWriter, r *http.Request) {
return
}

raw, err := shell.Execf(`mysqladmin -u root -p "%s" extended-status`, rootPassword)
if err = os.Setenv("MYSQL_PWD", rootPassword); err != nil {
service.Error(w, http.StatusInternalServerError, "设置环境变量失败")
return
}
raw, err := shell.Execf(`mysqladmin -u root extended-status`)
if err != nil {
service.Error(w, http.StatusInternalServerError, "获取负载失败")
return
}
_ = os.Unsetenv("MYSQL_PWD")

var load []map[string]string
expressions := []struct {
Expand Down
58 changes: 15 additions & 43 deletions internal/data/safe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cast"

"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
Expand Down Expand Up @@ -61,59 +60,32 @@ func (r *safeRepo) UpdateSSH(port uint, status bool) error {
}

func (r *safeRepo) GetPingStatus() (bool, error) {
if os.IsRHEL() {
out, err := shell.Execf(`firewall-cmd --list-all`)
if err != nil {
return true, errors.New(out)
}

if !strings.Contains(out, `rule protocol value="icmp" drop`) {
return true, nil
} else {
return false, nil
}
} else {
config, err := io.Read("/etc/ufw/before.rules")
if err != nil {
return true, err
}
if strings.Contains(config, "-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT") {
return true, nil
} else {
return false, nil
}
out, err := shell.Execf(`firewall-cmd --list-all`)
if err != nil {
return true, errors.New(out)
}

if !strings.Contains(out, `rule protocol value="icmp" drop`) {
return true, nil
}

return false, nil
}

func (r *safeRepo) UpdatePingStatus(status bool) error {
var out string
var err error
if os.IsRHEL() {
if status {
out, err = shell.Execf(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`)
} else {
out, err = shell.Execf(`firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'`)
}
if status {
_, err = shell.Execf(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`)
} else {
if status {
out, err = shell.Execf(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/g' /etc/ufw/before.rules`)
} else {
out, err = shell.Execf(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/g' /etc/ufw/before.rules`)
}
_, err = shell.Execf(`firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'`)
}

if err != nil {
return errors.New(out)
}

if os.IsRHEL() {
out, err = shell.Execf(`firewall-cmd --reload`)
} else {
out, err = shell.Execf(`ufw reload`)
return err
}

_, err = shell.Execf(`firewall-cmd --reload`)
if err != nil {
return errors.New(out)
return err
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions internal/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ func (r *settingRepo) Get(key biz.SettingKey, defaultValue ...string) (string, e

func (r *settingRepo) Set(key biz.SettingKey, value string) error {
setting := new(biz.Setting)
if err := app.Orm.Where("key = ?", key).FirstOrInit(setting).Error; err != nil {
return err
if err := app.Orm.Where("key = ?", key).First(setting).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
}

setting.Key = key
setting.Value = value
return app.Orm.Save(setting).Error
}
Expand Down Expand Up @@ -278,6 +281,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {

_, _ = shell.Execf("systemctl daemon-reload")
_ = io.Remove("/tmp/panel-storage.zip")
_ = io.Remove(filepath.Join(app.Root, "panel/config.example.yml"))

return nil
}
2 changes: 1 addition & 1 deletion internal/data/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *sshRepo) UpdateInfo(req *request.SSHUpdateInfo) error {
if err := r.settingRepo.Set(biz.SettingKeySshHost, req.Host); err != nil {
return err
}
if err := r.settingRepo.Set(biz.SettingKeySshPort, req.Port); err != nil {
if err := r.settingRepo.Set(biz.SettingKeySshPort, cast.ToString(req.Port)); err != nil {
return err
}
if err := r.settingRepo.Set(biz.SettingKeySshUser, req.User); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/data/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (r *websiteRepo) Create(req *request.WebsiteCreate) (*biz.Website, error) {
Name: req.Name,
Status: true,
Path: req.Path,
PHP: cast.ToInt(req.PHP),
PHP: req.PHP,
SSL: false,
}
if err := app.Orm.Create(w).Error; err != nil {
Expand Down Expand Up @@ -250,7 +250,7 @@ server
# ssl标记位结束
# php标记位开始
include enable-php-%s.conf;
include enable-php-%d.conf;
# php标记位结束
# 错误页配置,可自行设置
Expand Down
2 changes: 1 addition & 1 deletion internal/http/request/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package request

type SSHUpdateInfo struct {
Host string `json:"host" form:"host"`
Port string `json:"port" form:"port"`
Port int `json:"port" form:"port"`
User string `json:"user" form:"user"`
Password string `json:"password" form:"password"`
}
2 changes: 1 addition & 1 deletion internal/http/request/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type WebsiteCreate struct {
Domains []string `form:"domains" json:"domains"`
Ports []uint `form:"ports" json:"ports"`
Path string `form:"path" json:"path"`
PHP string `form:"php" json:"php"`
PHP int `form:"php" json:"php"`
DB bool `form:"db" json:"db"`
DBType string `form:"db_type" json:"db_type"`
DBName string `form:"db_name" json:"db_name"`
Expand Down
62 changes: 31 additions & 31 deletions internal/route/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,48 +166,48 @@ func Http(r chi.Router) {
container := service.NewContainerService()
r.Get("/", container.List)
r.Get("/search", container.Search)
r.Post("/create", container.Create)
r.Post("/remove", container.Remove)
r.Post("/start", container.Start)
r.Post("/stop", container.Stop)
r.Post("/restart", container.Restart)
r.Post("/pause", container.Pause)
r.Post("/unpause", container.Unpause)
r.Get("/inspect", container.Inspect)
r.Post("/kill", container.Kill)
r.Post("/rename", container.Rename)
r.Get("/stats", container.Stats)
r.Get("/exist", container.Exist)
r.Get("/logs", container.Logs)
r.Post("/", container.Create)
r.Delete("/{id}", container.Remove)
r.Post("/{id}/start", container.Start)
r.Post("/{id}/stop", container.Stop)
r.Post("/{id}/restart", container.Restart)
r.Post("/{id}/pause", container.Pause)
r.Post("/{id}/unpause", container.Unpause)
r.Get("/{id}/inspect", container.Inspect)
r.Post("/{id}/kill", container.Kill)
r.Post("/{id}/rename", container.Rename)
r.Get("/{id}/stats", container.Stats)
r.Get("/{id}/exist", container.Exist)
r.Get("/{id}/logs", container.Logs)
r.Post("/prune", container.Prune)
})
r.Route("/network", func(r chi.Router) {
containerNetwork := service.NewContainerNetworkService()
r.Get("/list", containerNetwork.List)
r.Post("/create", containerNetwork.Create)
r.Post("/remove", containerNetwork.Remove)
r.Get("/exist", containerNetwork.Exist)
r.Get("/inspect", containerNetwork.Inspect)
r.Post("/connect", containerNetwork.Connect)
r.Post("/disconnect", containerNetwork.Disconnect)
r.Get("/", containerNetwork.List)
r.Post("/", containerNetwork.Create)
r.Delete("/{id}", containerNetwork.Remove)
r.Get("/{id}/exist", containerNetwork.Exist)
r.Get("/{id}/inspect", containerNetwork.Inspect)
r.Post("/{network}/connect", containerNetwork.Connect)
r.Post("/{network}/disconnect", containerNetwork.Disconnect)
r.Post("/prune", containerNetwork.Prune)
})
r.Route("/image", func(r chi.Router) {
containerImage := service.NewContainerImageService()
r.Get("/list", containerImage.List)
r.Get("/exist", containerImage.Exist)
r.Post("/pull", containerImage.Pull)
r.Post("/remove", containerImage.Remove)
r.Get("/inspect", containerImage.Inspect)
r.Get("/", containerImage.List)
r.Get("/{id}/exist", containerImage.Exist)
r.Post("/", containerImage.Pull)
r.Delete("/{id}", containerImage.Remove)
r.Get("/{id}", containerImage.Inspect)
r.Post("/prune", containerImage.Prune)
})
r.Route("/volume", func(r chi.Router) {
containerVolume := service.NewContainerVolumeService()
r.Get("/list", containerVolume.List)
r.Post("/create", containerVolume.Create)
r.Get("/exist", containerVolume.Exist)
r.Post("/remove", containerVolume.Remove)
r.Get("/inspect", containerVolume.Inspect)
r.Get("/", containerVolume.List)
r.Post("/", containerVolume.Create)
r.Get("/{id}/exist", containerVolume.Exist)
r.Delete("/{id}", containerVolume.Remove)
r.Get("/{id}", containerVolume.Inspect)
r.Post("/prune", containerVolume.Prune)
})
})
Expand All @@ -219,7 +219,7 @@ func Http(r chi.Router) {
r.Get("/content", file.Content)
r.Post("/save", file.Save)
r.Post("/delete", file.Delete)
r.Post("/upload", file.Upload)
r.Post("/upload", file.Upload) // TODO fix
r.Post("/move", file.Move)
r.Post("/copy", file.Copy)
r.Get("/download", file.Download)
Expand Down
28 changes: 4 additions & 24 deletions internal/service/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"path/filepath"

"github.com/go-rat/utils/env"
"github.com/go-rat/utils/hash"
"github.com/goccy/go-yaml"
"github.com/gookit/color"
Expand Down Expand Up @@ -61,30 +60,11 @@ func (s *CliService) Update(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("获取最新版本失败:%v", err)
}

// TODO 需要修改接口直接把arch传过去
var ver, url, checksum string
if env.IsX86() {
for _, v := range panel.Downloads {
if v.Arch == "amd64" {
ver = panel.Version
url = v.URL
checksum = v.Checksum
break
}
}
} else if env.IsArm() {
for _, v := range panel.Downloads {
if v.Arch == "arm64" {
ver = panel.Version
url = v.URL
checksum = v.Checksum
break
}

}
} else {
return errors.New("不支持的架构")
download := str.FirstElement(panel.Downloads)
if download == nil {
return fmt.Errorf("下载地址为空")
}
ver, url, checksum := panel.Version, download.URL, download.Checksum

return s.setting.UpdatePanel(ver, url, checksum)
}
Expand Down
29 changes: 5 additions & 24 deletions internal/service/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/go-rat/chix"
"github.com/go-rat/utils/env"
"github.com/hashicorp/go-version"
"github.com/spf13/cast"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/TheTNB/panel/pkg/api"
"github.com/TheTNB/panel/pkg/db"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/str"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/pkg/types"
)
Expand Down Expand Up @@ -285,31 +285,12 @@ func (s *InfoService) Update(w http.ResponseWriter, r *http.Request) {
return
}

// TODO 需要修改接口直接把arch传过去
var ver, url, checksum string
if env.IsX86() {
for _, v := range panel.Downloads {
if v.Arch == "amd64" {
ver = panel.Version
url = v.URL
checksum = v.Checksum
break
}
}
} else if env.IsArm() {
for _, v := range panel.Downloads {
if v.Arch == "arm64" {
ver = panel.Version
url = v.URL
checksum = v.Checksum
break
}

}
} else {
Error(w, http.StatusInternalServerError, "不支持的架构")
download := str.FirstElement(panel.Downloads)
if download == nil {
Error(w, http.StatusInternalServerError, "获取下载链接失败")
return
}
ver, url, checksum := panel.Version, download.URL, download.Checksum

types.Status = types.StatusUpgrade
if err = s.settingRepo.UpdatePanel(ver, url, checksum); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions internal/service/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ func (s *SSHService) Session(w http.ResponseWriter, r *http.Request) {
upGrader := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
return true
},
}

ws, err := upGrader.Upgrade(w, r, nil)
Expand Down
Loading

0 comments on commit 99cd473

Please sign in to comment.