selfcare init

This commit is contained in:
zhangsz
2025-03-03 11:40:37 +08:00
parent 19f09dd7ea
commit aca2bace68
692 changed files with 273972 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package Nrestful
import (
mysql "proxy/Nmysql"
rdb "proxy/Nredis"
"strconv"
"strings"
)
func RdbScanAlertSmsRecord() {
keys := rdb.RdbScanAlertSmsRecord()
for _, key := range keys {
if strings.HasPrefix(key, "AlertSms:") {
smsContent := rdb.RdbGetAlertSmsContent(key)
if smsContent != "" {
ss := strings.Split(key, ":")
if len(ss) >= 3 {
SendNtfSms2Ocs(ss[1], smsContent)
alertId, _ := strconv.Atoi(ss[2])
_ = mysql.SetAlertSmsState2Sent(alertId)
}
}
rdb.RdsDelMsg2OcsKey(key)
} else if strings.HasPrefix(key, "CreateAcct:") {
ai := rdb.RdbGetCreateAcct(key)
if ai != nil {
ss := strings.Split(key, ":")
if len(ss) >= 3 {
cugId, err := mysql.QueryOfrIdByPrdInstId(ai.PrdInstId)
userClass := 1
ofrLevel, err := mysql.QueryOfrLevelByOfrId(ai.OfrId)
if err == nil {
userClass = ofrLevel
}
rent, _ := mysql.QueryRentByOfrId(ai.OfrId)
//SendCrtAcct2Ocs(ss[1], ai, cugId, userClass, rent)
CreateAcct2Ocs(ss[1], ai, cugId, userClass, rent, key)
}
}
}
}
//for _, key := range keys {
// rdb.RdsDelMsg2OcsKey(key)
//}
return
}

View File

