Example #1
0
 def send(self, proxy, payload, session_id, proxy_protocol=''):
     client_origin = proxy.server.jc_mine['en']
     client = self.find_client_by_origin(client_origin)
     if not client:
         print('ERROR: no client for this proxy is associated. %s' %
               client_origin)
         raise TypeError
     msg_list = []
     msg_list.append('TransactionOrigin: %s\r\n' %
                     proxy.server.west.jc['wstc'][client_origin]['nm'])
     msg_list.append('TransactionID: %s\r\n' % session_id)
     if proxy_protocol:
         msg_list.append('X-Proxy-Protocol: %s\r\n' % proxy_protocol)
     msg_list.append('\r\n')
     msg_list.append(payload)
     msg = ''.join(msg_list)
     #
     if is_debug(1, self.west):
         print('DEBUG: sending west message length=%d' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server sending---')
             print(msg)
             print('DEBUG: ---END---')
     self.send_message(client, msg)
     self.q_request[session_id] = proxy
Example #2
0
 def send(self, proxy, payload, session_id, proxy_protocol=''):
     client_origin = proxy.server.jc_mine['en']
     client = self.find_client_by_origin(client_origin)
     if not client:
         print('ERROR: no client for this proxy is associated. %s' %
               client_origin)
         raise TypeError
     msg_list = []
     msg_list.append('TransactionOrigin: %s\r\n' %
           proxy.server.west.jc['wstc'][client_origin]['nm'])
     msg_list.append('TransactionID: %s\r\n' % session_id)
     if proxy_protocol:
         msg_list.append('X-Proxy-Protocol: %s\r\n' % proxy_protocol)
     msg_list.append('\r\n')
     msg_list.append(payload)
     msg = ''.join(msg_list)
     #
     if is_debug(1, self.west):
         print('DEBUG: sending west message length=%d' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server sending---')
             print(msg)
             print('DEBUG: ---END---')
     self.send_message(client, msg)
     self.q_request[session_id] = proxy
Example #3
0
 def reply(self, client, reqmsg, resmsg):
     msg_list = []
     msg_list.extend(["%s: %s\r\n" % (k, v) for k, v in reqmsg["wh"].iteritems()])
     msg_list.append("\r\n")
     msg_list.append(resmsg)
     msg = "".join(msg_list)
     if is_debug(1, self.west):
         print("DEBUG: response message(len=%d)" % len(msg))
         if is_debug(2, self.west):
             print("DEBUG: ---BEGIN: west client replying---")
             print(msg)
             print("DEBUG: ---END---")
     self.so.send(msg)
Example #4
0
 def reply(self, client, reqmsg, resmsg):
     msg_list = []
     msg_list.extend(['%s: %s\r\n' %
                      (k,v) for k,v in reqmsg['wh'].iteritems()])
     msg_list.append('\r\n')
     msg_list.append(resmsg)
     msg = ''.join(msg_list)
     if is_debug(1, self.west):
         print('DEBUG: response message(len=%d)' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server replying---')
             print(msg)
             print('DEBUG: ---END---')
     self.send_message(client, msg)
Example #5
0
 def reply(self, client, reqmsg, resmsg):
     msg_list = []
     msg_list.extend(
         ['%s: %s\r\n' % (k, v) for k, v in reqmsg['wh'].iteritems()])
     msg_list.append('\r\n')
     msg_list.append(resmsg)
     msg = ''.join(msg_list)
     if is_debug(1, self.west):
         print('DEBUG: response message(len=%d)' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server replying---')
             print(msg)
             print('DEBUG: ---END---')
     self.send_message(client, msg)
Example #6
0
 def go(self):
     try:
         self.start_wsts()
         self.start_wstc()
         self.start_proxy()
         self.start_cport()
     except Exception as e:
         print('ERROR:', e)
         if is_debug(1, self):
             raise
     #
     if is_debug(2, self):
         self.jc.print_state()
     #
     sock_list = []
     sock_list.extend(self.s_proxy)
     sock_list.extend(self.s_wstc)
     if self.s_wsts:
         sock_list.append(self.s_wsts)
     sock_list.append(self.s_cport)
     #
     try:
         while True:
             (r, [], []) = select.select(sock_list, [], [], self.timeout)
             for i in self.s_proxy:
                 if i in r:
                     print('INFO: received on a proxy port',
                           i.server_address)
                     i._handle_request_noblock()
             for i in self.s_wstc:
                 if i in r:
                     print('INFO: received on west client port', i)
                     i._handle_request_noblock()
             if self.s_wsts in r:
                 print('INFO: received on west server port',
                     self.s_wsts.server_address)
                 self.s_wsts._handle_request_noblock()
             if self.s_cport in r:
                 print('INFO: received on the control port',
                     self.s_cport.server_address)
                 self.s_cport._handle_request_noblock()
     except KeyboardInterrupt:
         print()
         print('INFO: terminated by keyboard interrupt.')
     except Exception as e:
         print('ERROR:', e)
         if is_debug(1, self):
             raise
Example #7
0
 def pre_process(self):
     '''
     it called just immediately after the 1st line of a request is ready to
     be read.
     '''
     #
     if is_debug(1, self.server.west):
         print('DEBUG:', self.__class__, currentThread().getName())
     print('INFO: an http client connected from %s, request %s %s' %
           (repr(self.client_address), self.command, self.path))
     #
     # check whether there is a socket for the peer.
     # it it checked again later.
     #
     if not self.server.s_ws:
         print('ERROR: The peer proxy is not available.')
         self.send_error(503)
         self.end_headers()
         return False
     #
     # check whether the incoming url can be converted into a outgoing url.
     # before it read the whole message from the cient.
     #
     if (not self.server.jc_mine.has_key(self.path)
             or not self.server.jc_mine[self.path].has_key('ou')):
         print('ERROR: no url mapping for %s' % self.path)
         self.send_error(404)
         self.end_headers()
         return False
     return True
Example #8
0
 def pre_process(self):
     '''
     it called just immediately after the 1st line of a request is ready to
     be read.
     '''
     #
     if is_debug(1, self.server.west):
         print('DEBUG:', self.__class__, currentThread().getName())
     print('INFO: an http client connected from %s, request %s %s' %
           (repr(self.client_address), self.command, self.path))
     #
     # check whether there is a socket for the peer.
     # it it checked again later.
     #
     if not self.server.s_ws:
         print('ERROR: The peer proxy is not available.')
         self.send_error(503)
         self.end_headers()
         return False
     #
     # check whether the incoming url can be converted into a outgoing url.
     # before it read the whole message from the cient.
     #
     if (not self.server.jc_mine.has_key(self.path) or
             not self.server.jc_mine[self.path].has_key('ou')):
         print('ERROR: no url mapping for %s' % self.path)
         self.send_error(404)
         self.end_headers()
         return False
     return True
Example #9
0
 def handshake(self):
     '''
     override the original WebSocketHandler.handshake().
     to get the origin name from the WS request.
     to add some debug codes.
     '''
     try:
         message = self.request.recv(1024).decode().strip()
         if is_debug(2, self.server.west):
             print('DEBUG: ---BEGIN: west server received handshake---')
             print(message)
             print('DEBUG: ---END---')
         headers = self.parse_request(message)
         upgrade = headers.get('upgrade')
         if upgrade != 'websocket':
             self.keep_alive = False
             print('INFO: not for the west connection from',
                   self.client_address)
             return
         key = headers.get('sec-websocket-key')
         if not key:
             print("Client tried to connect but was missing a key")
             self.keep_alive = False
             return
         self.origin = headers.get('origin', '')
         if not self.origin:
             print('WARNING: Client does not send the origin.')
         #
         response = self.make_handshake_response(key)
         self.handshake_done = self.request.send(response.encode())
         self.valid_client = True
         self.server._new_client_(self)
     except Exception as e:
         print('ERROR: WebSocketHandler:', e)
         raise
Example #10
0
 def handshake(self):
     '''
     override the original WebSocketHandler.handshake().
     to get the origin name from the WS request.
     to add some debug codes.
     '''
     try:
         message = self.request.recv(1024).decode().strip()
         if is_debug(2, self.server.west):
             print('DEBUG: ---BEGIN: west server received handshake---')
             print(message)
             print('DEBUG: ---END---')
         headers = self.parse_request(message)
         upgrade = headers.get('upgrade')
         if upgrade != 'websocket':
             self.keep_alive = False
             print('INFO: not for the west connection from',
                   self.client_address)
             return
         key = headers.get('sec-websocket-key')
         if not key:
             print("Client tried to connect but was missing a key")
             self.keep_alive = False
             return
         self.origin = headers.get('origin', '')
         if not self.origin:
             print('WARNING: Client does not send the origin.')
         #
         response = self.make_handshake_response(key)
         self.handshake_done = self.request.send(response.encode())
         self.valid_client = True
         self.server._new_client_(self)
     except Exception as e:
         print('ERROR: WebSocketHandler:', e)
         raise
Example #11
0
 def remove_proxy_server_callback(self, pss, client):
     client_name = client['origin']
     if is_debug(2, self):
         print('DEBUG: removing proxy callback for west client %s' %
               client_name)
     for i in self.s_proxy:
         if i.jc_mine.has_key('en') and i.jc_mine['en'] == client_name:
             if not i.s_ws:
                 print('ERROR: proxy %s has not a callback' %
                       i.pss_name)
                 return False
             i.s_ws = None
             if self.jc['wstc'][client_name].has_key('so'):
                 self.jc['wstc'][client_name]['so'] = 'unconnected'
             if is_debug(2, self):
                 print('DEBUG: remove callback of proxy %s' % i.pss_name)
     return True
Example #12
0
 def send(self, proxy, payload, session_id, proxy_protocol=""):
     msg_list = []
     msg_list.append("TransactionOrigin: %s\r\n" % self.jc_mine["nm"])
     msg_list.append("TransactionID: %s\r\n" % session_id)
     if proxy_protocol:
         msg_list.append("X-Proxy-Protocol: %s\r\n" % proxy_protocol)
     msg_list.append("\r\n")
     msg_list.append(payload)
     msg = "".join(msg_list)
     #
     if is_debug(1, self.west):
         print("DEBUG: sending west message length=%d" % len(msg))
         if is_debug(2, self.west):
             print("DEBUG: ---BEGIN: west client sending---")
             print(msg)
             print("DEBUG: ---END---")
     self.so.send(msg)
     self.q_request[session_id] = proxy
Example #13
0
 def update_proxy_server_callback(self, pss, client):
     client_name = client['origin']
     if is_debug(2, self):
         print('DEBUG: updating proxy callback for west client %s' %
               client_name)
     for i in self.s_proxy:
         if i.jc_mine.has_key('en') and i.jc_mine['en'] == client_name:
             if i.s_ws:
                 #
                 # simply update the callback because a west client might not
                 # terminate the last session normally.
                 #
                 print('WARNING: proxy %s has a callback' % i.pss_name)
             i.s_ws = pss
             self.jc['wstc'][client_name]['so'] = client['address']
             if is_debug(2, self):
                 print('DEBUG: update callback of proxy %s' % i.pss_name)
     return True
Example #14
0
 def send(self, proxy, payload, session_id, proxy_protocol=''):
     msg_list = []
     msg_list.append('TransactionOrigin: %s\r\n' % self.jc_mine['nm'])
     msg_list.append('TransactionID: %s\r\n' % session_id)
     if proxy_protocol:
         msg_list.append('X-Proxy-Protocol: %s\r\n' % proxy_protocol)
     msg_list.append('\r\n')
     msg_list.append(payload)
     msg = ''.join(msg_list)
     #
     if is_debug(1, self.west):
         print('DEBUG: sending west message length=%d' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west client sending---')
             print(msg)
             print('DEBUG: ---END---')
     self.so.send(msg)
     self.q_request[session_id] = proxy
Example #15
0
 def _handle_request_noblock(self):
     try:
         msg = self.so.recv()
     except Exception as e:
         print('ERROR:', e)
         raise
     if is_debug(1, self.west):
         print('DEBUG: received wst message length=%d' % len(msg))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west client received---')
             print(msg)
             print('DEBUG: ---END---')
     reqmsg = west_parser(msg)
     if not reqmsg:
         raise
     #
     t_origin = reqmsg['wh'].get('TransactionOrigin')
     if not t_origin:
         print('ERROR: TransactionOrigin does not exist.')
         raise ValueError
     t_id = reqmsg['wh'].get('TransactionID')
     if not t_id:
         print('ERROR: TransactionID does not exist.')
         return ValueError
     #
     proxy = self.q_request.get(t_id)
     if proxy:
         #
         # response from the west server.
         #
         proxy.reply(t_id, reqmsg['hr'], reqmsg['hh'], reqmsg['hc'])
         self.q_request.pop(t_id)
         return
     #
     # new request from the west client.
     #
     if is_debug(1, self.west):
         print('DEBUG: new west request: %s, %s' % (t_origin, t_id))
     #
     http_proxy_http_client_thread(None,
                                   self,
                                   reqmsg,
                                   debug_level=self.west.jc['debug_level'])
Example #16
0
 def _handle_request_noblock(self):
     try:
         msg = self.so.recv()
     except Exception as e:
         print("ERROR:", e)
         raise
     if is_debug(1, self.west):
         print("DEBUG: received wst message length=%d" % len(msg))
         if is_debug(2, self.west):
             print("DEBUG: ---BEGIN: west client received---")
             print(msg)
             print("DEBUG: ---END---")
     reqmsg = west_parser(msg)
     if not reqmsg:
         raise
     #
     t_origin = reqmsg["wh"].get("TransactionOrigin")
     if not t_origin:
         print("ERROR: TransactionOrigin does not exist.")
         raise ValueError
     t_id = reqmsg["wh"].get("TransactionID")
     if not t_id:
         print("ERROR: TransactionID does not exist.")
         return ValueError
     #
     proxy = self.q_request.get(t_id)
     if proxy:
         #
         # response from the west server.
         #
         proxy.reply(t_id, reqmsg["hr"], reqmsg["hh"], reqmsg["hc"])
         self.q_request.pop(t_id)
         return
     #
     # new request from the west client.
     #
     if is_debug(1, self.west):
         print("DEBUG: new west request: %s, %s" % (t_origin, t_id))
     #
     http_proxy_http_client_thread(None, self, reqmsg, debug_level=self.west.jc["debug_level"])
Example #17
0
 def message_received(self, client, server, msg):
     if is_debug(1, self.west):
         print('DEBUG: received data len=%d from %s id=%d' %
               (len(msg), repr(client), client['id']))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server received---')
             print(msg)
             print('DEBUG: ---END---')
     reqmsg = west_parser(msg)
     if not reqmsg:
         raise ValueError
     #
     t_origin = reqmsg['wh'].get('TransactionOrigin')
     if not t_origin:
         print('ERROR: TransactionOrigin does not exist.')
         raise ValueError
     t_id = reqmsg['wh'].get('TransactionID')
     if not t_id:
         print('ERROR: TransactionID does not exist.')
         return ValueError
     #
     proxy = self.q_request.get(t_id)
     if proxy:
         #
         # response from the west client.
         #
         proxy.reply(t_id, reqmsg['hr'], reqmsg['hh'], reqmsg['hc'])
         self.q_request.pop(t_id)
         return
     #
     # new request from the west client.
     #
     if is_debug(1, self.west):
         print('DEBUG: new west request: %s, %s' % (t_origin, t_id))
     #
     http_proxy_http_client_thread(client,
                                   server,
                                   reqmsg,
                                   debug_level=self.west.jc['debug_level'])
Example #18
0
 def message_received(self, client, server, msg):
     if is_debug(1, self.west):
         print('DEBUG: received data len=%d from %s id=%d' % (
                 len(msg), repr(client), client['id']))
         if is_debug(2, self.west):
             print('DEBUG: ---BEGIN: west server received---')
             print(msg)
             print('DEBUG: ---END---')
     reqmsg = west_parser(msg)
     if not reqmsg:
         raise ValueError
     #
     t_origin = reqmsg['wh'].get('TransactionOrigin')
     if not t_origin:
         print('ERROR: TransactionOrigin does not exist.')
         raise ValueError
     t_id = reqmsg['wh'].get('TransactionID')
     if not t_id:
         print('ERROR: TransactionID does not exist.')
         return ValueError
     #
     proxy = self.q_request.get(t_id)
     if proxy:
         #
         # response from the west client.
         #
         proxy.reply(t_id, reqmsg['hr'], reqmsg['hh'], reqmsg['hc'])
         self.q_request.pop(t_id)
         return
     #
     # new request from the west client.
     #
     if is_debug(1, self.west):
         print('DEBUG: new west request: %s, %s' % (t_origin, t_id))
     #
     http_proxy_http_client_thread(client, server, reqmsg,
                                   debug_level=self.west.jc['debug_level'])
Example #19
0
 def reply(self, session_id, firstline, headers, content):
     if session_id != self.session_id:
         print('ERROR: unexpected session_id %s, should be %s', session_id,
               self.session_id)
         self.waiting_for_server.set()
         return
     try:
         #
         # failed
         #
         if firstline[1] != '200':
             print('ERROR: http error code %s' % firstline[1])
             self.send_error(int(firstline[1]))
             self.end_headers()
             self.waiting_for_server.set()
             return
         #
         # success
         #
         if is_debug(1, self.server.west):
             print('DEBUG: sending back message length=%d' % len(content))
             if is_debug(2, self.server.west):
                 print('DEBUG: ---BEGIN: proxy server replying---')
                 print(''.join(
                     ['%s: %s\n' % (k, v) for k, v in headers.iteritems()]))
                 print(content)
                 print('DEBUG: ---END---')
         self.send_response(200)
         for k, v in headers.iteritems():
             self.send_header(k, v)
         self.end_headers()
         self.wfile.write(content)
         self.wfile.write('\r\n')  # just make it sure
     except Exception:
         print(traceback.format_exc())
     self.waiting_for_server.set()
Example #20
0
 def reply(self, session_id, firstline, headers, content):
     if session_id != self.session_id:
         print('ERROR: unexpected session_id %s, should be %s',
                 session_id, self.session_id)
         self.waiting_for_server.set()
         return
     try:
         #
         # failed
         #
         if firstline[1] != '200':
             print('ERROR: http error code %s' % firstline[1])
             self.send_error(int(firstline[1]))
             self.end_headers()
             self.waiting_for_server.set()
             return
         #
         # success
         #
         if is_debug(1, self.server.west):
             print('DEBUG: sending back message length=%d' % len(content))
             if is_debug(2, self.server.west):
                 print('DEBUG: ---BEGIN: proxy server replying---')
                 print(''.join(['%s: %s\n' %
                                (k, v) for k, v in headers.iteritems()]))
                 print(content)
                 print('DEBUG: ---END---')
         self.send_response(200)
         for k, v in headers.iteritems():
             self.send_header(k, v)
         self.end_headers()
         self.wfile.write(content)
         self.wfile.write('\r\n')    # just make it sure
     except Exception:
         print(traceback.format_exc())
     self.waiting_for_server.set()
Example #21
0
 def post_read(self, contents):
     '''
     it called after the whole message from the client has been read.
     '''
     #
     # convert Path into the outgoing URL, and Host into the HTTP server.
     #
     a = inet_string(self.server.jc_mine[self.path]['ou'])
     self.headers = dict(self.headers)
     if self.headers.has_key('host'):
         if is_debug(2, self.server.west):
             print('DEBUG: convert Host into %s from %s' %
                   (self.headers['host'], a['url_host']))
         self.headers['host'] = a['url_host']
     if is_debug(2, self.server.west):
         print('DEBUG: convert Path into %s from %s' %
               (self.path, a['url_path']))
     #
     msg_list = []
     msg_list.append(' '.join(
         [self.command, a['url_path'], self.request_version]))
     msg_list.append('\r\n')
     msg_list.extend(
         ['%s: %s\r\n' % (k, v) for k, v in self.headers.iteritems()])
     msg_list.append('\r\n')
     msg_list.extend(contents)
     #
     msg = ''.join(msg_list)
     if is_debug(1, self.server.west):
         print('DEBUG: sending proxy message length=', len(msg))
         if is_debug(2, self.server.west):
             print('DEBUG: ---BEGIN: proxy server sending---')
             print(msg)
             print('DEBUG: ---END---')
     #
     '''
     http sessions must be identified.
     each session must be assigned uuid.
     chunked data have to be assiened an identical uuid.
     '''
     self.session_id = str(uuid.uuid4())
     #
     # double check again whether there is a socket for the peer.
     #
     if self.server.s_ws:
         response = self.server.s_ws.send(self,
                                          msg,
                                          self.session_id,
                                          proxy_protocol='http')
     else:
         print('ERROR: The peer proxy is not available.')
         self.send_error(503)
         self.end_headers()
         return
     #
     # waiting for a response from the server
     #
     if is_debug(1, self.server.west):
         print(
             'DEBUG: waiting for response from http server in %d seconds' %
             self.server_response_timer)
     self.waiting_for_server = Event()
     self.waiting_for_server.wait(self.server_response_timer)
     if not self.waiting_for_server.is_set():
         print('INFO: timeout for waiting a response from http server')
Example #22
0
 def post_read(self, contents):
     '''
     it called after the whole message from the client has been read.
     '''
     #
     # convert Path into the outgoing URL, and Host into the HTTP server.
     #
     a = inet_string(self.server.jc_mine[self.path]['ou'])
     self.headers = dict(self.headers)
     if self.headers.has_key('host'):
         if is_debug(2, self.server.west):
             print('DEBUG: convert Host into %s from %s' %
                   (self.headers['host'], a['url_host']))
         self.headers['host'] = a['url_host']
     if is_debug(2, self.server.west):
         print('DEBUG: convert Path into %s from %s' %
               (self.path, a['url_path']))
     #
     msg_list = []
     msg_list.append(' '.join([self.command, a['url_path'],
                               self.request_version]))
     msg_list.append('\r\n')
     msg_list.extend(['%s: %s\r\n' %
                      (k,v) for k,v in self.headers.iteritems()])
     msg_list.append('\r\n')
     msg_list.extend(contents)
     #
     msg = ''.join(msg_list)
     if is_debug(1, self.server.west):
         print('DEBUG: sending proxy message length=', len(msg))
         if is_debug(2, self.server.west):
             print('DEBUG: ---BEGIN: proxy server sending---')
             print(msg)
             print('DEBUG: ---END---')
     #
     '''
     http sessions must be identified.
     each session must be assigned uuid.
     chunked data have to be assiened an identical uuid.
     '''
     self.session_id = str(uuid.uuid4())
     #
     # double check again whether there is a socket for the peer.
     #
     if self.server.s_ws:
         response = self.server.s_ws.send(self, msg, self.session_id,
                                         proxy_protocol='http')
     else:
         print('ERROR: The peer proxy is not available.')
         self.send_error(503)
         self.end_headers()
         return
     #
     # waiting for a response from the server
     #
     if is_debug(1, self.server.west):
         print('DEBUG: waiting for response from http server in %d seconds' %
               self.server_response_timer)
     self.waiting_for_server = Event()
     self.waiting_for_server.wait(self.server_response_timer)
     if not self.waiting_for_server.is_set():
         print('INFO: timeout for waiting a response from http server')