registered-sites/app/controllers/admin/registered_sites_controller.rb

523 lines
18 KiB
Ruby
Raw Normal View History

2014-10-27 11:01:37 +00:00
# encoding: utf-8
class Admin::RegisteredSitesController < OrbitAdminController
before_action ->(module_app = @app_title) { set_variables module_app }
2014-10-27 11:01:37 +00:00
def index
@registeredsites = RegisteredSite.all.order_by(sort)
.with_categories(filters("category"))
2014-12-30 12:28:20 +00:00
.with_tags(filters("tag")).desc(:created_at)
2014-10-27 11:01:37 +00:00
@table_fields = table_fields
@tags = @module_app.tags
2020-04-01 03:44:59 +00:00
@categories = @module_app.categories.enabled
@filter_fields = filter_fields_without_status(@categories, @tags)
@registeredsites = search_data(@registeredsites,[:title,:site_domain]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def get_watch_list
site_annc = SiteAnnc.find(params['data'])
lists = site_annc.watch_list.group_by{|v| v[0]}.map{|k,v| [k,v.collect{|v1| v1[1]}]}
res = lists.collect do |v|
site = RegisteredSite.find(v[0]) rescue nil
par1 = site.nil? ? ['not found',''] : [site.show_name,site.site_domain]
par2 = v[1].join(', ')
[par1,par2]
end
render :json => res
end
def show
end
def announcement
@site_anncs = SiteAnnc.all.desc(:craete_at).page(params[:page]).per(10)
end
def edit_annc
@site_annc = SiteAnnc.find(params['site_annc_id']) rescue SiteAnnc.new
end
def update_annc
site_annc = SiteAnnc.find(params['site_annc_id']) rescue SiteAnnc.create
site_annc.update_attributes(title: params['title'],
see_more_link: params['see_more_link'],
content: params['content'],
recieve_sites: params['recieve_sites'],
visible: params['visible']=='true',
send_all: params['send_all']=='true')
redirect_to '/admin/registered_sites/announcement'
end
def get_select_box
tags = @module_app.tags.map{|v| v}
categories = @module_app.categories.enabled.map{|v| v}
sites = RegisteredSite.all.collect{|v1| [v1,v1.tags,v1.category]}
@sites_order_by_tags = tags.map{|v| {v=>sites.select{|v1| v1[1].include? v}.map{|v2| v2[0]}}}
@sites_order_by_tags << {'no_tag'=>sites.select{|v1| v1[1]== []}.map{|v2| v2[0]}}
@sites_order_by_cats = categories.map{|v| {v=>sites.select{|v1| v1[2]== v}.map{|v2| v2[0]}}}
@sites_order_by_cats << {'no_category'=>sites.select{|v1| v1[2].nil?}.map{|v2| v2[0]}}
end
def delete_annc
site_annc = SiteAnnc.find(params['site_annc_id']) rescue nil
if !site_annc.nil?
site_annc.destroy
end
redirect_to '/admin/registered_sites/announcement'
end
def update_status(site)
tp1 = site.site_statuses.first
tp1 = site.site_statuses.create if tp1.nil?
if site.site_domain.to_s.blank?
urls = nil
else
urls = site.self_test
end
if urls.nil?
tp1.status = 'domain is blank'
2021-01-28 03:19:11 +00:00
tp1.can_login = false
2020-04-01 03:44:59 +00:00
elsif urls == [nil,nil]
tp1.status = 'domain not found'
tp1.href = 'http://'+site.site_domain
2021-01-28 03:19:11 +00:00
tp1.can_login = false
2020-04-01 03:44:59 +00:00
elsif !urls[0].to_s.empty? || !urls[1].to_s.empty?
tp1.status = 'ok'
if !urls[0].empty?
root_url = urls[0]
else
root_url = urls[1]
end
tp1.href = root_url
uri = URI(root_url)
2021-01-28 03:19:11 +00:00
Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 30,read_timeout: 30,verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
2020-04-01 03:44:59 +00:00
r3 = login_reomote(http,root_url)
if r3.code != '200'
tp1.can_login = true
else
tp1.can_login = false
end
r4 = get_website(uri,http,r3)
uri2 = URI ("#{root_url}/admin/playground")
r5 = get_website(uri2,http,r4)
if r5.code.to_s == '200'
uri3 = URI ("#{root_url}/admin/playground/command")
doc = Nokogiri::HTML(r5.body) rescue nil
csrf_data = doc.search("meta[name='csrf-token']")[0].attributes['content'].value rescue nil
r6 = post_website(uri3,http,r5,{'command' => "ip addr | grep \"inet \" | grep -v 127.0.0.1 | awk '{print $2}'| xargs | awk '{print $1}'"},csrf_data)
res = JSON.parse(r6.body) rescue ''
if (res == {"success"=>true} || r6.code.to_s != '200')
uri4 = URI ("#{root_url}/admin/playground/console_output")
count = 0
while count < 5
r7 = get_website(uri4,http,r6,{'count' => '0'},csrf_data)
if (r7.code.to_s == '200' rescue false)
r6 = r7
response = JSON.parse(r6.body)['response'] rescue nil
end
if Array(response).count != 0
real_ip = Array(response)[0].to_s.split(/\//)[0].scan(/\d+.\d+.\d+.\d+/)[0]
tp1.status = tp1.status + " ,ip: #{real_ip}"
if !real_ip.to_s.empty?
site.real_ip = real_ip
site.save
end
break
end
count = count + 1
sleep 1
end
end
end
end
else
tp1.href = 'http://'+site.site_domain
tp1.status = 'bad gateway'
2021-01-28 03:19:11 +00:00
tp1.can_login = false
2020-04-01 03:44:59 +00:00
end
tp1.save!
end
def get_check_log
tp = CheckSiteTemp.first
tp_all_ids = tp.all_ids
count = tp_all_ids.index(params['data'])
count = count.nil? ? 0 : (count+1)
all_ids = tp_all_ids[count..-1]
item = RegisteredSite.find(all_ids).collect do |v|
site_status = v.site_statuses.first
site_name = v.title.nil? ? v.site_domain : v.title
{ id: v.id.to_s,
can_login: site_status.can_login,
href: site_status.href,
site_name: site_name,
status: site_status.status}
end
render :json =>{status: tp.status,item:item,now_id:params['data']}.to_json
end
def check_site
site = RegisteredSite.find(params['item_id'])
update_status(site)
render :text => "ip: #{site.real_ip}"
end
def check_all_site
tp = CheckSiteTemp.first
tp = CheckSiteTemp.create() if tp.nil?
tp.all_ids = []
tp.status = 'running'
tp.save
Thread.new do
count = RegisteredSite.where(:is_hidden.ne=>true).count
batch_l = (count/50.0).ceil
(1..batch_l).each do |v|
sites = RegisteredSite.where(:is_hidden.ne=>true).sort_by{|v| v.id}[(v-1)*50..(v*50)]
sites.each do |site|
begin
update_status(site)
system('sleep 0.5')
tp.all_ids << site.id.to_s
tp.save
rescue => e
puts e.inspect
end
end
end
tp.status = 'finish'
tp.save
end
render :text => ''
end
def request_website(type,uri,http,pre_req,data,csrf_data)
req = (type =='get' ? Net::HTTP::Get.new(uri) : Net::HTTP::Post.new(uri))
req.set_form_data(data)
req['Cookie'] = pre_req['set-cookie'].to_s.gsub(' path=/; HttpOnly','') rescue ''
req['X-CSRF-Token'] = csrf_data
req['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
http.request req
end
def get_website(uri,http,pre_req=nil,data={},csrf_data=nil)
request_website('get',uri,http,pre_req,data,csrf_data)
end
def post_website(uri,http,pre_req=nil,data={},csrf_data=nil)
request_website('post',uri,http,pre_req,data,csrf_data)
end
def login_reomote(http,root_url)
uri = URI(root_url)
r1 = get_website(uri,http)
r2 = get_website(uri,http,r1)
doc = Nokogiri::HTML(r2.body)
checkvalue = doc.css('.login-body').css('input[name=authenticity_token]')[0].attr('value') rescue nil
if checkvalue.nil?
puts [root_url,r2,doc.css('.login-body').css('input[name=authenticity_token]')]
end
uri1 = URI("#{root_url}/sessions")
data = {'authenticity_token' => checkvalue,'user_name' => 'rulingcom', 'password' =>'orbit_is_great_1','referer_url'=>root_url}
r3 = post_website(uri1,http,r2,data)
end
def bundle_update_remote(site)
root_url,log = site.fetch_web_url_and_log
tp = BundleUpdateTemp.first
tp.now_id = site.id.to_s
tp.save
timeout_flag = false
sel_flag = false
not_login_flag = false
not_accept_flag = false
timeout_count = 0
if !root_url.nil?
uri = URI(root_url)
puts 'starting'
begin
2021-01-28 03:19:11 +00:00
Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 30,read_timeout: 30,verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
2020-04-01 03:44:59 +00:00
r3 = login_reomote(http,root_url)
r4 = get_website(uri,http,r3)
uri2 = URI ("#{root_url}/admin/playground")
r5 = get_website(uri2,http,r4)
if r5.code.to_s != '200'
not_login_flag = true
end
if !not_login_flag
not_accept_flag = true
uri3 = URI ("#{root_url}/admin/playground/command")
doc = Nokogiri::HTML(r5.body) rescue nil
csrf_data = doc.search("meta[name='csrf-token']")[0].attributes['content'].value rescue nil
r6 = post_website(uri3,http,r5,{'command' => 'bundle'},csrf_data)
count = 0
flag = false
res = JSON.parse(r6.body) rescue ''
if (res == {"success"=>true} || r6.code.to_s != '200')
not_accept_flag = false
last_length = 0
while 1
uri4 = URI ("#{root_url}/admin/playground/console_output")
r7 = get_website(uri4,http,r6,{'count' => '0'},csrf_data)
message = log.message
timeout_count = timeout_count + 1
if timeout_count > 30
timeout_flag = true
end
if (r7.code.to_s == '200' rescue false)
r6 = r7
response = JSON.parse(r6.body)['response'] rescue nil
last_message = nil
else
response = ''
message << "code:#{r7.code.to_s}"
last_message = "code:#{r7.code.to_s}"
if flag
count = count + 1
end
end
array_res = Array(response).clone
res = array_res[-1]
if !res.blank?
flag = true
end
if array_res.length > last_length
message << array_res[last_length..-1]
elsif array_res.length < last_length
message << array_res
end
message = message.flatten 1
if res.blank? && flag
message << res.inspect
count = count + 1
elsif res.blank?
count = count + 0.3
else
count = 0
last_message = last_message || res
if last_message != log.last_message
timeout_count = 0
end
end
last_length = array_res.length
log.message = message
log.last_message = last_message
log.save!
if message.length >= 5
message[-5..-1].each do |v|
if v.to_s.downcase.scan(/bundle.*update|run `bundle clean --force`|bundle.*complete!/).length>0 || v.to_s=='finish'
sel_flag = true
end
end
end
if count>30
timeout_flag = true
end
if sel_flag || timeout_flag
break
end
system('sleep 1')
end
else
log.status = 'failed'
log.message << r6.body rescue 'no response'
log.last_message = r6.body rescue 'no response'
log.save
end
end
#r6 = post_website(uri3,http,r6,{'command' => 'restart_server'},csrf_data)
#if (JSON.parse(r6.body) == {"success"=>true} rescue false)
# log.last_message += "<br>restart success"
#else
# log.last_message += "<br>restart failed"
#end
#log.message[-1] = log.last_message
#log.save!
end
rescue => e
2021-01-28 03:19:11 +00:00
timeout_flag = true
2020-04-01 03:44:59 +00:00
puts e
end
if not_accept_flag || not_login_flag
log.status = 'failed'
log.last_message = "can't login to admin"
elsif !timeout_flag
2021-01-28 03:19:11 +00:00
system('sleep 5')
2020-04-01 03:44:59 +00:00
if sel_flag || site.is_alive?
log.status = 'finish'
else
log.status = 'failed'
end
else
system('sleep 10')
if site.is_alive?
log.status = 'finish'
else
log.status = 'failed'
#update_flags = UpdateFlags.first
#update_flags.bundle_update_flag = false
#update_flags.save!
end
end
log.last_time = Time.now
log.save!
end
end
def stop_bundle_update
update_flags = UpdateFlags.first
update_flags.bundle_update_flag = false
update_flags.save
render :text => ''
end
def get_detail
site = RegisteredSite.find(params['item_id']) rescue nil
message = ''
if !site.nil?
message = site.bundle_update_logs.first.message.join('<br>') rescue ''
end
render :text => message
end
def destroy_bundle_temp
render :text => (BundleUpdateTemp.first.destroy.inspect rescue '')
end
def bundle_update
update_flags = UpdateFlags.first.nil? ? UpdateFlags.new() : UpdateFlags.first
update_flags.bundle_update_flag = true
update_flags.save
if BundleUpdateTemp.first.nil?
if params['item_ids'][0] == 'all'
item_ids = RegisteredSite.all.collect(&:id).map{|v| v.to_s}.reverse
else
item_ids = params['item_ids']
end
BundleUpdateTemp.create(all_ids: item_ids,now_id: nil)
Thread.new do
clone_item_ids = item_ids.clone
clone_item_ids.each do |site_id|
if !UpdateFlags.first.bundle_update_flag
tp = BundleUpdateTemp.first
tp.now_id = 'terminated'
tp.save
Thread.current.terminate!
end
site = RegisteredSite.find(site_id) rescue nil
if !site.nil? && (site.site_statuses.first.can_login rescue true)
bundle_update_remote(site)
2021-01-28 03:19:11 +00:00
system('sleep 5')
2020-04-01 03:44:59 +00:00
elsif !site.nil?
if site.bundle_update_logs.first.nil?
log = site.bundle_update_logs.create()
else
log = site.bundle_update_logs.first
end
log.status = 'failed'
log.last_message = "can't login"
log.save
end
end
tp = BundleUpdateTemp.first
tp.now_id = 'finish'
tp.save
end
render :json => {status: 'success'}
elsif BundleUpdateTemp.first.all_ids == params['item_ids']
render :json => {status: 'success'}
else
render :json => {status: 'failed'}
end
end
def get_message
tp = BundleUpdateTemp.first.now_id
if tp.nil?
render :json => {status: 'initializing'}
elsif tp=='terminated'
BundleUpdateTemp.first.destroy
render :json => {status: tp}
else
site_id = (tp=='finish' ? BundleUpdateTemp.first.all_ids.last : tp)
if tp=='finish'
BundleUpdateTemp.first.destroy
end
site = RegisteredSite.find(site_id) rescue nil
if !site.nil?
status = tp=='finish' ? 'finish' : 'running'
log = site.bundle_update_logs.first
render :json => {status: status,
item: {item_id: site_id,
status: log.status,
last_time: log.last_time,
last_message: log.last_message}}
else
render :json => {status: 'not found',
item: {item_id: site_id,
status: 'not found',
last_time: 'not found',
last_message: 'not found'}}
end
end
end
def bundle_update_remote_show
@registeredsites = RegisteredSite.all.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag")).desc(:created_at)
@table_fields = table_fields_for_bundle_update
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = filter_fields_without_status(@categories, @tags)
@registeredsites = search_data(@registeredsites,[:title,:site_domain]).page(params[:page]).per(RegisteredSite.all.count)#.per(10)
if request.xhr?
render :partial => "bundle_update_remote_show"
end
2014-10-27 11:01:37 +00:00
end
def edit
@registered_site = RegisteredSite.find(params[:id])
if can_edit_or_delete?(@registered_site)
else
render_401
end
end
2017-02-06 07:54:26 +00:00
def progress
end
def update
uid = params[:id].split('-').last
registered_site = RegisteredSite.find_by(:uid => uid)
registered_site.update_attributes(registered_site_params)
registered_site.save
redirect_to params['referer_url']
end
2014-10-27 11:01:37 +00:00
def change_access_status
2014-12-30 13:48:02 +00:00
if !request.xhr?
2015-08-28 09:30:12 +00:00
redirect_to admin_registered_sites_path(:page => params[:page], :keywords => params[:keywords])
2014-12-30 13:48:02 +00:00
return
end
2014-10-27 11:01:37 +00:00
status = params[:status]
site = RegisteredSite.find_by(:uid => params[:uid])
case status
when "revoke"
site.access_granted = false
site.save
when "grant"
site.access_granted = true
site.save
end
@table_fields = table_fields
2015-08-28 09:30:12 +00:00
@registeredsites = RegisteredSite.all.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag")).desc(:created_at)
2015-08-28 10:10:30 +00:00
@registeredsites = search_data(@registeredsites,[:title,:site_domain]).page(params[:page]).per(10)
2014-10-27 11:01:37 +00:00
# render :partial => "index"
end
2015-08-28 09:30:12 +00:00
def destroy
registeredsite = RegisteredSite.find(params[:id]) rescue nil
if !registeredsite.nil?
registeredsite.destroy
end
@table_fields = table_fields
2015-08-28 10:10:30 +00:00
2015-08-28 09:30:12 +00:00
@registeredsites = RegisteredSite.all.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag")).desc(:created_at)
2015-08-28 10:10:30 +00:00
@registeredsites = search_data(@registeredsites,[:title,:site_domain]).page(params[:page]).per(10)
2015-08-28 09:30:12 +00:00
end
2014-10-27 11:01:37 +00:00
private
def table_fields
[:domain, :admin_email, :status, :category, :tags, :permission]
end
2020-04-01 03:44:59 +00:00
def table_fields_for_bundle_update
[:domain, 'registered_sites.bundle_update', 'registered_sites.bundle_update_status', :category, 'registered_sites.last_message']
end
def registered_site_params
params.require(:registered_site).permit!
2014-10-27 11:01:37 +00:00
end
end