update license

This commit is contained in:
Thein Lin Kyaw
2020-01-23 11:33:02 +06:30
parent 12941cfe66
commit eb2db57d91
2 changed files with 64 additions and 61 deletions

View File

@@ -2,6 +2,7 @@ require 'yaml'
class License class License
include HTTParty include HTTParty
include ActionDispatch::Http
base_uri "https://l.doemal.app//api" base_uri "https://l.doemal.app//api"
@@ -93,6 +94,7 @@ class License
# License Activation # License Activation
def license_activate (shop, license_key, db_host, db_schema, db_user, db_password) def license_activate (shop, license_key, db_host, db_schema, db_user, db_password)
aes = MyAesCrypt.new aes = MyAesCrypt.new
aes_key, aes_iv = aes.export_key(license_key) aes_key, aes_iv = aes.export_key(license_key)
@@ -101,26 +103,26 @@ class License
@activate = response.parsed_response @activate = response.parsed_response
if (@activate["status"]) if (@activate["status"])
##Check from local redis - if available load local otherwise get from remote ##Check from local redis - if available load local otherwise get from remote
cache_key = "shop:#{@activate["shop_name"]}" cache_key = "shop:#{@activate["shop_name"]}"
cache_license = nil
##Get redis connection from connection pool ##Get redis connection from connection pool
# redis = Redis.new
# cache_license = redis.get(cache_key)
Rails.logger.info "Cache key - " + cache_key.to_s
# if cache_license.nil?
cache = {"shop" => @activate["shop_name"], "key" => aes_key, "iv" => @activate["iv_key"], "renewable_date" => @activate["renewable_date"] }
redis = Redis.new redis = Redis.new
cache_license = redis.get(cache_key) redis.set(cache_key, Marshal.dump(cache))
# end
Rails.logger.info "Cache key - " + cache_key.to_s
if cache_license.nil?
cache = {"shop" => @activate["shop_name"], "key" => aes_key, "iv" => @activate["iv_key"], "renewable_date" => @activate["renewable_date"] }
redis = Redis.new
redis.set(cache_key, Marshal.dump(cache))
end
Rails.logger.info "License - " + response.parsed_response.to_s Rails.logger.info "License - " + response.parsed_response.to_s
response = create_license_file(@activate) response = create_license_file(@activate)
if(response[:status]) if(response[:status])
#sym_path = "/home/user/symmetric/" #sym_path = "/home/user/symmetric/"
sym_path = File.expand_path("~/symmetric/") sym_path = File.expand_path("~/symmetric/")
@@ -197,7 +199,21 @@ class License
def self.check_license_file(lookup) def self.check_license_file(lookup)
return unless File.exist?("config/license.yml") return unless File.exist?("config/license.yml")
if license = YAML.load_file("config/license.yml") if license = YAML.load_file("config/license.yml")
license[lookup].present? if license[lookup].nil?
if ENV["SERVER_MODE"] == "application"
license[lookup] = license.values.first
else
tld_length = Rails.application.config.action_dispatch.tld_length
subdomains = URL.extract_subdomains(lookup, tld_length)
if key = license.keys.find { |k| URL.extract_subdomains(k, tld_length).last == subdomains.last}
license[lookup] = license[key]
end
end
if license[lookup]
File.open("config/license.yml", "w") { |file| file.write license.to_yaml }
end
end
return license[lookup]
end end
end end
@@ -238,7 +254,7 @@ class License
license[lookup][content] = encode_str(crypted_str) license[lookup][content] = encode_str(crypted_str)
# To write changes to the file, use: # To write changes to the file, use:
File.open("config/license.yml", "w") {|file| file.puts new_file_str } File.open("config/license.yml", "w") {|file| file.write(license.to_yaml) }
# File.open("config/license.yml").each do |line| # File.open("config/license.yml").each do |line|
# new_file_str = line.gsub(content, crypted_str) # new_file_str = line.gsub(content, crypted_str)
@@ -286,8 +302,6 @@ class License
##Check from local redis - if available load local otherwise get from remote ##Check from local redis - if available load local otherwise get from remote
cache_key = "shop:#{shop.chomp}" cache_key = "shop:#{shop.chomp}"
cache_shop = nil
##Get redis connection from connection pool ##Get redis connection from connection pool
redis = Redis.new redis = Redis.new
cache_shop = redis.get(cache_key) cache_shop = redis.get(cache_key)
@@ -310,45 +324,36 @@ class License
# License File Creation # License File Creation
def create_license_file(response_data) def create_license_file(response_data)
if File.exist?("config/license.yml")
delete_license_file
end
begin begin
# Licese File Creation # Licese File Creation
# File.open("config/license.yml", "w") do |f| config = { "iv_key" => response_data['iv_key'],
# f.puts("iv_key: #{response_data['iv_key']}") "shop_name" => response_data['shop_name'],
# f.puts("shop_name: #{response_data['shop_name']}") "email" => response_data['email'],
# f.puts("email: #{response_data['email']}") "telephone" => response_data['telephone'],
# f.puts("telephone: #{response_data['telephone']}") "fax" => response_data['fax'],
# f.puts("fax: #{response_data['fax']}") "address" => response_data['address'],
# f.puts("address: #{response_data['address']}") "dbhost" => response_data['dbhost'],
# f.puts("dbhost: #{response_data['dbhost']}") "dbschema" => response_data['dbschema'],
# f.puts("dbschema: #{response_data['dbschema']}") "dbusername" => response_data['dbusername'],
# f.puts("dbusername: #{response_data['dbusername']}") "dbpassword" => response_data['dbpassword'],
# f.puts("dbpassword: #{response_data['dbpassword']}") "api_token" => response_data['api_token'],
# f.puts("api_token: #{response_data['api_token']}") "app_token" => response_data['app_token'],
# f.puts("app_token: #{response_data['app_token']}") "plan_sku" => response_data['plan_sku'],
# f.puts("plan_sku: #{response_data['plan_sku']}") "renewable_date" => response_data['renewable_date'],
# f.puts("renewable_date: #{response_data['renewable_date']}") "plan_name" => response_data['plan_name'] }
# f.puts("plan_name: #{response_data['plan_name']}")
# end if File.exist?("config/license.yml")
license = {lookup => {"iv_key" => response_data['iv_key'], license = YAML.load_file("config/license.yml")
"shop_name" => response_data['shop_name'], end
"email" => response_data['email'],
"telephone" => response_data['telephone'], if license
"fax" => response_data['fax'], license.each do |k,v|
"address" => response_data['address'], license[k] = config if v["shop_name"] == config["shop_name"]
"dbhost" => response_data['dbhost'], end
"dbschema" => response_data['dbschema'], license[lookup] = config
"dbusername" => response_data['dbusername'], end
"dbpassword" => response_data['dbpassword'],
"api_token" => response_data['api_token'], license ||= {lookup => config}
"app_token" => response_data['app_token'],
"plan_sku" => response_data['plan_sku'],
"renewable_date" => response_data['renewable_date'],
"plan_name" => response_data['plan_name']}
}
File.open("config/license.yml", "w"){ |file| file.write(license.to_yaml) } File.open("config/license.yml", "w"){ |file| file.write(license.to_yaml) }
rescue IOError rescue IOError
@@ -407,10 +412,10 @@ class License
# f.write("initial.load.use.extract.job.enabled=true\n") # f.write("initial.load.use.extract.job.enabled=true\n")
f.close f.close
rescue IOError rescue IOError
response = { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"} return { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"}
end end
response = { "status": true, "message": "Success Activation. License also created."}
end end
{ "status": true, "message": "Success Activation. License also created." }
end end
# Run Symmetric # Run Symmetric

