1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
# Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
import socket
import threading
import time
import random
import string
from SocketServer import TCPServer
from SocketServer import BaseRequestHandler
class TCPCommandListener(threading.Thread):
"""
This listener is to use a SocketServer.TCPServer on the logic for listening commands
the advantage of this version is that is basically a server shipped with python so
more error prone.
It requires the implementation of a Handler class which will contain all the handshaking
logic that was implemented on the original SocketServer (below).
There's a specific TODO on this version: need to find the way to tell the server to stop
once the command execution has completed.
"""
def __init__(self, output_handler):
threading.Thread.__init__(self)
self.handshake = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
self.close_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
self._server = TCPServer(('127.0.0.1', 0), lambda r, c, s: HandShakeHandler(r, c, s, (self.handshake, self.close_key), output_handler), False)
self._server.server_bind()
self.port = self._server.server_address[1]
def run(self):
self._server.server_activate()
self._server.serve_forever()
class HandShakeHandler(BaseRequestHandler):
"""
This is the command handler needed when using the TCPServer distributed in the python standard library
It is basically a port of the logic contained on the initial SocketServer class (below) that was
created for the management of the command executions.
"""
def __init__(self, request, client_address, server, handshake_keys, output_handler):
self._handshake = handshake_keys[0]
self._close_key = handshake_keys[1]
# If no handshake, authentication is assummed
self._wait_for_right_client = True if self._handshake else False
self._client_connected = False
self._pinger = None
self._closed_by_client = False
self._output_handler = output_handler
BaseRequestHandler.__init__(self, request, client_address, server)
def authenticate_client(self, handshake):
if handshake == self._handshake:
self.request.send("OK")
self._wait_for_right_client = False
else:
self.request.send("ERROR")
self.request.close()
self._client_connected = False
def _ping(self):
if self._client_connected:
try:
# When client still connected this should succeed
self.request.send(".")
self._pinger = threading.Timer(1, self._ping)
self._pinger.start()
except Exception, e:
self.exit_status = 1
self.exit_message = repr(e)
self._client_connected = False
# TODO: Find the correct way to shutdown the server once
# it has completed...
#self.server.shutdown_request(self.request)
#self.server.shutdown()
def handle(self):
# The server will be waiting for client connections while nothing indicates
# The stablished criteria to stop the server are:
# - When the thread is stopped
# - When a connected client sends the close_key
# - When authentication occurred (the right client was connected) and the connection got closed
keep_alive = True
self._pinger = threading.Timer(1, self._ping)
self._pinger.start()
# Once a client is connected the server will process the incoming data
# There are three scenarios:
# - The incoming data is a handshake to identify the server should listen that
# specific client
# - The incoming message is a message indicating the client has disconnected
# - The incoming message is a message indicating the server should STOP listening
# - The incoming message is just data that needs processing
self._client_connected = True
while self._client_connected:
try:
data = self.request.recv(1024)
if data:
# If authentication is needed
if self._wait_for_right_client:
self.authenticate_client(data)
# The client has explicitly indicated its processing
# is done
elif self._close_key and self._close_key == data:
self._closed_by_client = True
self._client_connected = False
# TODO: Find the correct way to shutdown the server once
# it has completed...
#self.server.shutdown_request(self.request)
#self.server.shutdown()
else:
self.process_data(data)
except Exception, e:
# Error 10054 indicates the client connection terminated
print "EXCEPTION : ", e
#if e.errno == 10054:
# self._client_connected = False
#else:
# raise
def process_data(self, data):
self._output_handler(data)
class SocketServer(threading.Thread):
"""
Custom socket server class handling:
- optional handshake with connected client
- optional server shutdown based on close_key received by the client
This class is currently used to manage the command execution through the
admin helper script which communicates to this server by using an instance of
the SocketClient class below
"""
def __init__(self, host, port, handshake = "", close_key = ""):
threading.Thread.__init__(self)
self.host = host
self.port = port
self._handshake = handshake
self._close_key = close_key
# If no handshake, authentication is assummed
self._wait_for_right_client = True if self._handshake else False
self._client_connected = False
self._pinger = None
self._closed_by_client = False
self._bound = False
self.exit_status = 0
self.exit_message = ""
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def bind(self, port, catch_error = True):
self.port = port
try:
self._socket.bind((self.host, self.port))
self.port = self._socket.getsockname()
self._bound = True
except Exception, e:
self.exit_status = 1
self.exit_message = repr(e)
def authenticate_client(self, handshake):
if handshake == self._handshake:
self._connection.send("OK")
self._wait_for_right_client = False
else:
self._connection.send("ERROR")
self._connection.close()
self._client_connected = False
def _ping(self):
if self._client_connected:
try:
# When client still connected this should succeed
self._connection.send(".")
self._pinger = threading.Timer(1, self._ping)
self._pinger.start()
except Exception, e:
self.exit_status = 1
self.exit_message = repr(e)
self._client_connected = False
def run(self):
try:
if not self._bound:
self.bind(self.port, False)
# The server will be waiting for client connections while nothing indicates
# The stablished criteria to stop the server are:
# - When the thread is stopped
# - When a connected client sends the close_key
# - When authentication occurred (the right client was connected) and the connection got closed
keep_alive = True
while self._wait_for_right_client:
self._socket.listen(1)
self._connection, self._address = self._socket.accept()
# At this point a client is connected
self._client_connected = True
self._pinger = threading.Timer(1, self._ping)
self._pinger.start()
# Once a client is connected the server will process the incoming data
# There are three scenarios:
# - The incoming data is a handshake to identify the server should listen that
# specific client
# - The incoming message is a message indicating the client has disconnected
# - The incoming message is a message indicating the server should STOP listening
# - The incoming message is just data that needs processing
while self._client_connected:
try:
data = self._connection.recv(1024)
if data:
# If authentication is needed
if self._wait_for_right_client:
self.authenticate_client(data)
# The client has explicitly indicated its processing
# is done
elif self._close_key and data.find(self._close_key) == 0:
self._closed_by_client = True
self._client_connected = False
self._connection.close()
# The closing key comes in the format of:
# <close_key> <status>[ <message>]
# Where:
# - close_key is the token generated on this class to notify it that the server should be stopped
# - status : 0 if all ended OK on the client. Non 0 value on other case
# - message: information about the ocurred error
key, return_data = data.split(' ', 1)
if not return_data.startswith('0'):
ret_code, error = return_data.split(' ', 1)
self.exit_status = 1
self.exit_message = error
else:
self.process_data(data)
except Exception, e:
self.exit_status = 1
self.exit_message = repr(e)
self._client_connected = False
# Any error starting the server will be printed
except socket.error, e:
self.exit_status = 1
self.exit_message = repr(e)
def process_data(self, data):
pass
class CustomCommandListener(SocketServer):
"""
This is the implementationn of a socket server which will be listening for commands
executed as admin in local windows, the output is received through a socket
it is used only when the output is required and all the received data will be sent
to the configured output handler
"""
def __init__(self, output_handler):
self.handshake = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
self.close_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
SocketServer.__init__(self, '127.0.0.1', 0, self.handshake, self.close_key)
SocketServer.bind(self, 0)
self.host, self.port = self._socket.getsockname()
self._handler = output_handler
def process_data(self, data):
self._handler(data)
class SocketClient(object):
"""
Custom socket client class handling:
- optional handshake with server
- optional server shutdown based on close_key sent by the client
"""
def __init__(self, host, port, handshake = "", close_key = ""):
self._host = host
self._port = port
self._handshake = handshake
self._close_key = close_key
self._connected = False
self._authenticated = None
def start(self):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._socket.connect((self._host, self._port))
self._connected = True
# If a handshake is needed the client sends it and waits
# for the server's answer
if self._handshake:
self._socket.send(self._handshake)
# Validates the server response
validated = None
while self._authenticated is None:
response = self._socket.recv(1024)
if response:
self._authenticated = (response == "OK")
if not self._authenticated:
self._connected = False
self.close()
except socket.error, e:
# Connection was not done, probably target is not listening
# This error just gets printed as there's no way to tell the
# server
if e.errno == 10061:
print e
return self._connected
def send(self, data):
if self._connected:
self._socket.send(data)
def close(self, exit_status = 0, msg = ""):
if self._connected:
# The server is waiting to receive the closing token alone
# This delay is inserted so the server reads any pending data
time.sleep(2)
if self._close_key and self._authenticated:
closing_message = "%s %d %s" % (self._close_key, exit_status, msg)
closing_message.strip()
self._socket.send(closing_message)
self._socket.close()
self._connected = False
|