# This file is part of bish-bosh. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT. No part of bish-bosh, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of bish-bosh. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT.


core_usesIn core functions
core_functions_register _bishbosh_backend_registration ncat

bishbosh_backend_ncat_check()
{
	case "$bishbosh_tunnel" in
		
		none|tls)
			:
		;;
		
		*)
			return 1
		;;
		
	esac
	
	# ncat, part of nmap; provides built-in ssl support; offered as separate RPM (but debian and homebrew bundle it with nmap)
	if core_compatibility_whichNoOutput ncat; then
		bishbosh_backend_name=ncat
		bishbosh_backend_path="$(core_compatibility_which ncat)"
		return 0
	fi
	return 1
}

bishbosh_backend_ncat_port()
{
	if [ "$bishbosh_tunnel" = 'none' ]; then
		printf '%s' 1883
	elif [ "$bishbosh_tunnel" = 'tls' ]; then
		printf '%s' 8883
	else
		printf '%s' 'invalid-tunnel'
	fi
}

_bishbosh_backend_ncat_start_addProxy()
{
	local proxyValue="$1"
	
	local proxyAddress
	if core_variable_isSet bishbosh_proxyPort; then
		proxyAddress="${proxyAddress}:${bishbosh_proxyPort}"
	else
		proxyAddres="$bishbosh_proxyAddress"
	fi
	core_variable_array_append options --proxy "$proxyAddress"
	
	core_variable_array_append options --proxy-type "$proxyValue"
	
	if core_variable_isSet bishbosh_proxyUsername; then
		
		local proxyAuth
		
		case "$proxyValue" in
			
			http)
				if core_variable_isUnset bishbosh_proxyPassword; then
					core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat requires a --proxy-password when using a HTTP proxy-kind"
				fi
				proxyAuth="${bishbosh_proxyUsername}:${bishbosh_proxyPassword}"
			;;
			
			*)
				if core_variable_isSet bishbosh_proxyPassword; then
					core_message WARN "The backend nc does not support specifing the proxy password for SOCKS proxies"
				fi
				proxyAuth="$bishbosh_proxyUsername"
			;;
			
		esac

		core_variable_array_append options --proxy-auth "$proxyAuth"
	fi
}

core_usesIn core variable variable/array
bishbosh_backend_ncat_start()
{
	local options
	local options_initialised
	core_variable_array_initialise options
	
	case "$bishbosh_transport" in
		
		inet)
			:
		;;
		
		inet4)
			core_variable_array_append options '-4'
		;;
		
		inet6)
			core_variable_array_append options '-6'
		;;
		
		unix)
			core_variable_array_append options '-U'
		;;
		
		serial)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat does not support the 'serial' transport."
		;;
		
		*)
			core_exitError $core_commandLine_exitCode_SOFTWARE "Please validate the values for bishbosh_transport ('$bishbosh_transport')"
		;;
		
	esac
	
	if core_variable_isSet bishbosh_sourceAddress; then
		core_variable_array_append options '-s' "$bishbosh_sourceAddress"
	fi
	
	if core_variable_isSet bishbosh_sourcePort; then
		core_variable_array_append options '-p' "$bishbosh_sourcePort"
	fi
	
	case "$bishbosh_proxyKind" in
		
		SOCKS4)
			_bishbosh_backend_ncat_start_addProxy 'socks4'
		;;
		
		SOCKS4a)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat does not support the 'SOCKS4a' bishbosh_proxyKind."
		;;
		
		SOCKS5)
			_bishbosh_backend_ncat_start_addProxy 'socks5'
		;;
		
		HTTP)
			_bishbosh_backend_ncat_start_addProxy 'http'
		;;
		
	esac
	
	if [ $bishbosh_connectTimeout -ne 0 ]; then
		core_variable_array_append options -w $bishbosh_connectTimeout
	fi
	
	case $(core_init_verbosity) in
		
		0)
			:
		;;
		
		1)
			core_variable_array_append options -v
		;;
		
		2)
			core_variable_array_append options -v
		;;
		
		*)
			core_variable_array_append options -v -v
		;;
	esac
	
	if [ "$bishbosh_tunnel" = 'tls' ]; then
		
		if core_variable_isSet bishbosh_tunnelTlsCaPath; then
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat does not support the option '--tunnel-tls-ca-path' (or configuration setting 'bishbosh_tunnelTlsCaPath')."
		fi
		
		if core_variable_isTrue "$bishbosh_tunnelTlsUseDer"; then
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat does not support DER certificates and keys."
		fi
		
		core_variable_array_append options --ssl
		
		if core_variable_isSet bishbosh_tunnelTlsCertificate; then
			core_variable_array_append options --ssl-cert "$bishbosh_tunnelTlsCertificate"
		fi
		
		if core_variable_isSet bishbosh_tunnelTlsKey; then
			core_variable_array_append options --ssl-key "$bishbosh_tunnelTlsKey"
		fi
		
		if core_variable_isSet bishbosh_tunnelTlsCaBundle; then
			core_variable_array_append options --ssl-trustfile "$bishbosh_tunnelTlsCaBundle"
		fi
		
		if core_variable_isTrue "$bishbosh_tunnelTlsVerify"; then
			core_variable_array_append options --ssl-verify
		fi
		
		if core_variable_isSet bishbosh_tunnelTlsCiphers; then
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncat does not support the option '--tunnel-tls-ciphers' (or configuration setting 'bishbosh_tunnelTlsCiphers')."
		fi
		
	fi

	core_variable_array_append options "$bishbosh_server"
		
	core_variable_array_append options "$bishbosh_port"
	
	bishbosh_backend_debugOptions
	
	core_variable_array_passToFunctionAsArguments options "$bishbosh_backend_path" <"$bishbosh_connection_toServerFifo" >"$bishbosh_connection_fromServerFifo" &
}
