Skip to content

Commit 9ae8d72

Browse files
authored
Merge pull request #140 from hahwul/hahwul-dev
Hahwul dev
2 parents 76615aa + 410ac2b commit 9ae8d72

File tree

13 files changed

+128
-17
lines changed

13 files changed

+128
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
| Java | Spring ||| X | X | X |
3636
| Kotlin | Spring ||| X | X | X |
3737
| JS | Express ||| X | X | X |
38+
| Rust | Axum ||| X | X | X |
3839
| C# | ASP.NET MVC || X | X | X | X |
3940
| JS | Next | X | X | X | X | X |
4041

spec/unit_test/models/deliver_spec.cr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ describe "Initialize" do
55
options = default_options
66
options[:base] = "noir"
77
options[:send_proxy] = "http://localhost:8090"
8+
options[:nolog] = "yes"
89

910
it "Deliver" do
1011
object = Deliver.new options
@@ -16,4 +17,16 @@ describe "Initialize" do
1617
object = Deliver.new options
1718
object.headers["X-API-Key"].should eq("abcdssss")
1819
end
20+
21+
it "Deliver with matchers" do
22+
options[:use_matchers] = "/admin"
23+
object = Deliver.new options
24+
object.matchers.should eq(["/admin"])
25+
end
26+
27+
it "Deliver with filters" do
28+
options[:use_filters] = "/admin"
29+
object = Deliver.new options
30+
object.filters.should eq(["/admin"])
31+
end
1932
end

spec/utils_spec.cr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ describe "remove_start_slash" do
99
end
1010
end
1111

12+
describe "get_relative_path" do
13+
it "start with ./" do
14+
get_relative_path("./abcd", "1.cr").should eq("1.cr")
15+
end
16+
17+
it "start with /" do
18+
get_relative_path("/abcd", "1.cr").should eq("1.cr")
19+
end
20+
21+
it "end with /" do
22+
get_relative_path("/abcd/", "1.cr").should eq("1.cr")
23+
end
24+
end
25+
1226
describe "get_symbol" do
1327
it "get" do
1428
get_symbol("GET").should eq(:get)

src/analyzer/analyzers/analyzer_jsp.cr

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ class AnalyzerJsp < Analyzer
77
begin
88
Dir.glob("#{base_path}/**/*") do |path|
99
next if File.directory?(path)
10-
if base_path[-1].to_s == "/"
11-
relative_path = path.sub("#{base_path}", "").sub("./", "").sub("//", "/")
12-
else
13-
relative_path = path.sub("#{base_path}/", "").sub("./", "").sub("//", "/")
14-
end
15-
relative_path = remove_start_slash(relative_path)
10+
11+
relative_path = get_relative_path(base_path, path)
1612

1713
if File.exists?(path) && File.extname(path) == ".jsp"
1814
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|

src/analyzer/analyzers/analyzer_php_pure.cr

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ class AnalyzerPhpPure < Analyzer
77
begin
88
Dir.glob("#{base_path}/**/*") do |path|
99
next if File.directory?(path)
10-
if base_path[-1].to_s == "/"
11-
relative_path = path.sub("#{base_path}", "").sub("./", "").sub("//", "/")
12-
else
13-
relative_path = path.sub("#{base_path}/", "").sub("./", "").sub("//", "/")
14-
end
15-
relative_path = remove_start_slash(relative_path)
10+
11+
relative_path = get_relative_path(base_path, path)
1612

1713
if File.exists?(path) && File.extname(path) == ".php"
1814
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|

src/analyzer/analyzers/analyzer_rust_axum.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class AnalyzerRustAxum < Analyzer
88
begin
99
Dir.glob("#{base_path}/**/*") do |path|
1010
next if File.directory?(path)
11+
1112
if File.exists?(path) && File.extname(path) == ".rs"
1213
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
1314
file.each_line do |line|
@@ -17,7 +18,7 @@ class AnalyzerRustAxum < Analyzer
1718
begin
1819
route_argument = match[1]
1920
callback_argument = match[2]
20-
result << Endpoint.new(route_argument, callback_to_method(callback_argument))
21+
result << Endpoint.new("#{@url}#{route_argument}", callback_to_method(callback_argument))
2122
rescue
2223
end
2324
end

src/deliver/send_elasticsearch.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ class SendElasticSearch < Deliver
99
uri.port = 9200
1010
end
1111

12+
applied_endpoints = apply_all(endpoints)
13+
1214
body = {
13-
"endpoints" => endpoints,
15+
"endpoints" => applied_endpoints,
1416
}.to_json
1517
es_headers = @headers
1618
es_headers["Content-Type"] = "application/json"

src/deliver/send_proxy.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ require "../models/deliver"
55
class SendWithProxy < Deliver
66
def run(endpoints : Array(Endpoint))
77
proxy_url = URI.parse(@proxy)
8-
endpoints.each do |endpoint|
8+
applied_endpoints = apply_all(endpoints)
9+
applied_endpoints.each do |endpoint|
910
begin
1011
if endpoint.params.size > 0
1112
endpoint_hash = endpoint.params_to_hash

