Ejemplo n.º 1
0
def db_send(db, num):

    ts = str(time.time())

    comms.database_log([db, num, ts])

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                       socket.IPPROTO_UDP) as s:
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
        s.sendto(
            aes_crypt.aes_enc(rsa_encrypt.get_pub_key_auth(),
                              "DBUP:" + db + ":" + num + ":" + ts),
            (settings.MULT_ADDR, settings.MULT_PORT))

    comms.send_clients()
Ejemplo n.º 2
0
def get_nodes():

    print("Grabbing list of active nodes")
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('0.0.0.0', 55559))
        s.listen(1)

        try:
            auth_update.challenge(my_number, "updN")

        except:
            s.close()
            print("No auth node found")
            return

        cli, addr = s.accept()

        #Create empty data string
        data = b""

        #Recieve until done
        try:
            while 1 == 1:
                temp = cli.recv(4096)
                if temp and len(temp) == 4096:
                    data += temp
                else:
                    break
            data += temp

        #if the connection dies
        except:
            #Return no updates
            return 0

        #close the socket
        cli.close()

        data = json.loads(str(data, 'ascii'))
        for i in data['payload']:
            comms.database_log([i['type'], i['id'], i['timestamp']])
Ejemplo n.º 3
0
def handle_response(data, address, keys, dbkeys):

    #Decrypt message and validate
    data = aes_crypt.aes_dec(rsa_encrypt.get_priv_key_auth(), data)

    #invalid data is ignored
    if data == -1 or data == -2:
        return

    #split the message and determine how to respond to it
    data = str(data, 'ascii').split(":")

    #Node is sending share for authentication
    if data[0] == "auth":
        add_secret(data[1:])

    #Node is sending db info
    elif data[0] == "DBUP":
        if data[1] in settings.DBS + ["auth"]:
            comms.database_log(data[1:])

    #Node needs an auth node, so the auth contest is started using a node public key
    elif data[0] == "who?":
        contest(address[0], data[1], keys)

    #If node is asking to register a user via web
    elif data[0] == "usrW":

        #register the user
        webreg(data[1:])

    #An auth node has woken up, so the auth contest is started with the auth public key
    elif data[0] == "regA":
        if not data[1] == my_number:
            contest_auth(address[0])

    #Recieve an update when a user is registered or deleted, unless it comes from this node
    elif data[0] == "here":
        #dont recieve a share sent by self
        if not int(data[1]) == my_number:
            recv_update(data[2], address[0])

    #A node has picked an auth node to use, check if it is this server
    elif data[0] == "you!":
        if int(data[1]) == my_number:

            #if asking for database info
            if data[2] == "sndC":
                comms.send_clients()

            #if asking for user info
            if data[2] == "sndU":
                comms.send_users()

            if data[2] == "sndA":
                comms.send_both()

            if data[2] == "updN":
                comms.send_clients_full(address[0])

            #respond to startup update for client node
            elif data[2] == "imup":

                print("Sending Update to Client Node")
                register_node(data[3:], address, keys, dbkeys)

            #respond to startup update for auth node
            elif data[2] == "woke":

                print("Sending Update to Auth Node")
                auth_update.updater(address[0])