Skip to content

Commit

Permalink
Fix signatures (#171)
Browse files Browse the repository at this point in the history
* fix: signatures

* fix: signature

Wordpress is a bit annoying and needs cnames with at least one alpha character.  Need to revisit

* fix: cnames always have an alpha
  • Loading branch information
SimonGurney authored Mar 10, 2024
1 parent 0770f96 commit 0bfa004
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 142 deletions.
3 changes: 2 additions & 1 deletion signatures/github_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
test = cname_or_ip_found_but_string_in_body(
cname=github_pages_cname,
ips=github_pages_ipv4 + github_pages_ipv6,
domain_not_configured_message="There isn't a GitHub Pages site here",
domain_not_configured_message="<title>Site not found ",
service="Github Pages",
https=True,
)
16 changes: 8 additions & 8 deletions signatures/launchrock_cname.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .templates.cname_found_but_status_code import cname_found_but_status_code

test = cname_found_but_status_code(
cname="cname.launchrock.com",
code=500,
service="Launchrock",
https=True,
)
from .templates.cname_found_but_status_code import cname_found_but_status_code

test = cname_found_but_status_code(
cname="host.launchrock.com",
code=0,
service="Launchrock",
https=True,
)
34 changes: 17 additions & 17 deletions signatures/shopify.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from .templates.cname_or_ip_found_but_string_in_body import (
cname_or_ip_found_but_string_in_body,
)

ipv4 = ["23.227.38.65"]
ipv6 = []

cname = "shops.myshopify.com"

test = cname_or_ip_found_but_string_in_body(
cname=cname,
ips=ipv4 + ipv6,
domain_not_configured_message="Only one step left!",
service="Shopify",
)

# https://help.shopify.com/en/manual/domains/add-a-domain/connecting-domains/connect-domain-manual#step-1-change-your-dns-records-in-your-third-party-domain-provider-account
from .templates.cname_or_ip_found_but_string_in_body import (
cname_or_ip_found_but_string_in_body,
)

ipv4 = ["23.227.38.65"]
ipv6 = []

cname = "shops.myshopify.com"

test = cname_or_ip_found_but_string_in_body(
cname=cname,
ips=ipv4 + ipv6,
domain_not_configured_message="Create an Ecommerce Website and Sell Online! Ecommerce Software by Shopify",
service="Shopify",
)

# https://help.shopify.com/en/manual/domains/add-a-domain/connecting-domains/connect-domain-manual#step-1-change-your-dns-records-in-your-third-party-domain-provider-account
16 changes: 8 additions & 8 deletions signatures/smartjobboard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body

test = cname_found_but_string_in_body(
cname="cname.smartjobboard.com",
domain_not_configured_message="404 Not Found",
service="smartjobboard.com",
https=True,
)
from .templates.ip_found_but_string_in_body import ip_found_but_string_in_body

test = ip_found_but_string_in_body(
ips=["52.16.160.97"],
domain_not_configured_message="job board website is either expired or its domain name is invalid.",
service="smartjobboard.com",
https=True,
)
15 changes: 8 additions & 7 deletions signatures/wishpond.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body

test = cname_found_but_string_in_body(
cname="cname.wishpond.com",
domain_not_configured_message="https://www.wishpond.com/404?campaign=true",
service="wishpond.com",
)
from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body

test = cname_found_but_string_in_body(
cname="cname.wishpond.com",
domain_not_configured_message="https://www.wishpond.com/404?campaign=true",
service="wishpond.com",
https=True,
)
15 changes: 8 additions & 7 deletions signatures/wordpress_com_cname.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body

test = cname_found_but_string_in_body(
cname=".wordpress.com",
domain_not_configured_message="Do you want to register",
service="wordpress.com",
)
from .templates.cname_found_but_string_in_body import cname_found_but_string_in_body

test = cname_found_but_string_in_body(
cname=".wordpress.com",
domain_not_configured_message="Do you want to register",
service="wordpress.com",
https=True,
)
188 changes: 94 additions & 94 deletions tests/mocks.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
from collections import namedtuple
from domain import Domain
import requests
import dns.resolver
import ipaddress
from uuid import uuid4


def random_string():
return uuid4().hex[:8]


def mock_web_response_with_static_value(
domain: Domain, body: str = "", status_code: int = 0
) -> Domain:
def mock_fetch_web(**kwargs):
return namedtuple("web_response", ["body", "status_code"])(body, status_code)

domain.fetch_web = mock_fetch_web


def mock_web_request_by_providing_static_host_resolution(
domain: Domain, hostname: str
) -> Domain:
requests_adapter = HostHeaderAdapter()
requests_adapter.set_static_host_resolution(hostname)
patched_requests = requests.session()
patched_requests.mount("https://", requests_adapter)
patched_requests.mount("http://", requests_adapter)
domain.requests = patched_requests


def mock_web_request_by_providing_custom_ns(domain: Domain, ns: str) -> Domain:
requests_adapter = HostHeaderAdapter()
requests_adapter.set_ns_for_resolution(ns)
patched_requests = requests.session()
patched_requests.mount("https://", requests_adapter)
patched_requests.mount("http://", requests_adapter)
domain.requests = patched_requests


class HostHeaderAdapter(requests.adapters.HTTPAdapter):
def set_static_host_resolution(self, host):
self.host = self.resolve_to_ip(host)

def set_ns_for_resolution(self, ns):
self.ns = self.resolve_to_ip(ns)

def resolve_via_ns(self, domain):
resolver = dns.resolver.Resolver()
resolver.nameservers = [self.ns]
response = resolver.resolve(domain)
return [record.to_text() for record in response][0]

def resolve_to_ip(self, name):
try:
# return the name if its already an ip
ipaddress.ip_address(name)
return name
except:
response = dns.resolver.resolve(name)
ip = [record.to_text() for record in response][0]
return self.resolve_to_ip(ip)

def wrap_if_ipv6(self, ip):
if ip.count(":") > 1:
return f"[{ip}]"
return ip

def send(self, request, **kwargs):
from urllib.parse import urlparse

connection_pool_kwargs = self.poolmanager.connection_pool_kw
result = urlparse(request.url)
try:
# Try use a static ip
resolved_ip = self.host
except:
# If that fails, try and resolve
resolved_ip = self.resolve_to_ip(result.hostname)

resolved_ip = self.wrap_if_ipv6(resolved_ip)

request.url = request.url.replace(
"//" + result.hostname,
"//" + resolved_ip,
)
if "https" in request.url:
connection_pool_kwargs["server_hostname"] = result.hostname # SNI
connection_pool_kwargs["assert_hostname"] = result.hostname

# overwrite the host header
request.headers["Host"] = result.hostname
return super(HostHeaderAdapter, self).send(request, **kwargs)
from collections import namedtuple
from domain import Domain
import requests
import dns.resolver
import ipaddress
from uuid import uuid4


def random_string():
return f"a{uuid4().hex[:8]}"


def mock_web_response_with_static_value(
domain: Domain, body: str = "", status_code: int = 0
) -> Domain:
def mock_fetch_web(**kwargs):
return namedtuple("web_response", ["body", "status_code"])(body, status_code)

domain.fetch_web = mock_fetch_web


def mock_web_request_by_providing_static_host_resolution(
domain: Domain, hostname: str
) -> Domain:
requests_adapter = HostHeaderAdapter()
requests_adapter.set_static_host_resolution(hostname)
patched_requests = requests.session()
patched_requests.mount("https://", requests_adapter)
patched_requests.mount("http://", requests_adapter)
domain.requests = patched_requests


def mock_web_request_by_providing_custom_ns(domain: Domain, ns: str) -> Domain:
requests_adapter = HostHeaderAdapter()
requests_adapter.set_ns_for_resolution(ns)
patched_requests = requests.session()
patched_requests.mount("https://", requests_adapter)
patched_requests.mount("http://", requests_adapter)
domain.requests = patched_requests


class HostHeaderAdapter(requests.adapters.HTTPAdapter):
def set_static_host_resolution(self, host):
self.host = self.resolve_to_ip(host)

def set_ns_for_resolution(self, ns):
self.ns = self.resolve_to_ip(ns)

def resolve_via_ns(self, domain):
resolver = dns.resolver.Resolver()
resolver.nameservers = [self.ns]
response = resolver.resolve(domain)
return [record.to_text() for record in response][0]

def resolve_to_ip(self, name):
try:
# return the name if its already an ip
ipaddress.ip_address(name)
return name
except:
response = dns.resolver.resolve(name)
ip = [record.to_text() for record in response][0]
return self.resolve_to_ip(ip)

def wrap_if_ipv6(self, ip):
if ip.count(":") > 1:
return f"[{ip}]"
return ip

def send(self, request, **kwargs):
from urllib.parse import urlparse

connection_pool_kwargs = self.poolmanager.connection_pool_kw
result = urlparse(request.url)
try:
# Try use a static ip
resolved_ip = self.host
except:
# If that fails, try and resolve
resolved_ip = self.resolve_to_ip(result.hostname)

resolved_ip = self.wrap_if_ipv6(resolved_ip)

request.url = request.url.replace(
"//" + result.hostname,
"//" + resolved_ip,
)
if "https" in request.url:
connection_pool_kwargs["server_hostname"] = result.hostname # SNI
connection_pool_kwargs["assert_hostname"] = result.hostname

# overwrite the host header
request.headers["Host"] = result.hostname
return super(HostHeaderAdapter, self).send(request, **kwargs)

0 comments on commit 0bfa004

Please sign in to comment.