src/deliver/send_req.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ require "../models/deliver"
44

55
class SendReq < Deliver
66
def run(endpoints : Array(Endpoint))
7-
endpoints.each do |endpoint|
7+
applied_endpoints = apply_all(endpoints)
8+
applied_endpoints.each do |endpoint|
89
begin
910
if endpoint.params.size > 0
1011
endpoint_hash = endpoint.params_to_hash

src/models/deliver.cr

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Deliver
88
@is_log : Bool
99
@proxy : String
1010
@headers : Hash(String, String) = {} of String => String
11+
@matchers : Array(String) = [] of String
12+
@filters : Array(String) = [] of String
1113

1214
def initialize(options : Hash(Symbol, String))
1315
@options = options
@@ -29,6 +31,65 @@ class Deliver
2931
end
3032
@logger.info_sub "#{@headers.size} headers added."
3133
end
34+
35+
@matchers = options[:use_matchers].split("::NOIR::MATCHER::SPLIT::")
36+
@matchers.delete("")
37+
if @matchers.size > 0
38+
@logger.system "#{@matchers.size} matchers added."
39+
end
40+
41+
@filters = options[:use_filters].split("::NOIR::FILTER::SPLIT::")
42+
@filters.delete("")
43+
if @filters.size > 0
44+
@logger.system "#{@filters.size} filters added."
45+
end
46+
end
47+
48+
def apply_all(endpoints : Array(Endpoint))
49+
result = endpoints
50+
@logger.debug "Matchers: #{@matchers}"
51+
@logger.debug "Filters: #{@filters}"
52+
53+
if @matchers.size > 0
54+
@logger.system "Applying matchers"
55+
result = apply_matchers(endpoints)
56+
end
57+
58+
if @filters.size > 0
59+
@logger.system "Applying filters"
60+
result = apply_filters(endpoints)
61+
end
62+
63+
result
64+
end
65+
66+
def apply_matchers(endpoints : Array(Endpoint))
67+
result = [] of Endpoint
68+
endpoints.each do |endpoint|
69+
@matchers.each do |matcher|
70+
if endpoint.url.includes? matcher
71+
@logger.debug "Endpoint '#{endpoint.url}' matched with '#{matcher}'."
72+
result << endpoint
73+
end
74+
end
75+
end
76+
77+
result
78+
end
79+
80+
def apply_filters(endpoints : Array(Endpoint))
81+
result = [] of Endpoint
82+
endpoints.each do |endpoint|
83+
@filters.each do |filter|
84+
if endpoint.url.includes? filter
85+
@logger.debug "Endpoint '#{endpoint.url}' filtered with '#{filter}'."
86+
else
87+
result << endpoint
88+
end
89+
end
90+
end
91+
92+
result
3293
end
3394

3495
def proxy
@@ -39,6 +100,14 @@ class Deliver
39100
@headers
40101
end
41102

103+
def matchers
104+
@matchers
105+
end
106+
107+
def filters
108+
@filters
109+
end
110+
42111
def run
43112
# After inheriting the class, write an action code here.
44113
end

src/noir.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ OptionParser.parse do |parser|
3838
parser.on "--with-headers X-Header:Value", "Add Custom Headers to be Used in Deliver" do |var|
3939
noir_options[:send_with_headers] += "#{var}::NOIR::HEADERS::SPLIT::"
4040
end
41+
parser.on "--use-matchers string", "Delivers URLs that match a specific condition" do |var|
42+
noir_options[:use_matchers] += "#{var}::NOIR::MATCHER::SPLIT::"
43+
end
44+
parser.on "--use-filters string", "Excludes URLs that match a specific condition." do |var|
45+
noir_options[:use_filters] += "#{var}::NOIR::FILTER::SPLIT::"
46+
end
4147

4248
parser.separator "\n Technologies:".colorize(:blue)
4349
parser.on "-t TECHS", "--techs rails,php", "Specify the technologies to use" { |var| noir_options[:techs] = var }

src/options.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ def default_options
22
noir_options = {
33
:base => "", :url => "", :format => "plain",
44
:output => "", :techs => "", :debug => "no", :color => "yes",
5-
:send_proxy => "", :send_req => "no", :send_with_headers => "", :send_es => "",
5+
:send_proxy => "", :send_req => "no", :send_with_headers => "", :send_es => "", :use_matchers => "", :use_filters => "",
66
:scope => "url,param", :set_pvalue => "", :nolog => "no",
77
:exclude_techs => "",
88
}

src/utils/utils.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ def remove_start_slash(input_path : String)
77
path
88
end
99

10+
def get_relative_path(base_path : String, path : String)
11+
if base_path[-1].to_s == "/"
12+
relative_path = path.sub("#{base_path}", "").sub("./", "").sub("//", "/")
13+
else
14+
relative_path = path.sub("#{base_path}/", "").sub("./", "").sub("//", "/")
15+
end
16+
relative_path = remove_start_slash(relative_path)
17+
18+
relative_path
19+
end
20+
1021
def str_to_bool(str)
1122
if str == "yes"
1223
return true

0 commit comments

Comments
 (0)