Beispiel #1
0
    def main(self):
        if not db.nodes.find("nodes", "all"):
            check = self.config['relay']
            if check:
                self.open_port = True
                print "You are running as a relay node."
            db.data.insert("data", {"port": self.port})
            print "Downloading Nodes..."
            self.get_nodes()
            print "Checking in to nodes..."
            self.send_checkin()
            print "Done!"

        check = self.config['relay']
        if check:
            self.open_port = True
            print "You are running as a relay node."
        else:
            print "You are not running as a relay node."
        if self.open_port:
            sock = ssl.socket()
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind((self.host, self.port))
            sock.listen(5)
            while True:
                obj, conn = sock.accept()
                threading.Thread(target=self.handle,
                                 args=(obj, conn[0])).start()
        else:
            while True:
                check = db.nodes.find("nodes", "all")
                random.shuffle(check)
                node = None
                for x in check:
                    s = ssl.socket()
                    try:
                        s.connect((x['ip'], x['port']))
                    except:
                        s.close()
                        continue
                    else:
                        node = (x['ip'], x['port'])
                        s.close()
                        break

                sock = ssl.socket()
                try:
                    sock.connect(node)
                except:
                    continue
                else:
                    while True:
                        try:
                            get_messages.send_get_messages(x['ip'], x['port'])
                            get_nodes.send_get_nodes(x['ip'], x['port'])
                            time.sleep(10)
                        except Exception, error:
                            sock.close()
                            break
Beispiel #2
0
    def main(self):
        if not db.nodes.find("nodes", "all"):
            check = self.config["relay"]
            if check:
                self.open_port = True
                print "You are running as a relay node."
            db.data.insert("data", {"port": self.port})
            print "Downloading Nodes..."
            self.get_nodes()
            print "Checking in to nodes..."
            self.send_checkin()
            print "Done!"

        check = self.config["relay"]
        if check:
            self.open_port = True
            print "You are running as a relay node."
        else:
            print "You are not running as a relay node."
        if self.open_port:
            sock = ssl.socket()
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind((self.host, self.port))
            sock.listen(5)
            while True:
                obj, conn = sock.accept()
                threading.Thread(target=self.handle, args=(obj, conn[0])).start()
        else:
            while True:
                check = db.nodes.find("nodes", "all")
                random.shuffle(check)
                node = None
                for x in check:
                    s = ssl.socket()
                    try:
                        s.connect((x["ip"], x["port"]))
                    except:
                        s.close()
                        continue
                    else:
                        node = (x["ip"], x["port"])
                        s.close()
                        break

                sock = ssl.socket()
                try:
                    sock.connect(node)
                except:
                    continue
                else:
                    while True:
                        try:
                            get_messages.send_get_messages(x["ip"], x["port"])
                            get_nodes.send_get_nodes(x["ip"], x["port"])
                            time.sleep(10)
                        except Exception, error:
                            sock.close()
                            break
Beispiel #3
0
def send_msg(msg, title, to, addr):
    try:
        data = db.nodes.find("nodes", {"addr":to})[0]
    except:
        return "Address doesn't exist"
    if data['publickey'].startswith("PublicKey(") and data['publickey'].endswith(")"):
       # msg = encrypt(msg, eval(data['publickey'])) <--- Old encryption code
	aeskey = str(uuid.uuid4().hex) # Generate new AES Key
	key = encrypt(aeskey, eval(data['publickey'])) # Encrypt AES key with target's RSA Public Key
	key = base64.b64encode(key) # Base64 encode the key
	msg = aes.encryptData(aeskey,msg) # Encrypt Message with AES Key
        msg = base64.b64encode(msg) # Base64 encode the message
	as_num, as_nonce = antispam.find_antispam(to,addr,msg,antispam.get_required_difficulty(msg))
    else:
        return "Invalid public key for", addr
    id = ""
    while True:
        id = uuid.uuid4().hex
        if db.messages.find("messages", {"id":id}):
            continue
        else:
            break
    print "Sending message ID " + id + " with key " + key
    nodes = db.nodes.find("nodes", "all")
    for x in nodes:
        s = ssl.socket()
        try:
            s.settimeout(1)
            s.connect((x['ip'], x['port']))
            s.send(json.dumps({"cmd":"message", "id":id, "message":msg, "title":title, "to":to, "from":addr,"num":as_num,"nonce":as_nonce,"key":key}))
            s.close()
        except Exception, error:
            db.unsent.insert("unsent", {"to":[x['ip'], x['port']], "message":{"cmd":"message", "id":id, "message":msg, "title":title, "to":to, "from":addr,"num":as_num,"nonce":as_nonce,"key":key}})
