代码拉取完成,页面将自动刷新
同步操作将从 TNK-Studio/gortal 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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)),
),
)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。