#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#P2Pʶʧ
	LANG002="Error"		#WWWǿʧ
	LANG003="Error"		#Ѹǿʶʧ
	LANG004="Error"		#αIPʧ
	LANG005="Error"		#IPv6ʶ
	LANG006="Success"	#ɹ
	LANG007="Set_DPI"	#P2Pʶ
	LANG008="Set_DPI"	#WWWǿ
	LANG009="Set_DPI"	#Ѹǿʶ
	LANG010="Set_DPI"	#αIP
	LANG011="Set_DPI"	#IPv6ʶ
    LANG012="Error"    #NPMʱӷʧ
    LANG013="Set_NPM"	#NPMʱӷ
    LANG014="Set_GRE"   #GRE
    LANG015="Set_QUIC"   #QUIC
    LANG016="Set_udproxy" #UDPǿ
fi


load_dpi_stat()
{
    p2p_sntrack=`${FLOWEYE} dpi stat | grep "p2p_sntrack" | cut -d"=" -f2`
    http_trackdns=`${FLOWEYE} app get http | grep "trackdns" | cut -d"=" -f2`
    thunder_enable=`${FLOWEYE} thunder stat | grep "enable" | cut -d"=" -f2`
    ipverify_enable=`${FLOWEYE} ipobj stat | grep "ipverify_enable" | cut -d"=" -f2`
    quic_enable=`${FLOWEYE} httpsni stat | grep "quic_decrypt_enable" | cut -d"=" -f2`
    ipv6_enable=`${FLOWEYE} v6flow stat | grep "enable=" | cut -d"=" -f2`
    npm_enable=`${FLOWEYE} npmflow stat | grep "enable=" | cut -d"=" -f2`
    gre_decode=`${FLOWEYE} system stat | grep "gre_decode=" | cut -d"=" -f2`
    udproxy_enable=`${FLOWEYE} nat stat | grep "udproxy_enable=" | cut -d"=" -f2`
    account_share=`${FLOWEYE} rateobj stat | grep "account_share" | cut -d"=" -f2`
    trackja3_enable=`${FLOWEYE} dpi stat | grep "trackja3" | cut -d"=" -f2`

    [ "${trackja3_enable}" = "" ] && trackja3_enable=0

    #if [ "${TAP}" = "1" ]; then
    #    deduppkt_enable=`${FLOWEYE} deduppkt stat | grep "enable=" | cut -d"=" -f2`
    #fi
    
    printf "{"
    printf "\"npm_enable\":${npm_enable}"
    printf ",\"p2p_sntrack\":${p2p_sntrack}"
    printf ",\"gre_decode\":${gre_decode}"
    printf ",\"ipv6_enable\":${ipv6_enable}"
    printf ",\"http_trackdns\":${http_trackdns}"
    printf ",\"thunder_enable\":${thunder_enable}"
    printf ",\"quic_enable\":${quic_enable}"
    printf ",\"udproxy_enable\":${udproxy_enable}"
    printf ",\"ipverify_enable\":${ipverify_enable}"
    printf ",\"account_share\":${account_share}"
    printf ",\"trackja3_enable\":${trackja3_enable}"

    #if [ "${TAP}" = "1" ]; then
    #    printf ",\"deduppkt_enable\":${deduppkt_enable}"
    #fi

    printf "}"
}

load_dup_pac(){
    deduppkt_enable=`${FLOWEYE} deduppkt stat | grep "enable=" | cut -d"=" -f2`
    cond=`${FLOWEYE} deduppkt stat | grep "cond=" | cut -d"=" -f2`
    mode=`${FLOWEYE} deduppkt stat | grep "mode=" | cut -d"=" -f2`


    printf "{"
    printf "\"deduppkt_enable\":${deduppkt_enable}"
    printf ",\"cond\":\"${cond}\""
    printf ",\"mode\":\"${mode}\""
    printf "}"
}