Beispiel #4
0
 def ssl_connect(self,
                 min_version=ssl.TLSVersion.TLSv1_1,
                 max_version=ssl.TLSVersion.TLSv1_3,
                 ciphers=None,
                 cert=None,
                 key=None,
                 ca_cert=None):
     context = ssl.SSLContext(ssl.PROTOCOL_TLS)
     context.minimum_version = min_version
     context.maximum_version = max_version
     if ciphers:
         context.set_ciphers(ciphers)
     if not ca_cert:
         ca_cert = self.ca_cert
     context.load_verify_locations(ca_cert)
     if cert and key:
         context.load_cert_chain(certfile=cert,
                                 keyfile=key,
                                 password=self.password)
     with context.wrap_socket(ssl.socket()) as s:
         # For TLS 1.3 the client certificate authentication happens after the handshake,
         # leading to s.connect not failing for invalid or missing client certs.
         # The authentication happens when the client performs the first read.
         # Setting a timeout helps speed up the tests, as `s.recv` is blocking.
         # Timeout errors only happen when the cient can read from the socket,
         # otherwise a SSLError occurs.
         s.connect((self.host, self.port))
         s.sendall(str.encode("sending TLS data"))
         s.settimeout(0.5)
         try:
             s.recv(8)
         except socket.timeout:
             pass
         s.close()
Beispiel #5
0
 def send_checkin(self):
     nodes = db.nodes.find("nodes", "all")
     for x in nodes:
         s = ssl.socket()
         try:
             s.settimeout(1)
             s.connect((x['ip'], x['port']))
             s.send(
                 json.dumps({
                     "cmd": "checkin",
                     "addr": self.addr,
                     "port": self.port,
                     "publickey": self.pubkey
                 }))
             s.close()
         except Exception, error:
             s.close()
             db.unsent.insert(
                 "unsent", {
                     "to": [x['ip'], x['port']],
                     "message": {
                         "cmd": "checkin",
                         "addr": self.addr,
                         "port": self.port,
                         "publickey": self.pubkey
                     }
                 })
Beispiel #6
0
def send_delete(id, addr):
    message = db.messages.find("messages", {"id": id})
    if not message:
        return "Message with that ID doesn't exist."
    else:
        db.messages.remove("messages", message[0])
        nodes = db.nodes.find("nodes", "all")
        for x in nodes:
            try:
                sock = ssl.socket()
                sock.settimeout(1)
                sock.connect((x['ip'], x['port']))
                sock.send(json.dumps({"cmd": "delete", "to": addr, "id": id}))
            except:
                db.unsent.insert(
                    "unsent", {
                        "to": [x['ip'], x['port']],
                        "message": {
                            "cmd": "delete",
                            "addr": addr,
                            "id": id
                        }
                    })
            sock.close()
        return "Message Removed!"
Beispiel #7
0
 def main(self):
     s = ssl.socket()
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     s.bind(("", self.port))
     s.listen(5)
     while True:
         obj, conn = s.accept()
         threading.Thread(target=self.handle, args=(obj, conn[0])).start()
Beispiel #8
0
 def ssl_connect(self, protocol=ssl.PROTOCOL_TLSv1_2, ciphers=None):
     context = ssl.SSLContext(protocol)
     if ciphers:
         context.set_ciphers(ciphers)
     context.load_verify_locations(self.ca_cert)
     context.load_cert_chain(certfile=self.server_cert, keyfile=self.server_key, password=self.password)
     s = context.wrap_socket(ssl.socket())
     s.connect((self.host, self.port))
Beispiel #9
0
 def main(self):
     s = ssl.socket()
     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     s.bind(("", self.port))
     s.listen(5)
     while True:
         obj, conn = s.accept()
         threading.Thread(target=self.handle, args=(obj, conn[0])).start()
Beispiel #10
0
def send_get_messages(ip, port):
    sock = ssl.socket()
    sock.connect((ip, port))
    sock.send(json.dumps({"cmd":'get_messages'}))
    with open("messages.db", 'wb') as file:
        while True:
            data = sock.recv(1024)
            if data:
                file.write(data)
            else:
                break
    sock.close()
Beispiel #11
0
 def send_checkin(self):
     nodes = db.nodes.find("nodes", "all")
     for x in nodes:
         s = ssl.socket()
         try:
             s.settimeout(1)
             s.connect((x['ip'], x['port']))
             s.send(json.dumps({"cmd":"checkin", "addr":self.addr, "port":self.port, "publickey":self.pubkey}))
             s.close()
         except Exception, error:
             s.close()
             db.unsent.insert("unsent",  {"to":[x['ip'], x['port']], "message":{"cmd":"checkin", "addr":self.addr, "port":self.port, "publickey":self.pubkey}}) 
