#!/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_url_proxy.settings.enabled=1"
set_switch_off="uci set http_url_proxy.settings.enabled=0"
set_switch_commit="uci commit http_url_proxy"
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`
PROXY_PORT=8381
HIMAN_PORT=8080
ssl_port=443
local_ssl_port=8443
fastpath=""

APP_CTF_MGR="/usr/sbin/ctf_manger.sh"
export EXTRA_COMMANDS=" on off log_stat"
export EXTRA_HELP="	on	Switch to the start state and start
	off	Switch to the stop state and stop
	log_stat	log statistics"

KR_EXECMD="/usr/sbin/tquery"
KR_EXTRA_FLAG="/usr/sbin/tquery"

# only R1CL now
is_support_model() {
    boardversion=`cat /proc/xiaoqiang/model 2>/dev/null`
    if [ $boardversion == "R1CL" ]; then
        return 1
    fi
    return 0
}

is_repeater() {
    is_support_model
    if [ $? -eq 1 ]; then
        netmode=`uci -q -S get xiaoqiang.common.NETMODE`
        if [ "$netmode" == "lanapmode" ]; then
            return 1
        elif [ "$netmode" == "wifiapmode" ]; then
            return 2
        fi
    fi
    return 0
}

log_stat_normal() {
    url_stat_file="/proc/http_url/url_stats"
    if [ -f $url_stat_file ]; then
        line=`cat $url_stat_file`
        v1=`echo $line | awk -F ";" '{print $1}' | awk -F ":" '{print $2}'`
        v2=`echo $line | awk -F ";" '{print $2}' | awk -F ":" '{print $2}'`
        v3=`echo $line | awk -F ";" '{print $3}' | awk -F ":" '{print $2}'`
        v4=`echo $line | awk -F ";" '{print $4}' | awk -F ":" '{print $2}'`
        v5=`echo $line | awk -F ";" '{print $5}' | awk -F ":" '{print $2}'`
        v6=`echo $line | awk -F ";" '{print $6}' | awk -F ":" '{print $2}'`
        v7=`echo $line | awk -F ";" '{print $7}' | awk -F ":" '{print $2}'`
        v8=`echo $line | awk -F ";" '{print $8}' | awk -F ":" '{print $2}'`
        logger stat_points_privacy reap_url_stat=$v1,$v2,$v3,$v4,$v5,$v6,$v7,$v8
        echo "0" > $url_stat_file
    fi
}

br_log_stat() {
    url_stat_file="/proc/br_http_url/url_stats"
    if [ -f $url_stat_file ]; then
        line=`cat $url_stat_file`
        v1=`echo $line | awk -F ";" '{print $1}' | awk -F ":" '{print $2}'`
        v2=`echo $line | awk -F ";" '{print $2}' | awk -F ":" '{print $2}'`
        v3=`echo $line | awk -F ";" '{print $3}' | awk -F ":" '{print $2}'`
        v4=`echo $line | awk -F ";" '{print $4}' | awk -F ":" '{print $2}'`
        logger stat_points_privacy br_reap_url_stat=$v1,$v2,$v3,$v4
        echo "0" > $url_stat_file
    fi
}

log_stat() {
    is_repeater
    if [ $? -eq "1" ]; then
        br_log_stat
    else
        log_stat_normal
    fi
}

init_kmod() {
    insmod nf_conn_ext_http >/dev/null 2>&1
    is_repeater
    type=$?
    if [ $type -eq "1" ]; then
        insmod br_http up_devname="eth0.2" >/dev/null 2>&1
        insmod br_http_url >/dev/null 2>&1
    elif [ $type -eq "2" ]; then
        insmod br_http up_devname="apcli0" >/dev/null 2>&1
        insmod br_http_url >/dev/null 2>&1
    else
        insmod nf_tcp_proxy >/dev/null 2>&1
        #echo "open and set proxy action to kernel"
        echo "ADD 7 $LIP $PROXY_PORT" > $CFG_PATH
        echo "ADD 10 $LIP $HIMAN_PORT" > $CFG_PATH
        # ensure start switch
        echo "1" > $SWITCH_PATH
        insmod http_url_plus >/dev/null 2>&1
        sysctl -w net.ipv4.tcp_timestamps=0 >/dev/null 2>&1
    fi
}

start() {
    #if in ft_mode, do not start!
    ft_mode=`cat /proc/xiaoqiang/ft_mode`
    if [ "$ft_mode" -ne "0" ]; then
	return 0
    fi

    config_load "http_url_proxy"
    etc_size=$(df |grep /etc|awk 'BEGIN{t=0;} { t++; } END{if(t==1) print $4}')
    local switch
    switch=`uci get http_url_proxy.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_url_proxy: can work with hw_nat."
    else
        echo "http_url_proxy.init: unknown fastpath type! Treat as std!"
    fi
    init_kmod

    ipset flush    tquery
    ipset destroy  tquery
    ipset create   tquery hash:net
    iptables -t mangle -A fwmark -p tcp -m set --match-set tquery dst -m comment --comment tquery -j MARK --set-xmark 0x4/0x4
    ipset_redirect.sh create tquery_s $ssl_port $local_ssl_port

uci -q batch <<-EOF >/dev/null
    set http_url_proxy.settings.etc_size=${etc_size}
    commit http_url_proxy
EOF

uci -q batch <<-EOF >/dev/null
    set firewall.kr_query=include
    set firewall.kr_query.path="/lib/firewall.sysapi.loader kr_query"
    set firewall.kr_query.reload=1
    commit firewall
EOF

    export PROCLINE="${KR_EXECMD}"
    export PROCFLAG="${KR_EXTRA_FLAG}"
    export PROCNUM='1'
    /usr/sbin/supervisord start

    return 0
}

stop() {
    kill -9 `cat /tmp/tquery.pid`
    export PROCLINE="${KR_EXECMD}"
    export PROCFLAG="${KR_EXTRA_FLAG}"
    /usr/sbin/supervisord stop

    # log stat before stop
    log_stat
    rmmod http_url_plus >/dev/null 2>&1
    rmmod nf_tcp_proxy >/dev/null 2>&1
    # for repeater mode
    rmmod br_http_url >/dev/null 2>&1
    rmmod br_http >/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_url_proxy: stopped."
    else
        echo "http_url_proxy: unknown fastpath type! Treat as std!"
    fi

uci -q batch <<-EOF >/dev/null
    del firewall.kr_query
    commit firewall
EOF

    ipset_redirect.sh destroy tquery_s $ssl_port $local_ssl_port
    iptables -t mangle -D fwmark -p tcp -m set --match-set tquery dst -m comment --comment tquery -j MARK --set-mark 0x04/0x00000004
    ipset flush    tquery
    ipset destroy  tquery

    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 $?
}

