mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 14:08:06 +08:00
fix: 一些被忽略的句柄关闭问题 (#5552)
This commit is contained in:
parent
32e0c6fb3e
commit
064fddb2ee
@ -236,6 +236,7 @@ func (b *BaseApi) DownloadWebsiteSSL(c *gin.Context) {
|
|||||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
info, err := file.Stat()
|
info, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||||
|
@ -129,13 +129,10 @@ func (u *DockerService) LoadDockerConf() *dto.DaemonJsonConf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *DockerService) UpdateConf(req dto.SettingUpdate) error {
|
func (u *DockerService) UpdateConf(req dto.SettingUpdate) error {
|
||||||
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
err := createIfNotExistDaemonJsonFile()
|
||||||
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
_, _ = os.Create(constant.DaemonJsonPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.ReadFile(constant.DaemonJsonPath)
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -217,15 +214,26 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func createIfNotExistDaemonJsonFile() error {
|
||||||
func (u *DockerService) UpdateLogOption(req dto.LogOption) error {
|
|
||||||
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, _ = os.Create(constant.DaemonJsonPath)
|
var daemonFile *os.File
|
||||||
|
daemonFile, err = os.Create(constant.DaemonJsonPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer daemonFile.Close()
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *DockerService) UpdateLogOption(req dto.LogOption) error {
|
||||||
|
err := createIfNotExistDaemonJsonFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
file, err := os.ReadFile(constant.DaemonJsonPath)
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -254,11 +262,9 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error {
|
func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error {
|
||||||
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
err := createIfNotExistDaemonJsonFile()
|
||||||
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
_, _ = os.Create(constant.DaemonJsonPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.ReadFile(constant.DaemonJsonPath)
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
||||||
@ -300,11 +306,9 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error {
|
|||||||
_ = os.Remove(constant.DaemonJsonPath)
|
_ = os.Remove(constant.DaemonJsonPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
err := createIfNotExistDaemonJsonFile()
|
||||||
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
_, _ = os.Create(constant.DaemonJsonPath)
|
|
||||||
}
|
}
|
||||||
file, err := os.OpenFile(constant.DaemonJsonPath, os.O_WRONLY|os.O_TRUNC, 0640)
|
file, err := os.OpenFile(constant.DaemonJsonPath, os.O_WRONLY|os.O_TRUNC, 0640)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -92,6 +91,7 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error {
|
|||||||
return errors.New(string(stdout))
|
return errors.New(string(stdout))
|
||||||
}
|
}
|
||||||
ticker := time.NewTicker(3 * time.Second)
|
ticker := time.NewTicker(3 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
||||||
if err := func() error {
|
if err := func() error {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
@ -195,13 +195,10 @@ func (u *ImageRepoService) CheckConn(host, user, password string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *ImageRepoService) handleRegistries(newHost, delHost, handle string) error {
|
func (u *ImageRepoService) handleRegistries(newHost, delHost, handle string) error {
|
||||||
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
err := createIfNotExistDaemonJsonFile()
|
||||||
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
_, _ = os.Create(constant.DaemonJsonPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
daemonMap := make(map[string]interface{})
|
daemonMap := make(map[string]interface{})
|
||||||
file, err := os.ReadFile(constant.DaemonJsonPath)
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -218,7 +218,7 @@ func (u *SSHService) GenerateSSH(req dto.GenerateSSH) error {
|
|||||||
}
|
}
|
||||||
secretFile := fmt.Sprintf("%s/.ssh/id_item_%s", currentUser.HomeDir, req.EncryptionMode)
|
secretFile := fmt.Sprintf("%s/.ssh/id_item_%s", currentUser.HomeDir, req.EncryptionMode)
|
||||||
secretPubFile := fmt.Sprintf("%s/.ssh/id_item_%s.pub", currentUser.HomeDir, req.EncryptionMode)
|
secretPubFile := fmt.Sprintf("%s/.ssh/id_item_%s.pub", currentUser.HomeDir, req.EncryptionMode)
|
||||||
authFile := currentUser.HomeDir + "/.ssh/authorized_keys"
|
authFilePath := currentUser.HomeDir + "/.ssh/authorized_keys"
|
||||||
|
|
||||||
command := fmt.Sprintf("ssh-keygen -t %s -f %s/.ssh/id_item_%s | echo y", req.EncryptionMode, currentUser.HomeDir, req.EncryptionMode)
|
command := fmt.Sprintf("ssh-keygen -t %s -f %s/.ssh/id_item_%s | echo y", req.EncryptionMode, currentUser.HomeDir, req.EncryptionMode)
|
||||||
if len(req.Password) != 0 {
|
if len(req.Password) != 0 {
|
||||||
@ -235,8 +235,12 @@ func (u *SSHService) GenerateSSH(req dto.GenerateSSH) error {
|
|||||||
_ = os.Remove(secretPubFile)
|
_ = os.Remove(secretPubFile)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if _, err := os.Stat(authFile); err != nil {
|
if _, err := os.Stat(authFilePath); err != nil && errors.Is(err, os.ErrNotExist) {
|
||||||
_, _ = os.Create(authFile)
|
authFile, err := os.Create(authFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer authFile.Close()
|
||||||
}
|
}
|
||||||
stdout1, err := cmd.Execf("cat %s >> %s/.ssh/authorized_keys", secretPubFile, currentUser.HomeDir)
|
stdout1, err := cmd.Execf("cat %s >> %s/.ssh/authorized_keys", secretPubFile, currentUser.HomeDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,9 +21,11 @@ func Init() {
|
|||||||
}
|
}
|
||||||
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
|
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
|
||||||
if _, err := os.Stat(fullPath); err != nil {
|
if _, err := os.Stat(fullPath); err != nil {
|
||||||
if _, err := os.Create(fullPath); err != nil {
|
f, err := os.Create(fullPath)
|
||||||
|
if err != nil {
|
||||||
panic(fmt.Errorf("init db file failed, err: %v", err))
|
panic(fmt.Errorf("init db file failed, err: %v", err))
|
||||||
}
|
}
|
||||||
|
_ = f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
newLogger := logger.New(
|
newLogger := logger.New(
|
||||||
@ -65,9 +67,11 @@ func initMonitorDB(newLogger logger.Interface) {
|
|||||||
}
|
}
|
||||||
fullPath := path.Join(global.CONF.System.DbPath, "monitor.db")
|
fullPath := path.Join(global.CONF.System.DbPath, "monitor.db")
|
||||||
if _, err := os.Stat(fullPath); err != nil {
|
if _, err := os.Stat(fullPath); err != nil {
|
||||||
if _, err := os.Create(fullPath); err != nil {
|
f, err := os.Create(fullPath)
|
||||||
|
if err != nil {
|
||||||
panic(fmt.Errorf("init db file failed, err: %v", err))
|
panic(fmt.Errorf("init db file failed, err: %v", err))
|
||||||
}
|
}
|
||||||
|
_ = f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{
|
db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{
|
||||||
|
@ -27,6 +27,7 @@ func NewParser(filePath string) (*Parser, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
l := newLexer(bufio.NewReader(f))
|
l := newLexer(bufio.NewReader(f))
|
||||||
l.file = filePath
|
l.file = filePath
|
||||||
p := NewParserFromLexer(l)
|
p := NewParserFromLexer(l)
|
||||||
|
@ -131,9 +131,11 @@ func (f *Fail2ban) ListIgnore() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initLocalFile() error {
|
func initLocalFile() error {
|
||||||
if _, err := os.Create(defaultPath); err != nil {
|
f, err := os.Create(defaultPath)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
initFile := `#DEFAULT-START
|
initFile := `#DEFAULT-START
|
||||||
[DEFAULT]
|
[DEFAULT]
|
||||||
bantime = 600
|
bantime = 600
|
||||||
|
Loading…
x
Reference in New Issue
Block a user