Beispiel #12
0
 def get_nodes(self):
     send = {"addr": self.addr, "port": self.port, "publickey": self.pubkey}
     s = ssl.socket()
     s.connect(self.broker)
     s.send(json.dumps(send))
     with open("nodes.db", "wb") as file:
         while True:
             data = s.recv(1024)
             if data:
                 file.write(data)
             else:
                 break
     print "Downloaded Nodes!"
Beispiel #13
0
 def get_nodes(self):
     send = {"addr": self.addr, "port": self.port, "publickey": self.pubkey}
     s = ssl.socket()
     s.connect(self.broker)
     s.send(json.dumps(send))
     with open("nodes.db", 'wb') as file:
         while True:
             data = s.recv(1024)
             if data:
                 file.write(data)
             else:
                 break
     print "Downloaded Nodes!"
Beispiel #14
0
def send_delete(id, addr):
    message = db.messages.find("messages", {"id":id})
    if not message:
        return "Message with that ID doesn't exist."
    else:
        db.messages.remove("messages", message[0])
        nodes = db.nodes.find("nodes", "all")
        for x in nodes:
            try:
                sock = ssl.socket()
                sock.settimeout(1)
                sock.connect((x['ip'], x['port']))
                sock.send(json.dumps({"cmd":"delete", "to":addr, "id":id}))
            except:
                db.unsent.insert("unsent", {"to":[x['ip'], x['port']], "message":{"cmd":"delete", "addr":addr, "id":id}})
            sock.close()
        return "Message Removed!"
Beispiel #15
0
def send_delete(id, addr):
    message = db.messages.find("messages", {"id":id})
    if not message:
        return "Message with that ID doesn't exist."
    else:
       db.messages.remove("messages", message[0])
       nodes = db.nodes.find("nodes", "all")
       signature = base64.b64encode(sign("delete" + id, eval(db.data.find("data","all")[0]['privatekey']),"SHA-1"))
       for x in nodes:
            try:
                sock = ssl.socket()
                sock.settimeout(1)
                sock.connect((x['ip'], x['port']))
                sock.send(json.dumps({"cmd":"delete", "to":addr, "id":id, "signature":signature}))
            except:
                db.unsent.insert("unsent", {"to":[x['ip'], x['port']], "message":{"cmd":"delete", "to":addr, "id":id, "signature":signature}})
            sock.close()
       return "Message Removed!"
Beispiel #16
0
def unsent():
    #{'unsent':[{'to':[ip, port], 'message':{}]}
    while True:
        messages = db.unsent.find("unsent", "all")
        if messages:
            for x in messages:
                to = tuple(x['to'])
                message = x['message']
                s = ssl.socket()
                try:
                    s.settimeout(2)
                    s.connect(to)
                except:
                    s.close()
                    continue
                else:
                    s.send(json.dumps(message))
                    s.close()
                    db.unsent.remove("unsent", x)
        time.sleep(60)
Beispiel #17
0
def unsent():
    #{'unsent':[{'to':[ip, port], 'message':{}]}
    while True:
        messages = db.unsent.find("unsent", "all")
        if messages:
            for x in messages:
                to = tuple(x['to'])
                message = x['message']
                s = ssl.socket()
                try:
                    s.settimeout(2)
                    s.connect(to)
                except:
                    s.close()
                    continue
                else:
                    s.send(json.dumps(message))
                    s.close()
                    db.unsent.remove("unsent", x)
        time.sleep(60)
