Skip to content
Snippets Groups Projects
user avatar
David Cowden authored
Group related concepts more closely. Add more color to the Server
introduction.
ac2d2ff9
History

sshutil

A single-dependency utility package that provides a net/http style SSH server.

sshutil is part of the Smallstep crypto suite (step, step-ca, etc.).

Why sshutil?

The sshutil package depends solely on the Go x/crypto module. The x/crypto/ssh package provides convenient support for both the ssh wire protocol and the ssh authentication protocol. The authentication protocol API is, however, scoped to single connections—whereas servers generally accept many connections. A small, but tedious, amount of work is required to implement a full connection-tracking server for production-like settings. sshutil fills in the gap.

Examples

Hello SSH

package main

import "go.step.sm/sshutil"

func() hello(stream sshutil.Session) {
	stream.Terminal.Write([]byte("Hello SSH\n")
}

func main() {
	server := &sshutil.Server{Addr: ":2022"}
	server.Channel("session", sshutil.NewSessionHandler(hello))
	server.ListenAndServe()
}

Output:

$ ./main
$ ssh localhost -p 2022
Hello SSH
Server closed remote connection to localhost.

Echo server

The default session handler is an echo server. Easily configure a persistent host key.

package main

import (
	"log"

	"go.step.sm/sshutil"
)

func main() {
	server := &sshutil.Server{
		Addr: ":2022",
		Config: sshutil.DefaultServerConfig(),
	}

	key, err := sshutil.LoadHostKeyFromFile("example/server.key", "")
	if err != nil {
		log.Fatalf("error loading key: %v", err)
	}
	server.Config.AddHostKey(key)

	err = server.ListenAndServe()
	log.Print(err)
}

Output:

$ ssh localhost -p 2022
> echo
echo
> ^D
Client closed connection to localhost.