Пример #1
0
def run():

    #Grab database keys and device keys
    keys = rsa_encrypt.get_keys_nodes()
    dbkeys = rsa_encrypt.get_keys(settings.DBS)

    #initialize database for comms
    comms.initialize_db()

    #initialize shares db
    initialize_db()

    #add self to database
    db_send("auth", str(comms_number))

    #Run the auth node update process which is required for the server to start properly
    print("Looking for updates")
    auth_update.updateee(my_number)
    threading.Thread(target=timer_update_start).start()

    #Grab all node entries for our webUI
    get_nodes()

    #Set up multicast listener
    address = settings.MULT_ADDR
    port = settings.MULT_PORT
    tup = ('', port)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(tup)
    group = socket.inet_aton(address)
    mreq = struct.pack('4sL', group, socket.INADDR_ANY)
    s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

    #Start the listener for local inserts and deletes
    threading.Thread(target=broadcast_socket).start()

    #Officialy start the server
    while 1 == 1:
        sys.stdout.flush()
        #grab data and sender from the multicast address
        #continue if error in recv
        try:
            data, address = s.recvfrom(8192)

            #start response handler
            threading.Thread(target=handle_response,
                             args=[data, address, keys, dbkeys]).start()

        except KeyboardInterrupt:
            return
Пример #2
0
def add_shares(username, shares, keys, currtime):

    #Grab database keys
    db_keys = rsa_encrypt.get_keys(settings.DBS)

    #shares must be equal to dbs to prevent loss or oversharing
    if ((not len(shares) == len(settings.DBS))):
        return -1

    #For each database
    for i in range(len(settings.DBS)):

        #initiate database connection
        conn = sqlite3.connect(settings.DBdir + settings.DBS[i] + ".db")
        c = conn.cursor()

        #make sure the shares table exists
        create = "CREATE TABLE IF NOT EXISTS enc_shares(id PRIMARY KEY, share, timestamp DOUBLE)"
        c.execute(create)

        #Convert share data to a string
        payload = username + ":" + str(shares[i][0]) + ":" + str(
            shares[i][1]) + ":" + str(keys[i])

        #Grab the database key for the current database
        k = db_keys[settings.DBS[i]].key

        #encrypt the share string with the database public key
        payload = rsa_encrypt.encrypt_str(k, payload)

        #insert or replace the encrypted share, the username, and a timestamp into the database
        c.execute("REPLACE INTO enc_shares VALUES(?, ?, ?)",
                  [username, payload, currtime])

        #commit the action and close the database
        conn.commit()
        conn.close()
    return
Пример #3
0
def add_shares(username, shares, keys, currtime):

    #Grab database keys
    db_keys = rsa_encrypt.get_keys(settings.DBS)

    #shares must be equal to dbs to prevent loss or oversharing
    if ((not len(shares) == len(settings.DBS))):
        return -1

    #For each database
    for i in range(len(settings.DBS)):

        #initiate database connection
        conn = sqlite3.connect(settings.DBdir + settings.DBS[i] + ".db")
        c = conn.cursor()

        #make sure the shares table exists
        create = "CREATE TABLE IF NOT EXISTS enc_shares(id PRIMARY KEY, share, timestamp DOUBLE)"
        c.execute(create)

        #Convert share data to a string
        payload = username + ":" + str(shares[i][0]) + ":" + str(
            shares[i][1]) + ":" + str(keys[i])

        # if the string is longer than a sha256 hash, chunk the string and encrypt each chunk individually
        if len(payload) > 240:

            # store the individual chunks
            chunks = []
            curr_len = len(payload)
            start = 0
            end = 240
            while curr_len > 240:

                # decrement the curr_len to indicate that a chunk has been encrypted
                curr_len -= 240

                # append a chunk of the payload
                chunks.append(payload[start:end])
                start += 240
                end += 240

            #append the last chunk to the chunks list
            chunks.append(payload[start:])
            k = db_keys[settings.DBS[i]].key
            chunk_payload = ""

            # encrypt each chunk in the payload
            for chunk in chunks:
                chunk_payload += rsa_encrypt.encrypt_str(k, chunk)
            c.execute("REPLACE INTO enc_shares VALUES(?, ?, ?)",
                      [username, chunk_payload, currtime])
        else:

            #Grab the database key for the current database
            k = db_keys[settings.DBS[i]].key

            #encrypt the share string with the database public key
            payload = rsa_encrypt.encrypt_str(k, payload)

            #insert or replace the encrypted share, the username, and a timestamp into the database
            c.execute("REPLACE INTO enc_shares VALUES(?, ?, ?)",
                      [username, payload, currtime])

        #commit the action and close the database
        conn.commit()
        conn.close()
    return