require 'sinatra/base' require 'json' require 'securerandom' require 'base64' require 'openssl' # require 'digest' module Adminix class ServerSetup < Sinatra::Base get '/' do %{ Adminix start } end get '/ping' do 'pong' end post '/data' do config = Adminix::Config.instance config.password = SecureRandom.hex content_type :json { password: config.password, image: config.image_sname }.to_json end post '/connect' do content_type :json data = {} config = Adminix::Config.instance begin decoded = Base64.decode64 request.body.read.encode('ascii-8bit') decipher = OpenSSL::Cipher::AES256.new :CBC decipher.decrypt decipher.key = config.password decipher.iv = config.iv decrypted_data = decipher.update(decoded) + decipher.final data = JSON.parse(decrypted_data)['server_setup'] rescue { success: false }.to_json end config.password = '' config.service_id = data['service_id'] config.secret_key = data['secret_key'] config.export_credentials Thread.start do sleep(2) Adminix::Service.instance.restart! end { success: true }.to_json end set :bind, '0.0.0.0' set :port, Adminix::Config::DEFAULT_SETUP_SERVER_PORT end end