antispam 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.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +72 -0
  4. data/Rakefile +18 -0
  5. data/app/assets/config/antispam_manifest.js +1 -0
  6. data/app/assets/images/antispam/captcha/a.gif +0 -0
  7. data/app/assets/images/antispam/captcha/b.gif +0 -0
  8. data/app/assets/images/antispam/captcha/blank.gif +0 -0
  9. data/app/assets/images/antispam/captcha/c.gif +0 -0
  10. data/app/assets/images/antispam/captcha/d.gif +0 -0
  11. data/app/assets/images/antispam/captcha/e.gif +0 -0
  12. data/app/assets/images/antispam/captcha/f.gif +0 -0
  13. data/app/assets/images/antispam/captcha/g.gif +0 -0
  14. data/app/assets/images/antispam/captcha/h.gif +0 -0
  15. data/app/assets/images/antispam/captcha/i.gif +0 -0
  16. data/app/assets/images/antispam/captcha/j.gif +0 -0
  17. data/app/assets/images/antispam/captcha/k.gif +0 -0
  18. data/app/assets/images/antispam/captcha/l.gif +0 -0
  19. data/app/assets/stylesheets/antispam/application.css +15 -0
  20. data/app/assets/stylesheets/antispam/blocks.css +4 -0
  21. data/app/assets/stylesheets/antispam/challenges.css +4 -0
  22. data/app/assets/stylesheets/antispam/clears.css +4 -0
  23. data/app/assets/stylesheets/scaffold.css +80 -0
  24. data/app/controllers/antispam/application_controller.rb +4 -0
  25. data/app/controllers/antispam/blocks_controller.rb +70 -0
  26. data/app/controllers/antispam/challenges_controller.rb +49 -0
  27. data/app/controllers/antispam/clears_controller.rb +63 -0
  28. data/app/controllers/antispam/validate_controller.rb +12 -0
  29. data/app/helpers/antispam/application_helper.rb +4 -0
  30. data/app/helpers/antispam/blocks_helper.rb +4 -0
  31. data/app/helpers/antispam/challenges_helper.rb +4 -0
  32. data/app/helpers/antispam/clears_helper.rb +4 -0
  33. data/app/jobs/antispam/application_job.rb +4 -0
  34. data/app/mailers/antispam/application_mailer.rb +6 -0
  35. data/app/models/antispam/application_record.rb +5 -0
  36. data/app/models/antispam/block.rb +4 -0
  37. data/app/models/antispam/challenge.rb +26 -0
  38. data/app/models/antispam/clear.rb +4 -0
  39. data/app/models/antispam/ip.rb +7 -0
  40. data/app/views/antispam/blocks/_form.html.erb +37 -0
  41. data/app/views/antispam/blocks/edit.html.erb +6 -0
  42. data/app/views/antispam/blocks/index.html.erb +37 -0
  43. data/app/views/antispam/blocks/new.html.erb +5 -0
  44. data/app/views/antispam/blocks/show.html.erb +24 -0
  45. data/app/views/antispam/challenges/_form.html.erb +32 -0
  46. data/app/views/antispam/challenges/edit.html.erb +6 -0
  47. data/app/views/antispam/challenges/index.html.erb +31 -0
  48. data/app/views/antispam/challenges/new.html.erb +5 -0
  49. data/app/views/antispam/challenges/show.html.erb +19 -0
  50. data/app/views/antispam/clears/_form.html.erb +42 -0
  51. data/app/views/antispam/clears/edit.html.erb +6 -0
  52. data/app/views/antispam/clears/index.html.erb +32 -0
  53. data/app/views/antispam/clears/new.html.erb +5 -0
  54. data/app/views/antispam/clears/show.html.erb +29 -0
  55. data/app/views/antispam/validate/index.html.erb +14 -0
  56. data/app/views/layouts/antispam/application.html.erb +15 -0
  57. data/config/routes.rb +7 -0
  58. data/db/migrate/20210130213708_create_antispam_ips.rb +12 -0
  59. data/db/migrate/20210130214835_create_antispam_challenges.rb +11 -0
  60. data/db/migrate/20210130234107_create_antispam_blocks.rb +12 -0
  61. data/db/migrate/20210130235537_create_antispam_clears.rb +13 -0
  62. data/lib/antispam.rb +11 -0
  63. data/lib/antispam/blacklists/httpbl.rb +43 -0
  64. data/lib/antispam/engine.rb +5 -0
  65. data/lib/antispam/tools.rb +49 -0
  66. data/lib/antispam/version.rb +3 -0
  67. data/lib/tasks/antispam_tasks.rake +4 -0
  68. metadata +141 -0
