mihari 6.2.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +13 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +62 -0
  7. data/exe/mihari +2 -1
  8. data/lefthook.yml +8 -0
  9. data/lib/mihari/actor.rb +4 -4
  10. data/lib/mihari/analyzers/base.rb +16 -0
  11. data/lib/mihari/analyzers/binaryedge.rb +4 -2
  12. data/lib/mihari/analyzers/censys.rb +7 -5
  13. data/lib/mihari/analyzers/circl.rb +5 -3
  14. data/lib/mihari/analyzers/crtsh.rb +4 -1
  15. data/lib/mihari/analyzers/dnstwister.rb +1 -1
  16. data/lib/mihari/analyzers/feed.rb +12 -20
  17. data/lib/mihari/analyzers/fofa.rb +6 -8
  18. data/lib/mihari/analyzers/greynoise.rb +4 -2
  19. data/lib/mihari/analyzers/hunterhow.rb +4 -2
  20. data/lib/mihari/analyzers/onyphe.rb +4 -2
  21. data/lib/mihari/analyzers/otx.rb +5 -3
  22. data/lib/mihari/analyzers/passivetotal.rb +29 -12
  23. data/lib/mihari/analyzers/pulsedive.rb +5 -3
  24. data/lib/mihari/analyzers/securitytrails.rb +32 -8
  25. data/lib/mihari/analyzers/shodan.rb +4 -2
  26. data/lib/mihari/analyzers/urlscan.rb +4 -2
  27. data/lib/mihari/analyzers/virustotal.rb +5 -5
  28. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
  29. data/lib/mihari/analyzers/zoomeye.rb +4 -2
  30. data/lib/mihari/cli/{main.rb → application.rb} +17 -5
  31. data/lib/mihari/cli/artifact.rb +14 -0
  32. data/lib/mihari/cli/config.rb +14 -0
  33. data/lib/mihari/cli/rule.rb +1 -0
  34. data/lib/mihari/cli/tag.rb +14 -0
  35. data/lib/mihari/clients/base.rb +2 -2
  36. data/lib/mihari/clients/binaryedge.rb +2 -2
  37. data/lib/mihari/clients/crtsh.rb +2 -9
  38. data/lib/mihari/clients/fofa.rb +1 -1
  39. data/lib/mihari/clients/hunterhow.rb +1 -1
  40. data/lib/mihari/clients/mmdb.rb +28 -0
  41. data/lib/mihari/clients/passivetotal.rb +7 -20
  42. data/lib/mihari/clients/securitytrails.rb +19 -43
  43. data/lib/mihari/clients/shodan_internet_db.rb +28 -0
  44. data/lib/mihari/clients/the_hive.rb +7 -5
  45. data/lib/mihari/commands/alert.rb +53 -11
  46. data/lib/mihari/commands/artifact.rb +66 -0
  47. data/lib/mihari/commands/config.rb +23 -0
  48. data/lib/mihari/commands/database.rb +1 -1
  49. data/lib/mihari/commands/rule.rb +40 -27
  50. data/lib/mihari/commands/search.rb +10 -11
  51. data/lib/mihari/commands/sidekiq.rb +31 -0
  52. data/lib/mihari/commands/tag.rb +46 -0
  53. data/lib/mihari/commands/web.rb +6 -7
  54. data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
  55. data/lib/mihari/concerns/configurable.rb +72 -0
  56. data/lib/mihari/concerns/database_connectable.rb +16 -0
  57. data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
  58. data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
  59. data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
  60. data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
  61. data/lib/mihari/config.rb +13 -12
  62. data/lib/mihari/database.rb +30 -42
  63. data/lib/mihari/emitters/database.rb +5 -6
  64. data/lib/mihari/emitters/misp.rb +4 -11
  65. data/lib/mihari/emitters/slack.rb +7 -5
  66. data/lib/mihari/emitters/the_hive.rb +8 -58
  67. data/lib/mihari/emitters/webhook.rb +6 -6
  68. data/lib/mihari/enrichers/google_public_dns.rb +1 -1
  69. data/lib/mihari/enrichers/mmdb.rb +28 -0
  70. data/lib/mihari/enrichers/shodan.rb +3 -5
  71. data/lib/mihari/enrichers/whois.rb +3 -3
  72. data/lib/mihari/entities/alert.rb +3 -10
  73. data/lib/mihari/entities/artifact.rb +9 -9
  74. data/lib/mihari/entities/config.rb +2 -2
  75. data/lib/mihari/entities/cpe.rb +1 -0
  76. data/lib/mihari/entities/dns.rb +1 -0
  77. data/lib/mihari/entities/geolocation.rb +1 -0
  78. data/lib/mihari/entities/ip_address.rb +1 -3
  79. data/lib/mihari/entities/messages.rb +17 -0
  80. data/lib/mihari/entities/pagination.rb +11 -0
  81. data/lib/mihari/entities/port.rb +1 -0
  82. data/lib/mihari/entities/reverse_dns.rb +1 -0
  83. data/lib/mihari/entities/rule.rb +2 -20
  84. data/lib/mihari/entities/tag.rb +2 -2
  85. data/lib/mihari/entities/whois.rb +1 -0
  86. data/lib/mihari/errors.rb +2 -4
  87. data/lib/mihari/http.rb +4 -0
  88. data/lib/mihari/models/alert.rb +21 -76
  89. data/lib/mihari/models/artifact.rb +51 -31
  90. data/lib/mihari/models/autonomous_system.rb +5 -13
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +3 -10
  93. data/lib/mihari/models/dns.rb +2 -6
  94. data/lib/mihari/models/geolocation.rb +7 -12
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +3 -8
  97. data/lib/mihari/models/rule.rb +16 -60
  98. data/lib/mihari/models/tag.rb +17 -1
  99. data/lib/mihari/models/tagging.rb +1 -1
  100. data/lib/mihari/models/whois.rb +1 -4
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +2 -2
  104. data/lib/mihari/schemas/concerns/orrable.rb +24 -0
  105. data/lib/mihari/schemas/emitter.rb +1 -2
  106. data/lib/mihari/schemas/enricher.rb +3 -4
  107. data/lib/mihari/schemas/macros.rb +1 -1
  108. data/lib/mihari/schemas/options.rb +0 -2
  109. data/lib/mihari/schemas/rule.rb +1 -2
  110. data/lib/mihari/services/{rule_builder.rb → builders.rb} +1 -6
  111. data/lib/mihari/services/creators.rb +22 -0
  112. data/lib/mihari/services/destroyers.rb +41 -0
  113. data/lib/mihari/services/enrichers.rb +25 -0
  114. data/lib/mihari/services/feed.rb +107 -0
  115. data/lib/mihari/services/getters.rb +58 -0
  116. data/lib/mihari/services/initializers.rb +22 -0
  117. data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
  118. data/lib/mihari/services/searchers.rb +91 -0
  119. data/lib/mihari/sidekiq/application.rb +13 -0
  120. data/lib/mihari/sidekiq/jobs.rb +36 -0
  121. data/lib/mihari/structs/censys.rb +1 -1
  122. data/lib/mihari/structs/config.rb +10 -10
  123. data/lib/mihari/structs/filters.rb +12 -86
  124. data/lib/mihari/structs/google_public_dns.rb +1 -1
  125. data/lib/mihari/structs/greynoise.rb +1 -1
  126. data/lib/mihari/structs/mmdb.rb +115 -0
  127. data/lib/mihari/structs/onyphe.rb +1 -1
  128. data/lib/mihari/structs/shodan.rb +2 -2
  129. data/lib/mihari/version.rb +1 -1
  130. data/lib/mihari/web/{app.rb → application.rb} +28 -15
  131. data/lib/mihari/web/endpoints/alerts.rb +34 -81
  132. data/lib/mihari/web/endpoints/artifacts.rb +43 -63
  133. data/lib/mihari/web/endpoints/configs.rb +3 -5
  134. data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
  135. data/lib/mihari/web/endpoints/rules.rb +58 -137
  136. data/lib/mihari/web/endpoints/tags.rb +21 -17
  137. data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
  138. data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
  139. data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
  140. data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
  141. data/lib/mihari/web/public/index.html +2 -3
  142. data/lib/mihari/web/public/redoc-static.html +385 -381
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +16 -8
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +173 -42
  148. data/lib/mihari/commands/mixins.rb +0 -11
  149. data/lib/mihari/enrichers/ipinfo.rb +0 -52
  150. data/lib/mihari/entities/message.rb +0 -9
  151. data/lib/mihari/feed/parser.rb +0 -38
  152. data/lib/mihari/feed/reader.rb +0 -111
  153. data/lib/mihari/mixins/configurable.rb +0 -68
  154. data/lib/mihari/schemas/mixins.rb +0 -20
  155. data/lib/mihari/services/alert_runner.rb +0 -20
  156. data/lib/mihari/structs/ipinfo.rb +0 -53
  157. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  158. data/lib/mihari/web/public/assets/index-1d77cd61.js +0 -1756
  159. data/lib/mihari/web/public/assets/index-4c8509ee.css +0 -1
  160. /data/lib/mihari/web/public/assets/{mode-yaml-24faa242.js → mode-yaml-BC4MIiYj.js} +0 -0
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Services
5
+ class ResultValue
6
+ # @return [Array<Object>]
7
+ attr_reader :results
8
+
9
+ # @return [Integer]
10
+ attr_reader :total
11
+
12
+ # @return [Mihari::Structs::Filters::Search]
13
+ attr_reader :filter
14
+
15
+ #
16
+ # @param [Array<Object>] results
17
+ # @param [Integer] total
18
+ # @param [Mihari::Structs::Filters::Search] filter
19
+ #
20
+ def initialize(results:, total:, filter:)
21
+ @results = results
22
+ @total = total
23
+ @filter = filter
24
+ end
25
+ end
26
+
27
+ class BaseSearcher < Service
28
+ #
29
+ # @param [Hash] params
30
+ #
31
+ # @return [ResultValue]
32
+ #
33
+ def call(params)
34
+ filter = build_filter(params)
35
+ ResultValue.new(
36
+ total: klass.count_by_filter(filter),
37
+ results: klass.search_by_filter(filter),
38
+ filter: filter
39
+ )
40
+ end
41
+
42
+ private
43
+
44
+ #
45
+ # @param [Hash] params
46
+ #
47
+ # @return [Mihari::Structs::Filters::Search]
48
+ #
49
+ def build_filter(params)
50
+ normalized = params.to_h.to_snake_keys.symbolize_keys
51
+ Structs::Filters::Search.new(**normalized)
52
+ end
53
+ end
54
+
55
+ class AlertSearcher < BaseSearcher
56
+ def klass
57
+ Models::Alert
58
+ end
59
+ end
60
+
61
+ class ArtifactSearcher < BaseSearcher
62
+ def klass
63
+ Models::Artifact
64
+ end
65
+ end
66
+
67
+ class RuleSearcher < BaseSearcher
68
+ def klass
69
+ Models::Rule
70
+ end
71
+ end
72
+
73
+ class TagSearcher < BaseSearcher
74
+ def klass
75
+ Models::Tag
76
+ end
77
+ end
78
+
79
+ class ConfigSearcher < Service
80
+ #
81
+ # @return [Array<Mihari::Structs::Config>]
82
+ #
83
+ def call
84
+ # NOTE: literally it does not search but list configs
85
+ (Mihari.analyzers + Mihari.emitters + Mihari.enrichers).filter_map do |klass|
86
+ Mihari::Structs::Config.from_class klass
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+
5
+ require "mihari/sidekiq/jobs"
6
+
7
+ Sidekiq.configure_server do |config|
8
+ config.redis = { url: Mihari.config.sidekiq_redis_url.to_s }
9
+ end
10
+
11
+ Sidekiq.configure_client do |config|
12
+ config.redis = { url: Mihari.config.sidekiq_redis_url.to_s }
13
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+
5
+ module Mihari
6
+ module Jobs
7
+ class SearchJob
8
+ include Sidekiq::Job
9
+ include Concerns::DatabaseConnectable
10
+
11
+ #
12
+ # @param [String] path_or_id
13
+ #
14
+ def perform(path_or_id)
15
+ with_db_connection do
16
+ rule = Mihari::Rule.from_model(Mihari::Models::Rule.find(path_or_id))
17
+ rule.call
18
+ end
19
+ end
20
+ end
21
+
22
+ class ArtifactEnrichJob
23
+ include Sidekiq::Job
24
+ include Concerns::DatabaseConnectable
25
+
26
+ #
27
+ # @param [Integer] id
28
+ #
29
+ def perform(id)
30
+ with_db_connection do
31
+ Services::ArtifactEnricher.call id
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module Censys
6
6
  class AutonomousSystem < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] asn
