浏览代码

connection done

Shayan 4 年之前
父节点
当前提交
8207356779

+ 40 - 0
command.py

@@ -0,0 +1,40 @@
+
+import os
+
+
+def install():
+    # print(("git submodule init"))
+    # print(os.system("git submodule init"))
+    # print(("git submodule update"))
+    # print(os.system("git submodule update"))
+    # print(("mkdir ./MP-SPDZ/Player-Data"))
+    # print(os.system("mkdir ./MP-SPDZ/Player-Data"))
+    # print(("apt-get install automake build-essential git libboost-dev libboost-thread-dev libsodium-dev libssl-dev libtool m4 python texinfo yasm"))
+    # print(os.system("apt-get install automake build-essential git libboost-dev libboost-thread-dev libsodium-dev libssl-dev libtool m4 python texinfo yasm"))
+    # print(("./MP-SPDZ/Scripts/tldr.sh"))
+    # print(os.system("./MP-SPDZ/Scripts/tldr.sh"))
+    # print(("cd MP-SPDZ && ls && make -j8 tldr"))
+    # print(os.system("cd MP-SPDZ && ls && make -j8 tldr"))
+    # print(("create Link from Player"))
+    # print(os.system("ln -s MP-SPDZ/Player-Data/ ."))
+    # print(("create Link from local"))
+    # print(os.system("ln -s MP-SPDZ/local/ ."))
+    # print(("create Link from Programs"))
+    # print(os.system("ln -s MP-SPDZ/Programs/ ."))
+    # print(("./MP-SPDZ/compile.py tutorial"))
+    # print(os.system("./MP-SPDZ/compile.py tutorial"))
+    # print(("cd MP-SPDZ && ls && make -j8 mascot-party"))
+    # print(os.system("cd MP-SPDZ && ls && make -j8 mascot"))
+    print(("echo 1 2 3 4 > ./Player-Data/Input-P0-0"))
+    print(os.system("echo 1 2 3 4 > ./Player-Data/Input-P0-0"))
+    print(("echo 1 2 3 4 > ./Player-Data/Input-P1-0"))
+    print(os.system("echo 1 2 3 4 > ./Player-Data/Input-P1-0"))
+    print(("./MP-SPDZ/Scripts/mascot.sh tutorial"))
+    print(os.system("./MP-SPDZ/Scripts/mascot.sh tutorial"))
+
+
+def comp(a):
+    print(("cp to Programs"))
+    print(os.system("cp ./mpc/"+a+".mpc ./Programs/Source/"))
+    print(("compile"))
+    print(os.system("./MP-SPDZ/compile.py "+a))

+ 0 - 0
src/oracle/__init__.py → oracle/__init__.py


+ 0 - 0
src/oracle/broadcast.py → oracle/broadcast.py


+ 42 - 0
oracle/connection.py

@@ -0,0 +1,42 @@
+
+import socket
+import threading
+import json
+import config as cfg
+
+class Connection(threading.Thread):
+    def __init__(self,connection,address,oracleSocketPort,typee):
+        super(Connection,self).__init__(None, None, 
+			              None, None)
+        self.conn = connection
+        self.addr = address
+        self.history = []
+        self.type = typee
+        self.oracleSocketPort = oracleSocketPort
+
+
+        t = threading.Thread(target=self.receive)
+        t.start()
+        # self.send("Connection")
+
+    def toSEND(self, data):
+        j = json.dumps(x)
+        l = LenFunc - len(str(len(j)))
+        self.send("0000"[0:l]+str(len(j))+j)
+
+    def send(self,Message):
+        Message=bytes(Message,'utf-8')
+        self.conn.sendall(Message)
+        # self.history.append(("send",Message))
+
+
+    def receive(self):
+        while True: 
+            len = self.conn.recv(cfg.LenFunc)
+            if not len:
+                break
+            data = self.conn.recv(int(len))
+
+            print("data",data)
+            self.type.parser(data.decode("utf-8"),self.conn)
+            # self.history.append(("receive",data))

+ 2 - 1
src/oracle/oracle.py → oracle/oracle.py