@@ -0,0 +1,575 @@
package Nrestful
import (
"time"
"encoding/binary"
_ "strings"
"strconv"
l4g "proxy/logger"//"github.com/sirupsen/logrus"
)
func decode_authcode_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.AuthCodeRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.AuthCodeRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
err_code := binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.AuthCodeRsp.Code = strconv.Itoa(int(err_code))
rsp.AuthCodeRsp.Message = Rest_proxy_errcodemsg(err_code)
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_authcode_rsp: %#v", rsp.AuthCodeRsp)
return 0;
}
func decode_query_userData_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.QueryUserDataRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.QueryUserDataRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
err_code := binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.QueryUserDataRsp.Code = strconv.Itoa(int(err_code))
rsp.QueryUserDataRsp.Message = Rest_proxy_errcodemsg(err_code)
case IE_MSISDN:
rsp.QueryUserDataRsp.TelNumber = string(buf[offset:offset+int(vallen)])
case IE_REMARK:
rsp.QueryUserDataRsp.Remark = string(buf[offset:offset+int(vallen)])
case IE_GROUP_NAME:
rsp.QueryUserDataRsp.UserGroupName = string(buf[offset:offset+int(vallen)])
case IE_STATUS:
rsp.QueryUserDataRsp.Status = strconv.Itoa(int(buf[offset]))
case IE_BALANCE:
rsp.QueryUserDataRsp.Balance = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_EXPIRY_TIME:
tm := time.Unix(int64(binary.BigEndian.Uint64(buf[offset:offset+8])), 0)
rsp.QueryUserDataRsp.ExpireDate = tm.Format("2006-01-02 15:04:05")
case IE_REMAIN_MO_VOICE_MIN:
rsp.QueryUserDataRsp.RemainMoVoiceMinute = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_REMAIN_MT_VOICE_MIN:
rsp.QueryUserDataRsp.RemainMtVoiceMinute = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_REMAIN_SMS_NUM:
rsp.QueryUserDataRsp.RemainSmsVolume = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_REMAIN_DATA_VOL_MB:
rsp.QueryUserDataRsp.RemainDataVolume = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_query_userData_rsp: %#v", rsp.QueryUserDataRsp)
return 0;
}
func decode_bundleSubs_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.BundleSubsRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.BundleSubsRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
err_code := binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.BundleSubsRsp.Code = strconv.Itoa(int(err_code))
rsp.BundleSubsRsp.Message = Rest_proxy_errcodemsg(err_code)
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_bundleSubs_rsp: %#v", rsp.BundleSubsRsp)
return 0
}
func decode_query_balance_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.QueryBalanceRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.QueryBalanceRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
err_code := binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.QueryBalanceRsp.Code = strconv.Itoa(int(err_code))
rsp.QueryBalanceRsp.Message = Rest_proxy_errcodemsg(err_code)
case IE_STATUS:
rsp.QueryBalanceRsp.Status = strconv.Itoa(int(buf[offset]))
case IE_BALANCE:
rsp.QueryBalanceRsp.Balance = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_MO_EXPIRY:
tm := time.Unix(int64(binary.BigEndian.Uint32(buf[offset:offset+4])), 0)
rsp.QueryBalanceRsp.ExpireDate = tm.Format("2006-01-02 15:04:05")
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_query_balance_rsp: %#v", rsp.QueryBalanceRsp)
return 0
}
func decode_recharge_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.RechargeRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.RechargeRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
var err_code uint32
if vallen == 1 {
err_code = uint32(buf[offset])
} else {
err_code = binary.BigEndian.Uint32(buf[offset:offset+4])
}
rsp.RechargeRsp.Code = strconv.Itoa(int(err_code))
rsp.RechargeRsp.Message = Rest_proxy_errcodemsg(err_code)
case IE_STATUS:
rsp.RechargeRsp.Status = strconv.Itoa(int(buf[offset]))
case IE_BALANCE:
rsp.RechargeRsp.Balance = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
case IE_MO_EXPIRY:
tm := time.Unix(int64(binary.BigEndian.Uint32(buf[offset:offset+4])), 0)
rsp.RechargeRsp.ExpiredTimestamp = tm.Format("2006-01-02 15:04:05")
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_recharge_rsp: %#v", rsp.RechargeRsp)
return 0
}
func decode_transfer_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.TransferRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.TransferRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
var err_code uint32
if vallen == 1 {
err_code = uint32(buf[offset])
} else {
err_code = binary.BigEndian.Uint32(buf[offset:offset+4])
}
rsp.TransferRsp.Code = strconv.Itoa(int(err_code))
rsp.TransferRsp.Message = Rest_proxy_errcodemsg(err_code)
case IE_AMOUNT:
rsp.TransferRsp.Balance = strconv.Itoa(int(binary.BigEndian.Uint32(buf[offset:offset+4])))
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_transfer_rsp: %#v", rsp.TransferRsp)
return 0
}
func decode_create_acct_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.CreateAccountRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.CreateAccountRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
var err_code uint32
if vallen == 1 {
err_code = uint32(buf[offset])
} else {
err_code = binary.BigEndian.Uint32(buf[offset:offset+4])
}
rsp.CreateAccountRsp.Code = strconv.Itoa(int(err_code))
rsp.CreateAccountRsp.Message = Rest_proxy_errcodemsg(err_code)
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_create_acct_rsp: %#v", rsp.CreateAccountRsp)
return 0
}
func decode_update_subs_req(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
rsp.UpdateSubsReq.OptFlag = 0
for {
if offset >= msglen {
break
}
tag = buf[offset]
offset++
vallen = buf[offset]
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_MSISDN:
rsp.UpdateSubsReq.TelNumber = string(buf[offset:offset+int(vallen)])
case IE_ACCOUNT_ID:
rsp.UpdateSubsReq.AccountIdU32 = binary.BigEndian.Uint32(buf[offset:offset+4])
case IE_STATUS:
rsp.UpdateSubsReq.StatusU8 = buf[offset]
rsp.UpdateSubsReq.OptFlag |= 0x01
case IE_BALANCE:
rsp.UpdateSubsReq.BalanceU32 = binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.UpdateSubsReq.OptFlag |= 0x02
case IE_EXPIRY_TIME:
rsp.UpdateSubsReq.ExpDateU32 = binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.UpdateSubsReq.OptFlag |= 0x04
case IE_PLAN_ID:
rsp.UpdateSubsReq.PackageIdU32 = binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.UpdateSubsReq.OptFlag |= 0x08
case IE_RENT_CHARGE:
rsp.UpdateSubsReq.RentChargeU32 = binary.BigEndian.Uint32(buf[offset:offset+4])
rsp.UpdateSubsReq.OptFlag |= 0x10
case IE_VAS_CUG_STATUS:
rsp.UpdateSubsReq.VasCugStatusU8 = buf[offset]
rsp.UpdateSubsReq.OptFlag |= 0x20
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_update_subs_req: %#v", rsp.UpdateSubsReq)
return 0
}
func decode_update_subs_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset];
offset++
vallen = buf[offset];
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
if result == 0 {
rsp.UpdateSubsRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.UpdateSubsRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
var err_code uint32
if vallen == 1 {
err_code = uint32(buf[offset])
} else {
err_code = binary.BigEndian.Uint32(buf[offset:offset+4])
}
rsp.UpdateSubsRsp.Code = strconv.Itoa(int(err_code))
rsp.UpdateSubsRsp.Message = Rest_proxy_errcodemsg(err_code)
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_update_subs_rsp: %#v", rsp.UpdateSubsRsp)
return 0
}
func decode_delete_subs_rsp(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
var result byte = 1
for {
if offset >= msglen {
break
}
tag = buf[offset]
offset++
vallen = buf[offset]
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_RESULT:
result = buf[offset]
rsp.DeleteSubsRsp.Result = result
if result == 0 {
rsp.DeleteSubsRsp.Code = strconv.Itoa(int(ERRCODE_SUCCESS))
rsp.DeleteSubsRsp.Message = Rest_proxy_errcodemsg(ERRCODE_SUCCESS)
}
case IE_ERROR_CODE:
var err_code uint32
if vallen == 1 {
err_code = uint32(buf[offset])
} else {
err_code = binary.BigEndian.Uint32(buf[offset:offset+4])
}
rsp.DeleteSubsRsp.ErrorCode = err_code
rsp.DeleteSubsRsp.Code = strconv.Itoa(int(err_code))
rsp.DeleteSubsRsp.Message = Rest_proxy_errcodemsg(err_code)
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decode_update_subs_rsp: %#v", rsp.UpdateSubsRsp)
return 0
}
func decodeUpdateBundleUsageReq(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
var tag byte = 0
var vallen byte = 0
for {
if offset >= msglen {
break
}
tag = buf[offset]
offset++
vallen = buf[offset]
offset++
switch tag {
case IE_SRC_REF:
rsp.Src_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_DST_REF:
rsp.Dst_id = binary.BigEndian.Uint16(buf[offset:offset+2])
case IE_MSISDN:
rsp.UpdateBundleUsageReq.Msisdn = string(buf[offset:offset+int(vallen)])
case IE_SERVICE_TYPE:
rsp.UpdateBundleUsageReq.ServiceType = buf[offset]
case IE_ACCOUNT_ID:
rsp.UpdateBundleUsageReq.AccountId = binary.BigEndian.Uint32(buf[offset:offset+4])
case IE_PLAN_ID:
rsp.UpdateBundleUsageReq.PlanId = binary.BigEndian.Uint32(buf[offset:offset+4])
case IE_BUNDLE_ID:
rsp.UpdateBundleUsageReq.BundleId = binary.BigEndian.Uint32(buf[offset:offset+4])
case IE_PLAN_VALUE:
rsp.UpdateBundleUsageReq.PlanValue = binary.BigEndian.Uint64(buf[offset:offset+8])
case IE_PLAN_USED_VALUE:
rsp.UpdateBundleUsageReq.PlanUsedValue = binary.BigEndian.Uint64(buf[offset:offset+8])
case IE_SESS_UPDATE_TIME:
rsp.UpdateBundleUsageReq.SessUpdateTime = binary.BigEndian.Uint32(buf[offset:offset+4])
case IE_PLAN_VALUE_ADD_THIS_TIME:
rsp.UpdateBundleUsageReq.PlanValueAddThisTime = binary.BigEndian.Uint64(buf[offset:offset+8])
default:
}
offset += int(vallen)
}
l4g.RestLog.Debugf("decodeUpdateBundleUsageReq: %#v", rsp.UpdateSubsRsp)
return 0
}
func Decode_udp_msg(buf []byte, msglen int, rsp *RestRsp) int {
var offset int = 0
if msglen <= 9 {
return -1
}
rsp.MsgType = buf[offset]
offset++
switch (rsp.MsgType) {
case REST_SEND_AUTHCODE_RSP:
return decode_authcode_rsp(buf[1:], msglen-1, rsp)
case REST_QUERY_USERDATA_RSP:
return decode_query_userData_rsp(buf[1:], msglen-1, rsp)
case REST_BUNDLE_SUBS_RSP:
return decode_bundleSubs_rsp(buf[1:], msglen-1, rsp)
case REST_RECHARGE_RSP:
return decode_recharge_rsp(buf[1:], msglen-1, rsp)
case REST_TRANSFER_RSP:
return decode_transfer_rsp(buf[1:], msglen-1, rsp)
case REST_QUERY_BALANCE_RSP:
return decode_query_balance_rsp(buf[1:], msglen-1, rsp)
case REST_CRM_CREATE_ACCT_RES:
return decode_create_acct_rsp(buf[1:], msglen-1, rsp)
case REST_CRM_UPDATE_SUBS_REQ:
return decode_update_subs_req(buf[1:], msglen-1, rsp)
case REST_CRM_UPDATE_SUBS_RES:
return decode_update_subs_rsp(buf[1:], msglen-1, rsp)
case REST_CRM_DELETE_SUBS_RES:
return decode_delete_subs_rsp(buf[1:], msglen-1, rsp)
case REST_CRM_UPDATE_PLAN_INFO_REQ:
return decodeUpdateBundleUsageReq(buf[1:], msglen-1, rsp)
default:
return -1
}
}

View File

@@ -0,0 +1,44 @@
package Nrestful
import (
)
const ERRCODE_SUCCESS uint32 = 2001
const ERRCODE_COM_UNSUPPORT uint32 = 3001
const ERRCODE_UNKNOWN_PEER uint32 = 3011
const ERRCODE_INVALID_USER_STATUS uint32 = 4001
const ERRCODE_DEST_USER_NOT_ALLOWED uint32 = 4008
const ERRCODE_CREDIT_LIMIT uint32 = 4012
const ERRCODE_INVALID_PARAM_VALUE uint32 = 5004
const ERRCODE_MISSING_PARAM uint32 = 5005
const ERRCODE_INVALID_RECHARGE_PASSWORD uint32 = 5006
const ERRCODE_UNABLE_TO_COMPLY uint32 = 5012
const ERRCODE_USER_UNKNOWN uint32 = 5030
const MAX_ERRCODE uint32 = 5000
var restCode2StrMap = make(map[uint32]string)
func init() {
restCode2StrMap[ERRCODE_SUCCESS] = "success"
restCode2StrMap[ERRCODE_COM_UNSUPPORT] = "command unsupported"
restCode2StrMap[ERRCODE_UNKNOWN_PEER] = "unknown peer"
restCode2StrMap[ERRCODE_INVALID_USER_STATUS] = "invalid user status"
restCode2StrMap[ERRCODE_DEST_USER_NOT_ALLOWED] = "dest user not allowed"
restCode2StrMap[ERRCODE_CREDIT_LIMIT] = "credit limit"
restCode2StrMap[ERRCODE_INVALID_PARAM_VALUE] = "invalid parameter value"
restCode2StrMap[ERRCODE_MISSING_PARAM] = "missing parameter"
restCode2StrMap[ERRCODE_INVALID_RECHARGE_PASSWORD] = "invalid recharge password"
restCode2StrMap[ERRCODE_UNABLE_TO_COMPLY] = "unable to comply"
restCode2StrMap[ERRCODE_USER_UNKNOWN] = "user unknown"
}
func Rest_proxy_errcodemsg(errcode uint32) string {
errStr, ok := restCode2StrMap[errcode]
if !ok {
return "unkown"
} else {
return errStr
}
}

View File

@@ -0,0 +1,104 @@
package Nrestful
import (
mysql "proxy/Nmysql"
rds "proxy/Nredis"
"proxy/config"
)
func handleOcsDeleteSubsRsp(req *RestRsp) {
}
func handleOcsUpdateSubsReq(req *RestRsp) {
expNtfFlag := false
var expiredDate int64= 0
if req.UpdateSubsReq.OptFlag & 0x04 == 0x04 {
expNtfFlag = true
expiredDate = int64(req.UpdateSubsReq.ExpDateU32)
}
accountId := req.UpdateSubsReq.AccountIdU32
if expNtfFlag {
planId := req.UpdateSubsReq.PackageIdU32
_ = mysql.InsertExpiredNotifySms(accountId, planId, expiredDate)
} else {
status := req.UpdateSubsReq.StatusU8
_ = mysql.UpdateUserStatus2Mdb(accountId, status)
}
}
func handleUpdatePlanUsage(req *RestRsp) {
opFlag := false
thisTimeAdded := req.UpdateBundleUsageReq.PlanValueAddThisTime
if thisTimeAdded & 0x8000000000000000 == 0x8000000000000000 {
opFlag = true
}
thisTimeAdded &= 0x7FFFFFFFFFFFFFFF
totalValue := req.UpdateBundleUsageReq.PlanValue
if totalValue <= 0 {
return
}
usedValue := req.UpdateBundleUsageReq.PlanUsedValue
serviceType := req.UpdateBundleUsageReq.ServiceType
rateUsed, oldRateUsed := 0, 0
sendNotifyFlag := false
if bSendNotifySms(serviceType) {//(service_type == 2)//data only; 1: voice; 2: data; 3: sms;
rateUsed = int(usedValue*100 / totalValue)
if rateUsed > 75 {
oldRateUsed = int((usedValue-thisTimeAdded)*100 / totalValue)
if oldRateUsed <= 75 {
sendNotifyFlag = true
rateUsed = 75
}
}
}
updateExpiryDate := false
if usedValue >= totalValue {
usedValue = totalValue
rateUsed = 100
updateExpiryDate = true
if false {
sendNotifyFlag = true
}
}
bundleId := req.UpdateBundleUsageReq.BundleId
if serviceType == 2 {
mysql.UpdateDataPlanInfo(bundleId, thisTimeAdded, opFlag)
} else {
mysql.UpdateVoiceAndSmsPlanInfo(serviceType, bundleId, usedValue, totalValue, updateExpiryDate)
}
if sendNotifyFlag {
planId := req.UpdateBundleUsageReq.PlanId
if !rds.CheckIfBundleLimitSent(planId, rateUsed) {
accountId := req.UpdateBundleUsageReq.AccountId
mysql.AddNotificationSms(accountId, planId, serviceType, updateExpiryDate, rateUsed)
rds.SetPlanExpire(planId, rateUsed)
}
}
}
func bSendNotifySms(serviceType byte) bool {
ntfCfg := &config.Config.BundleUsageNotify
switch serviceType {
case 1:
if ntfCfg.Voice75Percent {
return true
}
case 2:
if ntfCfg.Data75Percent {
return true
}
case 3:
if ntfCfg.Sms75Percent {
return true
}
default:
}
return true
}

View File

@@ -0,0 +1,92 @@
package Nrestful
import (
l4g "proxy/logger" //"github.com/sirupsen/logrus"
"sync"
)
var mutex sync.Mutex
type Port struct {
CurIndex int
UseFlag int
Dst_id uint16
Rsp chan RestRsp
}
const MAX_PORT_NUM uint16 = 8192
type PortMngt struct {
Seq uint16
CurIndex uint16
Ports [MAX_PORT_NUM]Port
}
var portMngt PortMngt
func AssignPort(req *RestReq) *Port {
var id uint16 = 0
var i uint16 = 0
var port *Port = nil
mutex.Lock()
for i=0; i<MAX_PORT_NUM; i++ {
id = (i+portMngt.CurIndex) % MAX_PORT_NUM
if portMngt.Ports[id].UseFlag == 0 {
portMngt.CurIndex = (id + 1) % MAX_PORT_NUM
portMngt.Ports[id].UseFlag = 1
req.Src_id = id
req.Dst_id = portMngt.Seq
portMngt.Ports[id].Dst_id = req.Dst_id
portMngt.Seq ++
port = &portMngt.Ports[id]
break
}
}
mutex.Unlock()
return port
}
func GetPortInfo(src_id uint16, dst_id uint16) *Port {
if src_id < 0 || src_id >= MAX_PORT_NUM {
return nil
}
port := &portMngt.Ports[src_id]
if port.UseFlag == 0 || port.Dst_id != dst_id {
l4g.RestLog.Errorf("Get port error, src[%d], dst[%d], used_flag[%d]", src_id, dst_id, port.UseFlag)
return nil
}
return port
}
/*func ClosePortChan(id int, ts int64) {
if id < 0 || id >= MAX_PORT_NUM {
return
}
mutex.Lock()
if portMngt.Ports[id].UseFlag && (ts == portMngt.Ports[id].Ts) {
close(portMngt.Ports[id].Rsp)
}
mutex.Unlock()
return
}*/
func ReleasePort(id uint16) int {
if id < 0 || id >= MAX_PORT_NUM {
return -1
}
mutex.Lock()
portMngt.Ports[id].UseFlag = 0
mutex.Unlock()
return 0
}

View File

@@ -0,0 +1,353 @@
package Nrestful
import (
)
type AuthCodeReq struct {
CODE_TYPE string `json:"CODE_TYPE"`// ignore?
CUST_CODE uint32 `json:"CUST_CODE"`// ignore?
Content string `json:"content"`
TelNumber string `json:"telNumber"`
}
type AuthCodeRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
}
type QueryUserDataReq struct {
TelNumber string `json:"telNumber"`
}
type QueryUserDataRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
Status string `json:"status"`// from uint8
Remark string `json:"remark"`
TelNumber string `json:"telNumber"`
UserGroupName string `json:"userGroupName"`
Balance string `json:"balance"`// from uint32
ExpireDate string `json:"expireDate"`// "YYYY-MM-DD HH:MM:SS"
VoiceMinute string `json:"VoiceMinute,omitempty"`// from uint32
RemainMoVoiceMinute string `json:"remainVoiceMinute"`// from uint32
RemainMtVoiceMinute string `json:"remainMtVoiceMinute,omitempty"`// from uint32
SmsVolume string `json:"smsVolume,omitempty"`// from uint32
RemainSmsVolume string `json:"remainSmsVolume"`// from uint32
DataVolume string `json:"dataVolume,omitempty"`// from uint32
RemainDataVolume string `json:"remainDataVolume"`// from uint32
}
type BundleSubsReq struct {
TelNumber string `json:"telNumber"`
PayType string `json:"payType"`// from uint8
ChargedAmount string `json:"chargedAmount"`// from uint32
MoVoiceMinute string `json:"moVoiceMinute"`// from uint32
MtVoiceMinute string `json:"mtVoiceMinute"`// from uint32
DataVolume string `json:"dataVolume"`// from uint32, in MB
SmsVolume string `json:"smsVolume"`// from uint32
ValidPeriod string `json:"validPeriod"`// from uint32
}
type BundleSubsRsp struct {
Code string `json:"code"`// 2001: succ; else: fail// from uint32
Message string `json:"message"`
}
type BundleUsageReq struct {
TelNumber string `json:"telNumber"`
}
type BundleUsageRsp struct {
Code uint32 `json:"code"`// 2001: succ; else: fail
Message string `json:"message"`
MoVoiceInSecond uint32 `json:"moVoiceInSecond"`
MtVoiceInSecond uint32 `json:"mtVoiceInSecond"`
DataVolumeInKB uint32 `json:"dataVolumeInKB"`
SmsVolume uint32 `json:"smsVolume"`
ExpiredTimestamp string `json:"expiredTimestamp"`// "YYYY-MM-DD HH:MM:SS"
}
type RechargeReq struct {
AccountId string `json:"accountId"`
Amount string `json:"amount"`// to uint32
OpType string `json:"opType"`// to uint8
StaffId string `json:"staffId"`
TelNumber string `json:"telNumber"`
ValidyDays string `json:"validyDays"`// to uint16
}
type RechargeRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
Status string `json:"status"` // from uint8
Balance string `json:"balance"`// from uint32
ExpiredTimestamp string `json:"expiredTimestamp"`// "YYYY-MM-DD HH:MM:SS"
}
type TransferReq struct {
TransferOut string `json:"transferOut"`
TransferIn string `json:"transferIn"`
Amount string `json:"amount"`// from uint32
}
type TransferRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
Balance string `json:"balance"`// from uint32
}
type RechargeCardReq struct {
TelNumber string `json:"telNumber"`
CardPwd string `json:"cardPwd"`
}
type RechargeCardRsp struct {
Code uint32 `json:"code"`// 2001: succ; else: fail
Message string `json:"message"`
RechargeAmount uint32 `json:"rechargeAmount"`
Balance uint32 `json:"balance"`
ExpiredTimestamp string `json:"expiredTimestamp"`// "%02d-%02d-%02d %02d:%02d:%02d"
}
type CheckBalanceReq struct {
TelNumber string `json:"telNumber"`
Amount uint32 `json:"amount"`
}
type CheckBalanceRsp struct {
Code uint32 `json:"code"`// 2001: succ; else: fail
Message string `json:"message"`
Available uint32 `json:"available"`
}
type QueryBalanceReq struct {
TelNumber string `json:"telNumber"`
}
type QueryBalanceRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
Status string `json:"status"`// from uint8
Balance string `json:"balance"`// from uint32
ExpireDate string `json:"expireDate"`// "YYYY-MM-DD HH:MM:SS"
}
type QueryRechargeCardReq struct {
TelNumber string `json:"telNumber"`
}
type QueryRechargeCardRsp struct {
Code uint32 `json:"code"`// 2001: succ; else: fail
Message string `json:"message"`
}
type UpdateRechargeCardReq struct {
TelNumber string `json:"telNumber"`
}
type UpdateRechargeCardRsp struct {
Code uint32 `json:"code"`// 2001: succ; else: fail
Message string `json:"message"`
}
type CreateAccountReq struct {
ServiceNbr string `json:"serviceNbr"`
CustId string `json:"custId"`// from uint32
AccountId string `json:"accountId"`// from uint32
ProductInstId string `json:"productInstId"`// from uint32
PackageId string `json:"packageId"`// from uint32
Balance string `json:"balance"`// from uint32
//CustIdUint32 uint32 `json:"custIdUint32,omitempty"`// from uint32
//AcctIdUint32 uint32 `json:"acctIdUint32,omitempty"`// from uint32
BalanceExpDate string `json:"balanceExpDate"`
Birthday string `json:"birthday"`
RentCharge string `json:"rentCharge,omitempty"`// from uint32
CugId string `json:"cugId,omitempty"`// from uint32
UserClass string `json:"userClass,omitempty"`// from uint32
}
type CreateAccountRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
}
type UpdateSubsReq struct {
TelNumber string `json:"telNumber"`
AccountId string `json:"accountId,omitempty"`// from uint32
Status string `json:"status,omitempty"`// from uint8
Balance string `json:"balance,omitempty"`// from uint32
BalanceExpDate string `json:"balanceExpDate,omitempty"`
AccountIdU32 uint32 `json:"accountIdU32,omitempty"`
StatusU8 byte `json:"statusU8,omitempty"`
BalanceU32 uint32 `json:"balanceU32,omitempty"`
ExpDateU32 uint32 `json:"balanceExpDateU32,omitempty"`
OptFlag uint32 `json:"optFlagU32,omitempty"`
PackageIdU32 uint32 `json:"packageIdU32,omitempty"`// from uint32
RentChargeU32 uint32 `json:"rentChargeU32,omitempty"`// from uint32
VasCugStatusU8 byte `json:"vasCugStatusU8,omitempty"`// from uint8
PackageId string `json:"packageId,omitempty"`// from uint32
RentCharge string `json:"rentCharge,omitempty"`// from uint32
VasCugStatus string `json:"vasCugStatus,omitempty"`// from uint8
//Optional_flag uint32 `json:"optional_flag,omitempty"`
}
type UpdateSubsRsp struct {
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
}
type DeleteSubsReq struct {
TelNumber string `json:"telNumber"`
AcctType string `json:"acctType,omitempty"`// from uint8
AcctId string `json:"acctID,omitempty"`// from uint32
Cause string `json:"reason,omitempty"`// from uint8
}
type DeleteSubsRsp struct {
Result byte
ErrorCode uint32
Code string `json:"code"`// 2001: succ; else: fail, from uint32
Message string `json:"message"`
}
type UpdateBundleUsageReq struct {
Msisdn string // tag 2
ServiceType byte
AccountId uint32
PlanId uint32
BundleId uint32
PlanValue uint64
PlanUsedValue uint64
SessUpdateTime uint32
PlanValueAddThisTime uint64
}
//type RestMsgType int
const (
REST_SEND_AUTHCODE_REQ byte = 5
REST_SEND_AUTHCODE_RSP byte = 6
REST_QUERY_USERDATA_REQ byte = 7
REST_QUERY_USERDATA_RSP byte = 8
REST_BUNDLE_SUBS_REQ byte = 9
REST_BUNDLE_SUBS_RSP byte = 10
REST_BUNDLE_USAGE_REQ byte = 11
REST_BUNDLE_USAGE_RSP byte = 12
REST_RECHARGE_REQ byte = 13
REST_RECHARGE_RSP byte = 14
REST_TRANSFER_REQ byte = 15
REST_TRANSFER_RSP byte = 16
REST_RECHARGE_CARD_REQ byte = 17
REST_RECHARGE_CARD_RSP byte = 18
REST_CHECK_BALANCE_REQ byte = 19
REST_CHECK_BALANCE_RSP byte = 20
REST_QUERY_BALANCE_REQ byte = 21
REST_QUERY_BALANCE_RSP byte = 22
REST_QUERY_RECHARGE_CARD_REQ byte = 23
REST_QUERY_RECHARGE_CARD_RSP byte = 24
REST_UPDATE_RECHARGE_CARD_REQ byte = 25
REST_UPDATE_RECHARGE_CARD_RSP byte = 26
REST_CRM_PAYMENT_REQ byte = 27
REST_CRM_PAYMENT_RES byte = 28
REST_CRM_SMS_DELIVER_REQ byte = 29
REST_CRM_SMS_DELIVER_RES byte = 30
REST_CRM_CREATE_ACCT_REQ byte = 31
REST_CRM_CREATE_ACCT_RES byte = 32
REST_CRM_QUERY_TARIFF_REQ byte = 33
REST_CRM_QUERY_TARIFF_RES byte = 34
REST_CRM_UPDATE_SUBS_REQ byte = 35
REST_CRM_UPDATE_SUBS_RES byte = 36
REST_CRM_DELETE_SUBS_REQ byte = 37
REST_CRM_DELETE_SUBS_RES byte = 38
REST_CRM_UPDATE_SESS_INFO_REQ byte = 39
REST_CRM_UPDATE_SESS_INFO_RES byte = 40
REST_CRM_UPDATE_PLAN_INFO_REQ byte = 41
REST_CRM_UPDATE_PLAN_INFO_RES byte = 42
REST_CRM_RENT_CHARGE byte = 43
)
type RestReq struct {
MsgType byte
Src_id uint16
Dst_id uint16
AuthCodeReq AuthCodeReq
QueryUserDataReq QueryUserDataReq
BundleSubsReq BundleSubsReq
BundleUsageReq BundleUsageReq
RechargeReq RechargeReq
TransferReq TransferReq
RechargeCardReq RechargeCardReq
CheckBalanceReq CheckBalanceReq
QueryBalanceReq QueryBalanceReq
QueryRechargeCardReq QueryRechargeCardReq
UpdateRechargeCardReq UpdateRechargeCardReq
CreateAccountReq CreateAccountReq
UpdateSubsReq UpdateSubsReq
DeleteSubsReq DeleteSubsReq
}
type RestRsp struct {
MsgType byte
Src_id uint16
Dst_id uint16
AuthCodeRsp AuthCodeRsp
QueryUserDataRsp QueryUserDataRsp
BundleSubsRsp BundleSubsRsp
BundleUsageRsp BundleUsageRsp
RechargeRsp RechargeRsp
TransferRsp TransferRsp
RechargeCardRsp RechargeCardRsp
CheckBalanceRsp CheckBalanceRsp
QueryBalanceRsp QueryBalanceRsp
QueryRechargeCardRsp QueryRechargeCardRsp
UpdateRechargeCardRsp UpdateRechargeCardRsp
CreateAccountRsp CreateAccountRsp
UpdateSubsRsp UpdateSubsRsp
DeleteSubsRsp DeleteSubsRsp
UpdateBundleUsageReq UpdateBundleUsageReq
UpdateSubsReq UpdateSubsReq
}

