package net import ( "b612.me/starlog" "github.com/spf13/cobra" "os" "os/signal" "time" ) var ( tcps TcpServer tcpc TcpClient ) func init() { CmdTcps.Flags().StringVarP(&tcps.LocalAddr, "local", "l", "0.0.0.0:29127", "本地地址") CmdTcps.Flags().BoolVarP(&tcps.UsingKeepAlive, "keepalive", "k", true, "启用KeepAlive") CmdTcps.Flags().IntVarP(&tcps.KeepAlivePeriod, "keepalive-period", "p", 10, "KeepAlive重试周期(秒)") CmdTcps.Flags().IntVarP(&tcps.KeepAliveIdel, "keepalive-idel", "i", 15, "KeepAlive空闲时间(秒)") CmdTcps.Flags().IntVarP(&tcps.KeepAliveCount, "keepalive-count", "c", 3, "KeepAlive次数)") CmdTcps.Flags().BoolVarP(&tcps.Interactive, "interactive", "I", false, "交互模式") CmdTcps.Flags().IntVarP(&tcps.UserTimeout, "user-timeout", "u", 0, "用户超时时间(毫秒)") CmdTcps.Flags().BoolVarP(&tcps.ShowRecv, "show-recv", "r", true, "显示接收数据") CmdTcps.Flags().BoolVarP(&tcps.ShowAsHex, "show-hex", "H", false, "显示十六进制") CmdTcps.Flags().StringVarP(&tcps.SaveToFolder, "save", "s", "", "保存到文件夹") CmdTcps.Flags().StringVarP(&tcps.LogPath, "log", "L", "", "日志文件路径") CmdTcps.Flags().IntVarP(&tcpc.UseLinger, "linger", "g", -1, "Linger时间(秒)") Cmd.AddCommand(CmdTcps) CmdTcpc.Flags().StringVarP(&tcpc.LocalAddr, "local", "l", "", "本地地址") CmdTcpc.Flags().BoolVarP(&tcpc.UsingKeepAlive, "keepalive", "k", true, "启用KeepAlive") CmdTcpc.Flags().IntVarP(&tcpc.KeepAlivePeriod, "keepalive-period", "p", 1, "KeepAlive重试周期(秒)") CmdTcpc.Flags().IntVarP(&tcpc.KeepAliveIdel, "keepalive-idel", "i", 15, "KeepAlive空闲时间(秒)") CmdTcpc.Flags().IntVarP(&tcpc.KeepAliveCount, "keepalive-count", "c", 3, "KeepAlive次数") CmdTcpc.Flags().BoolVarP(&tcpc.Interactive, "interactive", "I", false, "交互模式") CmdTcpc.Flags().IntVarP(&tcpc.UserTimeout, "user-timeout", "u", 0, "用户超时时间(毫秒)") CmdTcpc.Flags().BoolVarP(&tcpc.ShowRecv, "show-recv", "r", true, "显示接收数据") CmdTcpc.Flags().BoolVarP(&tcpc.ShowAsHex, "show-hex", "H", false, "显示十六进制") CmdTcpc.Flags().StringVarP(&tcpc.SaveToFolder, "save", "s", "", "保存到文件夹") CmdTcpc.Flags().StringVarP(&tcpc.LogPath, "log", "L", "", "日志文件路径") CmdTcpc.Flags().IntVarP(&tcpc.DialTimeout, "dial-timeout", "t", 5, "连接超时时间(秒)") CmdTcpc.Flags().IntVarP(&tcpc.UseLinger, "linger", "g", -1, "Linger时间(秒)") Cmd.AddCommand(CmdTcpc) } var CmdTcps = &cobra.Command{ Use: "tcps", Short: "TCP服务端", Run: func(cmd *cobra.Command, args []string) { go func() { s := make(chan os.Signal, 1) signal.Notify(s, os.Interrupt, os.Kill) <-s tcps.Stop() time.Sleep(5 * time.Second) os.Exit(0) }() tcps.Run() }, } var CmdTcpc = &cobra.Command{ Use: "tcpc", Short: "TCP客户端", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { starlog.Errorln("请指定目标地址") return } tcpc.RemoteAddr = args[0] go func() { s := make(chan os.Signal, 1) signal.Notify(s, os.Interrupt, os.Kill) <-s tcpc.Stop() time.Sleep(5 * time.Second) os.Exit(0) }() tcpc.Run() }, }