Skip to content

Commit

Permalink
Merge pull request #24 from alfiankan/23-use-bash-c-maybe-leak-chaild…
Browse files Browse the repository at this point in the history
…-process

23 use bash c maybe leak chaild process
  • Loading branch information
alfiankan authored Jul 2, 2023
2 parents 8d49ae7 + f182217 commit f34f7f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion empty/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ teleterm:
shell_executor: "/bin/bash"
whitelist:
- <USER_ID>
- <USER_ID>
- <USER_ID>
execution_timeout_second: 10
49 changes: 42 additions & 7 deletions executor/command.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
package executor

import (
"errors"
"fmt"
"os/exec"
"time"

"github.com/spf13/viper"
)

type CommandOutputWriter struct{}
type CommandOutputWriter struct {
TimeoutSecond int
}

type CmdExecutionResult struct {
Stdout []byte
Stderr []byte
}


func (c *CommandOutputWriter) ExecFullOutput(command string) (outOk []byte, outErr []byte, err error) {

executor := viper.GetString("shell_executor")
chResponse := make(chan CmdExecutionResult)

executor := viper.GetString("shell_executor")
if executor == "" {
executor = "/bin/bash"
}

cmd := exec.Command(executor, "-c", command)

out, err := cmd.CombinedOutput()
if err != nil {
outErr = out
return
go func(cnl chan CmdExecutionResult, cmd *exec.Cmd) {

out, err := cmd.CombinedOutput()
errOut := []byte("")
if err != nil {
errOut = []byte(err.Error())
}
cnl <- CmdExecutionResult{Stdout: out, Stderr: errOut}

}(chResponse, cmd)

if c.TimeoutSecond == 0 {
result := <-chResponse
outOk = result.Stdout
outErr = result.Stderr
return
}

select {
case result := <-chResponse:
outOk = result.Stdout
outErr = result.Stderr
cmd.Process.Kill()
case <-time.After(time.Second * time.Duration(c.TimeoutSecond)):
cmd.Process.Kill()
errMsg := "TIMEOUT: EXCEEDED -- Process killed"
err = errors.New(errMsg)
outErr = []byte(errMsg)
}
outOk = out

return
}
Expand Down
20 changes: 12 additions & 8 deletions teleterm/telegram_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ func createButtonReplay(ctx context.Context, persist Persistence, menu *tele.Rep
func Start(ctx context.Context, db *sql.DB, telebotToken string) {

log := common.InitLog()
log.Info().Str("version", "v2.0.0").Msg("Starting Teleterm")
log.Info().Str("version", "v2.2.0").Msg("Starting Teleterm")

commandExecutor := new(executor.CommandOutputWriter)
commandExecutor := executor.CommandOutputWriter{
TimeoutSecond: viper.GetInt("execution_timeout_second"),
}
persist := Persistence{db: db}

pref := tele.Settings{
Expand All @@ -67,12 +69,14 @@ func Start(ctx context.Context, db *sql.DB, telebotToken string) {
}

configWhitelist := viper.GetIntSlice("whitelist")
var whitelist []int64
for _, id := range configWhitelist {
whitelist = append(whitelist, int64(id))
}
b.Use(middleware.Whitelist(whitelist...))

log.Info().Str("state", "white listing").Msg(fmt.Sprintf("total: %v", len(configWhitelist)))
if len(configWhitelist) > 0 {
var whitelist []int64
for _, id := range configWhitelist {
whitelist = append(whitelist, int64(id))
}
b.Use(middleware.Whitelist(whitelist...))
}
menu := &tele.ReplyMarkup{ResizeKeyboard: true}

b.Handle("/refresh", func(c tele.Context) error {
Expand Down

0 comments on commit f34f7f1

Please sign in to comment.