10
10
  # @return [Integer]
@@ -13,11 +13,11 @@ module Mihari
13
13
 
14
14
  # @!attribute [r] is_configured
15
15
  # @return [Boolean]
16
- attribute :is_configured, Types::Bool
16
+ attribute :configured, Types::Bool
17
17
 
18
18
  # @!attribute [r] values
19
19
  # @return [Array<Hash>, nil]
20
- attribute :values, Types.Array(Types::Hash).optional
20
+ attribute :items, Types.Array(Types::Hash).optional
21
21
 
22
22
  class << self
23
23
  #
@@ -28,9 +28,9 @@ module Mihari
28
28
  # @return [String, nil]
29
29
  #
30
30
  def get_type(klass)
31
- return "Analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
- return "Emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
- return "Enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
31
+ return "analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
+ return "emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
+ return "enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
34
34
 
35
35
  nil
36
36
  end
@@ -45,9 +45,9 @@ module Mihari
45
45
  def get_dummy(klass)
46
46
  type = get_type(klass)
47
47
  case type
48
- when "Analyzer"
48
+ when "analyzer"
49
49
  klass.new("dummy")
50
- when "Emitter"
50
+ when "emitter"
51
51
  klass.new(rule: nil)
52
52
  else
53
53
  klass.new
@@ -68,9 +68,9 @@ module Mihari
68
68
  begin
