84 lines
4.8 KiB
Python
84 lines
4.8 KiB
Python
import os
|
|
import shutil
|
|
|
|
from conpeek_setup import util
|
|
|
|
|
|
def subnet_mask_to_cidr(mask):
|
|
# Split the subnet mask into its octets
|
|
octets = mask.split('.')
|
|
# Convert each octet from string to integer, then to binary, count the '1' bits
|
|
try:
|
|
cidr = str(sum(bin(int(octet)).count('1') for octet in octets))
|
|
except Exception:
|
|
util.print_red(f"Mask {mask} is invalid")
|
|
raise Exception("Mask {mask} is invalid")
|
|
|
|
return cidr
|
|
|
|
|
|
def run(config):
|
|
util.print_black_light("Preparing freeswitch file")
|
|
|
|
output_directory = os.path.join(util.get_output_path(), "freeswitch")
|
|
template_freeswitch_directory = os.path.join(util.get_templates_path(), "freeswitch/conf")
|
|
|
|
shutil.rmtree(output_directory, ignore_errors=True)
|
|
os.makedirs(output_directory, exist_ok=True)
|
|
|
|
util.copy_directory(template_freeswitch_directory, os.path.join(output_directory, "conf"))
|
|
|
|
mod_verto_replacements = {
|
|
'VERTO_IP': config["new_machine_network"]["internal_ip"],
|
|
'EXTERNAL_IP': config["new_machine_network"]["external_ip"],
|
|
'EXTERNAL_NET_ADDRESS': config["new_machine_network"]["external_net_address"],
|
|
'EXTERNAL_NET_MASK': subnet_mask_to_cidr(config["new_machine_network"]['external_mask']),
|
|
'INTERNAL_IP': config["new_machine_network"]["internal_ip"],
|
|
'INTERNAL_NET_ADDRESS': config["new_machine_network"]["internal_net_address"],
|
|
'INTERNAL_NET_MASK': subnet_mask_to_cidr(config["new_machine_network"]['internal_mask']),
|
|
}
|
|
|
|
for name in mod_verto_replacements.keys():
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/verto.conf.xml"), name, mod_verto_replacements[name])
|
|
|
|
tenant_data = util.get_minimal_configuration()
|
|
|
|
# mod_event_socket
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/event_socket.conf.xml"), "ES_PORT", "9900")
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/event_socket.conf.xml"), "ES_IP", config["new_machine_network"]["internal_ip"])
|
|
|
|
# switch
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/switch.conf.xml"), "FS_DOMAIN", f"{tenant_data['freeswitch_name']}.{config['new_machine_network']['system_app_installation_domain']}")
|
|
|
|
if config.has_option("freeswitch", "rtp_start_port") and config.has_option("freeswitch", "rtp_end_port"):
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/switch.conf.xml"), "RTP_START_PORT", config["freeswitch"]["rtp_start_port"])
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/switch.conf.xml"), "RTP_END_PORT", config["freeswitch"]["rtp_end_port"])
|
|
else:
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/switch.conf.xml"), "RTP_START_PORT", "16384")
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/autoload_configs/switch.conf.xml"), "RTP_END_PORT", "32767")
|
|
|
|
# vars.xml
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/vars.xml"), "XML_RPC_PASSWORD", "works")
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/vars.xml"), "DEFAULT_PASSWORD", "Q3G2omiUNgD67fgwP6Xa")
|
|
|
|
# dialplan
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/dialplan/virtual_contact.xml"), "GATEWAY_NAME", tenant_data["gateway_server_name"])
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/dialplan/virtual_contact.xml"), "INTERNAL_IP", config["new_machine_network"]['internal_ip'])
|
|
|
|
# sip profiles
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/gateway0.xml"), "INTERNAL_IP", config["new_machine_network"]['internal_ip'])
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/line0_an1.xml"), "INTERNAL_IP", config["new_machine_network"]['internal_ip'])
|
|
|
|
is_nat = True
|
|
if config.has_option('kamailio', 'nat'):
|
|
is_nat = config["kamailio"]['nat'] == '1'
|
|
|
|
if is_nat == '1':
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/line0_an1.xml"), "LOCAL_NETWORK_ACL", 'nat.auto')
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/line0_an1.xml"), "EXT_RTP_IP", config["new_machine_network"]['external_ip'])
|
|
else:
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/line0_an1.xml"), "LOCAL_NETWORK_ACL", 'localnet.auto')
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/line0_an1.xml"), "EXT_RTP_IP", config["new_machine_network"]['internal_ip'])
|
|
|
|
util.basic_on_location_sed(os.path.join(output_directory, "conf/sip_profiles/gw1_internal0.xml"), "INTERNAL_IP", config["new_machine_network"]['internal_ip'])
|