#!/bin/sh /etc/rc.common

START=99

CFG_PATH="/proc/sys/net/ipv4/tcp_proxy_action"
SWITCH_PATH="/proc/sys/net/ipv4/tcp_proxy_switch"
set_switch_on="uci set http_status_stat.settings.enabled=1"
set_switch_off="uci set http_status_stat.settings.enabled=0"
set_switch_commit="uci commit http_status_stat"
restart_dnsmasq='/etc/init.d/dnsmasq restart'
LIP=`uci get network.lan.ipaddr 2>/dev/null`
LMASK=`uci get network.lan.netmask 2>/dev/null`
REFERER_STR="miwifi.com"
REFERER_PATH="/proc/http_conn/referer"
PROXY_PORT=8191
fastpath=""

APP_CTF_MGR="/usr/sbin/ctf_manger.sh"
export EXTRA_COMMANDS=" on off refresh_lan is_enabled"
export EXTRA_HELP="	on	Switch to the start state and start
	off	Switch to the stop state and stop
	is_enabled	is http status proxy enabled
	refresh_lan	refresh lan config when lanip change"

is_enabled() {
    config_load "http_status_stat"
    local switch
    switch=`uci get http_status_stat.settings.enabled -q`
    if [ $switch -eq "1" ]; then
        return 1
    fi
    return 0
}

refresh_lan() {
    is_enabled
    if [ $? -eq "1" ]; then
        logger -t "http_stat" "refresh lan config!"
        echo "ADD 5 $LIP $PROXY_PORT" > $CFG_PATH
    fi
}

start() {
    config_load "http_status_stat"
    local switch
    switch=`uci get http_status_stat.settings.enabled -q`
    if [ $switch -ne "1" ]; then
        return 0
    fi
    local cc=$(bdata get CountryCode)
    cc=${cc:-"CN"}
    if [ $cc != "CN" ]; then
        echo "http_stat: Bad Country!"
        return 0
    fi

    fastpath=`uci get misc.http_proxy.fastpath -q`
    [ -z $fastpath ] && return 0

    if [ $fastpath == "ctf" ]; then
        if [ -f $APP_CTF_MGR ]; then
            $APP_CTF_MGR http_stat http on
        fi
    elif [ $fastpath == "hwnat" ]; then
        echo "http status can work with hw_nat."
    else
        echo "http_status_stat.init: unknown fastpath type! Treat as std!"
    fi
    insmod nf_conn_ext_http >/dev/null 2>&1
    insmod nf_tcp_proxy >/dev/null 2>&1
    #echo "open and set proxy action to kernel"
    echo "ADD 5 $LIP $PROXY_PORT" > $CFG_PATH
    # ensure start switch
    echo "1" > $SWITCH_PATH
    insmod http_status_code_proxy >/dev/null 2>&1
    sysctl -w net.ipv4.tcp_timestamps=0 >/dev/null 2>&1
    [ -f $REFERER_PATH ] && echo $REFERER_STR > $REFERER_PATH 2>/dev/null
    return 0
}

stop() {
    rmmod http_status_code_proxy >/dev/null 2>&1
    rmmod nf_tcp_proxy >/dev/null 2>&1

    fastpath=`uci get misc.http_proxy.fastpath -q`
    [ -z $fastpath ] && return 0

    if [ $fastpath == "ctf" ]; then
        if [ -f $APP_CTF_MGR ]; then
            $APP_CTF_MGR http_stat http off
        fi
    elif [ $fastpath == "hwnat" ]; then
        echo "http status stopped."
    else
        echo "http_status_stat.init: unknown fastpath type! Treat as std!"
    fi
    return 0
}

off(){
    stop
    $set_switch_off >/dev/null 2>&1
    $set_switch_commit >/dev/null 2>&1
    $restart_dnsmasq
    return $?
}

on(){
    $set_switch_on >/dev/null 2>&1
    $set_switch_commit >/dev/null 2>&1
    $restart_dnsmasq
    start
    return $?
}

