Onionr/src/communicatorutils/servicecreator.py

46 lines
2.0 KiB
Python
Raw Normal View History

'''
Onionr - Private P2P Communication
Creates an onionr direct connection service by scanning all connection blocks
'''
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import communicator
from onionrblocks import onionrblockapi
2019-08-21 08:03:24 +00:00
import logger
2019-06-29 18:18:31 +00:00
from onionrutils import stringvalidators, bytesconverter
from coredb import blockmetadb
2019-08-21 08:03:24 +00:00
from onionrservices import server_exists
2019-03-24 02:56:46 +00:00
def service_creator(daemon):
assert isinstance(daemon, communicator.OnionrCommunicatorDaemon)
# Find socket connection blocks
# TODO cache blocks and only look at recently received ones
con_blocks = blockmetadb.get_blocks_by_type('con')
2019-03-24 02:56:46 +00:00
for b in con_blocks:
if not b in daemon.active_services:
2019-07-18 23:07:18 +00:00
bl = onionrblockapi.Block(b, decrypt=True)
2019-06-29 18:18:31 +00:00
bs = bytesconverter.bytes_to_str(bl.bcontent) + '.onion'
2019-08-21 08:03:24 +00:00
if server_exists(bl.signer):
continue
if stringvalidators.validate_pub_key(bl.signer) and stringvalidators.validate_transport(bs):
2019-06-29 18:18:31 +00:00
signer = bytesconverter.bytes_to_str(bl.signer)
2019-03-24 02:56:46 +00:00
daemon.active_services.append(b)
daemon.active_services.append(signer)
2019-08-21 08:03:24 +00:00
if not daemon.services.create_server(signer, bs, daemon):
daemon.active_services.remove(b)
daemon.active_services.remove(signer)
2019-08-16 20:41:56 +00:00
daemon.decrementThreadCount('service_creator')