From 17385de87073bf6518d6d3376090a14492ebc993 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 16:52:40 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index 359a844fc..2f29bee8f 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -29,7 +29,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 2000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) @@ -65,7 +65,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 2000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) From 03a53f56fd5904f6e64f140f5f65ea01b4db9ae8 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 16:56:12 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index 2f29bee8f..cfde9bd05 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -38,10 +38,12 @@ namespace :commit_log_to_db do data += "(#{user_id},#{project.id},#{project.repository&.id},'#{project.identifier}','#{project.owner.name}/#{project.identifier}','#{commit_sha}','#{ref}',\"#{commit_message}\",'#{commit_date_str}','#{commit_date_str}')," end data = data[0,data.length-1] - sql_connection = ActiveRecord::Base.connection - sql_connection.begin_db_transaction - sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" - sql_connection.execute(sql) + if data.present? + sql_connection = ActiveRecord::Base.connection + sql_connection.begin_db_transaction + sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" + sql_connection.execute(sql) + end end end @@ -74,10 +76,12 @@ namespace :commit_log_to_db do data += "(#{user_id},#{project.id},#{project.repository&.id},'#{project.identifier}','#{project.owner.name}/#{project.identifier}','#{commit_sha}','#{ref}',\"#{commit_message}\",'#{commit_date_str}','#{commit_date_str}')," end data = data[0,data.length-1] - sql_connection = ActiveRecord::Base.connection - sql_connection.begin_db_transaction - sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" - sql_connection.execute(sql) + if data.present? + sql_connection = ActiveRecord::Base.connection + sql_connection.begin_db_transaction + sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" + sql_connection.execute(sql) + end end end \ No newline at end of file From 3d24ed3780dffd0f4aac7a77db2faa62bc68b766 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 17:02:37 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85,?= =?UTF-8?q?message=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index cfde9bd05..cc6cdcd23 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -29,7 +29,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.size > 2000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 1000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) @@ -67,7 +67,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.size > 2000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 1000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) From 6608c799f07db922cadfbe1f92a0d17b02291ef1 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 21:36:26 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85,?= =?UTF-8?q?message=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index cc6cdcd23..6365c485f 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -29,7 +29,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.size > 1000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 200 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) @@ -67,7 +67,7 @@ namespace :commit_log_to_db do commit_sha = commit['sha'] next if CommitLog.find_by(commit_id: commit_sha).present? ref = "master" - commit_message = commit['commit']['message'].to_s.size > 1000 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") + commit_message = commit['commit']['message'].to_s.size > 200 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","") user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) From 11d22d5d5a0556b639fc22af0dd990491cc25c48 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 21:41:55 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85c?= =?UTF-8?q?ommit=5Fdb=5Ftransaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index 6365c485f..b5505beda 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -43,6 +43,7 @@ namespace :commit_log_to_db do sql_connection.begin_db_transaction sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" sql_connection.execute(sql) + sql_connection.commit_db_transaction end end end @@ -81,6 +82,7 @@ namespace :commit_log_to_db do sql_connection.begin_db_transaction sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}" sql_connection.execute(sql) + sql_connection.commit_db_transaction end end From 6cc03b1ab4cb9e3550d9a193c845d9378043db2a Mon Sep 17 00:00:00 2001 From: xxq250 Date: Tue, 18 Apr 2023 22:00:01 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E8=AE=A1=E5=9B=BEcommit=E8=A1=A5=E5=85=85?= =?UTF-8?q?=E6=97=A5=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/commit_log_to_db.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/commit_log_to_db.rake b/lib/tasks/commit_log_to_db.rake index b5505beda..fdb7ac1a3 100644 --- a/lib/tasks/commit_log_to_db.rake +++ b/lib/tasks/commit_log_to_db.rake @@ -33,7 +33,7 @@ namespace :commit_log_to_db do user = User.find_by(mail: commiter['email']) user_id = user&.id || project.user_id commit_date = Time.parse(commit['commit']['author']['date']) - commit_date_str = commit_date.strftime("%a %b %d %H:%M:%S") + commit_date_str = commit_date.strftime("%Y-%m-%d %H:%M:%S") data += "(#{user_id},#{project.id},#{project.repository&.id},'#{project.identifier}','#{project.owner.name}/#{project.identifier}','#{commit_sha}','#{ref}',\"#{commit_message}\",'#{commit_date_str}','#{commit_date_str}')," end From 82ba843d7dd939954ce35074ee3d2617826b5300 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 19 Apr 2023 10:15:07 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=82=AE=E4=BB=B6=E5=8F=8A=E7=9F=AD=E4=BF=A1=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 2 + app/controllers/api/v1/users_controller.rb | 15 +++-- app/controllers/application_controller.rb | 14 ++-- app/services/info_risk_control_service.rb | 77 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 app/services/info_risk_control_service.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 2046dfa20..0b713bdb3 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -324,6 +324,8 @@ class AccountsController < ApplicationController send_type = verify_type(login_type, type) verification_code = code.sample(6).join + status, message = InfoRiskControlService.call(value, request.remote_ip) + tip_exception(420, message) if status == 0 sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}") tip_exception(501, "请求不合理") if sign != params[:smscode] diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 3a750b519..f5ea37a90 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -9,21 +9,24 @@ class Api::V1::UsersController < Api::V1::BaseController mail = params[:email] code_type = params[:code_type] + status, message = InfoRiskControlService.call(0, request.remote_ip) + tip_exception(420, message) if status == 0 + sign = Digest::MD5.hexdigest("#{OPENKEY}#{mail}") Rails.logger.info sign tip_exception(501, "请求不合理") if sign != params[:smscode] # 60s内不能重复发送 - send_email_limit_cache_key = "send_email_60_second_limit:#{mail}" - tip_exception(-2, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) - send_email_control = LimitForbidControl::SendEmailCode.new(mail) - tip_exception(-2, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? + # send_email_limit_cache_key = "send_email_60_second_limit:#{mail}" + # tip_exception(-2, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) + # send_email_control = LimitForbidControl::SendEmailCode.new(mail) + # tip_exception(-2, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? begin UserMailer.update_email(mail, verification_code).deliver_now - Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) - send_email_control.increment! + # Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) + # send_email_control.increment! rescue Exception => e logger_error(e) tip_exception(-2,"邮件发送失败,请稍后重试") diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 517e1b2df..61541d09b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -112,12 +112,12 @@ class ApplicationController < ActionController::Base # 邮箱类型的发送 sigle_para = {email: value} # 60s内不能重复发送 - send_email_limit_cache_key = "send_email_60_second_limit:#{value}" - tip_exception(-1, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) + # send_email_limit_cache_key = "send_email_60_second_limit:#{value}" + # tip_exception(-1, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key) - # 短时间内不能大量发送 - send_email_control = LimitForbidControl::SendEmailCode.new(value) - tip_exception(-1, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? + # # 短时间内不能大量发送 + # send_email_control = LimitForbidControl::SendEmailCode.new(value) + # tip_exception(-1, '邮件发送太频繁,请稍后再试') if send_email_control.forbid? begin if send_type == 3 UserMailer.find_password(value, code).deliver_now @@ -126,8 +126,8 @@ class ApplicationController < ActionController::Base else UserMailer.register_email(value, code).deliver_now end - Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) - send_email_control.increment! + # Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute) + # send_email_control.increment! # Mailer.run.email_register(code, value) rescue Exception => e logger_error(e) diff --git a/app/services/info_risk_control_service.rb b/app/services/info_risk_control_service.rb new file mode 100644 index 000000000..f07b30c48 --- /dev/null +++ b/app/services/info_risk_control_service.rb @@ -0,0 +1,77 @@ +class InfoRiskControlService < ApplicationService + + attr_reader :receiver, :remote_ip + attr_accessor :status, :message + + + def initialize(receiver="", remote_ip="0.0.0.0") + @receiver = receiver + @remote_ip = remote_ip + @status = 1 + @message = "" + end + + def call + if receiver == "" + remote_ip_minute_risk_control + remote_ip_risk_control if @status = 1 + else + remote_ip_minute_risk_control + remote_ip_risk_control if @status = 1 + minute_risk_control + day_risk_control if @status = 1 + end + + return @status, @message + end + + private + def remote_ip_minute_risk_control + result = Rails.cache.read("InfoRiskControlService-RemoteIp-Minute-#{remote_ip}") + if result.present? + @status = 0 + @message = "您的请求过于频繁,请稍后再试" + else + Rails.cache.write("InfoRiskControlService-RemoteIp-Minute-#{remote_ip}", 1, expires_in: 1.minute) + end + end + + def remote_ip_risk_control + result = Rails.cache.read("InfoRiskControlService-RemoteIp-#{remote_ip}") + if result.present? + if result.to_i > 20 + @status = 0 + @message = "暂时无法请求,请稍后再试" + else + Rails.cache.write("InfoRiskControlService-RemoteIp-#{remote_ip}", result.to_i + 1) + end + else + Rails.cache.write("InfoRiskControlService-RemoteIp-#{remote_ip}", 1, expires_in: 1.day) + end + end + + def minute_risk_control + result = Rails.cache.read("InfoRiskControlService-Minute-#{receiver}") + if result.present? + @status = 0 + @message = "您的请求过于频繁,请稍后再试" + else + Rails.cache.write("InfoRiskControlService-Minute-#{receiver}", 1, expires_in: 1.minute) + end + end + + def day_risk_control + result = Rails.cache.read("InfoRiskControlService-Day-#{receiver}") + if result.present? + if result.to_i > 10 + @status = 0 + @message = "您的请求过于频繁,请稍后再试" + else + Rails.cache.write("InfoRiskControlService-Day-#{receiver}", result.to_i + 1) + end + else + Rails.cache.write("InfoRiskControlService-Day-#{receiver}", 1, expires_in: 1.days) + end + end + +end \ No newline at end of file From 71862ba6d6fe964a11b0da3de8270a7514b0942f Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 19 Apr 2023 10:26:07 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/users_controller.rb | 2 +- app/services/info_risk_control_service.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index f5ea37a90..a37db2524 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -9,7 +9,7 @@ class Api::V1::UsersController < Api::V1::BaseController mail = params[:email] code_type = params[:code_type] - status, message = InfoRiskControlService.call(0, request.remote_ip) + status, message = InfoRiskControlService.call(mail, request.remote_ip) tip_exception(420, message) if status == 0 sign = Digest::MD5.hexdigest("#{OPENKEY}#{mail}") diff --git a/app/services/info_risk_control_service.rb b/app/services/info_risk_control_service.rb index f07b30c48..f8bd993b5 100644 --- a/app/services/info_risk_control_service.rb +++ b/app/services/info_risk_control_service.rb @@ -14,12 +14,12 @@ class InfoRiskControlService < ApplicationService def call if receiver == "" remote_ip_minute_risk_control - remote_ip_risk_control if @status = 1 + remote_ip_risk_control if @status == 1 else remote_ip_minute_risk_control - remote_ip_risk_control if @status = 1 - minute_risk_control - day_risk_control if @status = 1 + remote_ip_risk_control if @status == 1 + minute_risk_control + day_risk_control if @status == 1 end return @status, @message