@@ -1,8 +1,9 @@
 
 from .broadcast import Broadcast
+from .socket import Socket
 import random 
 
 socketPort = int(random.random()*50000+1024)
 def run(broadcast, broadcastPort):
     b = Broadcast(broadcastPort=broadcastPort,socketPort=socketPort)
-    # b.start()
+    s = Socket(socketPort)

+ 74 - 0
oracle/socket.py

@@ -0,0 +1,74 @@
+
+import socket
+import threading
+from .worker.connector import Connector
+from .worker.cryptor import Cryptor
+from .worker.bearer import Bearer       
+import config as cfg
+import json
+
+class Socket():
+
+    def __init__(self, socketPort):
+        self.socketPort = socketPort 
+        t = threading.Thread(target=self.connection)
+        t.start()
+        print("socket is created and Listener is created",socketPort)
+
+    def connection(self):
+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+            s.bind(("localhost", self.socketPort))
+            s.listen()
+            self.socket = s
+            while True:
+                conn, addr = self.socket.accept()
+                print(conn, addr)
+                u = threading.Thread(target=Connection,args=(conn, addr,self.socketPort+1)) # TODO socketprot dynamic
+                u.start()
+
+class Connection(threading.Thread):
+    def __init__(self,connection,address,oracleSocketPort):
+        super(Connection,self).__init__(None, None, 
+			              None, None)
+        self.conn = connection
+        self.addr = address
+        self.history = []
+        self.type = None
+        self.oracleSocketPort = oracleSocketPort
+
+
+        t = threading.Thread(target=self.receive)
+        t.start()
+        # self.send("Connection")
+
+    def toSEND(self,oracle, data):
+        j = json.dumps(x)
+        l = LenFunc - len(str(len(j)))
+        oracle.send("0000"[0:l]+str(len(j))+j)
+        
+    def send(self,Message):
+        Message=bytes(Message,'utf-8')
+        self.conn.sendall(Message)
+        # self.history.append(("send",Message))
+
+
+    def receive(self):
+        while True: 
+            len = self.conn.recv(cfg.LenFunc)
+            if not len:
+                break
+            data = self.conn.recv(int(len))
+            if not self.type:
+                d = json.loads(data)
+                if d["type"] == "user": 
+                    if d["oracle"] == 0:
+                        self.type = Connector(self) 
+                    elif d["oracle"] == 1:
+                        self.type = Cryptor(self) 
+                    elif d["oracle"] == 2:
+                        self.type = Bearer(self) 
+
+            else:    
+                print("data",data)
+                self.type.parser(data.decode("utf-8"),self.conn)
+                # self.history.append(("receive",data))

+ 25 - 0
oracle/worker/bearer.py

@@ -0,0 +1,25 @@
+import socket
+import threading
+import config as cfg
+from .worker import Worker
+class Bearer(Worker):
+
+    def __init__(self, parent):
+        super().__init__(self,parent)
+        print("\n\n Bearer \n\n")
+
+    def socketOracle(self,HOST,PORT):
+        self.connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.connector.connect((HOST, PORT))
+
+        t = threading.Thread(target=self.receive)
+        t.start()
+        self.toSEND({
+            "type":"oracleInit",
+            "oracle":"Bearer"
+        })
+        print("socket is created and Listener is created\n")
+
+    def start(self,a):
+        print("Bearer",a)
+        self.socketOracle(self.connectorIP,self.connectorPort)

+ 42 - 0
oracle/worker/connector.py

