fix the code: errors: command-line-arguments .\main.go:8:5: "os" imported and not used…
- User
fix the code:
errors:
command-line-arguments
.\main.go:8:5: "os" imported and not used .\main.go:28:26: undefined: ssh.ServerConfig .\main.go:33:26: undefined: Asset .\main.go:40:25: undefined: ssh.ParsePrivateKey .\main.go:48:14: undefined: ssh.NewChanIdiom .\main.go:48:27: undefined: s .\main.go:57:32: undefined: ssh.ConnMetadata .\main.go:110:56: rw.ReadCh undefined (type io.ReadWriter has no field or method ReadCh) .\main.go:128:24: rw.ReadCh undefined (type io.ReadWriter has no field or method ReadCh)
code:
package main
import ( "fmt" "strings" "github.com/gliderlabs/ssh" "golang.org/x/crypto/ssh/terminal" "os" "io" )
var ( users = map[string]string{ "alice": "password1", "bob": "password2", "charlie": "password3", }
code// Create a custom ReadWriter rw io.ReadWriter)
func main() { // Attach the sessionHandler to the SSH server ssh.Handle(sessionHandler)
code// Set up server configuration serverConfig := &ssh.ServerConfig{ PasswordCallback: passwordCallback, } // Generate private key and check for errors privateBytes, err := Asset("private_key.pem") if err != nil { fmt.Println("Failed to load private key:", err) return } // Add the private key to the server’s configuration private, err := ssh.ParsePrivateKey(privateBytes) if err != nil { fmt.Println("Failed to parse private key:", err) return } serverConfig.AddHostKey(private) // Create a custom ReadWriter using the session’s channel rw = ssh.NewChanIdiom(s) // Start the SSH server err = ssh.ListenAndServe(":2222", nil, serverConfig) if err != nil { fmt.Println("Error starting SSH server:", err) }}
func passwordCallback(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) { username := conn.User()
codestoredPassword, found := users[strings.TrimSpace(username)] if found && storedPassword == string(password) { return nil, nil } return nil, fmt.Errorf("login failed") - Assistant