Skip to content

Commit

Permalink
add server-side plugins registry
Browse files Browse the repository at this point in the history
  • Loading branch information
francoismichel committed Mar 25, 2024
1 parent 20f2894 commit cec99f2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
11 changes: 11 additions & 0 deletions auth/plugins/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package plugins

import (
"github.com/francoismichel/ssh3/server_auth"
"github.com/francoismichel/ssh3/internal"
)

// Registers a new server-side auth plugin
func RegisterServerIdentity(name string, identity server_auth.Identity) error {
return internal.RegisterServerIdentity(name, identity)
}
9 changes: 9 additions & 0 deletions cmd/plugin_endpoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
cmd "github.com/francoismichel/ssh3/cmd"
)

func main() {
cmd.ClientMain()
}
3 changes: 3 additions & 0 deletions cmd/ssh3-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.uber.org/zap/zapcore"

ssh3 "github.com/francoismichel/ssh3"
"github.com/francoismichel/ssh3/internal"
ssh3Messages "github.com/francoismichel/ssh3/message"
"github.com/francoismichel/ssh3/server_auth"
util "github.com/francoismichel/ssh3/util"
Expand Down Expand Up @@ -708,6 +709,8 @@ func ServerMain() int {
return 0
}

internal.CloseRegistry()

if !enablePasswordLogin {
fmt.Fprintln(os.Stderr, "password login is disabled")
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/ssh3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/francoismichel/ssh3"
"github.com/francoismichel/ssh3/auth"
"github.com/francoismichel/ssh3/client"
"github.com/francoismichel/ssh3/internal"
"github.com/francoismichel/ssh3/util"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
Expand Down Expand Up @@ -330,6 +331,8 @@ func ClientMain() int {
flag.Parse()
args := flag.Args()

internal.CloseRegistry()

if *displayVersion {
fmt.Fprintln(os.Stdout, filepath.Base(os.Args[0]), "version", ssh3.GetCurrentSoftwareVersion())
return 0
Expand Down
43 changes: 43 additions & 0 deletions internal/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package internal

import (
"sync"

"github.com/francoismichel/ssh3/server_auth"
. "github.com/francoismichel/ssh3/util"
)

type serverPluginsRegistry struct {
registrationsOpen bool
serverPluginIdentities map[string]server_auth.Identity
}

var (
serverPluginsMutex sync.RWMutex
serverRegistry = &serverPluginsRegistry{ registrationsOpen: true, serverPluginIdentities: make(map[string]server_auth.Identity) }
)

// Registers a new server-side auth plugin
func RegisterServerIdentity(name string, identity server_auth.Identity) error {
serverPluginsMutex.Lock()
defer serverPluginsMutex.Unlock()
if !serverRegistry.registrationsOpen {
return ClosedPluginsRegistry{}
}
if identity == nil {
panic("plugin registry is nil")
}
if _, dup := serverRegistry.serverPluginIdentities[name]; dup {
panic("RegisterServerIdentity called twice for same auth plugin name " + name)
}
serverRegistry.serverPluginIdentities[name] = identity
return nil
}

// Closes the registry. This function should not be exported outside the scope of the module
// to avoid plugins closing the registry.
func CloseRegistry() {
serverPluginsMutex.Lock()
defer serverPluginsMutex.Unlock()
serverRegistry.registrationsOpen = false
}
8 changes: 8 additions & 0 deletions util/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func (e InvalidConfig) Error() string {
return fmt.Sprintf("Invalid %s config field: \"%s\"", e.Field, e.Value)
}

type ClosedPluginsRegistry struct {

}

func (e ClosedPluginsRegistry) Error() string {
return "The plugins registry is closed"
}

type BytesReadCloser struct {
*bytes.Reader
}
Expand Down

0 comments on commit cec99f2

Please sign in to comment.