@@ -0,0 +1,42 @@
+import socket
+import threading
+import config as cfg
+from .worker import Worker
+from src.oracle.connection import Connection
+# p = str('16 03 01 00 c1 01 00 00 bd 03 03 d1 47 a0 85 e9 cd 6d 34 cc ef b5 41 48 3f ec a1 f4 5c da e7 b0 27 c4 5d 0f 5f 65 bf d9 e8 6f a2 20 4a 71 5d 24 4a eb 65 da a9 7f b8 ec 55 32 2e 06 ff aa fc b8 77 58 a8 06 ec 19 05 67 e9 3546 eb 00 02 13 01 01 00 00 72 00 00 00 13 00 11 00 00 0e 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 00 0b 00 04 03 00 01 02 00 0a 00 04 00 02 00  1d 00 23 00 00 00 16 00 00 00 17 00 00 00 0d 00  04 00 02 08 07 00 2b 00 03 02 03 04 00 2d 00 02 01 01 00 33 00 26 00 24 00 1d 00 20 8a d0 24 78  1d 71 cd 22 3f 28 93 aa ff 0d 1b 4e ba 18 1f d4  4a d4 a0 80 ca 78 d9 0c a4 29 1b 3f')
+p = str('16 03 01 00 c1 01 00 00 bd 03 03 d1 47 a0 85 e9 cd 6d 34 cc ef b5 41 48 3f ec a1 f4 5c da e7 b0 27 c4 5d 0f 5f 65 bf d9 e8 6f a2 20 4a 71 5d 24 4a eb 65 da a9 7f b8 ec 55 32 2e 06 ff aa fc b8 77 58 a8 06 ec 19 05 67 e9 3546 eb 00 02 13 01 01 00 00 72 00 00 00 13 00 11 00 00 0e 77 77 77 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 00 0b 00 04 03 00 01 02 00 0a 00 04 00 02 00  1d 00 23 00 00 00 16 00 00 00 17 00 00 00 0d 00  04 00 02 08 07 00 2b 00 03 02 03 04 00 2d 00 02 01 01 00 33 00 26 00 24 00 1d 00 20 8a d0 24 78  1d 71 cd 22 3f 28 93 aa ff 0d 1b 4e ba 18 1f d4  4a d4 a0 80 ca 78 d9 0c a4 29 1b 3f')
+
+class Connector(Worker):
+
+    def __init__(self, parent):
+        super().__init__(self,parent)
+        self.path = None
+        print("\n\n Connector \n\n")
+
+        t = threading.Thread(target=self.oraclesConnection)
+        t.start()
+        print("socket is created and Listener is created",self.parent.oracleSocketPort)
+
+    def oraclesConnection(self):
+        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+            s.bind(("localhost", self.parent.oracleSocketPort))
+            s.listen()
+            self.socket = s
+            while True:
+                conn, addr = self.socket.accept()
+                print(conn, addr)
+                u = threading.Thread(target=Connection,args=(conn, addr,None,self)) # TODO socketprot dynamic
+                u.start()
+
+
+    def start(self,data):
+        self.path = data["path"]
+        print("Connector",data)
+
+
+
+    def createConnection(self):#216.58.205.228
+        print("Connector",self.key_exchange)
+        self.sock = socket.socket()
+        self.sock.connect(("216.58.205.228", 443))
+        self.sock.sendall(bytes.fromhex(p))

+ 38 - 0
oracle/worker/cryptor.py

@@ -0,0 +1,38 @@
+import socket
+import threading
+import config as cfg
+from .worker import Worker
+from nacl.public import PrivateKey
+
+def new_x25519():
+    private_key = PrivateKey.generate()
+    key_exchange = bytes(private_key.public_key)
+    return bytes(private_key).hex() , key_exchange.hex()
+
+class Cryptor(Worker):
+
+    def __init__(self, parent):
+        super().__init__(self,parent)
+        self.privatekey , self.key_exchange = new_x25519()
+        print("\n\n CRYPTOR,  \n ",self.privatekey ,type (self.key_exchange),"\n")
+
+    
+    def socketOracle(self,HOST,PORT):
+        self.connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.connector.connect((HOST, PORT))
+        t = threading.Thread(target=self.receive)
+        t.start()
+        self.toSEND({
+            "type":"oracleInit",
+            "oracle":"Cryptor"
+        })
+        print("socket is created and Listener is created\n")
+        self.toSEND({
+            "type":"key_exchange",
+            "key_exchange":self.key_exchange
+        })
+
+
+    def start(self,a):
+        print("Cryptor",a)
+        self.socketOracle(self.connectorIP,self.connectorPort)

