Function to extract port number from domain

main
Bob Mottram 2021-06-23 22:44:31 +01:00
parent e0aac3c6a3
commit bb09f2a655
6 changed files with 24 additions and 18 deletions

View File

@ -15,3 +15,13 @@ def removeDomainPort(domain: str) -> str:
if ':' in domain:
domain = domain.split(':')[0]
return domain
def getPortFromDomain(domain: str) -> int:
"""If the domain has a port number appended then return it
"""
if ':' in domain:
portStr = domain.split(':')[1]
if portStr.isdigit():
return int(portStr)
return None

View File

@ -62,6 +62,7 @@ from tests import runAllTests
from auth import storeBasicCredentials
from auth import createPassword
from domainhandler import removeDomainPort
from domainhandler import getPortFromDomain
from utils import hasUsersPath
from utils import getFullDomain
from utils import setConfigParam
@ -1081,7 +1082,7 @@ if args.message:
toDomain = toDomain.replace('\n', '').replace('\r', '')
toPort = 443
if ':' in toDomain:
toPort = toDomain.split(':')[1]
toPort = getPortFromDomain(toDomain)
toDomain = removeDomainPort(toDomain)
else:
if args.sendto.endswith('followers'):

View File

@ -14,6 +14,7 @@ import time
import random
from linked_data_sig import verifyJsonSignature
from domainhandler import removeDomainPort
from domainhandler import getPortFromDomain
from utils import hasObjectDict
from utils import dmAllowedFromDomain
from utils import isRecentPost
@ -1978,9 +1979,7 @@ def _sendToGroupMembers(session, baseDir: str, handle: str, port: int,
memberDomain = memberHandle.split('@')[1]
memberPort = port
if ':' in memberDomain:
memberPortStr = memberDomain.split(':')[1]
if memberPortStr.isdigit():
memberPort = int(memberPortStr)
memberPort = getPortFromDomain(memberDomain)
memberDomain = removeDomainPort(memberDomain)
sendSignedJson(postJsonObject, session, baseDir,
nickname, domain, port,
@ -2123,10 +2122,8 @@ def _bounceDM(senderPostId: str, session, httpPrefix: str,
senderDomain = sendingHandle.split('@')[1]
senderPort = port
if ':' in senderDomain:
senderPortStr = senderDomain.split(':')[1]
if senderPortStr.isdigit():
senderPort = int(senderPortStr)
senderDomain = removeDomainPort(senderDomain)
senderPort = getPortFromDomain(senderDomain)
senderDomain = removeDomainPort(senderDomain)
cc = []
# create the bounce DM

View File

@ -13,6 +13,7 @@ from follow import followedAccountRejects
from follow import removeFromFollowRequests
from utils import loadJson
from domainhandler import removeDomainPort
from domainhandler import getPortFromDomain
def manualDenyFollowRequest(session, baseDir: str,
@ -50,7 +51,7 @@ def manualDenyFollowRequest(session, baseDir: str,
denyHandle.split('@')[1].replace('\n', '').replace('\r', '')
denyPort = port
if ':' in denyDomain:
denyPort = denyDomain.split(':')[1]
denyPort = getPortFromDomain(denyDomain)
denyDomain = removeDomainPort(denyDomain)
followedAccountRejects(session, baseDir, httpPrefix,
nickname, domain, port,
@ -142,9 +143,7 @@ def manualApproveFollowRequest(session, baseDir: str,
handleOfFollowRequester.replace('\r', '')
port2 = port
if ':' in handleOfFollowRequester:
port2Str = handleOfFollowRequester.split(':')[1]
if port2Str.isdigit():
port2 = int(port2Str)
port2 = getPortFromDomain(handleOfFollowRequester)
requestsDir = accountDir + '/requests'
followActivityfilename = \
requestsDir + '/' + handleOfFollowRequester + '.follow'
@ -159,7 +158,7 @@ def manualApproveFollowRequest(session, baseDir: str,
approveDomain.replace('\r', '')
approvePort = port2
if ':' in approveDomain:
approvePort = approveDomain.split(':')[1]
approvePort = getPortFromDomain(approveDomain)
approveDomain = removeDomainPort(approveDomain)
print('Manual follow accept: Sending Accept for ' +
handle + ' follow request from ' +

View File

@ -33,6 +33,7 @@ from webfinger import webfingerHandle
from httpsig import createSignedHeader
from siteactive import siteIsActive
from domainhandler import removeDomainPort
from domainhandler import getPortFromDomain
from utils import hasObjectDict
from utils import rejectPostId
from utils import removeInvalidChars
@ -2665,7 +2666,7 @@ def sendToFollowers(session, baseDir: str,
index = 0
toDomain = followerHandles[index].split('@')[1]
if ':' in toDomain:
toPort = toDomain.split(':')[1]
toPort = getPortFromDomain(toDomain)
toDomain = removeDomainPort(toDomain)
cc = ''

View File

@ -16,6 +16,7 @@ import idna
import locale
from pprint import pprint
from domainhandler import removeDomainPort
from domainhandler import getPortFromDomain
from followingCalendar import addPersonToCalendar
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
@ -918,10 +919,7 @@ def getDomainFromActor(actor: str) -> (str, int):
if '/' in actor:
domain = domain.split('/')[0]
if ':' in domain:
portStr = domain.split(':')[1]
if not portStr.isdigit():
return None, None
port = int(portStr)
port = getPortFromDomain(domain)
domain = removeDomainPort(domain)
return domain, port