lib/mihari/structs/censys.rb in mihari-5.2.1 vs lib/mihari/structs/censys.rb in mihari-5.2.2
- old
+ new
@@ -7,16 +7,28 @@
include Mixins::AutonomousSystem
attribute :asn, Types::Int
#
+ # @return [Integer]
+ #
+ def asn
+ attributes[:asn]
+ end
+
+ #
# @return [Mihari::AutonomousSystem]
#
def to_as
Mihari::AutonomousSystem.new(asn: normalize_asn(asn))
end
+ #
+ # @param [Hash] d
+ #
+ # @return [AutonomousSystem]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
asn: d.fetch("asn")
)
@@ -26,10 +38,24 @@
class Location < Dry::Struct
attribute :country, Types::String.optional
attribute :country_code, Types::String.optional
#
+ # @return [String, nil]
+ #
+ def country
+ attributes[:country]
+ end
+
+ #
+ # @return [String, nil]
+ #
+ def country_code
+ attributes[:country_code]
+ end
+
+ #
# @return [Mihari::Geolocation] <description>
#
def to_geolocation
# sometimes Censys overlooks country
# then set geolocation as nil
@@ -39,10 +65,15 @@
country: country,
country_code: country_code
)
end
+ #
+ # @param [Hash] d
+ #
+ # @return [Location]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
country: d["country"],
country_code: d["country_code"]
@@ -52,16 +83,28 @@
class Service < Dry::Struct
attribute :port, Types::Integer
#
+ # @return [Integer]
+ #
+ def port
+ attributes[:port]
+ end
+
+ #
# @return [Mihari::Port]
#
def to_port
Port.new(port: port)
end
+ #
+ # @param [Hash] d
+ #
+ # @return [Service]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
port: d.fetch("port")
)
@@ -74,32 +117,69 @@
attribute :autonomous_system, AutonomousSystem
attribute :metadata, Types::Hash
attribute :services, Types.Array(Service)
#
+ # @return [String]
+ #
+ def ip
+ attributes[:ip]
+ end
+
+ #
+ # @return [Location]
+ #
+ def location
+ attributes[:location]
+ end
+
+ #
+ # @return [AutonomousSystem]
+ #
+ def autonomous_system
+ attributes[:autonomous_system]
+ end
+
+ #
+ # @return [Hash]
+ #
+ def metadata
+ attributes[:metadata]
+ end
+
+ #
+ # @return [Array<Service>]
+ #
+ def services
+ attributes[:services]
+ end
+
+ #
# @return [Array<Mihari::Port>]
#
def to_ports
services.map(&:to_port)
end
#
- # @param [String] source
- #
# @return [Mihari::Artifact]
#
- def to_artifact(source = "Censys")
+ def to_artifact
Artifact.new(
data: ip,
- source: source,
metadata: metadata,
autonomous_system: autonomous_system.to_as,
geolocation: location.to_geolocation,
ports: to_ports
)
end
+ #
+ # @param [Hash] d
+ #
+ # @return [Hit]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
ip: d.fetch("ip"),
location: Location.from_dynamic!(d.fetch("location")),
@@ -109,18 +189,37 @@
)
end
end
class Links < Dry::Struct
- attribute :next, Types::String
- attribute :prev, Types::String
+ attribute :next, Types::String.optional
+ attribute :prev, Types::String.optional
+ #
+ # @return [String, nil]
+ #
+ def next
+ attributes[:next]
+ end
+
+ #
+ # @return [String, nil]
+ #
+ def prev
+ attributes[:prev]
+ end
+
+ #
+ # @param [Hash] d
+ #
+ # @return [Links]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
- next: d.fetch("next"),
- prev: d.fetch("prev")
+ next: d["next"],
+ prev: d["prev"]
)
end
end
class Result < Dry::Struct
@@ -128,18 +227,49 @@
attribute :total, Types::Int
attribute :hits, Types.Array(Hit)
attribute :links, Links
#
- # @param [String] source
+ # @return [String]
#
+ def query
+ attributes[:query]
+ end
+
+ #
+ # @return [Integer]
+ #
+ def total
+ attributes[:total]
+ end
+
+ #
+ # @return [Array<Hit>]
+ #
+ def hits
+ attributes[:hits]
+ end
+
+ #
+ # @return [Links]
+ #
+ def links
+ attributes[:links]
+ end
+
+ #
# @return [Array<Mihari::Artifact>]
#
- def to_artifacts(source = "Censys")
- hits.map { |hit| hit.to_artifact(source) }
+ def to_artifacts
+ hits.map { |hit| hit.to_artifact }
end
+ #
+ # @param [Hash] d
+ #
+ # @return [Result]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
query: d.fetch("query"),
total: d.fetch("total"),
@@ -152,9 +282,35 @@
class Response < Dry::Struct
attribute :code, Types::Int
attribute :status, Types::String
attribute :result, Result
+ #
+ # @return [Integer]
+ #
+ def code
+ attributes[:code]
+ end
+
+ #
+ # @return [String]
+ #
+ def status
+ attributes[:status]
+ end
+
+ #
+ # @return [Result]
+ #
+ def result
+ attributes[:result]
+ end
+
+ #
+ # @param [Hash] d
+ #
+ # @return [Response]
+ #
def self.from_dynamic!(d)
d = Types::Hash[d]
new(
code: d.fetch("code"),
status: d.fetch("status"),