Skip to content

Commit 0f4eef8

Browse files
committed
fix: Code refactoring
1 parent 75033b3 commit 0f4eef8

File tree

14 files changed

+75
-30
lines changed

14 files changed

+75
-30
lines changed

app/handlers/external.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func ExternalAPI(c *fiber.Ctx) error {
16-
extension := &models.Extension{}
16+
var extension *models.Extension
1717
var err error
1818
if len(c.FormValue("extension_id")) > 0 {
1919
if !helpers.CheckUUID(c.FormValue("extension_id")) {

app/handlers/script.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ func ScriptRunner(c *fiber.Ctx) error {
4545
remotePath := ""
4646
if server.Os == "linux" {
4747
remotePath = "/tmp/" + filepath.Base(c.FormValue("local_path"))
48-
session.Run("rm " + remotePath)
48+
_, err := session.Run("rm " + remotePath)
49+
if err != nil {
50+
return err
51+
}
4952
} else {
5053
remotePath = session.WindowsPath + c.FormValue("remote_path") + ".ps1"
5154
}
@@ -57,7 +60,10 @@ func ScriptRunner(c *fiber.Ctx) error {
5760

5861
output := ""
5962
if server.Os == "linux" {
60-
session.Run("chmod +x " + remotePath)
63+
_, err := session.Run("chmod +x " + remotePath)
64+
if err != nil {
65+
return err
66+
}
6167

6268
if c.FormValue("root") == "yes" {
6369
credentials, err := liman.GetCredentials(&models.User{ID: c.Locals("user_id").(string)}, server)

app/middleware/app_logger/new.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,18 @@ func New() fiber.Handler {
2020
if !helpers.Contains(logger.ALL, c.Path()) {
2121
return c.Next()
2222
}
23-
break
2423
case "1":
2524
if !helpers.Contains(logger.MINIMAL, c.Path()) {
2625
return c.Next()
2726
}
28-
break
2927
case "2":
3028
if !helpers.Contains(logger.EXT_LOG, c.Path()) {
3129
return c.Next()
3230
}
33-
break
3431
case "3":
3532
if !helpers.Contains(logger.EXT_DETAIL, c.Path()) {
3633
return c.Next()
3734
}
38-
break
3935
default:
4036
return c.Next()
4137
}

internal/bridge/clean.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/limanmys/render-engine/pkg/helpers"
8+
"github.com/limanmys/render-engine/pkg/logger"
89
)
910

1011
func Clean() {
@@ -44,7 +45,11 @@ func Clean() {
4445
case <-ch:
4546
return
4647
default:
47-
session.SSH.SendRequest("[email protected]", true, nil)
48+
_, _, err := session.SSH.SendRequest("[email protected]", true, nil)
49+
if err != nil {
50+
logger.Sugar().Warnw("error when sending request")
51+
}
52+
4853
ch <- 1
4954
}
5055
}()
@@ -85,7 +90,11 @@ func Clean() {
8590
case <-ch:
8691
return
8792
default:
88-
tunnel.SshClient.SendRequest("[email protected]", true, nil)
93+
_, _, err := tunnel.SshClient.SendRequest("[email protected]", true, nil)
94+
if err != nil {
95+
logger.Sugar().Warnw("error when sending request")
96+
}
97+
8998
ch <- 1
9099
}
91100
}()

internal/bridge/session.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bridge
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/base64"
67
"errors"
78
"io"
@@ -11,6 +12,7 @@ import (
1112

1213
"github.com/acarl005/stripansi"
1314
"github.com/hirochachacha/go-smb2"
15+
"github.com/limanmys/render-engine/pkg/logger"
1416
"github.com/masterzen/winrm"
1517
"github.com/pkg/sftp"
1618
"golang.org/x/crypto/ssh"
@@ -35,23 +37,36 @@ type Session struct {
3537

3638
func (s *Session) CloseAllConnections() {
3739
if s.SSH != nil {
38-
s.SSH.Close()
40+
err := s.SSH.Close()
41+
if err != nil {
42+
logger.Sugar().Warnw("cannot close ssh session")
43+
}
3944
}
4045

4146
if s.SFTP != nil {
42-
s.SFTP.Close()
47+
err := s.SFTP.Close()
48+
if err != nil {
49+
logger.Sugar().Warnw("cannot close sftp session")
50+
}
4351
}
4452

4553
if s.SMB != nil {
46-
s.SMB.Logoff()
54+
err := s.SMB.Logoff()
55+
if err != nil {
56+
logger.Sugar().Warnw("cannot close smb session")
57+
}
4758
}
4859
}
4960

5061
func (val *Session) checkOutput(in io.Writer, output *bytes.Buffer) bool {
5162
val.Mutex.Lock()
5263
defer val.Mutex.Unlock()
5364
if output != nil && output.Len() > 0 && strings.Contains(output.String(), "liman-pass-sudo") {
54-
in.Write([]byte(val.password + "\n"))
65+
_, err := in.Write([]byte(val.password + "\n"))
66+
if err != nil {
67+
logger.Sugar().Warnw("cannot write sudo password")
68+
return false
69+
}
5570
return true
5671
}
5772
return false
@@ -99,7 +114,10 @@ func (val *Session) Run(command string) (string, error) {
99114
}
100115
}(in, stdoutB, endChan)
101116
}
102-
sess.Run("(" + command + ") 2> /dev/null")
117+
err = sess.Run("(" + command + ") 2> /dev/null")
118+
if err != nil {
119+
return err.Error(), err
120+
}
103121

104122
tmp := strings.Split(stdoutB.String(), "liman-pass-sudo")
105123
output := tmp[len(tmp)-1]
@@ -110,7 +128,7 @@ func (val *Session) Run(command string) (string, error) {
110128
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
111129
encoded, _ := encoder.String(command)
112130
command = base64.StdEncoding.EncodeToString([]byte(encoded))
113-
stdout, stderr, _, err := val.WinRM.RunWithString("powershell.exe -encodedCommand "+command, "")
131+
stdout, stderr, _, err := val.WinRM.RunWithContextWithString(context.TODO(), "powershell.exe -encodedCommand "+command, "")
114132
if err != nil {
115133
return "", err
116134
}

internal/bridge/sftp.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ func (s *Session) SftpGetFile(localPath, remotePath string) error {
8080

8181
_, err = os.Stat(localPath)
8282
if os.IsNotExist(err) {
83-
os.Create(localPath)
83+
_, err = os.Create(localPath)
84+
if err != nil {
85+
return err
86+
}
8487
}
8588

8689
srcFile, err := os.OpenFile(localPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)

internal/bridge/smb.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ func (s *Session) SmbGetFile(localPath, remotePath, remoteDisk string) error {
8181

8282
_, err = os.Stat(localPath)
8383
if os.IsNotExist(err) {
84-
os.Create(localPath)
84+
_, err = os.Create(localPath)
85+
if err != nil {
86+
return err
87+
}
8588
}
8689

8790
srcFile, err := os.OpenFile(localPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)

internal/bridge/ssh_tunnel.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,8 @@ func (t *Tunnel) bindTunnel(ctx context.Context, wg *sync.WaitGroup, hasError *b
246246
}
247247
}()
248248

249-
select {
250-
case <-ctx.Done():
251-
return
252-
}
249+
<-ctx.Done()
250+
return
253251
}
254252
}
255253

internal/bridge/winrm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bridge
22

33
import (
4+
"context"
45
"strconv"
56
"strings"
67

@@ -38,6 +39,6 @@ func VerifyWinRm(username, password, host, port string, secure bool) bool {
3839
return false
3940
}
4041

41-
stdout, _, _, _ := client.RunWithString("hostname", "")
42+
stdout, _, _, _ := client.RunWithContextWithString(context.TODO(), "hostname", "")
4243
return strings.TrimSpace(stdout) != ""
4344
}

internal/liman/credentials.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ func GetCredentials(user *models.User, server *models.Server) (*models.Credentia
2626
}
2727
}
2828

29-
json.Unmarshal(
29+
err := json.Unmarshal(
3030
[]byte(serverKey.Data),
3131
encryptedKey,
3232
)
33+
if err != nil {
34+
return nil, err
35+
}
3336

3437
credentials := encryptedKey.DecryptData(&models.User{ID: encrypterUser}, server)
3538
credentials.Type = serverKey.Type

0 commit comments

Comments
 (0)