|
|
package net
|
|
|
|
|
|
import (
|
|
|
"b612.me/apps/b612/netforward"
|
|
|
"b612.me/starlog"
|
|
|
"fmt"
|
|
|
"github.com/spf13/cobra"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var Cmd = &cobra.Command{
|
|
|
Use: "net",
|
|
|
Short: "网络工具,包括nat穿透,端口转发等",
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
Cmd.AddCommand(netforward.CmdNetforward)
|
|
|
}
|
|
|
|
|
|
var nattestc NatTesterClient
|
|
|
var nattests NatTesterServer
|
|
|
|
|
|
var natc NatClient
|
|
|
var nats NatServer
|
|
|
|
|
|
var dns, ipinfoaddr string
|
|
|
var timeout int
|
|
|
var maxHop int
|
|
|
var disableIpInfo bool
|
|
|
var bindAddr string
|
|
|
var hideIncorrect bool
|
|
|
var natt NatThroughs
|
|
|
|
|
|
var scanip ScanIP
|
|
|
var scanport ScanPort
|
|
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
|
|
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信息查询")
|
|
|
CmdNetTrace.Flags().StringVarP(&bindAddr, "bind", "b", "0.0.0.0", "绑定地址")
|
|
|
CmdNetTrace.Flags().BoolVarP(&hideIncorrect, "hide-incorrect", "H", false, "隐藏错误节点")
|
|
|
Cmd.AddCommand(CmdNetTrace, cmdSSHJar)
|
|
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
|
|
CmdNatThrough.Flags().StringVarP(&natt.STUN, "stun", "s", "turn.b612.me:3478", "stun服务器")
|
|
|
CmdNatThrough.Flags().StringVarP(&natt.Remote, "remote", "r", "baidu.com:80", "keepalive地址")
|
|
|
CmdNatThrough.Flags().IntVarP(&natt.KeepAlivePeriod, "keepalive-period", "p", 30, "KeepAlive周期")
|
|
|
CmdNatThrough.Flags().IntVarP(&natt.KeepAliveIdel, "keepalive-idel", "i", 30, "KeepAlive空闲时间")
|
|
|
CmdNatThrough.Flags().IntVarP(&natt.KeepAliveCount, "keepalive-count", "c", 5, "KeepAlive次数")
|
|
|
CmdNatThrough.Flags().BoolVarP(&natt.AutoUPnP, "auto-upnp", "u", true, "自动UPnP")
|
|
|
CmdNatThrough.Flags().IntVarP(&natt.WebPort, "web-port", "w", 8080, "web端口")
|
|
|
CmdNatThrough.Flags().IntVarP(&natt.HealthCheckInterval, "health-check-interval", "H", 30, "健康检查间隔")
|
|
|
CmdNatThrough.Flags().StringVarP(&natt.Type, "type", "t", "tcp", "穿透协议,tcp或udp")
|
|
|
Cmd.AddCommand(CmdNatThrough)
|
|
|
|
|
|
CmdScanIP.Flags().StringVarP(&scanip.Host, "ip", "i", "", "扫描IP地址")
|
|
|
CmdScanIP.Flags().IntVarP(&scanip.Port, "port", "p", 80, "TCP模式扫描端口")
|
|
|
CmdScanIP.Flags().IntVarP(&scanip.Timeout, "timeout", "t", 2000, "超时时间")
|
|
|
CmdScanIP.Flags().IntVarP(&scanip.Threads, "threads", "m", 100, "最大线程数")
|
|
|
CmdScanIP.Flags().StringVarP(&scanip.Log, "log", "l", "", "日志文件地址")
|
|
|
CmdScanIP.Flags().StringVarP(&scanip.Mask, "mask", "M", "", "掩码")
|
|
|
CmdScanIP.Flags().IntVarP(&scanip.CIDR, "cidr", "c", 24, "CIDR")
|
|
|
CmdScanIP.Flags().StringVarP(&scanip.ScanType, "type", "T", "icmp", "扫描类型")
|
|
|
CmdScanIP.Flags().IntVarP(&scanip.Retry, "retry", "r", 2, "重试次数")
|
|
|
CmdScanIP.Flags().BoolVarP(&scanip.WithHostname, "with-hostname", "H", false, "显示主机名")
|
|
|
Cmd.AddCommand(CmdScanIP)
|
|
|
|
|
|
CmdScanPort.Flags().StringVarP(&scanport.Host, "ip", "i", "", "扫描IP地址")
|
|
|
CmdScanPort.Flags().IntVarP(&scanport.Timeout, "timeout", "t", 2000, "超时时间")
|
|
|
CmdScanPort.Flags().IntVarP(&scanport.Threads, "threads", "m", 100, "最大线程数")
|
|
|
CmdScanPort.Flags().StringVarP(&scanport.Log, "log", "l", "", "日志文件地址")
|
|
|
CmdScanPort.Flags().IntVarP(&scanport.Retry, "retry", "r", 2, "重试次数")
|
|
|
Cmd.AddCommand(CmdScanPort)
|
|
|
|
|
|
}
|
|
|
|
|
|
var CmdNatPClient = &cobra.Command{
|
|
|
Use: "natpc",
|
|
|
Short: "nat穿透客户端",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
if natc.ServiceTarget == "" || natc.CmdTarget == "" {
|
|
|
cmd.Help()
|
|
|
return
|
|
|
}
|
|
|
natc.Run()
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdNatPServer = &cobra.Command{
|
|
|
Use: "natps",
|
|
|
Short: "nat穿透服务端",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
nats.Run()
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdNatClient = &cobra.Command{
|
|
|
Use: "natc",
|
|
|
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("-----------------")
|
|
|
fmt.Println("")
|
|
|
starlog.Green("您的NAT类型为%v - %v\nswitch可能显示NAT类型为%v\n%v", res.Code, res.RFC3489, res.NintendoSwitch, res.Desc)
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdNatServer = &cobra.Command{
|
|
|
Use: "nats",
|
|
|
Short: "nat类型测试工具 - 服务端",
|
|
|
Long: "基于RFC3489的nat类型测试工具(服务端)",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
nattests.Run()
|
|
|
},
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
Traceroute(target, bindAddr, dns, maxHop, time.Millisecond*time.Duration(timeout), ipinfoaddr, hideIncorrect)
|
|
|
fmt.Println("-----------------------------")
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdNatThrough = &cobra.Command{
|
|
|
Use: "natt",
|
|
|
Short: "nat tcp直接穿透",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
if len(args) == 0 {
|
|
|
starlog.Errorf("请按照如下格式输入:\n [远端地址] 或 [本地地址|远端地址] 或 [名字@本地地址|远端地址] 或 [协议|名字|本地地址|远端地址]\n")
|
|
|
return
|
|
|
}
|
|
|
if err := natt.Parse(args); err != nil {
|
|
|
starlog.Errorln(err)
|
|
|
return
|
|
|
}
|
|
|
if err := natt.Run(); err != nil {
|
|
|
starlog.Errorln(err)
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdScanIP = &cobra.Command{
|
|
|
Use: "scanip",
|
|
|
Short: "扫描IP",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
if scanip.Host == "" {
|
|
|
cmd.Help()
|
|
|
return
|
|
|
}
|
|
|
if scanip.ScanType == "icmp" {
|
|
|
scanip.ICMP()
|
|
|
} else {
|
|
|
scanip.TCP(scanip.Port)
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
|
|
|
var CmdScanPort = &cobra.Command{
|
|
|
Use: "scanport",
|
|
|
Short: "扫描端口",
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
if scanport.Host == "" {
|
|
|
cmd.Help()
|
|
|
return
|
|
|
}
|
|
|
if len(args) != 1 {
|
|
|
starlog.Errorln("请指定端口范围,如:80,443,1000-2000")
|
|
|
}
|
|
|
if err := scanport.Parse(args[0]); err != nil {
|
|
|
starlog.Errorln(err)
|
|
|
return
|
|
|
}
|
|
|
scanport.Run()
|
|
|
},
|
|
|
}
|