View File

@@ -0,0 +1,148 @@
package Nrestful
import (
"net/http"
//"fmt"
//"log"
"encoding/json"
"strconv"
//"time"
l4g "proxy/logger"//"github.com/sirupsen/logrus"
)
func sendAuthCodeRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.AuthCodeRsp.Code = strconv.Itoa(5001)
} else {
//rsp.AuthCodeRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send AuthCodeRsp: %v", rsp.AuthCodeRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.AuthCodeRsp)
//
}
func sendQueryUserDataRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.QueryUserDataRsp.Code = strconv.Itoa(5001)
} else {
//rsp.QueryUserDataRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send QueryUserDataRsp: %v", rsp.QueryUserDataRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.QueryUserDataRsp)
//
}
func sendBundleSubsRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.BundleSubsRsp.Code = strconv.Itoa(5001)
} else {
//rsp.BundleSubsRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send QueryUserDataRsp: %v", rsp.BundleSubsRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.BundleSubsRsp)
//
}
func sendRechargeRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.RechargeRsp.Code = strconv.Itoa(5001)
} else {
//rsp.RechargeRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send RechargeRsp: %v", rsp.RechargeRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.RechargeRsp)
//
}
func sendTransferRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.TransferRsp.Code = strconv.Itoa(5001)
} else {
//rsp.TransferRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send TransferRsp: %v", rsp.TransferRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.TransferRsp)
//
}
func sendQueryBalanceRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.QueryBalanceRsp.Code = strconv.Itoa(5001)
} else {
//rsp.QueryBalanceRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send QueryBalanceRsp: %v", rsp.QueryBalanceRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.QueryBalanceRsp)
//
}
func sendCreateAccountRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.CreateAccountRsp.Code = strconv.Itoa(5001)
} else {
//rsp.CreateAccountRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send CreateAccountRsp: %v", rsp.CreateAccountRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.CreateAccountRsp)
//
}
func sendUpdateSubsRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.UpdateSubsRsp.Code = strconv.Itoa(5001)
} else {
//rsp.UpdateSubsRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send UpdateSubsRsp: %v", rsp.UpdateSubsRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.UpdateSubsRsp)
//
}
func sendDeleteSubsRsp(w http.ResponseWriter, statusCode int, rsp *RestRsp) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if statusCode != http.StatusOK {
rsp.DeleteSubsRsp.Code = strconv.Itoa(5001)
} else {
//rsp.DeleteSubsRsp.Code = 2001
}
w.WriteHeader(statusCode)
l4g.RestLog.Warnf("Send DeleteSubsRsp: %v", rsp.DeleteSubsRsp)
// w.Write at last
json.NewEncoder(w).Encode(rsp.DeleteSubsRsp)
//
}

