network-infra-utility 0.6.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/CHANGELOG.md +31 -0
  4. data/GUIDE.md +201 -212
  5. data/README.md +1 -6
  6. data/Rakefile +1 -1
  7. data/bin/gen-get +144 -0
  8. data/bin/geo-api +40 -1
  9. data/bin/geo-get +18 -36
  10. data/bin/ngeo-get +253 -0
  11. data/bin/packbit +227 -0
  12. data/bin/probe +134 -0
  13. data/document/reference/DNS/346/212/200/346/234/257/346/226/207/346/241/243.md +912 -0
  14. data/network-infra-utility.gemspec +6 -4
  15. data/network.rb +19 -1
  16. data/service/geodb/GeoAPI.md +64 -0
  17. data/service/geodb/api.rb +77 -19
  18. data/service/geoquery/GeoQuery.md +309 -0
  19. data/service/geoquery/cache.rb +78 -0
  20. data/service/geoquery/geo_cache.rb +233 -0
  21. data/service/geoquery/geoquery.rb +327 -0
  22. data/service/geoquery/local.rb +151 -0
  23. data/service/geoquery/merge.rb +52 -0
  24. data/service/geoquery/normalize.rb +102 -0
  25. data/service/geoquery/online.rb +136 -0
  26. data/service/simlab/SimLab.md +140 -0
  27. data/service/simlab/bin/simctl +152 -0
  28. data/service/simlab/lib/simlab.rb +124 -0
  29. data/service/simlab/lib/simlab_engine.rb +146 -0
  30. data/service/simlab/lib/simlab_observer.rb +176 -0
  31. data/service/simlab/lib/simlab_router.rb +330 -0
  32. data/service/simlab/lib/simlab_scenario.rb +200 -0
  33. data/service/simlab/lib/simlab_switch.rb +182 -0
  34. data/service/simlab/lib/simlab_topology.rb +196 -0
  35. data/service/simlab/lib/simlab_traffic.rb +167 -0
  36. data/support/basic/packet.rb +167 -0
  37. data/support/routing/bgp.rb +279 -0
  38. data/support/routing/lpm_trie.rb +145 -0
  39. data/support/routing/ospf.rb +243 -0
  40. data/support/routing/prefix.rb +155 -0
  41. data/support/routing/rib.rb +167 -0
  42. data/support/routing/rip.rb +223 -0
  43. data/support/routing/static_route.rb +102 -0
  44. data/support/switching/frame_forward.rb +87 -0
  45. data/support/switching/mac_table.rb +127 -0
  46. data/support/switching/stp.rb +230 -0
  47. data/support/switching/vlan.rb +155 -0
  48. data/tool/packbit/README.md +136 -0
  49. data/tool/packbit/config/five-tuple.yml +25 -0
  50. data/tool/packbit/config/flow.yml +17 -0
  51. data/tool/packbit/config/packbit.yml +41 -0
  52. data/tool/packbit/erl/packbit.erl +588 -0
  53. data/tool/probe/README.md +88 -0
  54. data/tool/probe/lib/probe.rb +54 -0
  55. data/tool/probe/lib/probe_base.rb +98 -0
  56. data/tool/probe/lib/probe_dns.rb +64 -0
  57. data/tool/probe/lib/probe_icmp.rb +71 -0
  58. data/tool/probe/lib/probe_ntp.rb +51 -0
  59. data/tool/probe/lib/probe_radius.rb +58 -0
  60. data/tool/probe/lib/probe_registry.rb +44 -0
  61. data/tool/probe/lib/probe_result.rb +75 -0
  62. data/tool/probe/lib/probe_snmp.rb +114 -0
  63. data/tool/probe/lib/probe_tcp.rb +82 -0
  64. data/tool/probe/lib/probe_version.rb +7 -0
  65. data/tool/webssh/README.md +42 -0
  66. data/tool/webssh/app.py +788 -0
  67. data/tool/webssh/cli.bat +17 -0
  68. data/tool/webssh/config.yml.template +84 -0
  69. data/tool/webssh/extension//350/256/276/345/244/207/350/257/212/346/226/255.md +103 -0
  70. data/tool/webssh/requirements.txt +7 -0
  71. data/tool/webssh/ssh_cli.py +449 -0
  72. data/tool/webssh/ssh_worker.py +782 -0
  73. data/tool/webssh/static/vendor/xterm-addon-fit.min.js +8 -0
  74. data/tool/webssh/static/vendor/xterm.css +209 -0
  75. data/tool/webssh/static/vendor/xterm.min.js +8 -0
  76. data/tool/webssh/templates/index.html +2674 -0
  77. data/tool/webssh/web.bat +41 -0
  78. data/version.rb +1 -1
  79. metadata +87 -2
