To create a basic app using the github.com/gliderlabs/ssh package, you can follow the steps below. This code assumes you have a JSON file named users.json containing usernames and passwords.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strings"
"golang.org/x/crypto/ssh"
)
type User struct {
Username string `json:"username"`
Password string `json:"password"`
}
type Config struct {
Users []User `json:"users"`
}
func main() {
// Load the users from a JSON file
users, err := loadUsers("users.json")
if err != nil {
log.Fatal(err)
}
// Start the SSH server
err = ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
// Check if the provided username and password matches a user in the list
for _, user := range users {
if ctx.User() == user.Username && pass == user.Password {
return true
}
}
return false
}), ssh.KeyboardInteractive(func(ctx ssh.Context, challenger ssh.KeyboardInteractiveChallenge) bool {
log.Print("keyboard interactive not supported")
return false
}))
if err != nil {
log.Fatal(err)
}
}
func loadUsers(filename string) ([]User, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
return config.Users, nil
}
The above code sets up a basic SSH server using the provided users.json file to authenticate the users. Now, to add the requested functionality, you can modify the main function as follows:
func main() {
// Load the users from a JSON file
users, err := loadUsers("users.json")
if err != nil {
log.Fatal(err)
}
// Start the SSH server
err = ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
// Check if the provided username and password matches a user in the list
for _, user := range users {
if ctx.User() == user.Username && pass == user.Password {
return true
}
}
return false
}), ssh.KeyboardInteractive(func(ctx ssh.Context, challenger ssh.KeyboardInteractiveChallenge) bool {
log.Print("keyboard interactive not supported")
return false
}), ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
log.Print("public key authentication not supported")
return false
}), ssh.Terminal(func(ctx ssh.Context, term *ssh.Terminal) bool {
term.Write([]byte("Welcome to the Basic SSH App!\n"))
term.Prompt = fmt.Sprintf("[%s@cartel]~$ ", ctx.User())
return true
}), ssh.Subsystem(func(ctx ssh.Context, subsystem string) bool {
log.Print("subsystem not supported")
return false
}), ssh.Command(func(ctx ssh.Context, command string) ([]byte, error) {
switch command {
case "help":
return []byte("Commands:\nhelp - shows all commands including this one with descriptions\nwhoami - displays your username\nclear - clears screen and displays welcome message\n"), nil
case "whoami":
return []byte(fmt.Sprintf("Username: %s\n", ctx.User())), nil
case "clear":
log.Print("clearing screen")
term := ctx.Value(ssh.TerminalContextKey).(*ssh.Terminal)
term.Write([]byte("\033[H\033[2J"))
term.Write([]byte("Welcome to the Basic SSH App!\n"))
term.Prompt = fmt.Sprintf("[%s@cartel]~$ ", ctx.User())
return nil, nil
default:
return []byte("Invalid command. Type 'help' to see the available commands.\n"), nil
}
}))
if err != nil {
log.Fatal(err)
}
}
Now, when you run this code and connect to the server using an SSH client, you will see the welcome message and the username in the prompt. You can then execute the three commands: help, whoami, and clear as per the requirements.