package main import ( "fmt" "git.swzry.com/zry/go-hhc-cli/hhc_ast" "git.swzry.com/zry/go-hhc-cli/hhc_common" "net" "regexp" "strconv" "strings" ) var FRGEXP *regexp.Regexp var ExecSB strings.Builder func CtxToCommand(ctx *hhc_ast.SDTWalkContext) string { sb := strings.Builder{} for _, v := range ctx.ASTNodes { sb.WriteString(v.GetTokenRaw() + " ") } return sb.String() } func FExec(ctx *hhc_ast.SDTWalkContext) hhc_common.SDTWalkError { fmt.Println("! Command Execute: ", CtxToCommand(ctx)) ExecSB.WriteString(CtxToCommand(ctx) + "\r\n") return nil } func VLoopbackInf(t string) bool { i, e := strconv.Atoi(t) if e != nil { return false } if i < 0 || i > 7 { return false } return true } func VSubnetLen(t string) bool { i, e := strconv.Atoi(t) if e != nil { return false } if i < 1 || i > 31 { return false } return true } func VMTU(t string) bool { i, e := strconv.Atoi(t) if e != nil { return false } if i < 128 || i > 1500 { return false } return true } func VVlanInf(t string) bool { i, e := strconv.Atoi(t) if e != nil { return false } if i < 0 || i > 4095 { return false } return true } func VIPAddress(t string) bool { _, err := net.ResolveIPAddr("ip", t) return err == nil } func maketree() *hhc_ast.SyntaxDefinitionTreeRoot { var err error FRGEXP, err = regexp.Compile("INTEGER<([0-9]+)\\-([0-9]+)>") if err != nil { fmt.Println("Failed Compile Regexp:", err) return nil } treeroot := &hhc_ast.SyntaxDefinitionTreeRoot{} treeroot.AddCommand( (&hhc_ast.SDTNode_Command{ Name: "ip", Description: "Specify IP configuration", }).AddSubCommand( (&hhc_ast.SDTNode_Command{ Name: "address", Description: "Set the IP address of an interface", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "dhcp-alloc", Description: "Obtain an IP address through DHCP", }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "unnumbered", Description: "Share an address with another interface", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "interface", Description: "Specify the interface whose ip address was unnumbered", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "loopback", Description: "LoopBack interface", }).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<0-7>", Description: "LoopBack interface number", Validator: VLoopbackInf, }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, }))).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "vlan-interface", Description: "VLAN interface", }).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<0-4095>", Description: "Vlan-interface interface number", Validator: VVlanInf, }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, }))))).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "X.X.X.X", Description: "IP address", Validator: VIPAddress, }).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<1-31>", Description: "IP mask length", Validator: VSubnetLen, }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "sub", Description: "Indicate a subordinate address", }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "X.X.X.X", Description: "IP mask", Validator: VIPAddress, }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "sub", Description: "Indicate a subordinate address", }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })))).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "mtu", Description: "Specify the MTU of the interface", }).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<128-1500>", Description: "MTU in bytes", Validator: VMTU, }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })))).AddCommand((&hhc_ast.SDTNode_Command{ Name: "ipv6", Description: "Specify IPv6 configuration", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "dhcp", Description: "Configure DHCPv6", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "select", Description: "Specify process mode of DHCPv6 packet", }).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "relay", Description: "Relay mode", }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "server", Description: "Server mode", }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })))).AddSubCommand((&hhc_ast.SDTNode_Command{ Name: "mtu", Description: "Specify the IPv6 MTU of the interface", }).AddValArgument((&hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<128-1500>", Description: "MTU in bytes", Validator: VMTU, }).AddEnd(&hhc_ast.SDTNode_End{ Exec: FExec, })))) return treeroot }