Skip to content

Commit 90dc72e

Browse files
Minor refactoring
1 parent e31a379 commit 90dc72e

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

internal/deploy/deploy.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package deploy
33
import (
44
"ctf_dashboard/internal/common"
55
"github.com/pkg/sftp"
6+
"github.com/sirupsen/logrus"
67
"golang.org/x/crypto/ssh"
78
"io/ioutil"
89
"os"
@@ -14,7 +15,7 @@ func ValidPublicKey(key string) error {
1415
return err
1516
}
1617

17-
func UploadIdRsa(vulnbox common.Vulnbox, key string, keyfile string) error {
18+
func UploadSSHKey(vulnbox common.Vulnbox, key string, keyfile string) error {
1819
err := ValidPublicKey(key)
1920
if err != nil {
2021
return err
@@ -42,15 +43,21 @@ func UploadIdRsa(vulnbox common.Vulnbox, key string, keyfile string) error {
4243
if err != nil {
4344
return nil
4445
}
45-
defer client.Close()
46+
defer func() {
47+
if err := client.Close(); err != nil {
48+
logrus.Errorf("Error closing client: %v", err)
49+
}
50+
}()
4651

47-
sftp, err := sftp.NewClient(client)
52+
sftpClient, err := sftp.NewClient(client)
4853
if err != nil {
4954
return err
5055
}
51-
defer sftp.Close()
56+
if err := client.Close(); err != nil {
57+
logrus.Errorf("Error closing client: %v", err)
58+
}
5259

53-
authkeys, err := sftp.OpenFile(".ssh/authorized_keys", os.O_APPEND|os.O_CREATE|os.O_WRONLY)
60+
authkeys, err := sftpClient.OpenFile(".ssh/authorized_keys", os.O_APPEND|os.O_CREATE|os.O_WRONLY)
5461
if err != nil {
5562
return err
5663
}

internal/web/routes.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func (s Server) serveStartSploit() gin.HandlerFunc {
4040
}
4141
}
4242

43-
4443
func (s Server) addSSHKey() gin.HandlerFunc {
4544
return func(c *gin.Context) {
4645
var req UploadIdRSARequest
@@ -49,12 +48,11 @@ func (s Server) addSSHKey() gin.HandlerFunc {
4948
return
5049
}
5150
for _, vulnbox := range s.cfg.Vulnboxes {
52-
if err := deploy.UploadIdRsa(vulnbox, req.Key, s.cfg.KeyFile); err != nil {
51+
if err := deploy.UploadSSHKey(vulnbox, req.Key, s.cfg.KeyFile); err != nil {
5352
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
5453
return
5554
}
5655
}
5756
c.JSON(http.StatusOK, gin.H{"status": "ok"})
5857
}
5958
}
60-

0 commit comments

Comments
 (0)