#!/bin/sh /etc/rc.common # vim: set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 : # Copyright (C) 2015 ovh.com # Copyright (C) 2017 Ycarus (Yannick Chabanois) START=90 STOP=10 USE_PROCD=1 PROG_NAME=glorytun PROG=/usr/sbin/${PROG_NAME} _log() { logger -p daemon.info -t ${PROG_NAME} "$@" } _err() { logger -p daemon.err -t ${PROG_NAME} "$@" } validate_section() { uci_validate_section glorytun glorytun "${1}" \ 'enable:bool:0' \ 'mptcp:bool:0' \ 'mode:string' \ 'key:string' \ 'host:host' \ 'port:port' \ 'dev:string' \ 'timeout:uinteger:10000' \ 'chacha20:bool:0' \ 'multiqueue:bool:1' \ 'proto:string' \ 'localip:string' \ 'remoteip:string' } start_instance() { local enable key host port dev mptcp proto chacha20 mode multiqueue timeout localip remoteip local key_dir="/var/run/${PROG_NAME}" local key_file="${key_dir}/${1}.key" validate_section "${1}" || { _err "validation failed" return 1 } [ "${enable}" = "1" ] || return 1 [ "${proto}" = "tcp" ] || return 1 [ -n "${key}" ] || { _err "Key empty" return 1 } [ "${key}" != "secretkey" ] || return 1 [ -n "${port}" ] || return 1 [ -n "${dev}" ] || return 1 [ -n "${host}" ] || return 1 mkdir -p "$key_dir" chmod 0700 "$key_dir" ( umask 077; printf '%s\n' "$key" > "$key_file" ) [ -f "$key_file" ] || { _err "can't create key file" return 1 } key="" if [ "$(uci -q get network.omrvpn)" != "" ]; then uci -q set network.omrvpn.device=${dev} uci -q commit network fi _log "starting ${PROG_NAME} ${1} instance $*" procd_open_instance procd_set_param command ${PROG} \ keyfile "$key_file" \ ${port:+port "$port"} \ ${host:+host "$host"} \ ${dev:+dev "$dev"} [ "${mode}" = "listener" ] && procd_append_param command listener [ "${mptcp}" = "1" ] && procd_append_param command mptcp [ "${chacha20}" = "1" ] && procd_append_param command chacha20 [ "${multiqueue}" = "1" ] && procd_append_param command multiqueue procd_append_param command \ retry count -1 const 5000000 \ timeout ${timeout} \ keepalive count 5 idle 20 interval 2 \ buffer-size 65536 procd_set_param respawn 0 30 0 procd_set_param nice 10 procd_set_param file "$key_file" procd_set_param stdout 1 procd_set_param stderr 1 procd_close_instance # Configure the tunnel addresses once glorytun has created the device. # RPS/XPS steering for it is set by /etc/hotplug.d/net/20-rps-xps # (openmptcprouter package), which handles every interface. if [ -n "$localip" ] && [ -n "$remoteip" ]; then ( count=0 while [ $count -lt 10 ]; do if [ -d "/sys/class/net/$dev" ]; then ifconfig "$dev" "$localip" pointopoint "$remoteip" up && \ _log "Configured $dev with IP $localip peer $remoteip" break fi sleep 1 count=$((count + 1)) done ) & fi } start_service() { config_load glorytun config_foreach start_instance glorytun } service_triggers() { procd_add_reload_trigger glorytun network }