69
69
  instance = get_dummy(klass)
70
70
  new(
71
- name: klass.to_s.split("::").last.to_s,
72
- values: instance.configuration_values,
73
- is_configured: instance.configured?,
71
+ name: klass.class_key,
72
+ items: klass.configuration_items,
73
+ configured: instance.configured?,
74
74
  type: type
75
75
  )
76
76
  rescue ArgumentError
@@ -3,92 +3,18 @@
3
3
  module Mihari
4
4
  module Structs
5
5
  module Filters
6
- module Alert
7
- class SearchFilter < Dry::Struct
8
- # @!attribute [r] artifact_data
9
- # @return [String, nil]
10
- attribute? :artifact_data, Types::String.optional
11
-
12
- # @!attribute [r] rule_id
13
- # @return [String, nil]
14
- attribute? :rule_id, Types::String.optional
15
-
16
- # @!attribute [r] tag_name
17
- # @return [String, nil]
18
- attribute? :tag_name, Types::String.optional
19
-
20
- # @!attribute [r] from_at
21
- # @return [DateTime, nil]
22
- attribute? :from_at, Types::DateTime.optional
23
-
24
- # @!attribute [r] to_at
25
- # @return [DateTime, nil]
26
- attribute? :to_at, Types::DateTime.optional
27
- end
28
-
29
- class SearchFilterWithPagination < SearchFilter
30
- # @!attribute [r] page
31
- # @return [Integer, nil]
32
- attribute? :page, Types::Int.default(1)
33
-
34
- # @!attribute [r] limit
35
- # @return [Integer, nil]
36
- attribute? :limit, Types::Int.default(10)
37
-
38
- def without_pagination
39
- SearchFilter.new(
40
- artifact_data: artifact_data,
41
- from_at: from_at,
42
- rule_id: rule_id,
43
- tag_name: tag_name,
44
- to_at: to_at
45
- )
46
- end
47
- end
48
- end
49
-
50
- module Rule
51
- class SearchFilter < Dry::Struct
52
- # @!attribute [r] description
53
- # @return [String, nil]
54
- attribute? :description, Types::String.optional
55
-
56
- # @!attribute [r] tag_name
57
- # @return [String, nil]
58
- attribute? :tag_name, Types::String.optional
59
-
60
- # @!attribute [r] title
61
- # @return [String, nil]
62
- attribute? :title, Types::String.optional
63
-
64
- # @!attribute [r] from_at
65
- # @return [DateTime, nil]
66
- attribute? :from_at, Types::DateTime.optional
67
-
68
- # @!attribute [r] to_at
69
- # @return [DateTime, nil]
70
- attribute? :to_at, Types::DateTime.optional
71
- end
72
-
73
- class SearchFilterWithPagination < SearchFilter
74
- # @!attribute [r] page
75
- # @return [Integer, nil]
76
- attribute? :page, Types::Int.default(1)
77
-
78
- # @!attribute [r] limit
79
- # @return [Integer, nil]
80
- attribute? :limit, Types::Int.default(10)
81
-
82
- def without_pagination
83
- SearchFilter.new(
84
- description: description,
85
- from_at: from_at,
86
- tag_name: tag_name,
87
- title: title,
88
- to_at: to_at
89
- )
90
- end
91
- end
6
+ class Search < Dry::Struct
7
+ # @!attribute [r] q
8
+ # @return [String]
9
+ attribute :q, Types::String
10
+
11
+ # @!attribute [r] page
12
+ # @return [Integer, nil]
13
+ attribute? :page, Types::Int.default(1)
14
+
15
+ # @!attribute [r] limit
16
+ # @return [Integer, nil]
17
+ attribute? :limit, Types::Int.default(10)
92
18
  end
