webhookdump 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/webhookdump +3 -0
  3. data/lib/webhook_dump.rb +120 -0
  4. metadata +101 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f49b8299be7c721086315b6b1599bccbdf4f45d713626e2108995d1eecdfdb47
4
+ data.tar.gz: a4d3442adeb9b99d5f8ad79061bff5eb97d2d6be607492e7614e2ad275940c58
5
+ SHA512:
6
+ metadata.gz: 25b7318994daa111f9d33d500e3c636f597873a39ff722f3e36770546d60e7dcea1629a1e537c8c58dee545f5970a0475c6c4120ff396e4f4367760744c4d9a5
7
+ data.tar.gz: 3a41012a76154f87b40e326b9d25cde3978f9da522b7ab1a091aa43dfc9509543a8c1a854b20768fb186f8fab09b39d64336ffd9c62f74866dd091a2a6e03190
data/bin/webhookdump ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './lib/webhook_dump'
@@ -0,0 +1,120 @@
1
+ require 'thor'
2
+ require 'eventmachine'
3
+ require 'faye/websocket'
4
+ require 'json'
5
+ require 'faraday'
6
+
7
+ module WebhookDump
8
+ class CLI < Thor
9
+ WEBSOCKET_URL = 'wss://webhookdump.link/cable'
10
+ WEBSOCKET_URL_DEV = 'ws://localhost:3000/cable'
11
+
12
+ option :dev, type: :boolean, default: false, desc: 'Use development environment'
13
+
14
+ desc "forward WEBHOOKDUMP_ID FORWARD_URL", "Forward events from WebhookDump to a target URL"
15
+
16
+ def forward(webhookdump_id, forward_url)
17
+ EM.run do
18
+ puts "Start forwarding events from WebhookDump #{webhookdump_id} to #{forward_url}"
19
+
20
+ ws = init_faye_websocket_connection(webhookdump_id)
21
+
22
+ open_websocket_connection(ws)
23
+
24
+ handle_incoming_message(ws, forward_url)
25
+
26
+ close_websocket_connection(ws)
27
+ rescue => e
28
+ puts "Error in EventMachine: #{e.message}"
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def open_websocket_connection(ws)
35
+ ws.on :open do |event|
36
+ ws.send({
37
+ command: 'subscribe',
38
+ identifier: { channel: 'RequestForwarderChannel' }.to_json
39
+ }.to_json)
40
+ rescue => e
41
+ puts "Error opening connection: #{e.message}"
42
+ ensure
43
+ puts "Connection established. Waiting for incoming request..."
44
+ end
45
+ end
46
+
47
+ def handle_incoming_message(ws, forward_url)
48
+ ws.on :message do |event|
49
+ data = JSON.parse(event.data, symbolize_names: true)
50
+ message = {}
51
+ message = data[:message] if data[:message].is_a? Hash
52
+
53
+ if message.has_key? :id
54
+ forward_message(forward_url, message)
55
+ end
56
+ rescue => e
57
+ puts "Error forwarding request to #{forward_url}: #{e.message}"
58
+ puts "Request: #{e.backtrace}"
59
+ end
60
+ end
61
+
62
+ def close_websocket_connection(ws)
63
+ ws.on :close do |event|
64
+ reason = event.reason != ' ' ? 'Probably invalid WebhookDump ID' : event.reason
65
+ puts "Connection closed, #{event.code}, #{reason}"
66
+
67
+ EM.stop
68
+ end
69
+ end
70
+
71
+ def init_faye_websocket_connection(webhookdump_id)
72
+ Faye::WebSocket::Client.new("#{WEBSOCKET_URL_DEV}?id=#{webhookdump_id}")
73
+ end
74
+
75
+ def websocket_url
76
+ options[:dev] ? WEBSOCKET_URL_DEV : WEBSOCKET_URL
77
+ end
78
+
79
+ def forward_message(forward_url, message)
80
+ headers = parse_headers(message[:headers])
81
+ body = parse_body(message[:payload])
82
+
83
+ conn = Faraday.new(url: forward_url)
84
+
85
+ response = conn.send(message[:method].downcase.to_sym) do |req|
86
+ # req.url path
87
+ req.headers = headers
88
+ # set user agent
89
+ req.headers['User-Agent'] = 'WebhookDump Forwarder'
90
+ req.body = body.to_json if body
91
+ end
92
+
93
+ puts "Request forwarded with status: #{response.status}"
94
+ rescue JSON::ParserError => e
95
+ puts "Error parsing JSON: #{e.message}"
96
+ end
97
+
98
+ def parse_headers(headers)
99
+ return {} if headers.nil? || headers.empty?
100
+
101
+ JSON.parse(headers)
102
+ rescue JSON::ParserError => e
103
+ puts "Error parsing headers: #{e.message}"
104
+
105
+ {}
106
+ end
107
+
108
+ def parse_body(body)
109
+ return {} if body.nil? || body.empty?
110
+
111
+ JSON.parse(body)
112
+ rescue JSON::ParserError => e
113
+ puts "Error parsing body: #{e.message}"
114
+
115
+ {}
116
+ end
117
+ end
118
+ end
119
+
120
+ WebhookDump::CLI.start(ARGV)
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webhookdump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Risal Hidayat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faye-websocket
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A webhook dump http forwarder CLI
70
+ email: risalhidayat88@gmail.com
71
+ executables:
72
+ - webhookdump
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/webhookdump
77
+ - lib/webhook_dump.rb
78
+ homepage: https://rubygems.org/gems/webhook_dump_cli
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.4.10
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Webhook Dump CLI
101
+ test_files: []