def run(self): # Get the 3 keys from the client client_pub_a = pickle.loads(recv_data(conn)).get_msg() client_pub_b = pickle.loads(recv_data(conn)).get_msg() client_pub_c = pickle.loads(recv_data(conn)).get_msg() # Generate private keys and public keys with same large prime and # primitive root private_a_server = random.randint(1, client_pub_a.q - 1) server_pub_a = PubKey(client_pub_a.q, client_pub_a.alpha) server_pub_a.gen_pub_key(private_a_server) private_b_server = random.randint(1, client_pub_b.q - 1) server_pub_b = PubKey(client_pub_b.q, client_pub_b.alpha) server_pub_b.gen_pub_key(private_b_server) private_c_server = random.randint(1, client_pub_c.q - 1) server_pub_c = PubKey(client_pub_c.q, client_pub_c.alpha) server_pub_c.gen_pub_key(private_c_server) # Generate shared keys shared_a = sharedKey(client_pub_a, private_a_server) shared_b = sharedKey(client_pub_b, private_b_server) shared_c = sharedKey(client_pub_c, private_c_server) # Send the 3 public keys of server to client hdr = Hdr(10, settings.TCP_PORT, self.port) msg_a = Msg(hdr, server_pub_a) msg_b = Msg(hdr, server_pub_b) msg_c = Msg(hdr, server_pub_c) send_data(conn, pickle.dumps(msg_a)) send_data(conn, pickle.dumps(msg_b)) send_data(conn, pickle.dumps(msg_c)) while True: # serve the client according to the requests arrived msg = pickle.loads(recv_data(conn)) if msg.type() == "REQSERV": file_name = msg.get_msg() # now check if this file is present in the file system if os.path.isfile( 'data/' + file_name): # Check if file available in the server send_file(conn, 'data/' + file_name, settings.TCP_PORT, self.port, [shared_a, shared_b, shared_c]) else: # send disconnect file_not_present(conn, settings.TCP_PORT, self.port) if msg.type() == "DISCONNECT": # Disconnect this client from the server print("Client disconnected from the server") break
def _prod_heartbeat(self): logging.info('produce heartbeat...') body = { 'datetime': datetime.utcnow(), 'config_version': self._config.version } hb_msg = Msg.create_msg(self._agentid, Msg.A_HEARTBEAT, body) self._agent.add_msg(hb_msg)
def _collect_service(self, service): """ Collect defined metrics from service :param service: service info :return: collected result in dict """ logging.debug('collect service : %s', service) name = service['name'] stype = service.get('type', None) lookup = service['lookup_keyword'] puser, pid, penvs = process_info(lookup) if not pid: logging.info('can not find process "%s" by "%s".', name, lookup) return metric_names = service['metrics'] clocks = service['clocks'] env = service.get('env', {}) # interpret configured env with process envs for k, v in env.items(): env[k] = interpret_exp(v, penvs) env['pid'] = pid env['puser'] = puser logging.info( 'start to collect metrics for service [%s(%s)]: metrics=%s, clocks=%s.', name, pid, metric_names, clocks) service_result = { 'name': name, 'pid': pid, 'puser': puser, 'type': stype } service_metrics = {} for mname in metric_names: try: metric = self._config.service_metrics[mname] ocmd = metric['cmd'] logging.info('collect %s by %s', mname, ocmd) cmd = self._translate_cmd(ocmd, env) logging.info('command translated to %s', cmd) if not is_metric_valid(metric): logging.info('cmd %s is not a valid command, try next', cmd[0]) continue service_metrics[mname] = self._get_cmd_result(cmd) except Exception: logging.exception( 'collect metrics %s for service %s failed: cmd=%s', mname, name, ocmd) service_result['metrics'] = service_metrics # send message msg = Msg.create_msg(self._agentid, Msg.A_SERVICE_METRIC, service_result) msg.set_header(Msg.H_COLLECT_AT, datetime.utcnow()) self._agent.add_msg(msg) logging.info('%d metrics collected for %s.', len(service_metrics), name) return service_result
def _collect_nmetrics(self, loops): """ Collect node metrics :param loops: current loops """ logging.info('try to collecting node metrics, loops=%d', loops) nmetrics_result = {} for nm in self._config.valid_node_metrics: if loops % nm.get('clocks', 6) == 0: nmetrics_result[nm['name']] = self._get_cmd_result(nm['cmd']) if nmetrics_result: msg = Msg.create_msg(self._agentid, Msg.A_NODE_METRIC, nmetrics_result) msg.set_header(msg.H_COLLECT_AT, datetime.utcnow()) self._agent.add_msg(msg) logging.info('%d node metrics collected', len(nmetrics_result)) else: logging.info('no metric collected ')
def _agent_heartbeat(self, msg): agent = self.find_agent(msg.agentid) agent.set(last_msg_at=datetime.utcnow()) config_version = msg.body.get('config_version', None) curr_config = self._load_agent_config() curr_config_version = curr_config['version'] if config_version is None or curr_config_version > config_version: logging.info( 'current config version is %s, agent %s config version is %s, will update config to agent.', curr_config_version, agent.name, config_version) agent_addr = self._agent_addrs.get(msg.agentid) if agent_addr is None: logging.warn( 'can not find agnet addr for %s, config update msg will be disgarded', msg.agentid) else: cfgmsg = Msg.create_msg(msg.agentid, Msg.M_CONFIG_UPDATE, {'config': curr_config}) self._agent_manager.pub_msg(agent_addr, cfgmsg) logging.info('config message %s send to %s', cfgmsg, agent_addr) logging.debug('heart beat get from %s', agent) return True
# Create 3 public keys to be shared with the server a, b, c = PubKey(), PubKey(), PubKey() private_a = random.randint(1, a.q - 1) # Random number between 1 and a.q - 1 private_b = random.randint(1, b.q - 1) # Random number between 1 and b.q - 1 private_c = random.randint(1, c.q - 1) # Random number between 1 and c.q - 1 a.gen_pub_key(private_a) b.gen_pub_key(private_b) c.gen_pub_key(private_c) # Create header for public key sharing hdr = Hdr(settings.PUBKEY, tcpClientA.getsockname(), port) msg_a = Msg(hdr, a) msg_b = Msg(hdr, b) msg_c = Msg(hdr, c) send_data(tcpClientA, pickle.dumps(msg_a)) send_data(tcpClientA, pickle.dumps(msg_b)) send_data(tcpClientA, pickle.dumps(msg_c)) # Recieve server public keys server_pub_a = pickle.loads(recv_data(tcpClientA)).get_msg() server_pub_b = pickle.loads(recv_data(tcpClientA)).get_msg() server_pub_c = pickle.loads(recv_data(tcpClientA)).get_msg() # Generate shared keys at client shared_a = sharedKey(server_pub_a, private_a) shared_b = sharedKey(server_pub_b, private_b)
def _do_reg(self): """Produce a agent reg message after connected""" logging.info('do registration...') reg_data = {'os': ostype(), 'hostname': self._hostname} reg_msg = Msg.create_msg(self._agentid, Msg.A_REG, reg_data) self.add_msg(reg_msg)