93
19
  end
94
20
  end
@@ -5,7 +5,7 @@ module Mihari
5
5
  module GooglePublicDNS
6
6
  INT_TYPE_TO_TYPE =
7
7
  { 1 => :A, 38 => :A6, 28 => :AAAA, 18 => :AFSDB, 255 => :ANY, 42 => :APL, 34 => :ATMA, 252 => :AXFR, 37 => :CERT,
8
- 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }
8
+ 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }.freeze
9
9
 
10
10
  class Answer < Dry::Struct
11
11
  # @!attribute [r] name
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module GreyNoise
6
6
  class Metadata < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] country
10
10
  # @return [String]
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Structs
5
+ module MMDB
6
+ class Country < Dry::Struct
7
+ # @!attribute [r] iso_code
8
+ # @return [String]
9
+ attribute :iso_code, Types::String
10
+
11
+ # @!attribute [r] autonomous_system_number
12
+ # @return [String]
13
+ attribute :autonomous_system_number, Types::String.optional
14
+
15
+ class << self
16
+ def from_dynamic!(d)
17
+ d = Types::Hash[d]
18
+ new(
19
+ iso_code: d.fetch("iso_code"),
20
+ autonomous_system_number: d["AutonomousSystemNumber"]
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ class CountryInfo < Dry::Struct
27
+ # @!attribute [r] latitude
28
+ # @return [String]
29
+ attribute :latitude, Types::String
30
+
31
+ # @!attribute [r] longitude
32
+ # @return [String]
33
+ attribute :longitude, Types::String
34
+
35
+ def self.from_dynamic!(d)
36
+ d = Types::Hash[d]
37
+ new(
38
+ latitude: d.fetch("Latitude (average)"),
39
+ longitude: d.fetch("Longitude (average)")
40
+ )
41
+ end
42
+
43
+ class << self
44
+ def from_json!(json)
45
+ from_dynamic!(JSON.parse(json))
46
+ end
47
+ end
48
+ end
49
+
50
+ class Item < Dry::Struct
51
+ # @!attribute [r] country
52
+ # @return [Country]
53
+ attribute :country, Country
54
+
55
+ # @!attribute [r] country_info
56
+ # @return [CountryInfo]
57
+ attribute :country_info, CountryInfo
58
+
59
+ class << self
60
+ def from_dynamic!(d)
61
+ d = Types::Hash[d]
62
+ new(
63
+ country: Country.from_dynamic!(d.fetch("country")),
64
+ country_info: CountryInfo.from_dynamic!(d.fetch("country_info"))
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ class Response < Dry::Struct
71
+ # @!attribute [r] items
72
+ # @return [Array<Item>]
73
+ attribute :items, Types.Array(Item)
74
+
75
+ #
76
+ # @return [Item, nil]
77
+ #
78
+ def item
79
+ items.find { |item| item.country.autonomous_system_number }
80
+ end
81
+
82
+ #
83
+ # @return [Integer, nil]
84
+ #
85
+ def asn
86
+ item&.country&.autonomous_system_number&.to_i
87
+ end
88
+
89
+ #
90
+ # @return [String, nil]
91
+ #
92
+ def country_code
93
+ item&.country&.iso_code
94
+ end
95
+
96
+ #
97
+ # @return [String, nil]
98
+ #
99
+ def loc
100
+ return nil if item.nil?
101
+
102
+ [item.country_info.latitude, item.country_info.longitude].join(",")
103
+ end
104
+
105
+ class << self
106
+ def from_dynamic!(d)
107
+ new(
108
+ items: d.map { |x| Item.from_dynamic!(x) }
109
+ )
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module Onyphe
6
6
  class Result < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] asn
10
10
  # @return [String]
@@ -16,7 +16,7 @@ module Mihari
16
16
  # @return [Mihari::Geolocation, nil]
17
17
  #
18
18
  def geolocation
19
- return nil if country_name.nil? && country_code.nil?
19
+ return nil if country_name.nil? || country_code.nil?
20
20
 
21
21
  Mihari::Models::Geolocation.new(
22
22
  country: country_name,
@@ -41,7 +41,7 @@ module Mihari
41
41
  end
42
42
 
43
43
  class Match < Dry::Struct
44
- include Mixins::AutonomousSystem
44
+ include Concerns::AutonomousSystemNormalizable
45
45
 
46
46
  # @!attribute [r] asn
47
47
  # @return [String, nil]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "6.2.0"
4
+ VERSION = "7.0.0"
5
5
  end
@@ -1,21 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "launchy"
4
-
3
+ # Rack
5
4
  require "rack"
6
- require "rackup"
7
5
  require "rack/cors"
6
+ require "rack/session"
7
+ require "rackup"
8
8
 
9
9
  require "rack/handler/puma"
10
10
 
11
+ # Grape
11
12
  require "grape-swagger"
12
13
  require "grape-swagger-entity"
13
14
 
14
- require "mihari/web/middleware/connection_adapter"
15
- require "mihari/web/middleware/error_notification_adapter"
15
+ require "launchy"
16
+
17
+ require "mihari/web/middleware/connection"
18
+ require "mihari/web/middleware/capture_exceptions"
16
19
 
17
20
  require "mihari/web/api"
18
21
 
22
+ # Sidekiq
23
+ require "sidekiq/web"
24
+
25
+ require "mihari/sidekiq/application"
26
+
19
27
  module Mihari
20
28
  module Web
21
29
  #
@@ -41,10 +49,10 @@ module Mihari
41
49
  status, headers, body = API.call(env)
42
50
  return [status, headers, body] unless headers["x-cascade"] == "pass"
43
51
 
52
+ req = Rack::Request.new(env)
44
53
  # Check if the App wants us to pass the response along to others
45
- request_path = env["PATH_INFO"]
46
54
  filenames.each do |path|
47
- static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => request_path + path))
55
+ static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => req.path_info + path))
48
56
  return [static_status, static_headers, static_body] if static_status != 404
