work on sockets

This commit is contained in:
Kevin Froman 2018-09-20 00:13:26 -05:00
parent 557afb8d9a
commit 7baa7d5d5f
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
3 changed files with 26 additions and 13 deletions

View File

@ -469,9 +469,7 @@ class OnionrCommunicatorDaemon:
# Create a socket or connect to one.
# The socket handler (such as the plugin or app using it) is specified in startData['reason]
startData = json.loads(cmd[1])
rCallback = onionrsockets.getSocketCallbackRecieveHandler(self._core, startData['reason'], startData['create'])
sCallback = onionrsockets.getSocketCallbackSendHandler(self._core, startData['reason'], startData['create'])
self.onionrsockets.append(onionrsockets.OnionrSockets(self._core, startData, recieveCallback=rCallback, sendCallback=sCallback))
self.onionrsockets.append(onionrsockets.OnionrSockets(self._core, startData))
else:
logger.info('Recieved daemonQueue command:' + cmd[0])

View File

@ -26,6 +26,7 @@ if sys.version_info[0] == 2 or sys.version_info[1] < 5:
print('Error, Onionr requires Python 3.4+')
sys.exit(1)
import os, base64, random, getpass, shutil, subprocess, requests, time, platform, datetime, re, json, getpass, sqlite3
import webbrowser
from threading import Thread
import api, core, config, logger, onionrplugins as plugins, onionrevents as events
import onionrutils
@ -217,6 +218,8 @@ class Onionr:
'getpasswd': self.printWebPassword,
'get-passwd': self.printWebPassword,
'chat': self.onionrCore.chatInst.connect()
'friend': self.friendCmd
}
@ -826,7 +829,6 @@ class Onionr:
logger.error('%s add-file <filename>' % sys.argv[0], timestamp = False)
def openUI(self):
import webbrowser
url = 'http://127.0.0.1:%s/ui/index.html?timingToken=%s' % (config.get('client.port', 59496), self.onionrUtils.getTimeBypassToken())
print('Opening %s ...' % url)

View File

@ -36,7 +36,7 @@ def getSocketCallbackSendHandler(coreInst, reason, create):
retData = coreInst.chatInst.sendMessage
class OnionrSockets:
def __init__(self, coreInst, socketInfo, recieveCallback=None, sendCallback=None):
def __init__(self, coreInst, socketInfo):
'''Create a new Socket object. This interface is named a bit misleadingly
and does not actually forward network requests.
@ -49,12 +49,6 @@ class OnionrSockets:
self._core = coreInst
self.socketInfo = socketInfo
if not callable(sendCallback) or not callable(recieveCallback)
raise ValueError("callback must be a function")
self.sendCallback = sendCallback
self.recieveCallback = recieveCallback
# Make sure socketInfo provides all necessary values
for i in ('peer', 'address', 'create', 'port'):
try:
@ -69,6 +63,9 @@ class OnionrSockets:
self.serverAddress = socketInfo['address']
self.connected = False
self.readData = []
self.sendData = 0
if self.isServer:
self.createServer()
else:
@ -117,11 +114,25 @@ class OnionrSockets:
data = conn.recv(1024)
if data:
data = data.decode()
self.callback(self, data)
self.readData.append(data)
else:
sel.unregister(conn)
conn.close()
def sendData(self, data):
try:
data = data.encode()
except AttributeError:
pass
self.sendData = data
def readData(self):
try:
data = self.readData.pop(0)
except IndexError:
data = ''
return data
def connectServer(self):
# Set the Tor proxy
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', config.get('tor.socksport'), rdns=True)
@ -131,5 +142,7 @@ class OnionrSockets:
with remoteSocket as s:
s.connect((self.serverAddress, self.port))
data = s.recv(1024)
data.send(self.sendCallback(self, data.decode()))
if self.sendData != 0:
s.send(self.sendData)
self.sendData = 0
return