|
| 1 | +package routers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "embed" |
| 6 | + "fmt" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "gitlab.as203038.net/AS203038/looking-glass/server/errs" |
| 10 | + "gitlab.as203038.net/AS203038/looking-glass/server/utils" |
| 11 | + yaml "gopkg.in/yaml.v2" |
| 12 | +) |
| 13 | + |
| 14 | +type _tpl_data struct { |
| 15 | + Cfg *utils.RouterConfig |
| 16 | + IP *utils.IPNet |
| 17 | + Community string |
| 18 | + ASPath string |
| 19 | +} |
| 20 | + |
| 21 | +type Yaml struct { |
| 22 | + Path string |
| 23 | + Template struct { |
| 24 | + Name string `yaml:"name"` |
| 25 | + Ping struct { |
| 26 | + Any []string `yaml:"any"` |
| 27 | + IPv4 []string `yaml:"ipv4"` |
| 28 | + IPv6 []string `yaml:"ipv6"` |
| 29 | + } `yaml:"ping"` |
| 30 | + Traceroute struct { |
| 31 | + Any []string `yaml:"any"` |
| 32 | + IPv4 []string `yaml:"ipv4"` |
| 33 | + IPv6 []string `yaml:"ipv6"` |
| 34 | + } `yaml:"traceroute"` |
| 35 | + BGP struct { |
| 36 | + Route []string `yaml:"route"` |
| 37 | + Community []string `yaml:"community"` |
| 38 | + ASPath []string `yaml:"aspath"` |
| 39 | + } `yaml:"bgp"` |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +//go:embed all:*.yml |
| 44 | +var compiledRouters embed.FS |
| 45 | + |
| 46 | +func init() { |
| 47 | + // Load all routers |
| 48 | + files, err := compiledRouters.ReadDir(".") |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + for _, file := range files { |
| 53 | + y := &Yaml{} |
| 54 | + y.Path = file.Name() |
| 55 | + yamlFile, err := compiledRouters.ReadFile(y.Path) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + err = yaml.Unmarshal(yamlFile, &y.Template) |
| 60 | + if err != nil { |
| 61 | + panic(err) |
| 62 | + } |
| 63 | + register(y.Template.Name, y) |
| 64 | + fmt.Printf("Router %s registered\n", y.Template.Name) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (rt *Yaml) _tpl(name string, data _tpl_data) ([]string, error) { |
| 69 | + var tpl []string |
| 70 | + var ret []string |
| 71 | + switch name { |
| 72 | + case "ping": |
| 73 | + if rt.Template.Ping.Any != nil { |
| 74 | + tpl = rt.Template.Ping.Any |
| 75 | + } else if data.IP.IsIPv4() { |
| 76 | + tpl = rt.Template.Ping.IPv4 |
| 77 | + } else if data.IP.IsIPv6() { |
| 78 | + tpl = rt.Template.Ping.IPv6 |
| 79 | + } |
| 80 | + case "traceroute": |
| 81 | + if rt.Template.Traceroute.Any != nil { |
| 82 | + tpl = rt.Template.Traceroute.Any |
| 83 | + } else if data.IP.IsIPv4() { |
| 84 | + tpl = rt.Template.Traceroute.IPv4 |
| 85 | + } else if data.IP.IsIPv6() { |
| 86 | + tpl = rt.Template.Traceroute.IPv6 |
| 87 | + } |
| 88 | + case "bgp.route": |
| 89 | + tpl = rt.Template.BGP.Route |
| 90 | + case "bgp.community": |
| 91 | + tpl = rt.Template.BGP.Community |
| 92 | + case "bgp.aspath": |
| 93 | + tpl = rt.Template.BGP.ASPath |
| 94 | + } |
| 95 | + if tpl == nil { |
| 96 | + return nil, errs.OperationUnknown |
| 97 | + } |
| 98 | + for _, t := range tpl { |
| 99 | + var buf bytes.Buffer |
| 100 | + tt, err := template.New(t).Parse(t) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + err = tt.Execute(&buf, data) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + ret = append(ret, buf.String()) |
| 109 | + } |
| 110 | + return ret, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (rt *Yaml) Ping(cfg *utils.RouterConfig, ip *utils.IPNet) ([]string, error) { |
| 114 | + return rt._tpl("ping", _tpl_data{Cfg: cfg, IP: ip}) |
| 115 | +} |
| 116 | + |
| 117 | +func (rt *Yaml) Traceroute(cfg *utils.RouterConfig, ip *utils.IPNet) ([]string, error) { |
| 118 | + return rt._tpl("traceroute", _tpl_data{Cfg: cfg, IP: ip}) |
| 119 | +} |
| 120 | + |
| 121 | +func (rt *Yaml) BGPRoute(cfg *utils.RouterConfig, ip *utils.IPNet) ([]string, error) { |
| 122 | + return rt._tpl("bgp.route", _tpl_data{Cfg: cfg, IP: ip}) |
| 123 | +} |
| 124 | + |
| 125 | +func (rt *Yaml) BGPCommunity(cfg *utils.RouterConfig, community string) ([]string, error) { |
| 126 | + return rt._tpl("bgp.community", _tpl_data{Cfg: cfg, Community: community}) |
| 127 | +} |
| 128 | + |
| 129 | +func (rt *Yaml) BGPASPath(cfg *utils.RouterConfig, aspath string) ([]string, error) { |
| 130 | + return rt._tpl("bgp.aspath", _tpl_data{Cfg: cfg, ASPath: aspath}) |
| 131 | +} |
0 commit comments