@@ -0,0 +1,196 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ # 拓扑模型:定义 node(router/switch/host)、link(带宽/延迟/开销)、interface。
5
+ #
6
+ # 这是仿真的"图纸",不跑协议。支持 YAML 场景文件加载与校验,生成邻接关系。
7
+ #
8
+ # 用法:
9
+ # topo = Topology.new
10
+ # topo.add_router("R1", interfaces: {"Gi0/0" => "10.0.0.1/24"})
11
+ # topo.add_switch("S1", ports: ["Gi0/1", "Gi0/2"])
12
+ # topo.add_link("R1:Gi0/0", "S1:Gi0/1", bandwidth: 100_000, delay: 10)
13
+ # topo.devices # => {"R1" => Router, "S1" => Switch}
14
+
15
+ require 'set'
16
+
17
+ module NetworkInfraUtility
18
+ module SimLab
19
+ # 链路:连接两个设备的接口。
20
+ class Link
21
+ attr_reader :endpoint_a, :endpoint_b, :bandwidth, :delay, :cost
22
+ attr_accessor :status
23
+
24
+ def initialize(endpoint_a:, endpoint_b:, bandwidth:, delay:, cost:, status:)
25
+ @endpoint_a = endpoint_a
26
+ @endpoint_b = endpoint_b
27
+ @bandwidth = bandwidth
28
+ @delay = delay
29
+ @cost = cost
30
+ @status = status
31
+ end
32
+
33
+ def up?
34
+ status == :up
35
+ end
36
+
37
+ def down?
38
+ status == :down
39
+ end
40
+
41
+ def activate
42
+ self.status = :up
43
+ end
44
+
45
+ def deactivate
46
+ self.status = :down
47
+ end
48
+
49
+ def other_end(endpoint)
50
+ endpoint == endpoint_a ? endpoint_b : endpoint_a
51
+ end
52
+ end
53
+
54
+ # 接口端点:设备名 + 端口名。
55
+ Endpoint = Data.define(:device, :port) do
56
+ def to_s
57
+ "#{device}:#{port}"
58
+ end
59
+ end
60
+
61
+ class Topology
62
+ attr_reader :devices, :links, :hosts
63
+
64
+ def initialize
65
+ @devices = {} # name => Router/Switch
66
+ @links = [] # Link 数组
67
+ @hosts = {} # name => {ip:, gateway:}
68
+ @adjacency = {} # device_name => [{port:, peer_device:, peer_port:, link:}]
69
+ end
70
+
71
+ # 添加路由器。
72
+ def add_router(name, interfaces: {}, **opts)
73
+ require_relative "simlab_router"
74
+ router = Device::Router.new(name: name, interfaces: interfaces, **opts)
75
+ @devices[name] = router
76
+ end
77
+
78
+ # 添加交换机。
79
+ def add_switch(name, ports: [], **opts)
80
+ require_relative "simlab_switch"
81
+ switch = Device::Switch.new(name: name, ports: ports, **opts)
82
+ @devices[name] = switch
83
+ end
84
+
85
+ # 添加主机。
86
+ def add_host(name, ip:, gateway:, mac: nil)
87
+ @hosts[name] = { ip: ip, gateway: gateway, mac: mac }
88
+ end
89
+
90
+ # 添加链路。
91
+ # endpoint 格式: "Device:Port" 或 Endpoint 对象
92
+ def add_link(endpoint_a, endpoint_b, bandwidth: 1_000_000, delay: 0, cost: 10, status: :up)
93
+ ep_a = parse_endpoint(endpoint_a)
94
+ ep_b = parse_endpoint(endpoint_b)
95
+
96
+ link = Link.new(
97
+ endpoint_a: ep_a,
98
+ endpoint_b: ep_b,
99
+ bandwidth: bandwidth,
100
+ delay: delay,
101
+ cost: cost,
102
+ status: status
103
+ )
104
+
105
+ @links << link
106
+ update_adjacency(ep_a, ep_b, link)
107
+ update_adjacency(ep_b, ep_a, link)
108
+
109
+ link
110
+ end
111
+
112
+ # 获取设备的邻接关系。
113
+ def neighbors_of(device_name)
114
+ @adjacency[device_name] || []
115
+ end
116
+
117
+ # 获取设备上某端口连接的对端。
118
+ def peer_of(device_name, port)
119
+ (@adjacency[device_name] || []).find { |adj| adj[:port] == port }
120
+ end
121
+
122
+ # 查找两个设备之间的链路。
123
+ def link_between(device_a, device_b)
124
+ @links.find do |link|
125
+ (link.endpoint_a.device == device_a && link.endpoint_b.device == device_b) ||
126
+ (link.endpoint_a.device == device_b && link.endpoint_b.device == device_a)
127
+ end
128
+ end
129
+
130
+ # 所有设备名。
131
+ def device_names
132
+ @devices.keys
133
+ end
134
+
135
+ # 路由器列表。
136
+ def routers
137
+ @devices.select { |_, d| d.is_a?(Device::Router) }
138
+ end
139
+
140
+ # 交换机列表。
141
+ def switches
142
+ @devices.select { |_, d| d.is_a?(Device::Switch) }
143
+ end
144
+
145
+ # 拓扑校验。
146
+ def validate!
147
+ errors = []
148
+
149
+ @links.each do |link|
150
+ ep_a = link.endpoint_a
151
+ ep_b = link.endpoint_b
152
+
153
+ errors << "Unknown device: #{ep_a.device}" unless @devices.key?(ep_a.device)
154
+ errors << "Unknown device: #{ep_b.device}" unless @devices.key?(ep_b.device)
155
+ end
156
+
157
+ raise Error, "Topology validation failed: #{errors.join(', ')}" unless errors.empty?
158
+
159
+ true
160
+ end
161
+
162
+ # 导出为 Hash(便于序列化)。
163
+ def to_h
164
+ {
165
+ devices: @devices.transform_values { |d| d.to_h },
166
+ links: @links.map { |l| { a: l.endpoint_a.to_s, b: l.endpoint_b.to_s, bandwidth: l.bandwidth, delay: l.delay, cost: l.cost, status: l.status } },
167
+ hosts: @hosts
168
+ }
169
+ end
170
+
171
+ private
172
+
173
+ def parse_endpoint(arg)
174
+ case arg
175
+ when Endpoint
176
+ arg
177
+ when String
178
+ device, port = arg.split(':', 2)
179
+ Endpoint.new(device: device, port: port)
180
+ else
181
+ raise ArgumentError, "Invalid endpoint: #{arg}"
182
+ end
183
+ end
184
+
185
+ def update_adjacency(from_ep, to_ep, link)
186
+ @adjacency[from_ep.device] ||= []
187
+ @adjacency[from_ep.device] << {
188
+ port: from_ep.port,
189
+ peer_device: to_ep.device,
190
+ peer_port: to_ep.port,
191
+ link: link
192
+ }
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,167 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ # 流量与事件注入:host ping、持续数据流、链路 up/down 故障、节点启动/关闭。
5
+ #
6
+ # 把注入事件翻译成 engine 事件。
7
+ #
8
+ # 用法:
9
+ # traffic = Traffic.new
10
+ # traffic.ping(engine, topology, from: "H1", to: "10.0.0.2", at: 10)
11
+ # traffic.flap_link(engine, topology, "R1", "Gi0/1", at: 50)
12
+ # traffic.node_down(engine, topology, "R2", at: 100)
13
+
14
+ require_relative '../../../support/basic/packet'
15
+
16
+ module NetworkInfraUtility
17
+ module SimLab
18
+ class Traffic
19
+ # 注入 ping 事件。
20
+ # 从 from 设备向 to IP 发送 ICMP Echo。
21
+ def ping(engine, topology, from:, to:, at:, count: 1, interval: 1)
22
+ count.times do |i|
23
+ engine.schedule(at: at + i * interval) do
24
+ device = topology.devices[from]
25
+ next unless device
26
+
27
+ packet = IPPacket.new(
28
+ src: device.interfaces.values.first&.dig(:ip),
29
+ dst: to,
30
+ ttl: 64,
31
+ protocol: :icmp,
32
+ payload: ICMPPacket.new(type: :echo_request, seq: i)
33
+ )
34
+
35
+ # 发送到默认网关
36
+ route = device.forward_ip(packet)
37
+ # route 将被 engine 投递到下一跳
38
+ yield route if route && block_given?
39
+ end
40
+ end
41
+ end
42
+
43
+ # 注入链路 down 事件。
44
+ def link_down(engine, topology, device_name, port, at:)
45
+ engine.schedule(at: at) do
46
+ device = topology.devices[device_name]
47
+ next unless device
48
+
49
+ if device.is_a?(Device::Router)
50
+ device.interface_down(port)
51
+ elsif device.is_a?(Device::Switch)
52
+ device.port_down(port)
53
+ end
54
+
55
+ # 标记拓扑链路为 down
56
+ link = topology.link_between(device_name, topology.peer_of(device_name, port)&.dig(:peer_device))
57
+ link&.deactivate
58
+ end
59
+ end
60
+
61
+ # 注入链路 up 事件。
62
+ def link_up(engine, topology, device_name, port, at:)
63
+ engine.schedule(at: at) do
64
+ device = topology.devices[device_name]
65
+ next unless device
66
+
67
+ if device.is_a?(Device::Router)
68
+ device.interface_up(port)
69
+ elsif device.is_a?(Device::Switch)
70
+ device.port_up(port)
71
+ end
72
+
73
+ # 标记拓扑链路为 up
74
+ link = topology.link_between(device_name, topology.peer_of(device_name, port)&.dig(:peer_device))
75
+ link&.activate
76
+ end
77
+ end
78
+
79
+ # 链路 flapping。
80
+ def flap_link(engine, topology, device_name, port, at:, times: 3, interval: 10)
81
+ times.times do |i|
82
+ if i.even?
83
+ link_down(engine, topology, device_name, port, at: at + i * interval)
84
+ else
85
+ link_up(engine, topology, device_name, port, at: at + i * interval)
86
+ end
87
+ end
88
+ end
89
+
90
+ # 节点 down。
91
+ def node_down(engine, topology, device_name, at:)
92
+ engine.schedule(at: at) do
93
+ device = topology.devices[device_name]
94
+ next unless device
95
+
96
+ if device.is_a?(Device::Router)
97
+ device.interfaces.each_key { |port| device.interface_down(port) }
98
+ elsif device.is_a?(Device::Switch)
99
+ device.ports.each_key { |port| device.port_down(port) }
100
+ end
101
+ end
102
+ end
103
+
104
+ # 节点 up。
105
+ def node_up(engine, topology, device_name, at:)
106
+ engine.schedule(at: at) do
107
+ device = topology.devices[device_name]
108
+ next unless device
109
+
110
+ if device.is_a?(Device::Router)
111
+ device.interfaces.each_key { |port| device.interface_up(port) }
112
+ elsif device.is_a?(Device::Switch)
113
+ device.ports.each_key { |port| device.port_up(port) }
114
+ end
115
+ end
116
+ end
117
+
118
+ # 通用事件注入。
119
+ def inject(engine, event)
120
+ case event[:type]
121
+ when :ping
122
+ ping(engine, event[:topology], **event[:params])
123
+ when :link_down
124
+ link_down(engine, event[:topology], **event[:params])
125
+ when :link_up
126
+ link_up(engine, event[:topology], **event[:params])
127
+ when :node_down
128
+ node_down(engine, event[:topology], **event[:params])
129
+ when :node_up
130
+ node_up(engine, event[:topology], **event[:params])
131
+ else
132
+ engine.schedule(at: event[:at] || 0, &event[:block]) if event[:block]
133
+ end
134
+ end
135
+
136
+ # 协议更新注入:触发路由协议消息交换。
137
+ def protocol_exchange(engine, topology, at:)
138
+ engine.schedule(at: at) do
139
+ topology.routers.each_value do |router|
140
+ messages = router.announce
141
+ messages.each do |msg|
142
+ # 找到对端设备
143
+ adj = topology.peer_of(router.name, msg[:port])
144
+ next unless adj
145
+
146
+ peer = topology.devices[adj[:peer_device]]
147
+ next unless peer
148
+
149
+ engine.schedule_delayed(delay: 0) do
150
+ peer.receive_packet(port: adj[:peer_port], payload: msg[:payload])
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ # 周期性协议交换。
158
+ def periodic_exchange(engine, topology, interval: 10, until_time: 100)
159
+ t = 0
160
+ while t < until_time
161
+ protocol_exchange(engine, topology, at: t)
162
+ t += interval
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,167 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ # 统一报文抽象:EthernetFrame 与 IPPacket。
5
+ #
6
+ # 供路由/交换共同使用的仿真报文对象,字段对齐真实协议但不编码成字节。
7
+ # 支持嵌套封装:EthernetFrame.payload 可以是 IPPacket 或其他协议消息。
8
+ #
9
+ # 用法:
10
+ # frame = EthernetFrame.new(
11
+ # dst_mac: "ff:ff:ff:ff:ff:ff",
12
+ # src_mac: "00:1a:2b:3c:4d:5e",
13
+ # vlan_tag: 10,
14
+ # payload: IPPacket.new(src: "10.0.0.1", dst: "10.0.0.2", ttl: 64, protocol: :icmp)
15
+ # )
16
+ # frame.broadcast? # => true
17
+ # frame.payload.ip? # => true
18
+
19
+ require_relative 'mac_address'
20
+
21
+ # 以太网帧。
22
+ #
23
+ # 字段:dst_mac, src_mac(MacAddress 或字符串), vlan_tag(Integer 或 nil),
24
+ # payload(任意报文对象或 nil)。
25
+ class EthernetFrame
26
+ attr_reader :dst_mac, :src_mac, :vlan_tag, :payload
27
+
28
+ BROADCAST_MAC = 0xFFFFFFFFFFFF
29
+
30
+ def initialize(dst_mac:, src_mac:, vlan_tag: nil, payload: nil)
31
+ @dst_mac = mac_addr(dst_mac)
32
+ @src_mac = mac_addr(src_mac)
33
+ @vlan_tag = vlan_tag
34
+ @payload = payload
35
+ end
36
+
37
+ def broadcast?
38
+ dst_mac.number == BROADCAST_MAC
39
+ end
40
+
41
+ def multicast?
42
+ dst_mac.multicast?
43
+ end
44
+
45
+ def unicast?
46
+ !broadcast? && !multicast?
47
+ end
48
+
49
+ def tagged?
50
+ !vlan_tag.nil?
51
+ end
52
+
53
+ def vlan_id
54
+ vlan_tag & 0x0FFF if tagged?
55
+ end
56
+
57
+ def priority
58
+ (vlan_tag >> 13) & 0x07 if tagged?
59
+ end
60
+
61
+ def ip?
62
+ payload.is_a?(IPPacket)
63
+ end
64
+
65
+ def arp?
66
+ payload.is_a?(ArpPacket)
67
+ end
68
+
69
+ def to_s
70
+ "Frame[#{src_mac}->#{dst_mac}" +
71
+ (tagged? ? " vlan=#{vlan_id}" : "") +
72
+ (payload ? " #{payload}" : '') + ']'
73
+ end
74
+
75
+ alias inspect to_s
76
+
77
+ private
78
+
79
+ def mac_addr(arg)
80
+ arg.is_a?(MacAddress) ? arg : MacAddress.new(arg)
81
+ end
82
+ end
83
+
84
+ # IP 报文。
85
+ #
86
+ # 字段:src, dst(IPv4 或字符串), ttl, protocol, dscp, payload。
87
+ class IPPacket
88
+ attr_reader :src, :dst, :ttl, :protocol, :dscp, :payload
89
+
90
+ def initialize(src:, dst:, ttl: 64, protocol: :tcp, dscp: 0, payload: nil)
91
+ @src = ip_addr(src)
92
+ @dst = ip_addr(dst)
93
+ @ttl = ttl
94
+ @protocol = protocol
95
+ @dscp = dscp
96
+ @payload = payload
97
+ end
98
+
99
+ def decrement_ttl
100
+ @ttl -= 1
101
+ self
102
+ end
103
+
104
+ def expired?
105
+ @ttl <= 0
106
+ end
107
+
108
+ def to_s
109
+ "IP[#{src}->#{dst} ttl=#{ttl} proto=#{protocol}]"
110
+ end
111
+
112
+ alias inspect to_s
113
+
114
+ private
115
+
116
+ def ip_addr(arg)
117
+ return arg if arg.is_a?(IPv4) || arg.is_a?(IPv6)
118
+
119
+ if arg.is_a?(String)
120
+ arg.include?(':') ? IPv6.new(arg) : IPv4.new(arg)
121
+ else
122
+ arg
123
+ end
124
+ end
125
+ end
126
+
127
+ # ARP 报文(简化)。
128
+ class ArpPacket
129
+ attr_reader :sender_mac, :sender_ip, :target_ip
130
+
131
+ def initialize(sender_mac:, sender_ip:, target_ip:)
132
+ @sender_mac = sender_mac.is_a?(MacAddress) ? sender_mac : MacAddress.new(sender_mac)
133
+ @sender_ip = sender_ip.is_a?(IPv4) ? sender_ip : IPv4.new(sender_ip)
134
+ @target_ip = target_ip.is_a?(IPv4) ? target_ip : IPv4.new(target_ip)
135
+ end
136
+
137
+ def to_s
138
+ "ARP[who-has #{target_ip} tell #{sender_ip}]"
139
+ end
140
+
141
+ alias inspect to_s
142
+ end
143
+
144
+ # ICMP Echo 报文(简化)。
145
+ class ICMPPacket
146
+ attr_reader :type, :seq, :payload
147
+
148
+ def initialize(type: :echo_request, seq: 0, payload: nil)
149
+ @type = type
150
+ @seq = seq
151
+ @payload = payload
152
+ end
153
+
154
+ def echo_request?
155
+ type == :echo_request
156
+ end
157
+
158
+ def echo_reply?
159
+ type == :echo_reply
160
+ end
161
+
162
+ def to_s
163
+ "ICMP[#{type} seq=#{seq}]"
164
+ end
165
+
166
+ alias inspect to_s
167
+ end