Skip to content

Commit

Permalink
Merge pull request #8 from mplus-oss/development
Browse files Browse the repository at this point in the history
Release: v0.3.1-alya #6
  • Loading branch information
ikr4-m authored Aug 21, 2024
2 parents 1bafc1f + 12c61be commit 4f77f8c
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 5 deletions.
26 changes: 26 additions & 0 deletions cmd/mdrop-client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/mplus-oss/mdrop/internal"
"golang.org/x/term"
)

func AuthCommand(args []string) {
Expand Down Expand Up @@ -104,6 +107,21 @@ func getTunnelInstance() {

func authPrompt() (config internal.ConfigFile, err error) {
reader := bufio.NewReader(os.Stdin)
stdin := int(syscall.Stdin)
oldState, err := term.GetState(stdin)
if err != nil {
return internal.ConfigFile{}, err
}
defer term.Restore(stdin, oldState)

sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
go func() {
for _ = range sigch {
term.Restore(stdin, oldState)
os.Exit(1)
}
}()

fmt.Print("Instance Name: ")
instanceName, err := reader.ReadString('\n')
Expand Down Expand Up @@ -140,11 +158,19 @@ func authPrompt() (config internal.ConfigFile, err error) {
}
proxy = strings.Replace(proxy, "\n", "", -1)

fmt.Print("Private Key String [Set blank if none]: ")
privateKeyByte, err := term.ReadPassword(stdin)
if err != nil {
return internal.ConfigFile{}, err
}
privateKeyString := strings.Replace(string(privateKeyByte), "\n", "", -1)

config = internal.ConfigFile{
Name: instanceName,
Host: hostname,
Port: portInt,
Proxy: proxy,
Key: privateKeyString,
}
return config, nil
}
2 changes: 1 addition & 1 deletion cmd/mdrop-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

const version string = "v0.2.1-ame"
const version string = "v0.3.1-alya"

const subCmdHelpMeesage string = `
Subcommand:
Expand Down
19 changes: 19 additions & 0 deletions cmd/mdrop-client/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
Expand All @@ -24,6 +25,7 @@ func KillShell(pid int) error {
func StartShellTunnel(s internal.SSHParameter, isTunnel bool) (output string, err error) {
errChan := make(chan error, 0)
outputChan := make(chan string, 0)
debugEnv := os.Getenv("MDROP_SSH_DEBUG")

args := ""
if isTunnel {
Expand All @@ -32,6 +34,9 @@ func StartShellTunnel(s internal.SSHParameter, isTunnel bool) (output string, er
args = s.GenerateConnectSSHArgs()
}

if debugEnv == "1" {
fmt.Println(args)
}
cmd := exec.Command("sh", "-c", args)
stdout, err := cmd.StdoutPipe()
stderr, err := cmd.StderrPipe()
Expand All @@ -45,6 +50,10 @@ func StartShellTunnel(s internal.SSHParameter, isTunnel bool) (output string, er
s.Split(bufio.ScanLines)
for s.Scan() {
m := s.Text()
if debugEnv == "1" {
fmt.Println(m)
}

// Case: If connected
if strings.Contains(m, "Pong!") {
outputChan <- m
Expand All @@ -62,6 +71,12 @@ func StartShellTunnel(s internal.SSHParameter, isTunnel bool) (output string, er
outputChan <- "Not found"
errChan <- errors.New("Port full on server")
}

// Case: Invalid key
if strings.Contains(m, "Invalid key") {
outputChan <- "Invalid key"
errChan <- errors.New("Invalid key on execution")
}
}
}()

Expand All @@ -70,6 +85,10 @@ func StartShellTunnel(s internal.SSHParameter, isTunnel bool) (output string, er
s.Split(bufio.ScanLines)
for s.Scan() {
m := s.Text()
if debugEnv == "1" {
fmt.Println(m)
}

// False flag #1
if strings.Contains(m, "chdir") || strings.Contains(m, "Permanently added") {
continue
Expand Down
37 changes: 36 additions & 1 deletion cmd/mdrop-tunnel-tools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,39 @@ import (
"net"
"os"
"strconv"
"strings"
"time"
)

const KEY_FILE_LOCATION string = "/etc/mdrop-tunnel-key"

func main() {
// Parse args
flag.Parse()
args := flag.Args()

// Check key file
if _, err := os.Stat(KEY_FILE_LOCATION); os.IsNotExist(err) {
fmt.Println("Key/token file not found at /etc/mdrop-tunnel-key")
os.Exit(10)
}
byteKeyFile, err := os.ReadFile(KEY_FILE_LOCATION)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
keyFile := strings.TrimSpace(string(byteKeyFile))

// Check is command contains key or not
if keyFile != "" {
if !implContains(args, keyFile) {
fmt.Println("Invalid key")
os.Exit(10)
}
}

if len(args) < 1 {
fmt.Println("Usage: mdrop-tunnel <subcommand> <args>")
fmt.Println("Usage: mdrop-tunnel <subcommand> [key]")
os.Exit(1)
}

Expand Down Expand Up @@ -63,3 +88,13 @@ func GetPortCommand() {
func PingCommand() {
fmt.Println("Pong!")
}

func implContains(sl []string, name string) (found bool) {
found = false
for _, value := range sl {
if value == name {
found = true
}
}
return found
}
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ services:
sshd_tunnel:
container_name: sshd_tunnel
restart: unless-stopped
environment:
PRIVATE_MODE_TOKEN: mplusjayaselalu
build:
context: .
dockerfile: ./tunnel.Dockerfile
ports:
- 2222:22
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

set -e

# Create keyfile private for mdrop-tunnel
touch /etc/mdrop-tunnel-key
if [[ "$PRIVATE_MODE_TOKEN" != "" ]]; then
echo "$PRIVATE_MODE_TOKEN" >> /etc/mdrop-tunnel-key
fi

# Run MOTD on logs
echo "SSHD successfully launched. For reference, you can use this command to remote port to the container:"
echo " ssh -R <remote>:127.0.0.1:<local> -T -p <port> -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tunnel@localhost <command>"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ require (
github.com/gabriel-vasile/mimetype v1.4.5
github.com/google/uuid v1.6.0
github.com/schollz/progressbar/v3 v3.14.4
golang.org/x/term v0.23.0
)

require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/sys v0.23.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 8 additions & 1 deletion internal/prettify_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import (
)

func PrintErrorWithExit(instance string, err error, exitCode int) {
fmt.Println("\nFatal Error on " + instance + ":\n\t" + err.Error())
debug_env := os.Getenv("MDROP_DEBUG")

message := fmt.Sprintf("Error: %v", err.Error())
if debug_env == "1" {
message = fmt.Sprintf("\nFatal Error on %v:\n\t%v", instance, err.Error())
}

fmt.Println(message)
os.Exit(exitCode)
}

Expand Down
1 change: 1 addition & 0 deletions internal/rw_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ConfigFile struct {
Host string `json:"host"`
Port int `json:"port"`
Proxy string `json:"proxy"`
Key string `json:"key"`
}

func init() {
Expand Down
6 changes: 6 additions & 0 deletions internal/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (s SSHParameter) GenerateConnectSSHArgs() string {
}
args = append(args, s.Command...)
s.GenerateProxyArgs(&args)
if s.ConfigFile.Key != "" {
args = append(args, s.ConfigFile.Key)
}

return "ssh "+strings.Join(args, " ")
}
Expand Down Expand Up @@ -54,6 +57,9 @@ func (s SSHParameter) GenerateRemoteSSHArgs() string {
)
}
s.GenerateProxyArgs(&args)
if s.ConfigFile.Key != "" {
args = append(args, s.ConfigFile.Key)
}

return "ssh "+strings.Join(args, " ")
}
Expand Down

0 comments on commit 4f77f8c

Please sign in to comment.