|
@@ -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))
|