censor_bear 0.1.0 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/censor_bear/stop_words_controller.rb +1 -1
- data/app/helpers/censor_bear/application_helper.rb +4 -0
- data/app/views/censor_bear/logs/index.html.erb +2 -0
- data/app/views/censor_bear/logs/show.html.erb +10 -0
- data/app/views/censor_bear/stop_words/_form.html.haml +5 -5
- data/db/migrate/20211126133758_create_censor_bear_stop_words.rb +2 -0
- data/db/migrate/20211129093508_create_censor_bear_logs.rb +2 -0
- data/lib/censor_bear/censor.rb +29 -4
- data/lib/censor_bear/configuration.rb +5 -0
- data/lib/censor_bear/version.rb +1 -1
- data/lib/censor_bear.rb +21 -5
- data/lib/tasks/censor_bear_tasks.rake +58 -4
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '084b5a745f3e2eeef9a39a15bc2f87a9a0dadfe1b0cb41a30ac4bf131338c074'
|
4
|
+
data.tar.gz: c1351c23521fad8b976b36cb7d8d4eeaaf04688eb9c3463152ddd6edbd786019
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71533c0392c3e6af14ad00fed488dcbc41d1cfe8d6dff8ee8664735857ee158f78ae70ab644b073e9f5e60c48ba4a05ee1d35a61310bccf31368892d897f8714
|
7
|
+
data.tar.gz: f97b66983de762c530c79d2b541c756092c5fb1e2e6fd93178783cd26633121df88afe32cd28c4cc4851215c191901614ea255dc0375397cdceb42a321382157
|
@@ -46,7 +46,7 @@ module CensorBear
|
|
46
46
|
|
47
47
|
def load_stop_words
|
48
48
|
builder ||= stop_word_scope
|
49
|
-
builder = builder.where("
|
49
|
+
builder = builder.where("key ilike ?", "%#{params[:q]}%") if params[:q].present?
|
50
50
|
builder = builder.order(id: :desc)
|
51
51
|
|
52
52
|
@pagy, @stop_words = pagy(builder)
|
@@ -10,6 +10,16 @@
|
|
10
10
|
<%= @log.action %>
|
11
11
|
</p>
|
12
12
|
|
13
|
+
<p>
|
14
|
+
<strong>Scenario:</strong>
|
15
|
+
<%= @log.scenario %>
|
16
|
+
</p>
|
17
|
+
|
18
|
+
<p>
|
19
|
+
<strong>Stage:</strong>
|
20
|
+
<%= @log.stage %>
|
21
|
+
</p>
|
22
|
+
|
13
23
|
<p>
|
14
24
|
<strong>Filtered content:</strong>
|
15
25
|
<%= @log.filtered_content %>
|
@@ -10,19 +10,19 @@
|
|
10
10
|
%div= form.text_field :key, class: "border p-1 w-full rounded-md"
|
11
11
|
%div
|
12
12
|
%div{class: "text-gray-500 text-xs"}= form.label :ugc, "UGC内容处理方式"
|
13
|
-
%div= form.select :ugc, options_for_select(CensorBear.action_options,
|
13
|
+
%div= form.select :ugc, options_for_select(CensorBear.action_options, stop_word.ugc), class: "border p-1 w-full rounded-md"
|
14
14
|
%div
|
15
15
|
%div{class: "text-gray-500 text-xs"}= form.label :username, "用户名处理方式"
|
16
|
-
%div= form.select :username, options_for_select(CensorBear.action_options,
|
16
|
+
%div= form.select :username, options_for_select(CensorBear.action_options, stop_word.username), class: "border p-1 w-full rounded-md"
|
17
17
|
%div
|
18
18
|
%div{class: "text-gray-500 text-xs"}= form.label :nickname, "用户昵称处理方式"
|
19
|
-
%div= form.select :nickname, options_for_select(CensorBear.action_options,
|
19
|
+
%div= form.select :nickname, options_for_select(CensorBear.action_options, stop_word.nickname), class: "border p-1 w-full rounded-md"
|
20
20
|
%div
|
21
21
|
%div{class: "text-gray-500 text-xs"}= form.label :signature, "签名处理方式"
|
22
|
-
%div= form.select :signature, options_for_select(CensorBear.action_options,
|
22
|
+
%div= form.select :signature, options_for_select(CensorBear.action_options, stop_word.signature), class: "border p-1 w-full rounded-md"
|
23
23
|
%div
|
24
24
|
%div{class: "text-gray-500 text-xs"}= form.label :dialog, "私信处理方式"
|
25
|
-
%div= form.select :dialog, options_for_select(CensorBear.action_options,
|
25
|
+
%div= form.select :dialog, options_for_select(CensorBear.action_options, stop_word.dialog), class: "border p-1 w-full rounded-md"
|
26
26
|
%div
|
27
27
|
%div{class: "text-gray-500 text-xs"}= form.label :replacement, "过滤词替换"
|
28
28
|
%div= form.text_field :replacement, class: "border p-1 w-full rounded-md"
|
@@ -2,7 +2,9 @@ class CreateCensorBearLogs < ActiveRecord::Migration[6.1]
|
|
2
2
|
def change
|
3
3
|
create_table :censor_bear_logs do |t|
|
4
4
|
t.text :original_content
|
5
|
+
t.string :scenario
|
5
6
|
t.string :action
|
7
|
+
t.string :stage
|
6
8
|
t.text :filtered_content
|
7
9
|
t.string :mod_words, array: true, default: []
|
8
10
|
|
data/lib/censor_bear/censor.rb
CHANGED
@@ -10,6 +10,9 @@ module CensorBear
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class Censor
|
13
|
+
QQ_REG = /(?:[加茄qQ企鹅号码\s]{1,}|[群号]{1,}|[叩叩]{1,}|[抠抠]{1,}|[扣扣]{1,})(?:[\u4e00-\u9eff]*)(?:[:,:]?)([\d\s]{6,})/
|
14
|
+
WX_REG = /(?:[加+微++➕薇?vV威卫星♥❤姓xX信]{2,}|weixin|weix)(?:[,❤️.\s]?)(?:[\u4e00-\u9eff]?)(?:[:,:]?)([\w\s]{6,})/
|
15
|
+
|
13
16
|
def initialize(content, type = 'ugc')
|
14
17
|
@is_mod = false
|
15
18
|
@mod_words = []
|
@@ -21,9 +24,19 @@ module CensorBear
|
|
21
24
|
return Result.new(@content) if @content.blank?
|
22
25
|
return Result.new(@content) unless CensorBear::StopWord::FIELDS.include?(@type)
|
23
26
|
|
27
|
+
if @content.match(QQ_REG)
|
28
|
+
CensorBear.info(@content, @type, 'BANNED', 'qq_regex')
|
29
|
+
raise NotPassedException
|
30
|
+
end
|
31
|
+
|
32
|
+
if @content.match(WX_REG)
|
33
|
+
CensorBear.info(@content, @type, 'BANNED', 'wx_regex')
|
34
|
+
raise NotPassedException
|
35
|
+
end
|
36
|
+
|
24
37
|
local_check
|
25
38
|
|
26
|
-
#
|
39
|
+
# 用户相关类型直接禁止创建
|
27
40
|
raise NotPassedException if @is_mod && %w[username signature dialog nickname].include?(@type)
|
28
41
|
|
29
42
|
Result.new(@content, @is_mod, @mod_words.uniq)
|
@@ -40,14 +53,26 @@ module CensorBear
|
|
40
53
|
case action
|
41
54
|
when 'REPLACE'
|
42
55
|
@content = @content.gsub(finder, word.replacement)
|
43
|
-
CensorBear
|
56
|
+
CensorBear.info(
|
57
|
+
original_content, @type, action, 'local_check',
|
58
|
+
filtered_content: @content,
|
59
|
+
mod_words: [word.key]
|
60
|
+
)
|
44
61
|
when 'MOD'
|
45
62
|
@is_mod = true
|
46
|
-
CensorBear
|
63
|
+
CensorBear.info(
|
64
|
+
original_content, @type, action, 'local_check',
|
65
|
+
filtered_content: nil,
|
66
|
+
mod_words: [word.key]
|
67
|
+
)
|
47
68
|
when 'BANNED'
|
48
69
|
@is_mod = true
|
49
70
|
# 禁止的直接抛出异常
|
50
|
-
CensorBear
|
71
|
+
CensorBear.info(
|
72
|
+
original_content, @type, action, 'local_check',
|
73
|
+
filtered_content: nil,
|
74
|
+
mod_words: [word.key]
|
75
|
+
)
|
51
76
|
raise NotPassedException
|
52
77
|
end
|
53
78
|
end
|
data/lib/censor_bear/version.rb
CHANGED
data/lib/censor_bear.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'censor_bear/version'
|
2
|
+
require 'censor_bear/engine'
|
3
|
+
require 'censor_bear/censor'
|
4
|
+
require 'censor_bear/configuration'
|
5
5
|
|
6
|
-
require
|
6
|
+
require 'hamlit'
|
7
7
|
require 'pagy'
|
8
8
|
|
9
9
|
module CensorBear
|
@@ -17,6 +17,9 @@ module CensorBear
|
|
17
17
|
@config.user_class = 'User'
|
18
18
|
@config.current_user_method = 'current_user'
|
19
19
|
@config.user_name_method = 'name'
|
20
|
+
@config.import_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
|
21
|
+
@config.format_from_file_path = "#{Rails.root}/tmp/from_sensitives.txt"
|
22
|
+
@config.format_dest_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
|
20
23
|
@config
|
21
24
|
end
|
22
25
|
|
@@ -28,6 +31,19 @@ module CensorBear
|
|
28
31
|
Censor.new(content, type).check_text
|
29
32
|
end
|
30
33
|
|
34
|
+
def info(content, scenario, action, stage, options = {})
|
35
|
+
filtered_content = options['filtered_content'] || nil
|
36
|
+
mod_words = options['mod_words'] || []
|
37
|
+
|
38
|
+
CensorBear::Log.create(
|
39
|
+
original_content: content,
|
40
|
+
scenario: scenario,
|
41
|
+
action: action,
|
42
|
+
stage: stage,
|
43
|
+
filtered_content: filtered_content,
|
44
|
+
mod_words: mod_words
|
45
|
+
)
|
46
|
+
end
|
31
47
|
|
32
48
|
def action_options
|
33
49
|
I18n.t('censor_log.action').map { |k, v| [v, k.to_s] }
|
@@ -1,4 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
namespace :censor_bear do
|
2
|
+
desc 'Format words'
|
3
|
+
task format: :environment do
|
4
|
+
from_file = File.read(CensorBear.config.format_from_file_path)
|
5
|
+
File.open(CensorBear.config.format_dest_file_path, 'w') do |f|
|
6
|
+
from_file.each_line do |word|
|
7
|
+
line = "#{word.strip}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
|
8
|
+
f.write(line)
|
9
|
+
f.flush
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Import words'
|
15
|
+
task import: :environment do
|
16
|
+
is_overwrite = true
|
17
|
+
file = File.read(CensorBear.config.import_file_path)
|
18
|
+
# lines = doc.split(/\n/)
|
19
|
+
file.each_line do |line|
|
20
|
+
line.strip!
|
21
|
+
key, rule_str = line.split('=')
|
22
|
+
rules = rule_str.split('|')
|
23
|
+
ugc = parse(rules[0]).blank? ? 'replace' : parse(rules[0])
|
24
|
+
username = parse(rules[1]) || 'ignore'
|
25
|
+
signature = parse(rules[2]) || 'ignore'
|
26
|
+
dialog = parse(rules[3]) || 'ignore'
|
27
|
+
nickname = parse(rules[1]) || 'ignore'
|
28
|
+
replacement = ugc == 'replace' ? rules[0] : '**'
|
29
|
+
|
30
|
+
payload = {
|
31
|
+
key: key,
|
32
|
+
ugc: ugc,
|
33
|
+
username: username,
|
34
|
+
signature: signature,
|
35
|
+
dialog: dialog,
|
36
|
+
nickname: nickname,
|
37
|
+
replacement: replacement,
|
38
|
+
user_id: CensorBear.config.user_class.constantize.last&.id,
|
39
|
+
created_at: Time.now,
|
40
|
+
updated_at: Time.now
|
41
|
+
}
|
42
|
+
|
43
|
+
word = CensorBear::StopWord.find_by(key: key)
|
44
|
+
if word
|
45
|
+
word.update_columns(payload) if is_overwrite
|
46
|
+
else
|
47
|
+
CensorBear::StopWord.create(payload)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def parse(rule)
|
53
|
+
return nil if rule.blank?
|
54
|
+
|
55
|
+
r = rule.match(/^{(\w+)}$/)
|
56
|
+
r.blank? ? nil : r[1].downcase
|
57
|
+
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: censor_bear
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 42up
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6.
|
19
|
+
version: 6.0.4
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 6.
|
22
|
+
version: 6.0.4.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 6.
|
29
|
+
version: 6.0.4
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 6.
|
32
|
+
version: 6.0.4.1
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: hamlit
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|