mirror of
https://github.com/moul/sshportal.git
synced 2025-09-11 23:24:33 +08:00
Add database
This commit is contained in:
parent
84e8352338
commit
b2701e2024
4 changed files with 78 additions and 10 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
*.db
|
26
config.go
26
config.go
|
@ -1,9 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gliderlabs/ssh"
|
"github.com/gliderlabs/ssh"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
gossh "golang.org/x/crypto/ssh"
|
gossh "golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,18 +13,25 @@ type Config struct {
|
||||||
remoteAddr string
|
remoteAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig(s ssh.Session) (*Config, error) {
|
func getConfig(s ssh.Session, db *gorm.DB) (*Config, error) {
|
||||||
// TODO: get the config from a database
|
var host Host
|
||||||
|
db.Where("name = ?", s.User()).Find(&host)
|
||||||
|
if host.Name == "" {
|
||||||
|
// FIXME: add available hosts
|
||||||
|
return nil, fmt.Errorf("No such target: %q", s.User())
|
||||||
|
}
|
||||||
|
|
||||||
config := Config{
|
config := Config{
|
||||||
remoteAddr: os.Getenv("SSH_ADDR"),
|
remoteAddr: host.Addr,
|
||||||
clientConfig: &gossh.ClientConfig{
|
clientConfig: &gossh.ClientConfig{
|
||||||
User: os.Getenv("SSH_USERNAME"),
|
User: host.User,
|
||||||
HostKeyCallback: gossh.InsecureIgnoreHostKey(), // TODO: show the remote host to the client + store it in db if approved
|
HostKeyCallback: gossh.InsecureIgnoreHostKey(),
|
||||||
Auth: []gossh.AuthMethod{
|
Auth: []gossh.AuthMethod{},
|
||||||
gossh.Password(os.Getenv("SSH_PASSWORD")),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if host.Password != "" {
|
||||||
|
config.clientConfig.Auth = append(config.clientConfig.Auth, gossh.Password(host.Password))
|
||||||
|
}
|
||||||
|
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
35
db.go
Normal file
35
db.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
type Key struct {
|
||||||
|
gorm.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
type Host struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string
|
||||||
|
Addr string
|
||||||
|
User string
|
||||||
|
Password string
|
||||||
|
Key Key
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
gorm.Model
|
||||||
|
Keys []Key
|
||||||
|
}
|
||||||
|
|
||||||
|
func dbInit(db *gorm.DB) error {
|
||||||
|
db.LogMode(true)
|
||||||
|
db.AutoMigrate(&User{})
|
||||||
|
db.AutoMigrate(&Key{})
|
||||||
|
db.AutoMigrate(&Host{})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func dbDemo(db *gorm.DB) error {
|
||||||
|
var host Host
|
||||||
|
db.FirstOrCreate(&host, &Host{Name: "sdf", Addr: "sdf.org:22", User: "new"})
|
||||||
|
return nil
|
||||||
|
}
|
26
main.go
26
main.go
|
@ -9,6 +9,8 @@ import (
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/gliderlabs/ssh"
|
"github.com/gliderlabs/ssh"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,6 +39,14 @@ func main() {
|
||||||
Name: "demo",
|
Name: "demo",
|
||||||
Usage: "*unsafe* - demo mode: accept all connections",
|
Usage: "*unsafe* - demo mode: accept all connections",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "db-driver",
|
||||||
|
Value: "sqlite3",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "db-conn",
|
||||||
|
Value: "./sshportal.db",
|
||||||
|
},
|
||||||
// TODO: add verbose mode
|
// TODO: add verbose mode
|
||||||
// TODO: add web server
|
// TODO: add web server
|
||||||
}
|
}
|
||||||
|
@ -45,6 +55,20 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func server(c *cli.Context) error {
|
func server(c *cli.Context) error {
|
||||||
|
db, err := gorm.Open(c.String("db-driver"), c.String("db-conn"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
if err := dbInit(db); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if c.Bool("demo") {
|
||||||
|
if err := dbDemo(db); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ssh.Handle(func(s ssh.Session) {
|
ssh.Handle(func(s ssh.Session) {
|
||||||
log.Printf("New connection: user=%q remote=%q local=%q command=%q", s.User(), s.RemoteAddr(), s.LocalAddr(), s.Command())
|
log.Printf("New connection: user=%q remote=%q local=%q command=%q", s.User(), s.RemoteAddr(), s.LocalAddr(), s.Command())
|
||||||
|
|
||||||
|
@ -53,7 +77,7 @@ func server(c *cli.Context) error {
|
||||||
io.WriteString(s, banner)
|
io.WriteString(s, banner)
|
||||||
io.WriteString(s, "Configuration menu not yet implemented.\n\n")
|
io.WriteString(s, "Configuration menu not yet implemented.\n\n")
|
||||||
default:
|
default:
|
||||||
config, err := getConfig(s)
|
config, err := getConfig(s, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
io.WriteString(s, fmt.Sprintf("error: %v\n", err))
|
io.WriteString(s, fmt.Sprintf("error: %v\n", err))
|
||||||
// FIXME: drop a menu shell?
|
// FIXME: drop a menu shell?
|
||||||
|
|
Loading…
Add table
Reference in a new issue