sshd

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2023 License: MIT Imports: 13 Imported by: 0

README

参考 net/http 来设计的简单 sshd 框架, 简单易用.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultServeMux = NewServeMux()

DefaultServeMux is the default ServeMux used by Serve.

View Source
var DefaultSshServerConfig = NewDefaultSshServerConfig()
View Source
var (
	ErrServerClosed = errors.New("sshd: server closed")
)

Functions

func GetDefaultSshServerConfig

func GetDefaultSshServerConfig(_ context.Context) *ssh.ServerConfig

GetDefaultSshServerConfig 获取默认的 ssh.ServerConfig, 同时适配 GetSshServerConfig

func KeyboardInteractiveAuth

func KeyboardInteractiveAuth(fn func(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error))

KeyboardInteractiveAuth 通过交互方式认证

func ListenAndServe

func ListenAndServe(addr string, fn GetSshServerConfig, handler Handler, options ...Option) error

func NewDefaultSshServerConfig

func NewDefaultSshServerConfig() *ssh.ServerConfig

NewDefaultSshServerConfig 创建一个默认的 ssh.ServerConfig. Host key 类型为 rsa, bit size 为 3072.

算法集与 golang.org/x/crypto/ssh 对应

func PasswordAuth

func PasswordAuth(fn func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error))

PasswordAuth 通过密码认证

func PublicKeyAuth

func PublicKeyAuth(fn func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error))

PublicKeyAuth 通过公钥认证, 如果认证不通过, error 应返回非 nil.

func ReplaceDefaultSshServerConfig

func ReplaceDefaultSshServerConfig(conf *ssh.ServerConfig) (original *ssh.ServerConfig)

ReplaceDefaultSshServerConfig 替换默认的 ssh.ServerConfig

func ResolveHostKeys

func ResolveHostKeys(filepaths []string) ([]ssh.Signer, error)

ResolveHostKeys 读取 host key 文件, 并解析文件内容为 ssh.Signer.

func ResolveHostKeysWithDecode

func ResolveHostKeysWithDecode(filepaths []string, decodeFunc func(pem []byte) ([]byte, error)) ([]ssh.Signer, error)

ResolveHostKeysWithDecode 读取 host key 文件, 并解析文件内容. 实现 decodeFunc 函数可对文件内容解码, 再解析为 ssh.Signer.

func Serve

func Serve(ln net.Listener, fn GetSshServerConfig, handler Handler, options ...Option) error

Types

type Conn added in v1.0.2

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Conn) Close added in v1.0.2

func (c *Conn) Close() error

func (*Conn) Read added in v1.0.2

func (c *Conn) Read(b []byte) (int, error)

func (*Conn) Write added in v1.0.2

func (c *Conn) Write(b []byte) (int, error)

type ConnContext

type ConnContext func(conn net.Conn) context.Context

ConnContext 为 TCP 连接创建 context, 如在 context 中注入 uuid 等.

type GetSshServerConfig

type GetSshServerConfig func(ctx context.Context) *ssh.ServerConfig

GetSshServerConfig 获取 *ssh.ServerConfig 实例的函数, 将在 ssh 握手前调用.

type Handler

type Handler interface {
	// ServeChannel 对 ssh.NewChannel 处理, ctx 来源于 ConnContext,
	// 如果返回的 error 不为 nil 将会在 Server 中输出日志.
	ServeChannel(ctx context.Context, conn *ssh.ServerConn, newChannel ssh.NewChannel) error
}

Handler 在建立 channel 时调用 ServeChannel.

type HandlerFunc

type HandlerFunc func(ctx context.Context, conn *ssh.ServerConn, newChannel ssh.NewChannel) error

HandlerFunc 函数类型的 Handler

func (HandlerFunc) ServeChannel

func (h HandlerFunc) ServeChannel(ctx context.Context, conn *ssh.ServerConn, newChannel ssh.NewChannel) error

ServeChannel implements Handler.ServeChannel

type Option

type Option func(srv *Server)

Option 创建 Server 的可选类型

func WithConnContext

func WithConnContext(fn ConnContext) Option

func WithErrLogger

func WithErrLogger(logger *log.Logger) Option

func WithGetSshServerConfig

func WithGetSshServerConfig(fn GetSshServerConfig) Option

func WithHandler

func WithHandler(h Handler) Option

func WithHandlerFunc

func WithHandlerFunc(h HandlerFunc) Option

func WithIdleTimeout added in v1.0.2

func WithIdleTimeout(duration time.Duration) Option

func WithProxyProtocol

func WithProxyProtocol(enable bool) Option

func WithReadTimeout added in v1.0.2

func WithReadTimeout(duration time.Duration) Option

func WithWriteTimeout added in v1.0.2

func WithWriteTimeout(duration time.Duration) Option

type ServeMux

type ServeMux struct {
	// contains filtered or unexported fields
}

ServeMux is an SSH request multiplexer.

func NewServeMux

func NewServeMux() *ServeMux

NewServeMux allocates and returns a new ServeMux.

func (*ServeMux) Handle

func (mux *ServeMux) Handle(channelType string, handler Handler)

Handle registers the handler for the given channel type. Panics If a handler already existed for channel type.

func (*ServeMux) HandleFunc

func (mux *ServeMux) HandleFunc(channelType string, handler func(context.Context, *ssh.ServerConn, ssh.NewChannel) error)

HandleFunc registers the handler function for the given channel type.

func (*ServeMux) ServeChannel

func (mux *ServeMux) ServeChannel(ctx context.Context, conn *ssh.ServerConn, newChannel ssh.NewChannel) error

ServeChannel implements Handler

type Server

type Server struct {

	// ProxyProtocol 如果开启, 将可以解析 PROXY header.
	ProxyProtocol bool

	// ConnContext 为 TCP 连接创建 context, 如在 context 中注入 uuid 等.
	// 将在建立 TCP 连接之后调用.
	ConnContext ConnContext

	// GetSshServerConfig 获取 *ssh.ServerConfig 实例的函数, 将在 ssh 握手前调用.
	// 入参的 context 来源于 ConnContext.
	GetSshServerConfig GetSshServerConfig

	// Handler 建立 ssh channel 时调用 Handler.ServeChannel.
	// 默认为 DefaultServeMux, 不会处理任何类型的 channel.
	Handler Handler

	// ReadTimeout 读超时时间, 在读取数据时重置读超时
	ReadTimeout time.Duration
	// WriteTimeout 写超时时间, 在写入数据时重置写超时
	WriteTimeout time.Duration
	// IdleTimeout 连接空闲时间, 默认为 30m, 在关闭 tcp 连接时设置读写超时
	IdleTimeout time.Duration

	// ErrLogger 输出捕获到的错误日志, 默认为 log.Default
	ErrLogger *log.Logger
	// contains filtered or unexported fields
}

Server 支持为每一个 TCP 连接创建独立的 context, 确保在多次认证情况下 context 唯一. 支持 PROXY protocol, 并能够在处理 Channel 的请求时获取到 PROXY protocol 源数据.

func NewServer

func NewServer(fn GetSshServerConfig, handler Handler, options ...Option) *Server

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(addr string) error

ListenAndServe 监听 TCP 连接, 如果 addr 为空字符串则监听地址为 ":2222"

func (*Server) Serve

func (srv *Server) Serve(ln net.Listener) error

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

Shutdown 入参的 context, 如果非空则可用于优雅关闭

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL