From 2f0a6a76b1b3a83151d94c335c14f4e0e8c716fd Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 13 Oct 2022 01:18:28 +0800 Subject: [PATCH] Fix lib import --- Rakefile | 23 ----------------------- ext/rucaptcha/Cargo.lock | 1 + ext/rucaptcha/Cargo.toml | 1 + ext/rucaptcha/src/lib.rs | 30 ++++++++++++++++++++++++------ lib/rucaptcha.rb | 10 +++++++++- lib/rucaptcha/view_helpers.rb | 4 ++-- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/Rakefile b/Rakefile index f67e74c..85ff4d1 100644 --- a/Rakefile +++ b/Rakefile @@ -16,26 +16,3 @@ task :preview do warn res[0] puts res[1].pack("c*") end - -task :memory do - require "rucaptcha" - puts "Starting to profile memory..." - b = {} - puts "Before => #{GC.stat(b)[:heap_live_slots]}" - count = 10_000_000 - step = (count / 100).to_i - count.times do |i| - res = RuCaptcha.generate - print_memory if i % step == 0 - end - - print_memory - puts GC.start - puts "After GC" - print_memory -end - -def print_memory - rss = `ps -eo pid,rss | grep #{Process.pid} | awk '{print $2}'`.to_i - puts "rss: #{rss} live objects #{GC.stat[:heap_live_slots]}, total allocated: #{GC.stat[:total_allocated_objects]}" -end diff --git a/ext/rucaptcha/Cargo.lock b/ext/rucaptcha/Cargo.lock index 19b18f0..9c3f61a 100644 --- a/ext/rucaptcha/Cargo.lock +++ b/ext/rucaptcha/Cargo.lock @@ -566,6 +566,7 @@ version = "3.0.0" dependencies = [ "captcha", "magnus", + "rand", ] [[package]] diff --git a/ext/rucaptcha/Cargo.toml b/ext/rucaptcha/Cargo.toml index 7a57437..daa4d33 100644 --- a/ext/rucaptcha/Cargo.toml +++ b/ext/rucaptcha/Cargo.toml @@ -9,3 +9,4 @@ crate-type = ["cdylib"] [dependencies] captcha = "*" magnus = "0.3" +rand = "0.8.5" diff --git a/ext/rucaptcha/src/lib.rs b/ext/rucaptcha/src/lib.rs index b0fb079..de3d1bc 100644 --- a/ext/rucaptcha/src/lib.rs +++ b/ext/rucaptcha/src/lib.rs @@ -1,15 +1,34 @@ -use captcha::filters::{Cow, Dots, Noise, Wave}; +use captcha::filters::{Cow, Dots, Grid, Noise, Wave}; use captcha::{Captcha, Geometry}; use magnus::{define_class, function, Error, Object}; +// https://coolors.co/cc0b8f-7c0abe-5700c8-3c2ea4-3d56a8-3fa67e-45bb30-69d003-a0d003-d8db02 +const colors: [(u8, u8, u8); 10] = [ + (204, 11, 143), + (124, 10, 190), + (87, 0, 200), + (60, 46, 164), + (61, 86, 168), + (63, 166, 126), + (69, 187, 48), + (105, 208, 3), + (160, 208, 3), + (216, 219, 2), +]; + pub fn create(style: u32, len: u32, line: bool, noise: bool) -> (String, Vec) { let mut c = Captcha::new(); c.add_chars(len).view(220, 120); + if style == 1 { + let color = colors[rand::random::() % colors.len()]; + c.set_color([color.0, color.1, color.2]); + } + if line { - c.apply_filter( + c.apply_filter(Wave::new(2.0, 10.0)).apply_filter( Cow::new() - .min_radius(40) + .min_radius(30) .max_radius(50) .circles(1) .area(Geometry::new(40, 150, 50, 70)), @@ -17,9 +36,8 @@ pub fn create(style: u32, len: u32, line: bool, noise: bool) -> (String, Vec } if noise { - c.apply_filter(Noise::new(0.2)) - .apply_filter(Wave::new(2.0, 12.0)) - .apply_filter(Dots::new(2)); + c.apply_filter(Noise::new(0.4)) + .apply_filter(Grid::new(10, 10)); } let chars = c.chars_as_string(); diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index 904692d..8dfbcd1 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -1,7 +1,15 @@ require "rails" require "action_controller" require "active_support/all" -require "rucaptcha/rucaptcha" + +begin + # load the precompiled extension file + ruby_version = /(\d+\.\d+)/.match(::RUBY_VERSION) + require_relative "rucaptcha/#{ruby_version}/rucaptcha" +rescue LoadError + require "rucaptcha/rucaptcha" +end + require "rucaptcha/version" require "rucaptcha/configuration" require "rucaptcha/controller_helpers" diff --git a/lib/rucaptcha/view_helpers.rb b/lib/rucaptcha/view_helpers.rb index e625a60..083b0f2 100644 --- a/lib/rucaptcha/view_helpers.rb +++ b/lib/rucaptcha/view_helpers.rb @@ -5,14 +5,14 @@ def rucaptcha_input_tag(opts = {}) opts[:type] = "text" opts[:autocorrect] = "off" opts[:autocapitalize] = "off" - opts[:pattern] = "[a-zA-Z]*" + opts[:pattern] = "[a-zA-Z0-9]*" opts[:autocomplete] = "off" opts[:maxlength] = RuCaptcha.config.length tag(:input, opts) end def rucaptcha_image_tag(opts = {}) - @rucaptcha_image_tag__image_path_in_this_request ||= "#{ru_captcha.root_path}?t=#{Time.now.strftime("%s%L")}" + @rucaptcha_image_tag__image_path_in_this_request ||= "#{ru_captcha.root_path}?t=#{Time.now.strftime('%s%L')}" opts[:class] = opts[:class] || "rucaptcha-image" opts[:src] = @rucaptcha_image_tag__image_path_in_this_request opts[:onclick] = "this.src = '#{ru_captcha.root_path}?t=' + Date.now();"