set_dpi_stat()
{
    case "${CGI_name}" in
        "p2p_sntrack")
            errmsg=`${FLOWEYE} dpi config p2p_sntrack=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG001:=P2Pʶʧ}:${errmsg}"
            WEB_LOGGER "${LANG007:=P2Pʶ}" "enable=${CGI_enable}"
            sync_floweye "dpi config p2p_sntrack=${CGI_enable}"
            ;;

        "http_trackdns")
            errmsg=`${FLOWEYE} app set http trackdns=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG002:=WWWǿʧ}:${errmsg}"
            WEB_LOGGER "${LANG008:=WWWǿ}" "enable=${CGI_enable}"
            sync_floweye "app set http trackdns=${CGI_enable}"
            ;;

        "thunder_enable")
            errmsg=`${FLOWEYE} thunder config enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG003:=Ѹǿʶʧ}:${errmsg}"
            WEB_LOGGER "${LANG009:=Ѹǿʶ}" "enable=${CGI_enable}"
            sync_floweye "thunder config enable=${CGI_enable}"
            ;;

        "ipverify_enable")
            errmsg=`${FLOWEYE} ipobj config ipverify_enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG004:=αIPʧ}:${errmsg}"
            WEB_LOGGER "${LANG010:=αIP}" "enable=${CGI_enable}"
            sync_floweye "ipobj config ipverify_enable=${CGI_enable}"
            ;;

        "ipv6_enable")
            errmsg=`${FLOWEYE} v6flow config enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG005:=IPv6ʶ}:${errmsg}"
            WEB_LOGGER "${LANG011:=IPv6ʶ}" "enable=${CGI_enable}"
            sync_floweye "v6flow config enable=${CGI_enable}"
            ;;

        "npm_enable")
            ${FLOWEYE} npmhost config enable=${CGI_enable}
            errmsg=`${FLOWEYE} npmflow config enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "${LANG013:=NPMʱӷ}" "enable=${CGI_enable}"
            sync_floweye "npmflow config enable=${CGI_enable}"
            ;;

        "gre_decode")
            ${FLOWEYE} system config gre_decode=${CGI_enable}
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "${LANG014:=GRE}" "enable=${CGI_enable}"
            sync_floweye "system config gre_decode=${CGI_enable}"
            ;;

        "quic_enable")
            errmsg=`${FLOWEYE} httpsni config quic_decrypt_enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "${LANG015:=QUIC}" "enable=${CGI_enable}"
            sync_floweye "httpsni config quic_decrypt_enable=${CGI_enable}"
            ;;

        "udproxy_enable")
            errmsg=`${FLOWEYE} nat config udproxy_enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "${LANG016:=UDPǿ}" "enable=${CGI_enable}"
            sync_floweye "nat config udproxy_enable=${CGI_enable}"
            ;;

        "account_share")
            errmsg=`${FLOWEYE} rateobj config account_share=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "˺Ź" "enable=${CGI_enable}"
            sync_floweye "rateobj config account_share=${CGI_enable}"
            ;;

        "deduppkt_enable")
            errmsg=`${FLOWEYE} deduppkt config enable=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "ݰȥ" "enable=${CGI_enable}"
            sync_floweye "deduppkt config enable=${CGI_enable}"
            ;;

        "trackja3_enable")
            errmsg=`${FLOWEYE} dpi config trackja3=${CGI_enable}`
            [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
            WEB_LOGGER "HTTPSЭ̷" "enable=${CGI_enable}"
            sync_floweye "dpi config trackja3=${CGI_enable}"
            ;;

        *)
            retjson 1 "UNKNOW_NAME"
            ;;

    esac

    retjson 0 "${LANG006:=ɹ}"
}

set_dpi_deduppkt_con(){
    errmsg=`${FLOWEYE} deduppkt config enable=${CGI_enable} cond=${CGI_cond}`
    [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
    WEB_LOGGER "ݰȥ" "enable=${CGI_enable} cond=${CGI_cond}"
    retjson 0 "${LANG006:=ɹ}"
}

set_mode_stat(){
    errmsg=`${FLOWEYE} deduppkt config mode=${CGI_mode}`
    [ $? -ne 0 ] && retjson "${LANG012:=ʧ}:${errmsg}"
    WEB_LOGGER "ȥģʽ" "mode=${CGI_mode}"
    retjson 0 "${LANG006:=ɹ}"
}

case "${CGI_action}" in
    "load_dpi_stat")
        retjson 0 "OK" "`load_dpi_stat`"
        ;;

    "load_dup_pac")
        retjson 0 "OK" "`load_dup_pac`"
        ;;

    "set_dpi_stat")
        action_check
        set_dpi_stat
        ;;

    "set_dpi_deduppkt_con")
        action_check
        set_dpi_deduppkt_con
        ;;

    "set_mode_stat")
        action_check
        set_mode_stat
        ;;
    
    *)
        retjson 1 "UNKNOW_ACTION"
        ;;

esac
