archwhite
4/3/2017 - 6:21 AM

rect client dev ruby script

rect client dev ruby script

# only for bundle install --local
require 'bundler/setup'
#
require 'logger'
require 'rest-client'
require 'json'
require 'sequel'

Encoding.default_internal = 'utf-8'

connectiondata = {
  :server => 'server',
  :host => 'host',
  :database => 'database',
  :username => 'username',
  :password => 'password'
}

db = Sequel.connect(
  :adapter  => 'ado',
  :host     => connectiondata[:server],
  :database => connectiondata[:database],
  :user       => connectiondata[:username],  #not needed via SSO
  :password=>connectiondata[:password],    #not needed via SSO
  # :encoding =>Encoding::UTF_8,  #only MySQL
)
db.test_connection  #force exception if problem occured

# db = Sequel.tinytds(
#   :host     =>  connectiondata[:server],
#   :database => connectiondata[:database],
#   :user     => connectiondata[:username],  #No SSO possible
#   :password => connectiondata[:password],
# )
# db.test_connection  #force exception if problem occured

ds = db[:agents]
puts ds.all
# puts ds.where('id = ?', 8).update(:machinename => 111)
# puts ds.all

exit
module RestRequests

  Encoding.default_external='utf-8'

  proto = 'https'
  server = 'localhost:36058'
  loginurl =  "#{proto}://#{server}/Account/Login"

  RestClient.log='stdout'

  def get(url, headers = nil)
    RestClient::Request.execute(:method => :get, :url => url, :headers => headers, :verify_ssl => false)
  end

  def post(url, payload, headers = nil)
    RestClient::Request.execute(:method => :post, :url => url, :headers => headers, :verify_ssl => false)
  end

  # response = RestClient::Request.execute(:method => :get, :url => loginurl, :verify_ssl => false)#:headers => headers, &block)
  if false
    response = get loginurl
    r = response.to_s
    server_cookie = response.headers[:set_cookie][0].split(';')[0].split('=')
    cookie_name = server_cookie[0]
    cookie_value = server_cookie[1]

    token_match = '<input name="__RequestVerificationToken" type="hidden" value="'
    token_index = r.index(token_match)
    token_len = 'wZnAaCykzI4eQ46MCy0LunDe9mgK5KpO0R0cLwX4Bmn0EegwNgkcIifdRnRVFQYMpYP2jX7ca2BcUOlw8g4RlRSLpriJMtrMq92JUpqWU0I1'.length

    csrf_token = r[token_index + token_match.length, token_len]

    raise "can't fins csrf token" unless csrf_token.length == token_len
  end

  headers = {
  "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  "Accept-Encoding":"gzip, deflate, br",
  "Accept-Language":"ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4",
  "Cache-Control":"max-age=0",
  "Connection":"keep-alive",
  # "Content-Length":"157",
  "Content-Type":"application/x-www-form-urlencoded",
  # "Cookie":"__RequestVerificationToken=#{cookie_value}",
  "Cookie":'__RequestVerificationToken=XJw3UpXLxlvF42v14gqstkD0eb1_raPWwRRM-bPMDv0T0hHhjilgxKaANXITFfMoqwzvFj6OJVIEMrA97943Q3yKzG76I0mQgG32vAMoa181',
  "Host":"dev",
  "Origin":"https://dev",
  "Referer":"https://dev/Account/Login",
  "Upgrade-Insecure-Requests":"1",
  "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
  }

  r = post(loginurl, {'__RequestVerificationToken':csrf_token, 'UserName': '1', 'Password': '2'}, headers)
  p r
end