1 Star 0 Fork 21

chaho/gortal

forked from TNK-Studio/gortal 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
baiyigujiu 提交于 2020-10-30 20:55 . [FIX] fix remove users
package main
import (
"flag"
"fmt"
"log"
"time"
"encoding/base64"
"github.com/TNK-Studio/gortal/config"
"github.com/TNK-Studio/gortal/core/jump"
"github.com/TNK-Studio/gortal/core/sshd"
"github.com/TNK-Studio/gortal/utils"
"github.com/TNK-Studio/gortal/utils/logger"
"github.com/elfgzp/ssh"
)
var (
// Port port
Port *int
hostKeyFile *string
)
func init() {
Port = flag.Int("p", 2222, "Port")
hostKeyFile = flag.String("hk", "~/.ssh/id_rsa", "Host key file")
}
func passwordAuth(ctx ssh.Context, pass string) bool {
config.Conf.ReadFrom(*config.ConfPath)
var success bool
if (len(*config.Conf.Users)) < 1 {
success = (pass == "newuser")
} else {
success = jump.VarifyUser(ctx, pass)
}
if !success {
time.Sleep(time.Second * 3)
}
return success
}
func publickKeyAuth(ctx ssh.Context, key ssh.PublicKey) bool {
var pub string
config.Conf.ReadFrom(*config.ConfPath)
username := ctx.User()
for _, user := range *config.Conf.Users {
if user.Username == username {
pub = user.PublicKey
}
}
decodeBytes, _ := base64.StdEncoding.DecodeString(pub)
allowed, _, _, _, _ := ssh.ParseAuthorizedKey(decodeBytes)
return ssh.KeysEqual(key, allowed)
}
func sessionHandler(sess *ssh.Session) {
defer func() {
(*sess).Close()
}()
rawCmd := (*sess).RawCommand()
cmd, args, err := sshd.ParseRawCommand(rawCmd)
if err != nil {
sshd.ErrorInfo(err, sess)
return
}
switch cmd {
case "scp":
sshd.ExecuteSCP(args, sess)
default:
sshHandler(sess)
}
}
func sshHandler(sess *ssh.Session) {
jps := jump.Service{}
jps.Run(sess)
}
func scpHandler(args []string, sess *ssh.Session) {
sshd.ExecuteSCP(args, sess)
}
func main() {
flag.Parse()
if !utils.FileExited(*hostKeyFile) {
sshd.GenKey(*hostKeyFile)
}
ssh.Handle(func(sess ssh.Session) {
defer func() {
if e, ok := recover().(error); ok {
logger.Logger.Panic(e)
}
}()
sessionHandler(&sess)
})
log.Printf("starting ssh server on port %d...\n", *Port)
log.Fatal(ssh.ListenAndServe(
fmt.Sprintf(":%d", *Port),
nil,
ssh.PasswordAuth(passwordAuth),
ssh.PublicKeyAuth(publickKeyAuth),
ssh.HostKeyFile(utils.FilePath(*hostKeyFile)),
),
)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xitsm/gortal.git
git@gitee.com:xitsm/gortal.git
xitsm
gortal
gortal
master

搜索帮助