Documentation ¶
Index ¶
- Variables
- func GetDefaultSshServerConfig(_ context.Context) *ssh.ServerConfig
- func KeyboardInteractiveAuth(...)
- func ListenAndServe(addr string, fn GetSshServerConfig, handler Handler, options ...Option) error
- func NewDefaultSshServerConfig() *ssh.ServerConfig
- func PasswordAuth(fn func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error))
- func PublicKeyAuth(fn func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error))
- func ReplaceDefaultSshServerConfig(conf *ssh.ServerConfig) (original *ssh.ServerConfig)
- func ResolveHostKeys(filepaths []string) ([]ssh.Signer, error)
- func ResolveHostKeysWithDecode(filepaths []string, decodeFunc func(pem []byte) ([]byte, error)) ([]ssh.Signer, error)
- func Serve(ln net.Listener, fn GetSshServerConfig, handler Handler, options ...Option) error
- type Conn
- type ConnContext
- type GetSshServerConfig
- type Handler
- type HandlerFunc
- type Option
- func WithConnContext(fn ConnContext) Option
- func WithErrLogger(logger *log.Logger) Option
- func WithGetSshServerConfig(fn GetSshServerConfig) Option
- func WithHandler(h Handler) Option
- func WithHandlerFunc(h HandlerFunc) Option
- func WithIdleTimeout(duration time.Duration) Option
- func WithProxyProtocol(enable bool) Option
- func WithReadTimeout(duration time.Duration) Option
- func WithWriteTimeout(duration time.Duration) Option
- type ServeMux
- type Server
Constants ¶
This section is empty.
Variables ¶
var DefaultServeMux = NewServeMux()
DefaultServeMux is the default ServeMux used by Serve.
var DefaultSshServerConfig = NewDefaultSshServerConfig()
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 ¶
ResolveHostKeys 读取 host key 文件, 并解析文件内容为 ssh.Signer.
Types ¶
type ConnContext ¶
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 WithGetSshServerConfig ¶
func WithGetSshServerConfig(fn GetSshServerConfig) Option
func WithHandler ¶
func WithHandlerFunc ¶
func WithHandlerFunc(h HandlerFunc) Option
func WithIdleTimeout ¶ added in v1.0.2
func WithProxyProtocol ¶
func WithReadTimeout ¶ added in v1.0.2
func WithWriteTimeout ¶ added in v1.0.2
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is an SSH request multiplexer.
func (*ServeMux) Handle ¶
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 (*Server) ListenAndServe ¶
ListenAndServe 监听 TCP 连接, 如果 addr 为空字符串则监听地址为 ":2222"