work on sockets
This commit is contained in:
parent
d80e72d18c
commit
620897a2eb
@ -17,7 +17,8 @@
|
|||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
'''
|
'''
|
||||||
import stem
|
import stem.control
|
||||||
|
import socket, selectors
|
||||||
import onionrexceptions
|
import onionrexceptions
|
||||||
from dependencies import secrets
|
from dependencies import secrets
|
||||||
|
|
||||||
@ -36,16 +37,34 @@ class OnionrSockets:
|
|||||||
self.socketInfo = socketInfo
|
self.socketInfo = socketInfo
|
||||||
|
|
||||||
# Make sure socketInfo provides all necessary values
|
# Make sure socketInfo provides all necessary values
|
||||||
for i in ('peer', 'address', 'create'):
|
for i in ('peer', 'address', 'create', 'port'):
|
||||||
try:
|
try:
|
||||||
socketInfo[i]
|
socketInfo[i]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError('Must provide peer, address, and create in socketInfo dict argument')
|
raise ValueError('Must provide peer, address, and create in socketInfo dict argument')
|
||||||
|
|
||||||
self.isServer = socketInfo['create']
|
self.isServer = socketInfo['create'] # if we are the one creating the service
|
||||||
|
|
||||||
self.serverKey = socketInfo['peer']
|
self.remotePeer = socketInfo['peer']
|
||||||
|
self.socketPort = socketInfo['port']
|
||||||
self.serverAddress = socketInfo['address']
|
self.serverAddress = socketInfo['address']
|
||||||
|
|
||||||
|
if self.isServer:
|
||||||
|
self.createServer()
|
||||||
|
|
||||||
def createServer(self):
|
def createServer(self):
|
||||||
|
# Create our HS and advertise it via a block
|
||||||
|
dataID = uuid.uuid4().hex
|
||||||
|
ourAddress = ''
|
||||||
|
ourPort = 1337
|
||||||
|
ourInternalPort = 1338
|
||||||
|
|
||||||
|
# Setup the empheral HS
|
||||||
|
with stem.control.Controller.from_port() as controller:
|
||||||
|
controller.authenticate()
|
||||||
|
socketHS = controller.create_ephemeral_hidden_service({ourPort: ourInternalPort}, await_publication = True)
|
||||||
|
ourAddress = socketHS.service_id
|
||||||
|
|
||||||
|
meta = {'address': ourAddress, 'port': ourPort}
|
||||||
|
self._core.insertBlock(dataID, header='openSocket', encryptType='asym', asymPeer=self.remotePeer, sign=True, meta=meta)
|
||||||
return
|
return
|
@ -76,12 +76,17 @@ def on_processBlocks(api):
|
|||||||
_processForwardKey(api, myBlock)
|
_processForwardKey(api, myBlock)
|
||||||
# socket blocks
|
# socket blocks
|
||||||
elif blockType == 'openSocket':
|
elif blockType == 'openSocket':
|
||||||
if api.data['validSig'] == True:
|
if api.data['validSig'] == True and myBlock.decrypted: # we check if it is decrypted as a way of seeing if it was for us
|
||||||
try:
|
try:
|
||||||
address = api.data['address']
|
address = api.data['address']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise onionrexceptions.MissingAddress("Missing address for new socket")
|
raise onionrexceptions.MissingAddress("Missing address for new socket")
|
||||||
socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, create = False})
|
try:
|
||||||
|
port = api.data['port']
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError("Missing port for new socket")
|
||||||
|
|
||||||
|
socketInfo = json.dumps({'peer': api.data['signer'], 'address': address, 'port': port, create = False})
|
||||||
api.get_core().daemonQueueAdd('startSocket', socketInfo)
|
api.get_core().daemonQueueAdd('startSocket', socketInfo)
|
||||||
|
|
||||||
def on_init(api, data = None):
|
def on_init(api, data = None):
|
||||||
|
Loading…
Reference in New Issue
Block a user