+ 68 - 0
oracle/worker/worker.py

@@ -0,0 +1,68 @@
+import json
+import config as cfg
+from src.socketClient import Socket
+LenFunc = cfg.LenFunc
+
+
+
+class Worker():
+    def __init__(self,child,parent):
+        self.child          = child
+        self.parent         = parent
+        self.bearer         = None
+        self.crypter        = None
+        self.connector      = None
+        self.connectorIP    = None
+        self.connectorPort  = None
+        self.id             = None
+        self.server         = None
+        self.key_exchange   = None
+
+    def toJSON(self, data):
+        return json.loads(data)
+
+    def toSTRING(self, data):
+        return json.dumps(data)
+
+    def toSEND(self, data):
+        j = json.dumps(data)
+        l = LenFunc - len(str(len(j)))
+        self.send("0000"[0:l]+str(len(j))+j)
+
+
+    def send(self,Message):
+        Message=bytes(Message,'utf-8')
+        self.connector.sendall(Message)
+        print("Send",Message)
+
+    def receive(self):
+        while True:
+            len = self.connector.recv(LenFunc)
+            if not len:
+                break
+            data = self.connector.recv(int(len))
+            print("receive",data)
+            self.parser(data)
+
+    def parser(self,data,conn):
+        data = self.toJSON(data)
+        print(data)
+
+        if    (data["type"]       ==      "init"                ):
+            self.id             = data["id"]
+            self.connectorIP    = data["connectorIP"]
+            self.connectorPort  = data["connectorPort"]
+            self.server  = data["server"]
+            self.child.start(data)
+        elif  (data["type"]       ==      "oracleInit"          ):
+            if data["oracle"] == "Cryptor":
+                self.crypter = conn
+            elif data["oracle"] == "Bearer":
+                self.bearer = conn
+            else:
+                print("Wrong INITIAL ORACLE")
+        elif  (data["type"]       ==      "key_exchange"           ):
+            self.key_exchange      = data["key_exchange"]
+            self.child.createConnection()
+
+

+ 49 - 0
socketClient.py

@@ -0,0 +1,49 @@
+import socket
+import threading
+import config as cfg
+import json
+LenFunc = cfg.LenFunc
+
+class Socket(threading.Thread):
+
+    def __init__(self, HOST,PORT,data):
+
+        super(Socket,self).__init__(None,None,None,None)
+        self.port = PORT
+        self.Host = HOST
+        self.history = []
+        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.socket.connect((HOST, PORT))
+        t = threading.Thread(target=self.receive)
+        t.start()
+        print("socket is created and Listener is created\n")
+
+        self.toSEND(data)
+
+
+    def send(self,Message):
+        Message=bytes(Message,'utf-8')
+        self.socket.sendall(Message)
+        # self.history.append(("send",Message))
+        print("Send",Message)
+
+    def receive(self):
+        while True:
+            len = self.socket.recv(LenFunc)
+            if not len:
+                break
+            data = self.socket.recv(int(len))
+            print("data",data)
+            # self.history.append(("receive",data))
+
+
+    def toJSON(self, data):
+        return json.loads(data)
+    def toSTRING(self, data):
+        return json.dumps(data)
+    def toSEND(self, x):
+        j = json.dumps(x)
+        l = LenFunc - len(str(len(j)))
+        self.send("0000"[0:l]+str(len(j))+j)
+    def parser(self,data):
+        print("parser not yet implemented. ",data) 

+ 0 - 22
src/oracle/socket.py

@@ -1,22 +0,0 @@
-
-import socket
-import threading
-
-        
-class Socket():
-
-    def __init__(self, socketPort): 
-        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
-            s.bind((HOST, socketPort))
-            s.listen()
-            self.sock = s
-            t1 = threading.Thread(target=self.connection)
-            t1.start()
-            print("socket is created and Listener is created")
-
-    def connection(self):
-        while True:
-            conn, addr = self.sock.accept()
-            print(conn, addr)
-            # u = _thread.start_new_thread( user, (conn, addr))
-            # connectedUser.append((addr,u))

+ 0 - 0
src/oracle/worker/connector/__init__.py