Beispiel #18
0
def send_msg(msg, title, to, addr):
    # Copied and pasted from read.py
    my_key = db.data.find("data", "all")[0]["privatekey"]
    if my_key.startswith("PrivateKey(") and my_key.endswith(")"):
        my_key = eval(my_key)

    try:
        data = db.nodes.find("nodes", {"addr":to})[0]
    except:
        return "Address doesn't exist"
    if data['publickey'].startswith("PublicKey(") and data['publickey'].endswith(")"):
       # msg = encrypt(msg, eval(data['publickey'])) <--- Old encryption code
	aeskey = str(uuid.uuid4().hex) # Generate new AES Key
	key = encrypt(aeskey, eval(data['publickey'])) # Encrypt AES key with target's RSA Public Key
	key = base64.b64encode(key) # Base64 encode the key
	msg = aes.encryptData(aeskey,msg) # Encrypt Message with AES Key
        msg = base64.b64encode(msg) # Base64 encode the message
	as_num, as_nonce = antispam.find_antispam(to,addr,msg,antispam.get_required_difficulty(msg)) # Generate the AntiSpam
        signature = base64.b64encode(sign(msg + to,my_key,"SHA-1")) # Sign the message
    else:
        return "Invalid public key for", addr
    id = ""
    while True:
        id = uuid.uuid4().hex
        if db.messages.find("messages", {"id":id}):
            continue
        else:
            break
    print "Sending message ID " + id + " with key " + key + " and signature " + signature
    nodes = db.nodes.find("nodes", "all")
    t = time.localtime()
    time_ = "{0}/{1}/{2} {3}:{4}:{5}".format(t[1], t[2], t[0], t[3], t[4], t[5])
    for x in nodes:
        s = ssl.socket()
        try:
            s.settimeout(1)
            s.connect((x['ip'], x['port']))
            s.send(json.dumps({"cmd":"message", "time":time_ ,"id":id, "message":msg, "title":title, "to":to, "from":addr,"num":as_num,"nonce":as_nonce,"key":key,"signature":signature}))
            s.close()
        except Exception, error:
            db.unsent.insert("unsent", {"to":[x['ip'], x['port']], "message":{"cmd":"message", "time":time_, "id":id, "message":msg, "title":title, "to":to, "from":addr,"num":as_num,"nonce":as_nonce,"key":key,"signature":signature}})
Beispiel #19
0
    def connect(self):
        print 'Connecting to region: {0}'.format(self.region)

        self.token = GetLoginToken(self.user, self.password, self.region[2])
        self.socket = SSLSocket(socket(), ssl_version=3)
        self.socket.connect((self.host, self.port))
        self.connected = True

        if not self.doHandshake():
            return False

        self.socket.setblocking(0)
        self.stream = rtmp.decoder.FileBuffer(self.socket.makefile())
        self.reader = rtmp.decoder.PacketReader(self.stream)

        msg = {
            'videoCodecs': 252,
            'audioCodecs': 3575,
            'flashVer': u'WIN 10,6,602,161',
            'app': '',
            'tcUrl': 'rtmps://{0}:2099'.format(self.host),
            'videoFunction': 1,
            'capabilities': 239,
            'pageUrl': '',
            'fpad': False,
            'swfUrl': 'app:/LolClient.swf/[[DYNAMIC]]/32',
            'objectEncoding': 3
        }

        stream = self.encoder.encodeConnect(msg)

        self.writeBytes(stream)
        self.processThread = Thread(target=self._processMessages)
        self.processThread.start()

        return True
Beispiel #20
0
def send_msg(msg, title, to, addr):
    try:
        data = db.nodes.find("nodes", {"addr": to})[0]
    except:
        return "Address doesn't exist"
    if data['publickey'].startswith(
            "PublicKey(") and data['publickey'].endswith(")"):
        # msg = encrypt(msg, eval(data['publickey'])) <--- Old encryption code
        aeskey = str(uuid.uuid4().hex)  # Generate new AES Key
        key = encrypt(aeskey, eval(
            data['publickey']))  # Encrypt AES key with target's RSA Public Key
        key = base64.b64encode(key)  # Base64 encode the key
        msg = aes.encryptData(aeskey, msg)  # Encrypt Message with AES Key
        msg = base64.b64encode(msg)  # Base64 encode the message
        as_num, as_nonce = antispam.find_antispam(
            to, addr, msg, antispam.get_required_difficulty(msg))
    else:
        return "Invalid public key for", addr
    id = ""
    while True:
        id = uuid.uuid4().hex
        if db.messages.find("messages", {"id": id}):
            continue
        else:
            break
    print "Sending message ID " + id + " with key " + key
    nodes = db.nodes.find("nodes", "all")
    for x in nodes:
        s = ssl.socket()
        try:
            s.settimeout(1)
            s.connect((x['ip'], x['port']))
            s.send(
                json.dumps({
                    "cmd": "message",
                    "id": id,
                    "message": msg,
                    "title": title,
                    "to": to,
                    "from": addr,
                    "num": as_num,
                    "nonce": as_nonce,
                    "key": key
                }))
            s.close()
        except Exception, error:
            db.unsent.insert(
                "unsent", {
                    "to": [x['ip'], x['port']],
                    "message": {
                        "cmd": "message",
                        "id": id,
                        "message": msg,
                        "title": title,
                        "to": to,
                        "from": addr,
                        "num": as_num,
                        "nonce": as_nonce,
                        "key": key
                    }
                })