| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package main
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "os"
- "os/signal"
- "strings"
- "sync/atomic"
- "syscall"
- "time"
- "github.com/google/uuid"
- "github.com/peterh/liner"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- )
- const MODULE_NAME = "YFKJ_SSH_CLIENT"
- var (
- coupler *MQTTCoupler
- Version = "1.0.0.1"
- ErrBrokerAddressEmpty = errors.New("mqtt server address is empty")
- ErrIMEINotAvailable = errors.New("device imei is not available")
- )
- func main() {
- if baseapp.IsArgsParam("-h") {
- help()
- return
- }
- if baseapp.IsArgsParam("-v") {
- fmt.Println("程序版本:", Version, "\n构建时间:", baseapp.BuildTime)
- return
- }
- devIMEI := baseapp.GetArgsParamStr("-c", "")
- if devIMEI == "" {
- help()
- return
- }
- if err := loadAppConfig(); err != nil {
- fmt.Printf("[%s] 错误: %v!!\n", MODULE_NAME, err)
- return
- }
- if CfgServers.MQTTSrv.Address == "" {
- fmt.Printf("[%s] 错误: %v!!\n", MODULE_NAME, ErrBrokerAddressEmpty)
- return
- }
- ctx, cancel := context.WithCancel(context.Background())
- coupler = &MQTTCoupler{
- ctx: ctx,
- cancel: cancel,
- broker: CfgServers.MQTTSrv.Address,
- username: CfgServers.MQTTSrv.Username,
- password: CfgServers.MQTTSrv.Password,
- clientID: uuid.New().String(),
- imei: devIMEI,
- cwd: "/",
- }
- if err := coupler.init2(); err != nil {
- fmt.Printf("[%s] 错误: %v\n!!", MODULE_NAME, err)
- return
- }
- var pingState atomic.Bool
- heartbeatLoop(&pingState) // -启动-设备在线-心跳检测-
- for {
- if pingState.Load() { //// 等待成功连接上目标设备卍
- break
- }
- fmt.Printf("[%s] 正在尝试连接设备...\n", MODULE_NAME)
- time.Sleep(1 * time.Second)
- }
- term(&pingState) /////////////////// 启动终端模拟器卍
- }
- func term(pingState *atomic.Bool) {
- var executing atomic.Bool // 是否有正在执行中的命令
- var interrupted atomic.Bool // 用户是否按键取消了命令
- interruptLoop(&executing, &interrupted) // Ctrl+C卍
- printWelcome()
- line := liner.NewLiner()
- defer line.Close()
- line.SetCtrlCAborts(false)
- line.SetTabCompletionStyle(liner.TabCircular)
- historyFile := "history.txt"
- if f, err := os.Open(historyFile); err == nil {
- line.ReadHistory(f)
- f.Close()
- }
- defer func() {
- if f, err := os.Create(historyFile); err == nil {
- line.WriteHistory(f)
- f.Close()
- }
- }()
- for {
- if !pingState.Load() {
- fmt.Printf("[%s] 目标设备连接丢失!!\n", MODULE_NAME)
- break
- }
- if interrupted.Swap(false) {
- fmt.Println("^C")
- }
- prompt := fmt.Sprintf("root@%s:%s# ", coupler.imei, coupler.cwd)
- input, err := line.Prompt(prompt) /// 等待用户输入指令
- if err == nil {
- goto inputOK
- }
- if err == io.EOF { ////////////////// Ctrl+D 按键处理
- _, _ = coupler.quit()
- fmt.Println("bye")
- break
- }
- fmt.Println("读取用户输入失败:", err)
- continue
- inputOK:
- input = strings.TrimSpace(input)
- if input == "" {
- continue
- }
- line.AppendHistory(input) ///// 保存用户输入的历史命令
- if input == "exit" || input == "quit" {
- _, _ = coupler.quit()
- break
- }
- executing.Store(true)
- result, err := coupler.exec(input)
- executing.Store(false)
- if err != nil {
- fmt.Printf("执行错误: %v\n", err)
- continue
- }
- if result.Stdout != "" {
- fmt.Print(result.Stdout)
- }
- if result.Stderr != "" {
- fmt.Fprintln(os.Stderr, result.Stderr)
- }
- if result.ExitCode == 124 {
- fmt.Println("command timeout")
- }
- if result.Cwd != "" {
- coupler.cwd = result.Cwd
- }
- }
- }
- func help() {
- h := `
- -h 显示帮助提示
- -v 当前程序版本
- -c 连接目标设备(IMEI), 例如: -c 869523059113051
- `
- fmt.Println(h)
- }
- func interruptLoop(executing *atomic.Bool, interrupted *atomic.Bool) {
- sigCh := make(chan os.Signal, 1)
- signal.Notify(sigCh, syscall.SIGINT)
- go func() {
- for range sigCh {
- interrupted.Store(true)
- if executing.Load() {
- _, _ = coupler.stop()
- }
- }
- }()
- }
- func heartbeatLoop(pingState *atomic.Bool) {
- go func() {
- ticker := time.NewTicker(1 * time.Second)
- defer ticker.Stop()
- pingFailCount := 0
- pong := ""
- for range ticker.C {
- resp, err := coupler.ping()
- if err == nil && resp.Error == nil && resp.Result != nil &&
- json.Unmarshal(resp.Result, &pong) == nil && pong == "pong" {
- pingState.Store(true)
- pingFailCount = 0
- } else {
- pingFailCount++
- if pingFailCount >= 3 { ///// 连续3次ping失败, 可以认为设备离线
- pingState.Store(false)
- }
- } // end if
- } // end for
- }()
- }
- func printWelcome() {
- welcome := `
- _ _ _ _ _ _ _ _
- | \ | (_) | | | (_) | |
- | \| |_| |_ ___| |__ _| | | ___
- | . | | __/ __| '_ \| | | |/ _ \
- | |\ | | || (__| | | | | | | __/
- |_| \_|_|\__\___|_| |_|_|_|_|\___|
- ══════════════════════════════════
- 云飞科技RTU远程运维终端
- ══════════════════════════════════
- 提示: 输入'quit'命令, 退出终端
- `
- fmt.Println(welcome)
- }
|