+ 0 - 0
src/oracle/worker/cryptor/__init__.py


+ 0 - 26
src/oracle/worker/worker.py

@@ -1,26 +0,0 @@
-import threading
-
-
-class worker(threading.Thread):
-    def __init__(self,connection,address):
-        super(worker,self).__init__(None, None, 
-			              None, None)
-        self.conn = connection
-        self.addr = address
-        self.history = []
-        _thread.start_new_thread( self.receive, ())
-        self.send("Connected")
-
-    def send(self,Message):
-        Message=bytes(Message,'utf-8')
-        self.conn.sendall(Message)
-        self.history.append(("send",Message))
-
-    def receive(self):
-        while True: 
-            len = self.conn.recv(LenFunc)
-            if not len:
-                break
-            data = self.conn.recv(int(len))
-            padra.parse(data.decode("utf-8"))
-            self.history.append(("receive",data))

+ 0 - 0
src/user/__init__.py


+ 0 - 6
src/user/socket.py

@@ -1,6 +0,0 @@
-
-from .broadcast import Broadcast
-
-def run(broadcast, broadcastPort):
-    b = Broadcast(None, None,  None,None, broadcastPort=broadcastPort)
-    b.start()

+ 0 - 19
src/user/user.py

@@ -1,19 +0,0 @@
-
-from .broadcast import Broadcast
-import time
-
-
-minOracles = 3
-
-
-def run(data, server, broadcast, broadcastPort):
-
-    b = Broadcast(broadcast=broadcast,broadcastPort=broadcastPort)
-
-    while(len(b.onlineOracles)<minOracles):
-        print("\tWe need min  ",minOracles," Oracles. Waiting for mor Oracle. we have now ",len(b.onlineOracles)," online Oracles")
-        time.sleep(.5)
-
-
-    print("\nnow we have min ",minOracles,"Oracles. List of All Online Oracles ->\n \t\n",b.onlineOracles,"\n\n")
-

+ 0 - 0
src/oracle/worker/bearer/__init__.py → user/__init__.py


+ 0 - 0
src/user/broadcast.py → user/broadcast.py


+ 53 - 0
user/user.py

@@ -0,0 +1,53 @@
+
+from .broadcast import Broadcast
+from src.socketClient import Socket
+import time
+import config as cfg 
+
+
+
+def run(data, server, broadcast, broadcastPort):
+
+    b = Broadcast(broadcast=broadcast,broadcastPort=broadcastPort)
+
+    while(len(b.onlineOracles) < cfg.numberOfOracles):
+        print("\tWe need min  ",cfg.numberOfOracles," Oracles. Waiting for mor Oracle. we have now ",len(b.onlineOracles)," online Oracles")
+        time.sleep(.5)
+
+
+    print("\nnow we have min ",cfg.numberOfOracles,"Oracles. List of All Online Oracles ->\n \t\n",b.onlineOracles,"\n\n")
+    worker = []
+    for i in range(cfg.numberOfOracles):
+        print(i,b.onlineOracles[i])
+        worker.append(Socket("127.0.0.1",int(b.onlineOracles[i][1]),{"type":"user","oracle":i})) #_[0] 
+
+    print("Done",worker)
+    print("\n")
+    time.sleep(1)
+
+    worker[0].toSEND({
+        "type":"init",
+        "id":"test",
+        "connectorIP":"127.0.0.1",
+        "connectorPort":int(worker[0].port)+1,
+        "path":"/", # TODO
+        "server":"smarthome.agio360.com" # TODO
+    })
+    worker[1].toSEND({
+        "type":"init",
+        "id":"test",
+        "connectorIP":"127.0.0.1",
+        "connectorPort":int(worker[0].port)+1,
+        "server":"smarthome.agio360.com"
+
+    })
+    worker[2].toSEND({
+        "type":"init",
+        "id":"test",
+        "connectorIP":"127.0.0.1",
+        "connectorPort":int(worker[0].port)+1,
+        "server":"smarthome.agio360.com",
+        "data":"GET ......"# TODO
+    })
+
+