|
|
|
#!/usr/bin/python
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from suds import WebFault
|
|
|
|
from suds.transport import TransportError
|
|
|
|
|
|
|
|
from client_class import Client_suds
|
|
|
|
import traceback as tb
|
|
|
|
import time, logging
|
|
|
|
import os, sys
|
|
|
|
import threading, urllib2
|
|
|
|
from function import create_obj, get_sid, analysis, clear, get_entire_frame
|
|
|
|
from pid_information import client_list_methods
|
|
|
|
from cert_func import client_post_auth, client_post_request, client_get_cert, \
|
|
|
|
client_post_cert
|
|
|
|
from sid_func import session_clean, client_session_info
|
|
|
|
from cert_verify import get_CRL, VerifyError
|
|
|
|
|
|
|
|
import argparse, datetime
|
|
|
|
from calculate.core.datavars import DataVarsCore
|
|
|
|
|
|
|
|
from client_class import HTTPSClientCertTransport
|
|
|
|
from methods_func import call_method, get_method_argparser
|
|
|
|
from calculate.lib.cl_lang import setLocalTranslate
|
|
|
|
setLocalTranslate('calculate_console',sys.modules[__name__])
|
|
|
|
|
|
|
|
def client_signal(client):
|
|
|
|
Vars = DataVarsCore()
|
|
|
|
Vars.importCore()
|
|
|
|
Vars.flIniFile()
|
|
|
|
try:
|
|
|
|
client_active = Vars.Get('cl_core_client_active_period')
|
|
|
|
except:
|
|
|
|
client_active = 15
|
|
|
|
while True:
|
|
|
|
if os.path.exists(client.SID_FILE) :
|
|
|
|
fi = open(client.SID_FILE, 'r')
|
|
|
|
temp = fi.read()
|
|
|
|
fi.close()
|
|
|
|
sid = int(temp)
|
|
|
|
else:
|
|
|
|
sid = 0
|
|
|
|
try:
|
|
|
|
client.service.active_client(sid)
|
|
|
|
except:
|
|
|
|
print _('no connection to server!')
|
|
|
|
sys.exit()
|
|
|
|
time.sleep(float(client_active))
|
|
|
|
|
|
|
|
def test(client, com=None):
|
|
|
|
if not com:
|
|
|
|
method_name = 'test'
|
|
|
|
else:
|
|
|
|
method_name = com
|
|
|
|
view = client.service[0][method_name + '_view']()
|
|
|
|
|
|
|
|
cr = create_obj(client, method_name)
|
|
|
|
list_param = dir (cr)
|
|
|
|
|
|
|
|
param_list = []
|
|
|
|
for param in list_param:
|
|
|
|
if not param.startswith('_'):
|
|
|
|
param_list.append(param)
|
|
|
|
|
|
|
|
for Group in view.groups.GroupField:
|
|
|
|
print "GroupField name : ", Group.name
|
|
|
|
|
|
|
|
for field in Group.fields.Field:
|
|
|
|
if field.element == 'input':
|
|
|
|
if field.type == 'str':
|
|
|
|
cr[field.name] = raw_input(field.label)
|
|
|
|
if field.type == 'int':
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
var = raw_input(field.label)
|
|
|
|
cr[field.name] = int (var)
|
|
|
|
break
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
print 'Это не целое число'
|
|
|
|
elif field.element == 'bool':
|
|
|
|
while 1:
|
|
|
|
bool_var = raw_input(field.label+' (y/n): ')
|
|
|
|
if bool_var.lower() in ['y','yes']:
|
|
|
|
cr[field.name] = True
|
|
|
|
break
|
|
|
|
if bool_var.lower() in ['n','no']:
|
|
|
|
cr[field.name] = False
|
|
|
|
break
|
|
|
|
print 'Enter "Yes" or "No"!'
|
|
|
|
|
|
|
|
elif field.element == 'check':
|
|
|
|
choice = field.choice[0]
|
|
|
|
while 1:
|
|
|
|
print 'Select one: '
|
|
|
|
for i in range(1,len(choice)+1):
|
|
|
|
print choice[i-1], ' - %d' %i
|
|
|
|
try:
|
|
|
|
bool_var = int (raw_input(field.label))
|
|
|
|
if bool_var > 0:
|
|
|
|
cr[field.name] = choice[bool_var - 1]
|
|
|
|
print 'your choice %s' %cr[field.name]
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
#field.choice
|
|
|
|
#print field.help
|
|
|
|
|
|
|
|
sid = get_sid(client.SID_FILE)
|
|
|
|
s = client.service[0][method_name](sid, cr)
|
|
|
|
print s
|
|
|
|
|
|
|
|
def parse():
|
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser.add_argument(
|
|
|
|
'-h', '--help', action='store_true', default=False,
|
|
|
|
dest='help', help=_("show this help message and exit"))
|
|
|
|
parser.add_argument(
|
|
|
|
'--method', type=str, dest='method',
|
|
|
|
help=_('call method'))
|
|
|
|
parser.add_argument(
|
|
|
|
'-l', '--lang', type=str, dest='lang',
|
|
|
|
help=_('language for translate'))
|
|
|
|
parser.add_argument(
|
|
|
|
'-p', '--port', type=int, default = '8888', dest='port',
|
|
|
|
help=_('port number'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--host', type=str, default = 'localhost', dest='host',
|
|
|
|
help=_('host destination'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--gen-cert-by', type=str, dest='by_host', metavar = 'HOST',
|
|
|
|
help=_('post request a signed certificate by server'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--get-cert-from', type=str, dest='from_host', metavar = 'HOST',
|
|
|
|
help=_('get signed certificate from server'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--cert-path', type=str, dest='path_to_cert', metavar = 'PATH',
|
|
|
|
help=_('path to cert and key files'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--list-pid', action='store_true', default=False,
|
|
|
|
dest='list_pid', help=_("view a list of running processes"))
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--dump', action='store_true', default=False, dest = 'dump',
|
|
|
|
help=_('dump (using with key --list-pid)'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--pid-result', type=int, metavar = 'PID',
|
|
|
|
dest='pid_res', help=_("view result of process"))
|
|
|
|
parser.add_argument(
|
|
|
|
'--pid-kill', type=int, metavar = 'PID',
|
|
|
|
dest='pid_kill', help=_("kill selected process"))
|
|
|
|
parser.add_argument(
|
|
|
|
'--session-clean', action='store_true', default=False,
|
|
|
|
dest='session_clean', help=_('Clear cache session'))
|
|
|
|
parser.add_argument(
|
|
|
|
'--session-info', action='store_true', default=False,
|
|
|
|
dest='session_info', help=_("View session information"))
|
|
|
|
parser.add_argument(
|
|
|
|
'--update-crl', action='store_true', default=False,
|
|
|
|
dest='update_crl', help=_("update the certificate revocation lists"))
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def https_server(client, args, unknown_args, url, clVarsCore):
|
|
|
|
client_post_auth(client)
|
|
|
|
|
|
|
|
if args.session_clean:
|
|
|
|
session_clean(client)
|
|
|
|
|
|
|
|
if args.session_info:
|
|
|
|
client_session_info(client)
|
|
|
|
|
|
|
|
if args.list_pid:
|
|
|
|
if args.dump:
|
|
|
|
from pid_information import client_pid_info
|
|
|
|
client_pid_info(client)
|
|
|
|
else:
|
|
|
|
from pid_information import client_list_pid
|
|
|
|
client_list_pid(client)
|
|
|
|
|
|
|
|
if args.pid_res:
|
|
|
|
get_entire_frame(client, args.pid_res)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if args.pid_kill:
|
|
|
|
from pid_information import client_pid_kill
|
|
|
|
return client_pid_kill(client, args.pid_kill)
|
|
|
|
|
|
|
|
if not args.method:
|
|
|
|
client_list_methods(client)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
elif args.method and args.help:
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '6 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
method_parser, view = get_method_argparser(client, args)
|
|
|
|
method_parser.print_help()
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '7 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
client.frame_period = clVarsCore.Get('cl_core_get_frame_period')
|
|
|
|
except:
|
|
|
|
client.frame_period = 2
|
|
|
|
method_result = call_method(client, args)
|
|
|
|
|
|
|
|
try:
|
|
|
|
analysis(client, client.sid, method_result)
|
|
|
|
except urllib2.URLError, e:
|
|
|
|
print e
|
|
|
|
# Vars = DataVarsCore()
|
|
|
|
# Vars.importCore()
|
|
|
|
# Vars.flIniFile()
|
|
|
|
#
|
|
|
|
# signaling = threading.Thread(target=client_signal, args = (client, ))
|
|
|
|
# signaling.setDaemon(True)
|
|
|
|
# signaling.start()
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '1 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
parser = parse()
|
|
|
|
args, unknown_args = parser.parse_known_args()
|
|
|
|
|
|
|
|
if not args.method and args.help:
|
|
|
|
parser.print_help()
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '1/2 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.FATAL)
|
|
|
|
logging.getLogger('suds.client').setLevel(logging.FATAL)
|
|
|
|
logging.getLogger('suds.transport').setLevel(logging.FATAL)
|
|
|
|
logging.getLogger('suds.transport.http').setLevel(logging.FATAL)
|
|
|
|
logging.getLogger('suds.umx.typed').setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
clVarsCore = DataVarsCore()
|
|
|
|
clVarsCore.importCore()
|
|
|
|
clVarsCore.flIniFile()
|
|
|
|
|
|
|
|
homePath = clVarsCore.Get('ur_home_path')
|
|
|
|
|
|
|
|
# translate
|
|
|
|
if args.lang:
|
|
|
|
print 'code not found!!!!'
|
|
|
|
|
|
|
|
port = args.port
|
|
|
|
host = args.host
|
|
|
|
path_to_cert = args.path_to_cert
|
|
|
|
if not path_to_cert:
|
|
|
|
path_to_cert = clVarsCore.Get('cl_client_cert_dir')
|
|
|
|
path_to_cert = path_to_cert.replace("~",homePath)
|
|
|
|
|
|
|
|
if args.update_crl:
|
|
|
|
getCRL = threading.Thread(target=get_CRL, args = (path_to_cert, ))
|
|
|
|
getCRL.start()
|
|
|
|
getCRL.join()
|
|
|
|
print 'GRL updated'
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if args.by_host:
|
|
|
|
client_post_request (path_to_cert, args.by_host)
|
|
|
|
return 0
|
|
|
|
if args.from_host:
|
|
|
|
client_get_cert (path_to_cert, args)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
url = "https://%s:%d/?wsdl" %(host, port)
|
|
|
|
# print "url = %s" %url
|
|
|
|
|
|
|
|
clear()
|
|
|
|
try:
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '2 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
client = Client_suds(url, \
|
|
|
|
transport = HTTPSClientCertTransport(None,None, path_to_cert))
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '2/1 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
server_host_name = client.service.get_server_host_name()
|
|
|
|
# print server_host_name
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '2/2 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
del (client)
|
|
|
|
except urllib2.URLError, e:
|
|
|
|
print _('Failed to connect')+':', e
|
|
|
|
sys.exit(1)
|
|
|
|
# server_host_name = 'dspiridonov.local.calculate.ru'
|
|
|
|
|
|
|
|
try:
|
|
|
|
import glob
|
|
|
|
all_cert_list = glob.glob(os.path.join(path_to_cert, '*.crt'))
|
|
|
|
fit_cert_list = []
|
|
|
|
for client_cert_path in all_cert_list:
|
|
|
|
client_cert = client_cert_path.replace(path_to_cert, '')
|
|
|
|
client_cert_name = client_cert.replace('.crt', '')
|
|
|
|
if server_host_name.endswith(client_cert_name):
|
|
|
|
fit_cert_list.append(client_cert_name)
|
|
|
|
fit_cert_list.sort(key = len)
|
|
|
|
Connect_Error = 1
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '3 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
for i in range (0, len(fit_cert_list)):
|
|
|
|
#print 'fit_cert_list = ',fit_cert_list
|
|
|
|
cert_name = fit_cert_list.pop()
|
|
|
|
CERT_FILE = path_to_cert + cert_name + '.crt'
|
|
|
|
CERT_KEY = path_to_cert + cert_name + '.key'
|
|
|
|
try:
|
|
|
|
# print 111111111
|
|
|
|
client = Client_suds(url,\
|
|
|
|
transport = HTTPSClientCertTransport(CERT_KEY, CERT_FILE,\
|
|
|
|
path_to_cert))
|
|
|
|
client.set_parameters (path_to_cert, CERT_FILE, CERT_KEY)
|
|
|
|
client_post_cert(client)
|
|
|
|
Connect_Error = 0
|
|
|
|
except VerifyError, e:
|
|
|
|
print e.value
|
|
|
|
Connect_Error = 1
|
|
|
|
except urllib2.URLError, e:
|
|
|
|
print _('Failed to connect')+':', e
|
|
|
|
sys.exit(1)
|
|
|
|
if Connect_Error == 0:
|
|
|
|
break
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '4 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
#If the certificate file misses
|
|
|
|
if Connect_Error:
|
|
|
|
print 'CONNECT ERROR'
|
|
|
|
CERT_FILE = None
|
|
|
|
CERT_KEY = None
|
|
|
|
client = Client_suds(url,\
|
|
|
|
transport = HTTPSClientCertTransport(CERT_KEY, CERT_FILE,\
|
|
|
|
path_to_cert))
|
|
|
|
|
|
|
|
client.set_parameters (path_to_cert, CERT_FILE, CERT_KEY)
|
|
|
|
|
|
|
|
client.port = port
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print '5 ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
https_server(client, args, unknown_args, url, clVarsCore)
|
|
|
|
# now = datetime.datetime.now()
|
|
|
|
# print 'END ===> %ds %dms' %(now.second, now.microsecond)
|
|
|
|
#----------------------------------------------------
|
|
|
|
except WebFault, f:
|
|
|
|
print _("Exception: %s") %f
|
|
|
|
print f.fault
|
|
|
|
except TransportError, te:
|
|
|
|
print _("Exception: %s") %te
|
|
|
|
except Exception, e:
|
|
|
|
print _("Exception: %s") %e
|
|
|
|
tb.print_exc()
|