Skip to content

Commit

Permalink
Last commit before rewriting fax out to use Telnyx SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
msmhome committed Aug 4, 2024
1 parent bdb82fe commit ea10e56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Also, set the `TUNNEL_TOKEN` environment variable for the container with your Cl

### Cloudflare Tunnels (cloudflared)

Cloudflared is built in and recommended instead of an open port. Enable `Disable Chunked Encoding`
Cloudflared is built in and recommended instead of an open port. Set `Disable Chunked Encoding` to on

It's also recommended to get an origin server certificate and save the certificate and private key to `Faxes/certs/certificate.pem` and `Faxes/certs/key.pem`, respectively. Make sure to set your cloudflare tunnel configuration as HTTPS and set the origin server name.  

Expand Down
30 changes: 20 additions & 10 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import shutil
import ipaddress

#TODO: Create inbound_faxes and inbound_sms directories.
#TODO: Append phone number and timestamp to inbound and outbound final fax PDF files . similar to SMS

#TODO: Append phone number and timestamp to inbound and outbound final fax PDF files. similar to SMS

# Initialize FastAPI with rate limiter
limiter = Limiter(key_func=get_remote_address)
Expand All @@ -36,6 +34,7 @@
#TODO: Add debug logging level, make it put all HTTP requests in/out raw
#TODO: Standardize logging messages, include timestamp, type, direction, phone numbers, and file.
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)

# Read and process whitelisted IP ranges from environment variable
WHITELISTED_IP_RANGES_STR = os.getenv('WHITELISTED_IP_RANGES')
Expand All @@ -44,11 +43,22 @@
try:
# Ensure the string is correctly formatted for JSON
WHITELISTED_IP_RANGES_STR = WHITELISTED_IP_RANGES_STR.strip().replace("'", '"')
print(f"Formatted WHITELISTED_IP_RANGES_STR: '{WHITELISTED_IP_RANGES_STR}'")
ip_ranges = json.loads(WHITELISTED_IP_RANGES_STR)
WHITELISTED_IP_RANGES = []
for ip in ip_ranges:
try:
WHITELISTED_IP_RANGES.append(ipaddress.ip_network(ip))
except ValueError as e:
print(f"[ERROR]:Invalid IP range '{ip}' skipped: {e}")
print(f"Parsed Whitelisted IP ranges: {WHITELISTED_IP_RANGES}")
logging.debug(f"Parsed WHITELISTED_IP_RANGES: {WHITELISTED_IP_RANGES}")

except ipaddress.AddressValueError as e:
logging.debug(f"Unable to properly read whitelisted IP ranges. Are they set in environment and in proper JSON? {e}")
raise ValueError(f"Error decoding WHITELISTED_IP_RANGES: {e}")

WHITELISTED_IP_RANGES = [ipaddress.ip_network(ip) for ip in json.loads(WHITELISTED_IP_RANGES_STR)]
print(f"Parsed WHITELISTED_IP_RANGES: {WHITELISTED_IP_RANGES}")
except json.JSONDecodeError as e:
logging.debug(f"Unable to properly read whitelisted IP ranges. Are they set in environment and in proper JSON? {e}")
raise ValueError(f"Error decoding WHITELISTED_IP_RANGES: {e}")

def is_whitelisted(ip):
Expand All @@ -63,7 +73,7 @@ async def whitelist_middleware(request: Request, call_next):
response = await call_next(request)
return response

# Formatting
# Formatting for Fax In
class FaxData(BaseModel):
event_type: str
direction: str
Expand All @@ -72,11 +82,11 @@ class FaxData(BaseModel):
from_: str = Field(alias="from")
media_url: str

#Formatting for SMS In
def sanitize_and_store(message: str, from_number: str, directory="Faxes"):
sanitized_message = bleach.clean(message, strip=True)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f') # Using microseconds for uniqueness
file_name = f"SMS_from_{from_number}_at_{timestamp}.txt"

os.makedirs(directory, exist_ok=True) # Ensure the directory exists

file_path = os.path.join(directory, file_name)
Expand All @@ -88,6 +98,7 @@ def sanitize_and_store(message: str, from_number: str, directory="Faxes"):
class SmsData(BaseModel):
data: dict

#Sanitize and format Fax In File
def download_file(url, save_directory='Faxes'):
# Checking if the url is valid
try:
Expand Down Expand Up @@ -123,6 +134,7 @@ async def handle_sms(data: SmsData):
from_number = data.data.get('payload').get('from').get('phone_number')
sanitized_message = sanitize_and_store(message, from_number)
print(f"Received an SMS from {from_number}: {sanitized_message}")
logging.debug(f"Received an SMS from {from_number}: {'message.payload'}")
return Response(status_code=200)
except KeyError:
print("Incorrect data format received.")
Expand Down Expand Up @@ -241,9 +253,7 @@ def on_confirmed(self, faxed_to, confirmation_number):
return
file_path = os.path.join('Faxes/outbound', original_file_name)
new_file_name = f"{faxed_to}_{confirmation_number}_confirmed.pdf"
# new_file_path = os.path.join('Faxes/outbound/confirmations', new_file_name)
new_file_path = os.path.join('Faxes', 'outbound_confirmations', new_file_name)

try:
shutil.move(file_path, new_file_path)
print(f"Moved confirmed fax to {new_file_path}")
Expand Down

0 comments on commit ea10e56

Please sign in to comment.