You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB
Go

8 years ago
package main
import (
"bytes"
"io/ioutil"
4 years ago
"github.com/fangdingjun/go-log/v5"
8 years ago
"github.com/go-yaml/yaml"
"golang.org/x/crypto/ssh"
8 years ago
)
type listen struct {
Port int
Key string
Cert string
}
8 years ago
type serverConfig struct {
Listen []listen `yaml:"listen"`
Debug bool `yaml:"debug"`
HostKey string `yaml:"host_key_file"`
Users []serverUser `yaml:"users"`
8 years ago
}
type serverUser struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
AuthorizedKeyFile string `yaml:"authorized_key_file"`
publicKeys []ssh.PublicKey
}
func (c *serverConfig) getUser(user string) (serverUser, error) {
for _, u := range c.Users {
if u.Username == user {
return u, nil
}
}
return serverUser{}, nil
}
func loadConfig(f string) (*serverConfig, error) {
buf, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
var c serverConfig
if err := yaml.Unmarshal(buf, &c); err != nil {
return nil, err
}
for i := range c.Users {
buf1, err := ioutil.ReadFile(c.Users[i].AuthorizedKeyFile)
if err != nil {
log.Warnf("read publickey for %s failed, ignore", c.Users[i].Username)
8 years ago
continue
}
// parse authorized_key
//var err error
var p1 ssh.PublicKey
r := bytes.Trim(buf1, " \r\n")
for {
p1, _, _, r, err = ssh.ParseAuthorizedKey(r)
if err != nil {
//log.Println(err)
//log.Printf("%+v %+v", r, p1)
return nil, err
}
c.Users[i].publicKeys = append(c.Users[i].publicKeys, p1)
if len(r) == 0 {
break
}
}
}
return &c, nil
}