View File

@@ -4,9 +4,8 @@ class ActionController::Base
private private
def lookup_domain def lookup_domain
if request.subdomain.present? && request.subdomain != "www" && ENV["SERVER_MODE"] == "cloud" if request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_MODE"] == "cloud"
from = request.host @license = cache_license(ENV["SX_PROVISION_URL"], request.host) # request.subdomain.downcase
@license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase
if (!@license.nil?) if (!@license.nil?)
logger.info "Location - " + @license.dbschema logger.info "Location - " + @license.dbschema
ActiveRecord::Base.establish_connection(website_connection(@license)) ActiveRecord::Base.establish_connection(website_connection(@license))
@@ -18,7 +17,7 @@ class ActionController::Base
# redirect_to root_url(:host => request.domain) + "store_error" # redirect_to root_url(:host => request.domain) + "store_error"
render :json => [{ status: false, message: 'Invalid Access!'}] render :json => [{ status: false, message: 'Invalid Access!'}]
end end
elsif request.subdomain.present? && request.subdomain != "www" || ENV["SERVER_MODE"] == "application" elsif request.subdomains.last && request.subdomains.last != "www" || ENV["SERVER_MODE"] == "application"
# check for license file # check for license file
if check_license(request.host) if check_license(request.host)
current_license(ENV["SX_PROVISION_URL"], request.host) current_license(ENV["SX_PROVISION_URL"], request.host)
@@ -102,9 +101,8 @@ class ActionController::API
private private
def lookup_domain def lookup_domain
if request.subdomain.present? && request.subdomain != "www" && ENV["SERVER_CODE"] == "cloud" if request.subdomains.last && request.subdomains.last != "www" && ENV["SERVER_CODE"] == "cloud"
from = request.host @license = cache_license(ENV["SX_PROVISION_URL"], request.host) # request.subdomain.downcase
@license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase
if (!@license.nil?) if (!@license.nil?)
logger.info "Location - " + @license.dbschema logger.info "Location - " + @license.dbschema
ActiveRecord::Base.establish_connection(website_connection(@license)) ActiveRecord::Base.establish_connection(website_connection(@license))