You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/net/cmd.go

149 lines
5.2 KiB
Go

2 years ago
package net
9 months ago
import (
"b612.me/apps/b612/netforward"
8 months ago
"b612.me/starlog"
"fmt"
9 months ago
"github.com/spf13/cobra"
8 months ago
"time"
9 months ago
)
var Cmd = &cobra.Command{
Use: "net",
8 months ago
Short: "网络工具包括nat穿透端口转发等",
9 months ago
}
func init() {
Cmd.AddCommand(netforward.CmdNetforward)
}
8 months ago
var nattestc NatTesterClient
4 months ago
var nattests NatTesterServer
8 months ago
var natc NatClient
var nats NatServer
8 months ago
var dns, ipinfoaddr string
var timeout int
var maxHop int
var disableIpInfo bool
8 months ago
var bindAddr string
var hideIncorrect bool
8 months ago
8 months ago
func init() {
CmdNatPClient.Flags().StringVarP(&natc.ServiceTarget, "target", "t", "", "forward server target address")
CmdNatPClient.Flags().StringVarP(&natc.CmdTarget, "server", "s", "", "nat server command address")
CmdNatPClient.Flags().StringVarP(&natc.Passwd, "passwd", "p", "", "password")
CmdNatPClient.Flags().BoolVarP(&natc.enableTCP, "enable-tcp", "T", true, "enable tcp forward")
CmdNatPClient.Flags().BoolVarP(&natc.enableUDP, "enable-udp", "U", true, "enable udp forward")
CmdNatPClient.Flags().IntVarP(&natc.DialTimeout, "dial-timeout", "d", 10000, "dial timeout milliseconds")
CmdNatPClient.Flags().IntVarP(&natc.UdpTimeout, "udp-timeout", "D", 60000, "udp connection timeout milliseconds")
Cmd.AddCommand(CmdNatPClient)
8 months ago
CmdNatPServer.Flags().StringVarP(&nats.ListenAddr, "listen", "l", "", "listen address")
CmdNatPServer.Flags().StringVarP(&nats.Passwd, "passwd", "p", "", "password")
CmdNatPServer.Flags().Int64VarP(&nats.UDPTimeout, "udp-timeout", "D", 60000, "udp connection timeout milliseconds")
CmdNatPServer.Flags().Int64VarP(&nats.NetTimeout, "dial-timeout", "d", 10000, "dial timeout milliseconds")
CmdNatPServer.Flags().BoolVarP(&nats.enableTCP, "enable-tcp", "T", true, "enable tcp forward")
CmdNatPServer.Flags().BoolVarP(&nats.enableUDP, "enable-udp", "U", true, "enable udp forward")
Cmd.AddCommand(CmdNatPServer)
8 months ago
CmdNetTrace.Flags().StringVarP(&dns, "dns", "d", "", "自定义dns服务器")
CmdNetTrace.Flags().StringVarP(&ipinfoaddr, "ipinfo", "i", "https://ip.b612.me/{ip}/detail", "自定义ip信息查询地址")
CmdNetTrace.Flags().IntVarP(&timeout, "timeout", "t", 800, "超时时间,单位毫秒")
CmdNetTrace.Flags().IntVarP(&maxHop, "max-hop", "m", 32, "最大跳数")
CmdNetTrace.Flags().BoolVarP(&disableIpInfo, "disable-ipinfo", "D", false, "禁用ip信息查询")
8 months ago
CmdNetTrace.Flags().StringVarP(&bindAddr, "bind", "b", "0.0.0.0", "绑定地址")
CmdNetTrace.Flags().BoolVarP(&hideIncorrect, "hide-incorrect", "H", false, "隐藏错误节点")
7 months ago
Cmd.AddCommand(CmdNetTrace, cmdSSHJar)
8 months ago
CmdNatClient.Flags().IntVarP(&nattestc.RetryTime, "retry", "r", 2, "重试次数")
CmdNatClient.Flags().IntVarP(&nattestc.Timeout, "timeout", "t", 2, "超时时间")
CmdNatClient.Flags().StringSliceVarP(&nattestc.dns, "dns", "d", nil, "自定义dns服务器")
Cmd.AddCommand(CmdNatClient)
4 months ago
CmdNatServer.Flags().StringVarP(&nattests.MainIP, "main-ip", "m", "10.0.0.5", "主IP")
CmdNatServer.Flags().StringVarP(&nattests.AltIP, "alt-ip", "a", "10.0.0.2", "备用IP")
CmdNatServer.Flags().StringVarP(&nattests.MainPort, "main-port", "M", "41127", "主端口")
CmdNatServer.Flags().StringVarP(&nattests.AltPort, "alt-port", "A", "46610", "备用端口")
CmdNatServer.Flags().StringVarP(&nattests.LogPath, "log", "l", "", "日志文件")
Cmd.AddCommand(CmdNatServer)
8 months ago
}
var CmdNatPClient = &cobra.Command{
4 months ago
Use: "natpc",
8 months ago
Short: "nat穿透客户端",
8 months ago
Run: func(cmd *cobra.Command, args []string) {
if natc.ServiceTarget == "" || natc.CmdTarget == "" {
cmd.Help()
return
}
natc.Run()
},
}
var CmdNatPServer = &cobra.Command{
4 months ago
Use: "natps",
8 months ago
Short: "nat穿透服务端",
8 months ago
Run: func(cmd *cobra.Command, args []string) {
nats.Run()
},
}
8 months ago
var CmdNatClient = &cobra.Command{
Use: "natc",
4 months ago
Short: "nat类型测试工具 - 客户端",
Long: "基于RFC3489的nat类型测试工具客户端",
Run: func(cmd *cobra.Command, args []string) {
if nattestc.dns != nil {
UseCustomeDNS(nattestc.dns)
for _, v := range nattestc.dns {
starlog.Infoln("使用自定义DNS:", v)
}
}
natserver := "nat.b612.me"
if len(args) == 0 {
starlog.Infoln("使用默认NAT测试服务器nat.b612.me")
} else {
natserver = args[0]
starlog.Infoln("使用NAT测试服务器", natserver)
}
res, err := nattestc.ServeAndRun(natserver)
if err != nil {
starlog.Errorln("测试NAT类型失败", err)
return
}
fmt.Println("-----------------\n")
starlog.Green("您的NAT类型为%v - %v\nswitch可能显示NAT类型为%v\n%v", res.Code, res.RFC3489, res.NintendoSwitch, res.Desc)
},
}
4 months ago
var CmdNatServer = &cobra.Command{
Use: "nats",
Short: "nat类型测试工具 - 服务端",
Long: "基于RFC3489的nat类型测试工具服务端",
Run: func(cmd *cobra.Command, args []string) {
nattests.Run()
},
}
8 months ago
var CmdNetTrace = &cobra.Command{
Use: "trace",
Short: "网络路径追踪",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
if disableIpInfo {
ipinfoaddr = ""
}
for _, target := range args {
starlog.Infoln("Traceroute to ", target)
8 months ago
Traceroute(target, bindAddr, dns, maxHop, time.Millisecond*time.Duration(timeout), ipinfoaddr, hideIncorrect)
8 months ago
fmt.Println("-----------------------------")
}
},
}