mirror of
https://github.com/moul/sshportal.git
synced 2025-11-10 07:20:32 +08:00
Implement proxied connections
The feature is implemented as follows: - when creating a host, there is a possiblity to add a "hop" - hops are referend them with the name of the host in sshportal - the hop ID is then saved in the DB in the hosts table - when connecting to a host, sshportal will recurse through all the possible hops of a host (allowing chained proxies)
This commit is contained in:
parent
e6a02a85f0
commit
75c6840ecd
5 changed files with 102 additions and 29 deletions
2
db.go
2
db.go
|
|
@ -63,6 +63,8 @@ type Host struct {
|
||||||
HostKey []byte `sql:"size:10000" valid:"optional"`
|
HostKey []byte `sql:"size:10000" valid:"optional"`
|
||||||
Groups []*HostGroup `gorm:"many2many:host_host_groups;"`
|
Groups []*HostGroup `gorm:"many2many:host_host_groups;"`
|
||||||
Comment string `valid:"optional"`
|
Comment string `valid:"optional"`
|
||||||
|
Hop *Host
|
||||||
|
HopID uint
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserKey defines a user public key used by sshportal to identify the user
|
// UserKey defines a user public key used by sshportal to identify the user
|
||||||
|
|
|
||||||
24
dbinit.go
24
dbinit.go
|
|
@ -458,6 +458,30 @@ func dbInit(db *gorm.DB) error {
|
||||||
Rollback: func(tx *gorm.DB) error {
|
Rollback: func(tx *gorm.DB) error {
|
||||||
return fmt.Errorf("not implemented")
|
return fmt.Errorf("not implemented")
|
||||||
},
|
},
|
||||||
|
}, {
|
||||||
|
ID: "29",
|
||||||
|
Migrate: func(tx *gorm.DB) error {
|
||||||
|
type Host struct {
|
||||||
|
// FIXME: use uuid for ID
|
||||||
|
gorm.Model
|
||||||
|
Name string `gorm:"size:32"`
|
||||||
|
Addr string
|
||||||
|
User string
|
||||||
|
Password string
|
||||||
|
URL string
|
||||||
|
SSHKey *SSHKey `gorm:"ForeignKey:SSHKeyID"`
|
||||||
|
SSHKeyID uint `gorm:"index"`
|
||||||
|
HostKey []byte `sql:"size:10000"`
|
||||||
|
Groups []*HostGroup `gorm:"many2many:host_host_groups;"`
|
||||||
|
Comment string
|
||||||
|
Hop *Host
|
||||||
|
HopID uint
|
||||||
|
}
|
||||||
|
return tx.AutoMigrate(&Host{}).Error
|
||||||
|
},
|
||||||
|
Rollback: func(tx *gorm.DB) error {
|
||||||
|
return fmt.Errorf("not implemented")
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err := m.Migrate(); err != nil {
|
if err := m.Migrate(); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@ package bastionsession
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"os"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/gliderlabs/ssh"
|
|
||||||
"github.com/arkan/bastion/pkg/logchannel"
|
"github.com/arkan/bastion/pkg/logchannel"
|
||||||
|
"github.com/gliderlabs/ssh"
|
||||||
gossh "golang.org/x/crypto/ssh"
|
gossh "golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,7 +19,7 @@ type Config struct {
|
||||||
ClientConfig *gossh.ClientConfig
|
ClientConfig *gossh.ClientConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChannelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx ssh.Context, config Config) error {
|
func MultiChannelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx ssh.Context, configs []Config) error {
|
||||||
if newChan.ChannelType() != "session" {
|
if newChan.ChannelType() != "session" {
|
||||||
newChan.Reject(gossh.UnknownChannelType, "unsupported channel type")
|
newChan.Reject(gossh.UnknownChannelType, "unsupported channel type")
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -31,19 +31,37 @@ func ChannelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewCh
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// open client channel
|
var lastClient *gossh.Client
|
||||||
rconn, err := gossh.Dial("tcp", config.Addr, config.ClientConfig)
|
|
||||||
if err != nil {
|
// go through all the hops
|
||||||
return err
|
for _, config := range configs {
|
||||||
|
var client *gossh.Client
|
||||||
|
if lastClient == nil {
|
||||||
|
client, err = gossh.Dial("tcp", config.Addr, config.ClientConfig)
|
||||||
|
} else {
|
||||||
|
rconn, err := lastClient.Dial("tcp", config.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ncc, chans, reqs, err := gossh.NewClientConn(rconn, config.Addr, config.ClientConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
client = gossh.NewClient(ncc, chans, reqs)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = client.Close() }()
|
||||||
|
lastClient = client
|
||||||
}
|
}
|
||||||
defer func() { _ = rconn.Close() }()
|
rch, rreqs, err := lastClient.OpenChannel("session", []byte{})
|
||||||
rch, rreqs, err := rconn.OpenChannel("session", []byte{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
user := conn.User()
|
user := conn.User()
|
||||||
// pipe everything
|
// pipe everything
|
||||||
return pipe(lreqs, rreqs, lch, rch, config.Logs, user)
|
return pipe(lreqs, rreqs, lch, rch, configs[len(configs)-1].Logs, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pipe(lreqs, rreqs <-chan *gossh.Request, lch, rch gossh.Channel, logsLocation string, user string) error {
|
func pipe(lreqs, rreqs <-chan *gossh.Request, lch, rch gossh.Channel, logsLocation string, user string) error {
|
||||||
|
|
|
||||||
20
shell.go
20
shell.go
|
|
@ -642,6 +642,7 @@ GLOBAL OPTIONS:
|
||||||
cli.StringFlag{Name: "password, p", Usage: "If present, sshportal will use password-based authentication"},
|
cli.StringFlag{Name: "password, p", Usage: "If present, sshportal will use password-based authentication"},
|
||||||
cli.StringFlag{Name: "comment, c"},
|
cli.StringFlag{Name: "comment, c"},
|
||||||
cli.StringFlag{Name: "key, k", Usage: "`KEY` to use for authentication"},
|
cli.StringFlag{Name: "key, k", Usage: "`KEY` to use for authentication"},
|
||||||
|
cli.StringFlag{Name: "hop, o", Usage: "Hop to use for connecting to the server"},
|
||||||
cli.StringSliceFlag{Name: "group, g", Usage: "Assigns the host to `HOSTGROUPS` (default: \"default\")"},
|
cli.StringSliceFlag{Name: "group, g", Usage: "Assigns the host to `HOSTGROUPS` (default: \"default\")"},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
|
|
@ -665,7 +666,13 @@ GLOBAL OPTIONS:
|
||||||
host.Password = c.String("password")
|
host.Password = c.String("password")
|
||||||
}
|
}
|
||||||
host.Name = strings.Split(host.Hostname(), ".")[0]
|
host.Name = strings.Split(host.Hostname(), ".")[0]
|
||||||
|
if c.String("hop") != "" {
|
||||||
|
hop, err := HostByName(db, c.String("hop"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
host.Hop = hop
|
||||||
|
}
|
||||||
if c.String("name") != "" {
|
if c.String("name") != "" {
|
||||||
host.Name = c.String("name")
|
host.Name = c.String("name")
|
||||||
}
|
}
|
||||||
|
|
@ -776,7 +783,7 @@ GLOBAL OPTIONS:
|
||||||
}
|
}
|
||||||
|
|
||||||
table := tablewriter.NewWriter(s)
|
table := tablewriter.NewWriter(s)
|
||||||
table.SetHeader([]string{"ID", "Name", "URL", "Key", "Groups", "Updated", "Created", "Comment"})
|
table.SetHeader([]string{"ID", "Name", "URL", "Key", "Groups", "Updated", "Created", "Comment", "Hop"})
|
||||||
table.SetBorder(false)
|
table.SetBorder(false)
|
||||||
table.SetCaption(true, fmt.Sprintf("Total: %d hosts.", len(hosts)))
|
table.SetCaption(true, fmt.Sprintf("Total: %d hosts.", len(hosts)))
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
|
|
@ -790,6 +797,14 @@ GLOBAL OPTIONS:
|
||||||
for _, hostGroup := range host.Groups {
|
for _, hostGroup := range host.Groups {
|
||||||
groupNames = append(groupNames, hostGroup.Name)
|
groupNames = append(groupNames, hostGroup.Name)
|
||||||
}
|
}
|
||||||
|
var hop string
|
||||||
|
if host.HopID != 0 {
|
||||||
|
var hopHost Host
|
||||||
|
db.Model(&host).Related(&hopHost, "HopID")
|
||||||
|
hop = hopHost.Name
|
||||||
|
} else {
|
||||||
|
hop = ""
|
||||||
|
}
|
||||||
table.Append([]string{
|
table.Append([]string{
|
||||||
fmt.Sprintf("%d", host.ID),
|
fmt.Sprintf("%d", host.ID),
|
||||||
host.Name,
|
host.Name,
|
||||||
|
|
@ -799,6 +814,7 @@ GLOBAL OPTIONS:
|
||||||
humanize.Time(host.UpdatedAt),
|
humanize.Time(host.UpdatedAt),
|
||||||
humanize.Time(host.CreatedAt),
|
humanize.Time(host.CreatedAt),
|
||||||
host.Comment,
|
host.Comment,
|
||||||
|
hop,
|
||||||
//FIXME: add some stats about last access time etc
|
//FIXME: add some stats about last access time etc
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
ssh.go
37
ssh.go
|
|
@ -113,16 +113,33 @@ func channelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewCh
|
||||||
|
|
||||||
switch host.Scheme() {
|
switch host.Scheme() {
|
||||||
case BastionSchemeSSH:
|
case BastionSchemeSSH:
|
||||||
clientConfig, err := bastionClientConfig(ctx, host)
|
sessionConfigs := make([]bastionsession.Config, 0)
|
||||||
if err != nil {
|
currentHost := host
|
||||||
ch, _, err2 := newChan.Accept()
|
for currentHost != nil {
|
||||||
|
clientConfig, err2 := bastionClientConfig(ctx, currentHost)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
|
ch, _, err3 := newChan.Accept()
|
||||||
|
if err3 != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(ch, "error: %v\n", err2)
|
||||||
|
// FIXME: force close all channels
|
||||||
|
_ = ch.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Fprintf(ch, "error: %v\n", err)
|
sessionConfigs = append([]bastionsession.Config{{
|
||||||
// FIXME: force close all channels
|
Addr: currentHost.DialAddr(),
|
||||||
_ = ch.Close()
|
ClientConfig: clientConfig,
|
||||||
return
|
Logs: actx.config.logsLocation,
|
||||||
|
}}, sessionConfigs...)
|
||||||
|
if currentHost.HopID != 0 {
|
||||||
|
var newHost Host
|
||||||
|
actx.db.Model(currentHost).Related(&newHost, "HopID")
|
||||||
|
hostname := newHost.Name
|
||||||
|
currentHost, _ = HostByName(actx.db, hostname)
|
||||||
|
} else {
|
||||||
|
currentHost = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := Session{
|
sess := Session{
|
||||||
|
|
@ -140,11 +157,7 @@ func channelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewCh
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bastionsession.ChannelHandler(srv, conn, newChan, ctx, bastionsession.Config{
|
err = bastionsession.MultiChannelHandler(srv, conn, newChan, ctx, sessionConfigs)
|
||||||
Addr: host.DialAddr(),
|
|
||||||
ClientConfig: clientConfig,
|
|
||||||
Logs: actx.config.logsLocation,
|
|
||||||
})
|
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
sessUpdate := Session{
|
sessUpdate := Session{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue