加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
皮豪 提交于 2024-05-09 23:24 . init
package main
import (
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/BurntSushi/toml"
)
type config struct {
Host string `toml:"host"`
Port uint `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
IdentityFile string `toml:"identity_file"`
}
func main() {
var (
host string
port uint
user string
password string
identityFile string
configPath string
)
hostUsage := "the target host (required if no config file)"
portUsage := "the port to connect"
portDefualt := uint(22)
userUsage := "the login user (required if no config file)"
passwordUsage := "the login password"
identityFileUsage := "the identity file"
configPathUsage := "the path of config file (ignore other args if a config file exists)"
configPathDefualt := "./config.toml"
flag.StringVar(&host, "t", "", hostUsage)
flag.UintVar(&port, "p", portDefualt, portUsage)
flag.StringVar(&user, "u", "", userUsage)
flag.StringVar(&password, "s", "", passwordUsage)
flag.StringVar(&identityFile, "i", "", identityFileUsage)
flag.StringVar(&configPath, "c", configPathDefualt, configPathUsage)
flag.Parse()
var cfg config
var handler *sshHandler
if _, err := toml.DecodeFile(configPath, &cfg); errors.Is(err, os.ErrNotExist) {
if host == "" {
log.Fatal("host can not be empty")
}
if user == "" {
log.Fatal("user can not be empty")
}
if password == "" && identityFile == "" {
log.Fatal("password can not be empty")
}
addr := fmt.Sprintf("%s:%d", host, port)
handler = &sshHandler{addr: addr, user: user, secret: password}
} else if err != nil {
log.Fatal("could not parse config file: ", err)
} else {
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
log.Println(cfg, password)
if cfg.Password != "" {
handler = &sshHandler{addr: addr, user: cfg.User, secret: cfg.Password}
} else {
handler = &sshHandler{addr: addr, user: cfg.User, keyfile: cfg.IdentityFile}
}
}
http.Handle("/", http.FileServer(http.Dir("./front/")))
http.HandleFunc("/web-socket/ssh", handler.webSocket)
log.Fatal(http.ListenAndServe(":8080", nil))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化