func (s360 SDK360) Init(configpath string) { log.Debug("[Load]load 360 sdk config start") file, err := os.Open(configpath) if err != nil { log.Debug("[ERROR]360SDK Configuration File Not Found") return } decoder := json.NewDecoder(file) err = decoder.Decode(&s360) utils.Panic(err) defer file.Close() log.Debug("[Load]load 360 sdk config end") }
func RequestHandle(gameId int, body snet.REQUEST) (rbody interface{}, disconnect bool) { disconnect = false switch body.Request { case "CreateOrder": userId := body.Ex[0] playerId := body.Ex[1] amount := body.Ex[2] payType := body.Ex[3] productId := body.Ex[4] productCount := body.Ex[5] extData := body.Ex[6] sdkType := body.Ex[7] source := body.Ex[8] serverId := body.Param[0] rbody = requestCreateOrder(userId, playerId, amount, payType, productId, productCount, sdkType, extData, source, serverId, gameId) case "GetOrder": orderId := body.Param[0] rbody = requestGetOrder(orderId) case "VerifyPay": orderId := body.Param[0] rbody = requestVerifyOrder(orderId) default: log.Debug("Received packet type not recognized, disconnect") return nil, true } return }
func listenRequest(conn net.Conn) { defer func() { if x := recover(); x != nil { //common.PanicLog(x, string(debug.Stack()), "", "") log.Debug(fmt.Sprintf("%s %+v", debug.Stack(), x)) } }() log.Debugf("[Handle] start handle charge connection from %s to %s \n", conn.RemoteAddr().String(), conn.LocalAddr().String()) defer conn.Close() var gameId int for { gameId = 1 bodyStr, err := snet.ClientRecvBody(gameId, conn) if err != nil { break } var rqBody snet.REQUEST json.Unmarshal([]byte(bodyStr), &rqBody) log.Debugf("Client request %+v", rqBody) rBody, disconnect := RequestHandle(gameId, rqBody) if disconnect { return } var rsBody snet.RESPONSE rsBody.Response = rqBody.Request rsBody.Data = rBody log.Debugf("Server response %+v", rsBody) rsBytes, err := json.Marshal(rsBody) if err != nil { break } snet.ClientSendBody(gameId, conn, string(rsBytes)) } }
func PanicWithLog(err error, debugInfo string) { if debugInfo != "" { log.Debug(debugInfo) } if err != nil { panic(err.Error()) } }
func Init() { dbConnectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", content.DBUser, content.DBPassword, content.DBAddress, content.DBPort, content.DBSchema) dbt, err := sql.Open("mysql", dbConnectionString) utils.Panic(err) err = dbt.Ping() utils.Panic(err) db = dbt log.Debug("Init db finished") }
func socketChargeService() { ln, err := net.Listen("tcp", fmt.Sprintf(":%d", content.MainListenPort)) if err != nil { log.Debugf("listen on port %d failed %s \n", content.MainListenPort, err.Error()) Quit <- 1 return } log.Debug("Starting charge socket") for { conn, err := ln.Accept() if err != nil { log.Debug("Accept connection failed") continue } go listenRequest(conn) } }
func (s360 SDK360) ConfirmNotify(w http.ResponseWriter, dataBytes []byte) (rorder Order) { chargeMap := make(map[string]interface{}) json.Unmarshal(dataBytes, &chargeMap) log.Debugf("charegeInfo : %+v\n", chargeMap) //数据包合法性验证 coAppKey := chargeMap["app_key"].(string) coProductId := chargeMap["product_id"].(string) coAmount := chargeMap["amount"].(float64) coAppUid := chargeMap["app_uid"].(string) coAppExt1 := chargeMap["app_ext1"].(string) coAppExt2 := chargeMap["app_ext2"].(string) coUserId := int(chargeMap["user_id"].(float64)) coOrderId := int(chargeMap["order_id"].(float64)) coGatewayFlag := chargeMap["gateway_flag"].(string) coSignType := chargeMap["sign_type"].(string) coAppOrderId := chargeMap["app_order_id"].(string) // coSignReturn := chargeMap["sign_return"].(string) coSign := chargeMap["sign"].(string) //需要排序参数md5 paramMap := make(map[string]interface{}) paramMap["app_key"] = coAppKey paramMap["product_id"] = coProductId paramMap["amount"] = coAmount paramMap["app_uid"] = coAppUid paramMap["app_ext1"] = coAppExt1 paramMap["app_ext2"] = coAppExt2 paramMap["user_id"] = coUserId paramMap["order_id"] = coOrderId paramMap["gateway_flag"] = coGatewayFlag paramMap["sign_type"] = coSignType paramMap["app_order_id"] = coAppOrderId if chargeMap["app_key"].(string) != s360.AppKey { log.Debug("[Error]got 360 charge notify but appid check failed") return } paramStr := utils.SortStringMapToUrlParam(paramMap) if !utils.MD5Check(coSign, paramStr) { log.Debug("[Error]got 360 charge notify but md5 check failed") return } return }
func (snet *SNetD) ClientRecvBody(conn net.Conn) (body string, err error) { var header HEADER packetbuf := make([]byte, 16) conn.SetReadDeadline(time.Now().Add(60 * time.Second)) n, err := conn.Read(packetbuf) if err != nil { log.Debugf("read header error %s \n", err.Error()) return } buf := bytes.NewBuffer(packetbuf[0:n]) binary.Read(buf, binary.LittleEndian, &header) if header.Sign != FRHEADERSIGN { err = errors.New("header sign not match, disconnect") log.Debug("header sign not match, disconnect") return } bytesExpected := int(header.Bodysize) bodyBytes := make([]byte, 0) bytesRead := 0 for bytesRead < bytesExpected { bytes := make([]byte, bytesExpected-bytesRead) conn.SetReadDeadline(time.Now().Add(5 * time.Second)) n, err = conn.Read(bytes) //log.Debugf(">>>>>>>>>>>>>>>>>> %d %d <<<<<<<<<<<<<<<<<< |%s|", n, len(bytes[:n]), bytes[:n]) if err != nil { log.Debugf("read body error %s \n", err.Error()) return } bytesRead += n bodyBytes = append(bodyBytes, bytes[:n]...) } body = string(bodyBytes) { buf := new(bytes.Buffer) if _, e := buf.WriteString(body); e != nil { err = errors.New("received body gzip failed") log.Debug("received body gzip failed") return } r, e := gzip.NewReader(buf) if e != nil { err = errors.New("received body gzip failed") log.Debug("received body gzip failed") return } b, e := ioutil.ReadAll(r) if e != nil { err = errors.New("received body gzip failed") log.Debug("received body gzip failed") return } body = string(b) if e := r.Close(); e != nil { err = errors.New("received body gzip failed") log.Debug("received body gzip failed") return } } if header.ContentSize != uint32(len(body)) { err = errors.New("received body content size mismatch") log.Debug("received body content size mismatch") return } log.Debugf(">>> receiving %d->%d body:\n %s", header.ContentSize, header.Bodysize, body) return }