@@ -0,0 +1,14 @@
1
+ <% @challenge = Antispam::Challenge.create %>
2
+ <h1>Human Challenge</h1>
3
+
4
+ <p>Please prove that you are human.</p>
5
+
6
+ <img src="/antispam/challenges/<%= @challenge.id %>.jpg" width="200" height="40">
7
+
8
+ <%= form_for @challenge do |f| %>
9
+ <%= f.hidden_field :id, value: @challenge.id %>
10
+ <%= f.text_field :answer, value: '' %>
11
+ <%= submit_tag "Submit" %>
12
+ <% end %>
13
+
14
+ <% flash.each do |type, msg| %><div><%= msg %></div><% end %>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Antispam</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+ <style>.row { width:100%;display: flex;} .cx { width: 50%; }</style>
8
+ <%#= stylesheet_link_tag "antispam/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,7 @@
1
+ Antispam::Engine.routes.draw do
2
+ resources :clears
3
+ resources :blocks
4
+ resources :challenges
5
+ root to: 'ips#index'
6
+ get 'validate', to: 'validate#index'
7
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAntispamIps < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :antispam_ips do |t|
4
+ t.string :address
5
+ t.string :provider
6
+ t.integer :threat
7
+ t.datetime :expires_at
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateAntispamChallenges < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :antispam_challenges do |t|
4
+ t.string :question
5
+ t.string :answer
6
+ t.string :code
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class CreateAntispamBlocks < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :antispam_blocks do |t|
4
+ t.string :ip
5
+ t.string :provider
6
+ t.string :controllername
7
+ t.string :actionname
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ class CreateAntispamClears < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :antispam_clears do |t|
4
+ t.string :ip
5
+ t.string :result
6
+ t.string :answer
7
+ t.integer :threat_before
8
+ t.integer :threat_after
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require "antispam/version"
2
+ require "antispam/engine"
3
+ require "antispam/tools"
4
+ require "antispam/blacklists/httpbl"
5
+
6
+ module Antispam
7
+ ActiveSupport.on_load(:action_controller) do
8
+ # self refers to ActionController::Base here
9
+ self.include Antispam::Tools
10
+ end
11
+ end
@@ -0,0 +1,43 @@
1
+ require 'resolv'
2
+ module Antispam
3
+ module Blacklists
4
+ class Httpbl
5
+ def self.check(ip, key)
6
+ threat = 0
7
+ begin
8
+ return get_old_result(ip) if get_old_result(ip)
9
+ check = ip.split('.').reverse.join('.')
10
+ host = key + '.' + check + ".dnsbl.httpbl.org"
11
+ address = Resolv::getaddress(host)
12
+ z,days,threat,iptype = address.split('.')
13
+ Rails.logger.info "Spam located: #{iptype} type at #{threat} threat. (#{ip} - #{address})"
14
+ # Create or update
15
+ if (threat.to_i > 30)
16
+ Rails.logger.info "Spamcheck: Very high, over 30!"
17
+ end
18
+ rescue Exception => e
19
+ case e
20
+ when Resolv::ResolvError #Not spam! This blacklist gives an error when there's no spam threat.
21
+ Rails.logger.info "Spamcheck: OK! Resolve error means the httpbl does not consider this spam."
22
+ when Interrupt #Something broke while trying to check blacklist.
23
+ Rails.logger.info "Spamcheck: Interrupt when trying to resolve http blacklist. Possible timeout?"
24
+ else # Time Out
25
+ Rails.logger.info "Spamcheck: There was an error, possibly a time out, when checking this IP."
26
+ Rails.logger.info e.to_s
27
+ end
28
+ end
29
+ update_old_result(ip, threat)
30
+ return threat
31
+ end
32
+ def self.get_old_result(ip)
33
+ result = Antispam::Ip.find_by(address: ip, provider: 'httpbl')
34
+ return nil if (result.nil? || result.expired?)
35
+ return result.threat
36
+ end
37
+ def self.update_old_result(ip, threat)
38
+ result = Antispam::Ip.find_or_create_by(address: ip, provider: 'httpbl')
39
+ result.update(threat: threat, expires_at: 24.hours.from_now)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module Antispam
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Antispam
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module Antispam
2
+ module Tools
3
+ # before_action :check_ip_against_database
4
+ def check_ip_against_database(options = {ip_blacklists: {default: ''}})
5
+ return if request.get?
6
+ return if skip_if_user_whitelisted
7
+ return if controller_name == "validate"
8
+ ip = request.remote_ip
9
+ # First, check IP blacklists.
10
+ if (options[:ip_blacklists])
11
+ if options[:ip_blacklists][:default]
12
+ options[:ip_blacklists][:httpbl] = options[:ip_blacklists][:default]
13
+ options[:ip_blacklists].delete(:default)
14
+ end
15
+ check_ip_against_blacklists(ip, options[:ip_blacklists], options[:verbose])
16
+ end
17
+ # Second, check for weird countries.
18
+ if (options[:scrutinize_countries_except])
19
+
20
+ end
21
+ Rails.logger.info "Got to this function. #{ip}"
22
+ puts "Got to this function. #{ip}"
23
+ end
24
+ def check_ip_against_blacklists(ip, lists, verbose)
25
+ lists.each do |provider_name, provider_api_key|
26
+ puts "Checking provider: #{provider_name}" if verbose
27
+ if provider_name == :httpbl
28
+ result = Antispam::Blacklists::Httpbl.check(ip, provider_api_key)
29
+ puts result if verbose
30
+ result = 31
31
+ if (result > 30)
32
+ Block.create(ip: ip, provider: provider_name)
33
+ redirect_to '/antispam/validate'
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def skip_if_user_whitelisted
40
+ if respond_to? :current_user
41
+ if current_user && current_user.respond_to?(:antispam_whitelisted?)
42
+ return true if current_user.antispam_whitelisted?
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Antispam
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :antispam do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: antispam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Kopf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_processing
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Antispam checks DNS blacklists and helps prevent spam on your site.
42
+ email:
43
+ - antispam@ryankopf.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/config/antispam_manifest.js
52
+ - app/assets/images/antispam/captcha/a.gif
53
+ - app/assets/images/antispam/captcha/b.gif
54
+ - app/assets/images/antispam/captcha/blank.gif
55
+ - app/assets/images/antispam/captcha/c.gif
56
+ - app/assets/images/antispam/captcha/d.gif
57
+ - app/assets/images/antispam/captcha/e.gif
58
+ - app/assets/images/antispam/captcha/f.gif
59
+ - app/assets/images/antispam/captcha/g.gif
60
+ - app/assets/images/antispam/captcha/h.gif
61
+ - app/assets/images/antispam/captcha/i.gif
62
+ - app/assets/images/antispam/captcha/j.gif
63
+ - app/assets/images/antispam/captcha/k.gif
64
+ - app/assets/images/antispam/captcha/l.gif
65
+ - app/assets/stylesheets/antispam/application.css
66
+ - app/assets/stylesheets/antispam/blocks.css
67
+ - app/assets/stylesheets/antispam/challenges.css
68
+ - app/assets/stylesheets/antispam/clears.css
69
+ - app/assets/stylesheets/scaffold.css
70
+ - app/controllers/antispam/application_controller.rb
71
+ - app/controllers/antispam/blocks_controller.rb
72
+ - app/controllers/antispam/challenges_controller.rb
73
+ - app/controllers/antispam/clears_controller.rb
74
+ - app/controllers/antispam/validate_controller.rb
75
+ - app/helpers/antispam/application_helper.rb
76
+ - app/helpers/antispam/blocks_helper.rb
77
+ - app/helpers/antispam/challenges_helper.rb
78
+ - app/helpers/antispam/clears_helper.rb
79
+ - app/jobs/antispam/application_job.rb
80
+ - app/mailers/antispam/application_mailer.rb
81
+ - app/models/antispam/application_record.rb
82
+ - app/models/antispam/block.rb
83
+ - app/models/antispam/challenge.rb
84
+ - app/models/antispam/clear.rb
85
+ - app/models/antispam/ip.rb
86
+ - app/views/antispam/blocks/_form.html.erb
87
+ - app/views/antispam/blocks/edit.html.erb
88
+ - app/views/antispam/blocks/index.html.erb
89
+ - app/views/antispam/blocks/new.html.erb
90
+ - app/views/antispam/blocks/show.html.erb
91
+ - app/views/antispam/challenges/_form.html.erb
92
+ - app/views/antispam/challenges/edit.html.erb
93
+ - app/views/antispam/challenges/index.html.erb
94
+ - app/views/antispam/challenges/new.html.erb
95
+ - app/views/antispam/challenges/show.html.erb
96
+ - app/views/antispam/clears/_form.html.erb
97
+ - app/views/antispam/clears/edit.html.erb
98
+ - app/views/antispam/clears/index.html.erb
99
+ - app/views/antispam/clears/new.html.erb
100
+ - app/views/antispam/clears/show.html.erb
101
+ - app/views/antispam/validate/index.html.erb
102
+ - app/views/layouts/antispam/application.html.erb
103
+ - config/routes.rb
104
+ - db/migrate/20210130213708_create_antispam_ips.rb
105
+ - db/migrate/20210130214835_create_antispam_challenges.rb
106
+ - db/migrate/20210130234107_create_antispam_blocks.rb
107
+ - db/migrate/20210130235537_create_antispam_clears.rb
108
+ - lib/antispam.rb
109
+ - lib/antispam/blacklists/httpbl.rb
110
+ - lib/antispam/engine.rb
111
+ - lib/antispam/tools.rb
112
+ - lib/antispam/version.rb
113
+ - lib/tasks/antispam_tasks.rake
114
+ homepage: https://ryankopf.com
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ homepage_uri: https://ryankopf.com
120
+ source_code_uri: https://github.com/ryankopf/antispam
121
+ changelog_uri: https://github.com/ryankopf/antispam/CHANGELOG.md
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.1.4
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A spam prevention gem.
141
+ test_files: []