View File

@@ -0,0 +1,423 @@
package Nrestful
import (
"net/http"
"fmt"
"log"
"encoding/json"
mysql "proxy/Nmysql"
//"strconv"
"time"
l4g "proxy/logger"//"github.com/sirupsen/logrus"
)
func StartHttpServer(addr string) {
http.HandleFunc("/", handler)
http.HandleFunc("/authcode", authcodeHandler)
http.HandleFunc("/query_userdata", query_userdataHandler)
http.HandleFunc("/bundle_subs", bundle_subsHandler)
//http.HandleFunc("/bundle_usage", bundle_usageHandler)
http.HandleFunc("/recharge", rechargeHandler)
http.HandleFunc("/transfer", transferHandler)
//http.HandleFunc("/recharge_card", recharge_cardHandler)
//http.HandleFunc("/check_balance", check_balanceHandler)
http.HandleFunc("/query_balane", query_balanceHandler)// query_balane in Pcap from CRM
http.HandleFunc("/openPackage", openPackageHandler)
http.HandleFunc("/updateSubs", updateSubsHandler)
http.HandleFunc("/deleteSubs", deleteSubsHandler)
log.Fatal(http.ListenAndServe(addr, nil))
}
// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}
func authcodeHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_SEND_AUTHCODE_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.AuthCodeRsp.Message = "request body missing"
sendAuthCodeRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_SEND_AUTHCODE_REQ
err := json.NewDecoder(r.Body).Decode(&req.AuthCodeReq)
if err != nil {
rsp.AuthCodeRsp.Message = "err decode authCodeReq"
sendAuthCodeRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got authcode request: %v", req.AuthCodeReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.AuthCodeRsp.Message = "err port full"
sendAuthCodeRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for authcode request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.AuthCodeRsp.Message = "ocs rsp timeout"
sendAuthCodeRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendAuthCodeRsp(w, http.StatusOK, ocsRsp)
}
return
}
func query_userdataHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_QUERY_USERDATA_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.QueryUserDataRsp.Message = "request body missing"
sendQueryUserDataRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_QUERY_USERDATA_REQ
err := json.NewDecoder(r.Body).Decode(&req.QueryUserDataReq)
if err != nil {
rsp.QueryUserDataRsp.Message = "err decode QueryUserDataReq"
sendQueryUserDataRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got query_userdata request: %v", req.QueryUserDataReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.QueryUserDataRsp.Message = "err port full"
sendQueryUserDataRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for query_userdata request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.QueryUserDataRsp.Message = "ocs rsp timeout"
sendQueryUserDataRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendQueryUserDataRsp(w, http.StatusOK, ocsRsp)
}
return
}
func bundle_subsHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_BUNDLE_SUBS_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.BundleSubsRsp.Message = "request body missing"
sendBundleSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_BUNDLE_SUBS_REQ
err := json.NewDecoder(r.Body).Decode(&req.BundleSubsReq)
if err != nil {
rsp.BundleSubsRsp.Message = "err decode BundleSubsReq"
sendBundleSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got BundleSubs request: %v", req.BundleSubsReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.BundleSubsRsp.Message = "err port full"
sendBundleSubsRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for BundleSubs request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.BundleSubsRsp.Message = "ocs rsp timeout"
sendBundleSubsRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendBundleSubsRsp(w, http.StatusOK, ocsRsp)
}
return
}
func rechargeHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_RECHARGE_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.RechargeRsp.Message = "request body missing"
sendRechargeRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_RECHARGE_REQ
err := json.NewDecoder(r.Body).Decode(&req.RechargeReq)
if err != nil {
rsp.RechargeRsp.Message = "err decode RechargeReq"
sendRechargeRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got query_userdata request: %v", req.RechargeReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.RechargeRsp.Message = "err port full"
sendRechargeRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for query_userdata request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.RechargeRsp.Message = "ocs rsp timeout"
sendRechargeRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendRechargeRsp(w, http.StatusOK, ocsRsp)
}
return
}
func transferHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_TRANSFER_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.TransferRsp.Message = "request body missing"
sendTransferRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_TRANSFER_REQ
err := json.NewDecoder(r.Body).Decode(&req.TransferReq)
if err != nil {
rsp.TransferRsp.Message = "err decode TransferReq"
sendTransferRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got Transfer request: %v", req.TransferReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.TransferRsp.Message = "err port full"
sendTransferRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for Transfer request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.TransferRsp.Message = "ocs rsp timeout"
sendTransferRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendTransferRsp(w, http.StatusOK, ocsRsp)
}
return
}
func query_balanceHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_QUERY_BALANCE_RSP}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.QueryBalanceRsp.Message = "request body missing"
sendQueryBalanceRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_QUERY_BALANCE_REQ
err := json.NewDecoder(r.Body).Decode(&req.QueryBalanceReq)
if err != nil {
rsp.QueryBalanceRsp.Message = "err decode QueryBalanceReq"
sendQueryBalanceRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got QueryBalance request: %v", req.QueryBalanceReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.QueryBalanceRsp.Message = "err port full"
sendQueryBalanceRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for QueryBalance request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.QueryBalanceRsp.Message = "ocs rsp timeout"
sendQueryBalanceRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendQueryBalanceRsp(w, http.StatusOK, ocsRsp)
}
return
}
func openPackageHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_CRM_CREATE_ACCT_RES}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.CreateAccountRsp.Message = "request body missing"
sendCreateAccountRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_CRM_CREATE_ACCT_REQ
err := json.NewDecoder(r.Body).Decode(&req.CreateAccountReq)
if err != nil {
rsp.CreateAccountRsp.Message = "err decode CreateAccountReq"
sendCreateAccountRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got CreateAccount request: %v", req.CreateAccountReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.CreateAccountRsp.Message = "err port full"
sendCreateAccountRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for CreateAccount request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.CreateAccountRsp.Message = "ocs rsp timeout"
sendCreateAccountRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendCreateAccountRsp(w, http.StatusOK, ocsRsp)
}
return
}
func updateSubsHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_CRM_UPDATE_SUBS_RES}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.UpdateSubsRsp.Message = "request body missing"
sendUpdateSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_CRM_UPDATE_SUBS_REQ
err := json.NewDecoder(r.Body).Decode(&req.UpdateSubsReq)
if err != nil {
rsp.UpdateSubsRsp.Message = "err decode UpdateSubsReq"
sendUpdateSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got UpdateSubs request: %v", req.UpdateSubsReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.UpdateSubsRsp.Message = "err port full"
sendUpdateSubsRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for UpdateSubs request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.UpdateSubsRsp.Message = "ocs rsp timeout"
sendUpdateSubsRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendUpdateSubsRsp(w, http.StatusOK, ocsRsp)
}
return
}
func deleteSubsHandler(w http.ResponseWriter, r *http.Request) {
rsp := RestRsp{MsgType: REST_CRM_DELETE_SUBS_RES}
var pRsp *RestRsp = &rsp
if r.Body == nil {
rsp.DeleteSubsRsp.Message = "request body missing"
sendDeleteSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
var req RestReq
req.MsgType = REST_CRM_DELETE_SUBS_REQ
err := json.NewDecoder(r.Body).Decode(&req.DeleteSubsReq)
if err != nil {
rsp.DeleteSubsRsp.Message = "err decode DeleteSubsReq"
sendDeleteSubsRsp(w, http.StatusBadRequest, pRsp)
return
}
l4g.RestLog.Debugf("Got DeleteSubs request: %v", req.DeleteSubsReq)
mysql.CrmDeleteSubsProfile(req.DeleteSubsReq.TelNumber, req.DeleteSubsReq.AcctType, req.DeleteSubsReq.AcctId)
if req.DeleteSubsReq.AcctType != "1" {// not mobile
pRsp.DeleteSubsRsp.Result = 0
pRsp.DeleteSubsRsp.ErrorCode = 2001
sendDeleteSubsRsp(w, http.StatusOK, pRsp)
return
}
// handle ==========================================
port := AssignPort(&req)
if port == nil {
rsp.DeleteSubsRsp.Message = "err port full"
sendDeleteSubsRsp(w, http.StatusInternalServerError, pRsp)
return
}
l4g.RestLog.Debugf("Assign port for DeleteSubs request, src[%d], dst[%d]", req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
rsp.DeleteSubsRsp.Message = "ocs rsp timeout"
sendDeleteSubsRsp(w, http.StatusInternalServerError, pRsp)
} else {
sendDeleteSubsRsp(w, http.StatusOK, ocsRsp)
}
return
}
func getOcsRsp(req *RestReq, port *Port) (*RestRsp) {
c := make(chan RestRsp)
//port.Rsp = make(chan RestRsp)
port.Rsp = c
go SendOcsUdpReq(req)
defer ReleasePort(req.Src_id)
timeout := time.After(5 * time.Second)
select {
case rsp, ok := <-c:
if !ok {// chan closed
l4g.RestLog.Warnf("chan getOcsRsp closed by other!")
return nil
} else {
close(c)
l4g.RestLog.Debugf("getOcsRsp succ, close chan.")
return &rsp
}
case <-timeout:
l4g.RestLog.Warnf("chan getOcsRsp timeout, close chan!")
close(c)
return nil
}
}

View File

@@ -0,0 +1,5 @@
package Nrestful
import (
)

View File

@@ -0,0 +1,688 @@
package Nrestful
import (
"encoding/binary"
"fmt"
"net"
"proxy/MsgDef"
rdb "proxy/Nredis"
"strconv"
_ "strings"
"time"
l4g "proxy/logger" //"github.com/sirupsen/logrus"
)
var ocsConn *net.UDPConn
func closeConnect() {
ocsConn.Close()
ocsConn = nil
}
var locIp, ocsIp string
var locPort, ocsPort int
func Connect_ocs(loc string, rem string, loc_port int, rem_port int) error {
locIp = loc
ocsIp = rem
locPort = loc_port
ocsPort = rem_port
return connect_ocs()
}
func connect_ocs() error {
loc_ip := net.ParseIP(locIp)
rem_ip := net.ParseIP(ocsIp)
srcAddr := &net.UDPAddr{IP: loc_ip, Port: locPort}
dstAddr := &net.UDPAddr{IP: rem_ip, Port: ocsPort}
var err error
ocsConn, err = net.DialUDP("udp", srcAddr, dstAddr)
if err != nil {
fmt.Println(err)
return err
}
defer closeConnect()
//conn.Write([]byte("hello"))
data := make([]byte, 1500)
//var recLen uint16 = 0
//var msgId uint8 = 0
for {
n, err := ocsConn.Read(data)
//msgId = data[1]
//recLen = uint16(data[2])
//recLen = (recLen << 8) + uint16(data[3])
if err != nil {
//break
} else {
// check param
handle_udp_msg_from_ocs(data, n)
}
//fmt.Printf("read %s from <%s>\n", data[:n], conn.RemoteAddr())
}
l4g.RestLog.Errorln("ocs read thread exit")
return nil
}
//type UdpMsgIE byte
const (
IE_NULL = iota
IE_SRC_REF // UdpMsgIE = 1
IE_DST_REF
IE_MSISDN
IE_RESULT
IE_ERROR_CODE
IE_BALANCE// 6
IE_MO_EXPIRY
IE_MT_EXPIRY
IE_USERNAME
IE_PASSWORD
IE_MSG_CONTENT //value=11
IE_STATUS
IE_REMARK
IE_GROUP_NAME// 0x0e
IE_MO_VOICE_MIN// in minute
IE_REMAIN_MO_VOICE_SEC// in second
IE_REMAIN_MO_VOICE_MIN// in minute
IE_MT_VOICE_MIN// in minute
IE_REMAIN_MT_VOICE_SEC// in second
IE_REMAIN_MT_VOICE_MIN// in minute
IE_SMS_NUM//value=21
IE_REMAIN_SMS_NUM
IE_DATA_VOL_MB// in MB
IE_REMAIN_DATA_VOL_KB// in KB
IE_REMAIN_DATA_VOL_MB// in MB
IE_PAY_TYPE
IE_AMOUNT
IE_VALID_DAYS
IE_EXPIRY_TIME
IE_MSISDN_TRANS_OUT
IE_MSISDN_TRANS_IN //value=31
IE_BALANCE_AVAILABLE
IE_RECHARGE_AMOUNT
IE_RECHARGE_TYPE
IE_RECHARGE_CARD_STATUS
IE_RECHARGE_CARD_FACE_VALUE
IE_RECHARGE_CARD_EXPIRED_TS
IE_RECHARGE_CARD_UPDATED_TS
IE_CUSTOMER_ID
IE_ACCOUNT_ID
IE_PRODUCT_ID//value=41
IE_PLAN_ID
IE_RENT_CHARGE
IE_BIRTHDAY
IE_SMS_CONTENT
IE_SERVICE_TYPE
IE_TARIIFF_PREFIX
IE_TARIFF_UNIT
IE_TARIFF_CHARGE
IE_TARIFF_DISCOUNT
IE_PLAN_VALUE//value=51
IE_PLAN_USED_VALUE
IE_BUNDLE_ID
IE_CAUSE
IE_SESS_FLAG
IE_TIMESTAMP
IE_CONSUME_VALUE
IE_CALLED_NUMBER
IE_UE_IP
IE_GW_IP
IE_CUG_ID
IE_VAS_CUG_STATUS
IE_SESS_UPDATE_TIME
IE_PLAN_VALUE_ADD_THIS_TIME
IE_USER_CLASS
IE_MAX_NUM
)
func handle_udp_msg_from_ocs(buf []byte, msglen int) {
var rsp RestRsp
ret := Decode_udp_msg(buf, msglen, &rsp)
if ret < 0 {
l4g.RestLog.Errorf("Deocde UDP msg fail, msg[%v]", buf[:msglen])
return
}
switch rsp.MsgType {
case REST_CRM_UPDATE_PLAN_INFO_REQ:
go handleUpdatePlanUsage(&rsp)
return
case REST_CRM_DELETE_SUBS_RES:
handleOcsDeleteSubsRsp(&rsp)
case REST_CRM_CREATE_ACCT_RES:
//handleOcsDeleteSubsRsp(&rsp)
case REST_CRM_UPDATE_SUBS_REQ:
go handleOcsUpdateSubsReq(&rsp)
return
}
l4g.RestLog.Debugf("Recv UDP msg from OCS, msg_id[%d], src[%d], dst[%d]", rsp.MsgType, rsp.Src_id, rsp.Dst_id)
if rsp.Dst_id >= MAX_PORT_NUM {
return
}
port := GetPortInfo(rsp.Dst_id, rsp.Src_id)
if port == nil {
l4g.RestLog.Errorf("Get port by dst_id[%d] fail, msg id[%d]", rsp.Dst_id, rsp.MsgType)
return
}
l4g.RestLog.Debugf("Get port by dst_id[%d] from UDP rsp, msg id[%d]", rsp.Dst_id, rsp.MsgType)
go func() {
port.Rsp <- rsp
}()
return
}
func encode_udp_msg(req *RestReq, buf []byte) int {
switch req.MsgType {
case REST_SEND_AUTHCODE_REQ:
case REST_QUERY_USERDATA_REQ:
case REST_BUNDLE_SUBS_REQ:
case REST_RECHARGE_REQ:
case REST_TRANSFER_REQ:
case REST_QUERY_BALANCE_REQ:
case REST_CRM_CREATE_ACCT_REQ:
case REST_CRM_UPDATE_SUBS_REQ:
case REST_CRM_DELETE_SUBS_REQ:
default:
l4g.RestLog.Errorf("Encode rest unsupport msg id[%d]", req.MsgType)
return 0
}
var msglen int = 0
buf[msglen] = req.MsgType
msglen++
buf[msglen] = IE_SRC_REF
msglen++
buf[msglen] = 0x02
msglen++
binary.BigEndian.PutUint16(buf[msglen:msglen+2], req.Src_id)
msglen += 2
buf[msglen] = IE_DST_REF
msglen++
buf[msglen] = 0x02
msglen++
binary.BigEndian.PutUint16(buf[msglen:msglen+2], req.Dst_id)
msglen += 2
var vallen int = 0
switch (req.MsgType) {
case REST_SEND_AUTHCODE_REQ:
l4g.RestLog.Debugf("Encode REST_SEND_AUTHCODE_REQ[%d], %#v", req.MsgType, req.AuthCodeReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.AuthCodeReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.AuthCodeReq.TelNumber)))
msglen += vallen
// IE_MSG_CONTENT
buf[msglen] = IE_MSG_CONTENT
msglen++
vallen = len(req.AuthCodeReq.Content)
binary.BigEndian.PutUint16(buf[msglen:msglen+2], uint16(vallen))
msglen += 2
copy(buf[msglen:msglen+vallen], ([]byte(req.AuthCodeReq.Content)))
msglen += vallen
case REST_QUERY_USERDATA_REQ:
l4g.RestLog.Debugf("Encode REST_QUERY_USERDATA_REQ[%d], %#v", req.MsgType, req.QueryUserDataReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.QueryUserDataReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.QueryUserDataReq.TelNumber)))
msglen += vallen
case REST_BUNDLE_SUBS_REQ:
l4g.RestLog.Debugf("Encode REST_BUNDLE_SUBS_REQ[%d], %#v", req.MsgType, req.BundleSubsReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.BundleSubsReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.BundleSubsReq.TelNumber)))
msglen += vallen
buf[msglen] = IE_PAY_TYPE
msglen++
buf[msglen] = 0x01
msglen++
pay_type, _ := strconv.Atoi(req.BundleSubsReq.PayType)
buf[msglen] = uint8(pay_type)
msglen += 0x01
buf[msglen] = IE_AMOUNT
msglen++
buf[msglen] = 0x04
msglen++
amount, _ := strconv.Atoi(req.BundleSubsReq.ChargedAmount)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(amount))
msglen += 0x04
buf[msglen] = IE_MO_VOICE_MIN
msglen++
buf[msglen] = 0x04
msglen++
moVocMin, _ := strconv.Atoi(req.BundleSubsReq.MoVoiceMinute)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(moVocMin))
msglen += 0x04
buf[msglen] = IE_MT_VOICE_MIN
msglen++
buf[msglen] = 0x04
msglen++
mtVocMin, _ := strconv.Atoi(req.BundleSubsReq.MtVoiceMinute)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(mtVocMin))
msglen += 0x04
buf[msglen] = IE_SMS_NUM
msglen++
buf[msglen] = 0x04
msglen++
smsNum, _ := strconv.Atoi(req.BundleSubsReq.SmsVolume)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(smsNum))
msglen += 0x04
buf[msglen] = IE_DATA_VOL_MB
msglen++
buf[msglen] = 0x04
msglen++
dataVol, _ := strconv.Atoi(req.BundleSubsReq.DataVolume)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(dataVol))
msglen += 0x04
buf[msglen] = IE_VALID_DAYS
msglen++
buf[msglen] = 0x04
msglen++
validDays, _ := strconv.Atoi(req.BundleSubsReq.ValidPeriod)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(validDays))
msglen += 0x04
case REST_RECHARGE_REQ:
l4g.RestLog.Debugf("Encode REST_RECHARGE_REQ[%d], %#v", req.MsgType, req.RechargeReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.RechargeReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.RechargeReq.TelNumber)))
msglen += vallen
buf[msglen] = IE_AMOUNT
msglen++
buf[msglen] = 0x04
msglen++
amount, _ := strconv.Atoi(req.RechargeReq.Amount)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(amount))
msglen += 0x04
buf[msglen] = IE_RECHARGE_TYPE
msglen++
buf[msglen] = 0x01
msglen++
op_type, _ := strconv.Atoi(req.RechargeReq.OpType)
buf[msglen] = uint8(op_type)
msglen += 0x01
buf[msglen] = IE_VALID_DAYS
msglen++
buf[msglen] = 0x02
msglen++
valid_days, _ := strconv.Atoi(req.RechargeReq.ValidyDays)
binary.BigEndian.PutUint16(buf[msglen:msglen+2], uint16(valid_days))
msglen += 0x02
case REST_TRANSFER_REQ:
l4g.RestLog.Debugf("Encode REST_TRANSFER_REQ[%d], %#v", req.MsgType, req.TransferReq)
// IE_MSISDN_TRANS_OUT
buf[msglen] = IE_MSISDN_TRANS_OUT
msglen++
vallen = len(req.TransferReq.TransferOut)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.TransferReq.TransferOut)))
msglen += vallen
buf[msglen] = IE_MSISDN_TRANS_IN
msglen++
vallen = len(req.TransferReq.TransferIn)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.TransferReq.TransferIn)))
msglen += vallen
buf[msglen] = IE_AMOUNT
msglen++
buf[msglen] = 0x04
msglen++
amount, _ := strconv.Atoi(req.TransferReq.Amount)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(amount))
msglen += 0x04
case REST_QUERY_BALANCE_REQ:
l4g.RestLog.Debugf("Encode REST_QUERY_BALANCE_REQ[%d], %#v", req.MsgType, req.QueryBalanceReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.QueryBalanceReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.QueryBalanceReq.TelNumber)))
msglen += vallen
case REST_CRM_CREATE_ACCT_REQ:
l4g.RestLog.Debugf("Encode REST_CRM_CREATE_ACCT_REQ[%d], %#v", req.MsgType, req.CreateAccountReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.CreateAccountReq.ServiceNbr)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.CreateAccountReq.ServiceNbr)))
msglen += vallen
buf[msglen] = IE_CUSTOMER_ID
msglen++
buf[msglen] = 0x04
msglen++
custId, _ := strconv.Atoi(req.CreateAccountReq.CustId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(custId))
msglen += 0x04
buf[msglen] = IE_ACCOUNT_ID
msglen++
buf[msglen] = 0x04
msglen++
acctId, _ := strconv.Atoi(req.CreateAccountReq.AccountId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(acctId))
msglen += 0x04
buf[msglen] = IE_PRODUCT_ID
msglen++
buf[msglen] = 0x04
msglen++
prdId, _ := strconv.Atoi(req.CreateAccountReq.ProductInstId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(prdId))
msglen += 0x04
buf[msglen] = IE_PLAN_ID
msglen++
buf[msglen] = 0x04
msglen++
planId, _ := strconv.Atoi(req.CreateAccountReq.PackageId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(planId))
msglen += 0x04
buf[msglen] = IE_BALANCE
msglen++
buf[msglen] = 0x04
msglen++
balance, _ := strconv.Atoi(req.CreateAccountReq.Balance)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(balance))
msglen += 0x04
buf[msglen] = IE_EXPIRY_TIME
msglen++
buf[msglen] = 0x04
msglen++
expire_time, err := time.Parse("2006-01-02 15:04:05", req.CreateAccountReq.BalanceExpDate)
if err != nil {
l4g.RestLog.Errorf("Error CreateAccountReq BalanceExpDate[%s]", req.CreateAccountReq.BalanceExpDate)
return -1
}
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(expire_time.Unix()))
msglen += 0x04
buf[msglen] = IE_RENT_CHARGE
msglen++
buf[msglen] = 0x04
msglen++
rent_charge, _ := strconv.Atoi(req.CreateAccountReq.RentCharge)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(rent_charge))
msglen += 0x04
buf[msglen] = IE_BIRTHDAY
msglen++
buf[msglen] = 0x04
msglen++
birthday, err := time.Parse("2006-01-02", req.CreateAccountReq.Birthday)
if err != nil {
l4g.RestLog.Errorf("Error CreateAccountReq Birthday[%s]", req.CreateAccountReq.Birthday)
return -1
}
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(birthday.Unix()))
msglen += 0x04
buf[msglen] = IE_CUG_ID
msglen++
buf[msglen] = 0x04
msglen++
cugId, _ := strconv.Atoi(req.CreateAccountReq.CugId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(cugId))
msglen += 0x04
buf[msglen] = IE_USER_CLASS
msglen++
buf[msglen] = 0x01
msglen++
userClass, _ := strconv.Atoi(req.CreateAccountReq.UserClass)
buf[msglen] = byte(userClass)
msglen += 0x01
case REST_CRM_UPDATE_SUBS_REQ:
l4g.RestLog.Debugf("Encode REST_CRM_UPDATE_SUBS_REQ[%d], %#v", req.MsgType, req.UpdateSubsReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.UpdateSubsReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.UpdateSubsReq.TelNumber)))
msglen += vallen
buf[msglen] = IE_ACCOUNT_ID
msglen++
buf[msglen] = 0x04
msglen++
acctId, _ := strconv.Atoi(req.UpdateSubsReq.AccountId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(acctId))
msglen += 0x04
if (len(req.UpdateSubsReq.Status) > 0) {
buf[msglen] = IE_STATUS
msglen++
buf[msglen] = 0x01
msglen++
status, _ := strconv.Atoi(req.UpdateSubsReq.Status)
buf[msglen] = byte(status)
msglen += 0x01
}
if (len(req.UpdateSubsReq.Balance) > 0) {
buf[msglen] = IE_BALANCE
msglen++
buf[msglen] = 0x04
msglen++
balance, _ := strconv.Atoi(req.UpdateSubsReq.Balance)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(balance))
msglen += 0x04
}
if (len(req.UpdateSubsReq.BalanceExpDate) > 0) {
buf[msglen] = IE_EXPIRY_TIME
msglen++
buf[msglen] = 0x04
msglen++
expiryTime, err := time.Parse("2006-01-02 15:04:05", req.UpdateSubsReq.BalanceExpDate)
if err != nil {
l4g.RestLog.Errorf("Error UpdateSubsReq BalanceExpDate[%s]", req.UpdateSubsReq.BalanceExpDate)
return -1
}
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(expiryTime.Unix()))
msglen += 0x04
}
if (len(req.UpdateSubsReq.PackageId) > 0) {
buf[msglen] = IE_PLAN_ID
msglen++
buf[msglen] = 0x04
msglen++
planId, _ := strconv.Atoi(req.UpdateSubsReq.PackageId)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(planId))
msglen += 0x04
}
if (len(req.UpdateSubsReq.RentCharge) > 0) {
buf[msglen] = IE_RENT_CHARGE
msglen++
buf[msglen] = 0x04
msglen++
rentCharge, _ := strconv.Atoi(req.UpdateSubsReq.RentCharge)
binary.BigEndian.PutUint32(buf[msglen:msglen+4], uint32(rentCharge))
msglen += 0x04
}
if (len(req.UpdateSubsReq.VasCugStatus) > 0) {
buf[msglen] = IE_VAS_CUG_STATUS
msglen++
buf[msglen] = 0x01
msglen++
vasCugStatus, _ := strconv.Atoi(req.UpdateSubsReq.VasCugStatus)
buf[msglen] = byte(vasCugStatus)
msglen += 0x01
}
case REST_CRM_DELETE_SUBS_REQ:
l4g.RestLog.Debugf("Encode REST_CRM_DELETE_SUBS_REQ[%d], %#v", req.MsgType, req.DeleteSubsReq)
// IE_MSISDN
buf[msglen] = IE_MSISDN
msglen++
vallen = len(req.DeleteSubsReq.TelNumber)
buf[msglen] = byte(vallen)
msglen++
copy(buf[msglen:msglen+vallen], ([]byte(req.DeleteSubsReq.TelNumber)))
msglen += vallen
buf[msglen] = IE_CAUSE
msglen++
buf[msglen] = 0x01
msglen++
buf[msglen] = 0
msglen += 0x01
default:
return 0
}
return msglen
}
func SendOcsUdpReq(req *RestReq) {
if ocsConn == nil {
return
}
l4g.RestLog.Debugf("Send UDP msg to OCS, msg_id[%d], src[%d], dst[%d]", req.MsgType, req.Src_id, req.Dst_id)
buf := make([]byte, 1024)
msglen := encode_udp_msg(req, buf)
if msglen > 0 {
ocsConn.Write([]byte(buf[:msglen]))
}
return
}
func SendNtfSms2Ocs(serviceNbr string, smsContent string) {
var req RestReq
req.MsgType = REST_SEND_AUTHCODE_REQ
req.Src_id = 0xFFFF
req.Dst_id = 0xFFFF
req.AuthCodeReq.TelNumber = serviceNbr
req.AuthCodeReq.Content = smsContent
SendOcsUdpReq(&req)
}
func SendCrtAcct2Ocs(serviceNbr string, ai *MsgDef.ChgSyncMobile, cugId, userClass, rent int) {
var req RestReq
req.MsgType = REST_CRM_CREATE_ACCT_REQ
req.Src_id = 0xFFFF
req.Dst_id = 0xFFFF
req.CreateAccountReq.ServiceNbr = serviceNbr
req.CreateAccountReq.CustId = strconv.Itoa(ai.CustId)
req.CreateAccountReq.AccountId = strconv.Itoa(ai.AcctId)
req.CreateAccountReq.ProductInstId = strconv.Itoa(ai.PrdInstId)
req.CreateAccountReq.PackageId = strconv.Itoa(ai.OfrId)
req.CreateAccountReq.UserClass = strconv.Itoa(userClass)
req.CreateAccountReq.CugId = strconv.Itoa(cugId)
req.CreateAccountReq.Balance = strconv.Itoa(ai.Balance)
//t, _ := time.Parse("2006-01-02 15:04:05", ai.BirthDate)
req.CreateAccountReq.Birthday = ai.BirthDate
//t, _ := time.Parse("2006-01-02 15:04:05", ai.BalanceExpDate)
req.CreateAccountReq.BalanceExpDate = ai.BalanceExpDate
req.CreateAccountReq.RentCharge = strconv.Itoa(rent)
SendOcsUdpReq(&req)
}
func CreateAcct2Ocs(serviceNbr string, ai *MsgDef.ChgSyncMobile, cugId, userClass, rent int, key string) {
var req RestReq
req.MsgType = REST_CRM_CREATE_ACCT_REQ
req.Src_id = 0xFFFF
req.Dst_id = 0xFFFF
req.CreateAccountReq.ServiceNbr = serviceNbr
req.CreateAccountReq.CustId = strconv.Itoa(ai.CustId)
req.CreateAccountReq.AccountId = strconv.Itoa(ai.AcctId)
req.CreateAccountReq.ProductInstId = strconv.Itoa(ai.PrdInstId)
req.CreateAccountReq.PackageId = strconv.Itoa(ai.OfrId)
req.CreateAccountReq.UserClass = strconv.Itoa(userClass)
req.CreateAccountReq.CugId = strconv.Itoa(cugId)
req.CreateAccountReq.Balance = strconv.Itoa(ai.Balance)
//t, _ := time.Parse("2006-01-02 15:04:05", ai.BirthDate)
req.CreateAccountReq.Birthday = ai.BirthDate
//t, _ := time.Parse("2006-01-02 15:04:05", ai.BalanceExpDate)
req.CreateAccountReq.BalanceExpDate = ai.BalanceExpDate
req.CreateAccountReq.RentCharge = strconv.Itoa(rent)
l4g.RestLog.Debugf("Got createAcct request: %v", req.CreateAccountReq)
// handle ==========================================
port := AssignPort(&req)
if port == nil {
l4g.RestLog.Warnf("Assign port for createAcct request fail, serviceNbr[%s], key[%s]", serviceNbr, key)
return
}
l4g.RestLog.Debugf("Assign port for createAcct request, serviceNbr, src[%d], dst[%d]", serviceNbr, req.Src_id, req.Dst_id)
ocsRsp := getOcsRsp(&req, port)
if ocsRsp == nil {
l4g.RestLog.Warnf("getOcsRsp timeout for createAcct request, serviceNbr[%s], key[%s]", serviceNbr, key)
} else {
l4g.RestLog.Infof("getOcsRsp succ for createAcct request, serviceNbr[%s], key[%s]", serviceNbr, key)
rdb.RdsDelMsg2OcsKey(key)
}
return
}