49
57
  end
50
58
 
@@ -60,24 +68,29 @@ module Mihari
60
68
  resource "*", headers: :any, methods: %i[get post put delete options]
61
69
  end
62
70
  end
63
- use Middleware::ConnectionAdapter
64
- use Middleware::ErrorNotificationAdapter
71
+ use Middleware::Connection
72
+ use Middleware::CaptureExceptions
73
+ use BetterErrors::Middleware if Mihari.development? && defined?(BetterErrors::Middleware)
74
+
75
+ if Mihari.sidekiq?
76
+ use Rack::Session::Cookie, secret: SecureRandom.hex(32), same_site: true, max_age: 86_400
65
77
 
66
- use Sentry::Rack::CaptureExceptions if Sentry.initialized?
67
- use BetterErrors::Middleware if ENV["RACK_ENV"] == "development" && defined?(BetterErrors::Middleware)
78
+ map "/sidekiq" do
79
+ run Sidekiq::Web
80
+ end
81
+ end
68
82
 
69
83
  run App.new
70
84
  end.to_app
71
85
  end
72
86
 
73
87
  def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60, open: true)
74
- url = "http://#{host}:#{port}"
75
-
76
88
  # set maximum number of threads to use as PARALLEL_PROCESSOR_COUNT (if it is not set)
77
89
  # ref. https://github.com/grosser/parallel#tips
78
90
  # TODO: is this the best way?
79
91
  _min_thread, max_thread = threads.split(":")
80
- ENV["PARALLEL_PROCESSOR_COUNT"] = max_thread if ENV["PARALLEL_PROCESSOR_COUNT"].nil?
92
+ ENV["PARALLEL_PROCESSOR_COUNT"] ||= max_thread
93
+
81
94
  Rackup::Handler::Puma.run(
82
95
  instance,
83
96
  Port: port,
@@ -86,7 +99,7 @@ module Mihari
86
99
  Verbose: verbose,
87
100
  worker_timeout: worker_timeout
88
101
  ) do |_|
89
- Launchy.open(url) if ENV["RACK_ENV"] != "development" && open
102
+ Launchy.open("http://#{host}:#{port}") if !Mihari.development? && open
90
103
  rescue Launchy::CommandNotFoundError
91
104
  # ref. https://github.com/ninoseki/mihari/issues/477
92
105
  # do nothing