-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (48 loc) · 1.52 KB
/
main.go
File metadata and controls
56 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//go:build full || e2e
package main
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tg123/sshpiper/libplugin"
"github.com/urfave/cli/v2"
)
func parseTargetUser(raw string) (target string, username string, err error) {
// Expect format: [target:port]+user
parts := strings.SplitN(raw, "+", 2)
if len(parts) != 2 {
err = fmt.Errorf("invalid format (expected target:port+user)")
return
}
target = parts[0]
username = parts[1]
return
}
func main() {
libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{
Name: "username-router",
Usage: "routing based on target inside username, format: 'target:port+realuser@sshpiper-host'",
CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) {
return &libplugin.SshPiperPluginConfig{
PasswordCallback: func(conn libplugin.ConnMetadata, password []byte) (*libplugin.Upstream, error) {
address, user, err := parseTargetUser(conn.User())
if err != nil {
return nil, fmt.Errorf("invalid username format %q: %w", conn.User(), err)
}
host, port, err := libplugin.SplitHostPortForSSH(address)
if err != nil {
return nil, fmt.Errorf("invalid target address %q: %w", address, err)
}
log.Info("routing to address ", address, " with user ", user)
return &libplugin.Upstream{
UserName: user,
Host: host,
Port: int32(port),
IgnoreHostKey: true,
Auth: libplugin.CreatePasswordAuth(password),
}, nil
},
}, nil
},
})
}