senthor_rails 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e4f7db43a175a1dde7949f2fa93c2f114566545168d430fc1ab1187357799a6d
4
+ data.tar.gz: 858c4bb3b2d4b0341c341e5fe58b82c0a27ec726030c4aaa587b2170be5a2523
5
+ SHA512:
6
+ metadata.gz: 990e9b1c4dc5fb7780cca279bfbab6b81f57fd0ced9d4e709b47c244f0b2ceb9766dacf285fbbb587049e4e13a4cef2c9f84dca219acafec925bb417c7e473ed
7
+ data.tar.gz: e79a5da49b9ac08bce8a03e2f35203f9b48426a070f3d45c09ace588a663540ffc3bc2cd731199cdabd7babda30c7df015add6e8f61c2798fc2036a4e9ac2874
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 melmouk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # SenthorRails
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/senthor_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/senthor_rails.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,88 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Senthor
6
+ class Middleware
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ request = Rack::Request.new(env)
13
+
14
+ # Don't handle POST/PATCH ... requests
15
+ unless request.get?
16
+ return @app.call(env)
17
+ end
18
+
19
+ # Extract client IP
20
+ client_ip =
21
+ request.get_header('HTTP_X_FORWARDED_FOR')&.split(',')&.first&.strip ||
22
+ request.get_header('HTTP_X_REAL_IP') ||
23
+ request.get_header('HTTP_CF_CONNECTING_IP') ||
24
+ request.get_header('REMOTE_ADDR') ||
25
+ 'unknown'
26
+
27
+ # Collect all headers
28
+ headers = {}
29
+ excluded_headers = %w[
30
+ AUTHORIZATION
31
+ COOKIE
32
+ SET_COOKIE
33
+ PROXY_AUTHORIZATION
34
+ X_CSRF_TOKEN
35
+ X_XSRF_TOKEN
36
+ X_API_KEY
37
+ X_ACCESS_TOKEN
38
+ ]
39
+ env.each do |key, value|
40
+ if key.start_with?('HTTP_')
41
+ header_name = key.sub(/^HTTP_/, '').upcase
42
+ next if excluded_headers.include?(header_name)
43
+
44
+ # Format header name back to normal-case (e.g., "User-Agent")
45
+ pretty_name = header_name.split('_').map(&:capitalize).join('-')
46
+ headers[pretty_name] = value
47
+ end
48
+ end
49
+
50
+ # Build payload
51
+ payload = {
52
+ headers: headers,
53
+ request_url: request.url,
54
+ client_ip: client_ip
55
+ }
56
+
57
+ # Call Senthor API fo check GET request authorization
58
+ uri = URI('https://waf-api.senthor.io/api/check-request')
59
+ http = Net::HTTP.new(uri.host, uri.port)
60
+ http.use_ssl = true
61
+
62
+ # Disable SSL verification only in development/local
63
+ if Rails.env.development? || Rails.env.test?
64
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
65
+ http.verify_depth = 0
66
+ http.cert_store = OpenSSL::X509::Store.new
67
+ end
68
+
69
+ request = Net::HTTP::Post.new(uri)
70
+ request['Content-Type'] = 'application/json'
71
+ request.body = payload.to_json
72
+
73
+ response = http.request(request)
74
+
75
+ # Handle 402 response like in JS
76
+ if response.code.to_i == 402
77
+ return [
78
+ 402,
79
+ { 'Content-Type' => 'text/plain' },
80
+ ['This content is protected by a paywall. Visit https://senthor.io to find out how to access the content.']
81
+ ]
82
+ end
83
+
84
+ # Otherwise continue as normal
85
+ @app.call(env)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SenthorRails
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "senthor_rails/version"
4
+ require_relative "senthor_rails/middleware"
5
+
6
+ module Senthor
7
+ end
@@ -0,0 +1,4 @@
1
+ module SenthorRails
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: senthor_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Senthor
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Protect your content from AI crawlers and monetize every request with
13
+ Senthor. Real-time detection, crawler control, and detailed analytics.
14
+ email:
15
+ - contact@senthor.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - Rakefile
23
+ - lib/senthor_rails.rb
24
+ - lib/senthor_rails/middleware.rb
25
+ - lib/senthor_rails/version.rb
26
+ - sig/senthor_rails.rbs
27
+ homepage: https://www.senthor.io
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://www.senthor.io
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.2.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.6.9
47
+ specification_version: 4
48
+ summary: Senthor middleware for Ruby on Rails applications
49
+ test_files: []