Permalink
Switch branches/tags
Nothing to show
Find file Copy path
bc08113 Jul 20, 2018
1 contributor

Users who have contributed to this file

277 lines (226 sloc) 20.6 KB
package main
import (
"net/http"
"fmt"
"log"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload"
"github.com/boltdb/bolt"
"errors"
"encoding/json"
"github.com/go-zoo/bone"
"github.com/renstrom/shortuuid"
"strconv"
"flag"
"strings"
)
type BaseResponse struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Message string `json:"message"`
}
func responseString(code int, message string)string {
t, _ := json.Marshal(BaseResponse{Code:code,Message:message})
return string(t)
}
func responseData(code int, data map[string]interface{} , message string)string {
t, _ := json.Marshal(BaseResponse{Code:code,Data:data, Message:message})
return string(t)
}
func ping(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fmt.Fprint(w, responseData(200, map[string]interface{}{"version": "1.0.0"},"pong"))
}
func Index(w http.ResponseWriter, r *http.Request) {
key := bone.GetValue(r, "key")
category := bone.GetValue(r, "category")
title := bone.GetValue(r, "title")
body := bone.GetValue(r, "body")
defer r.Body.Close()
deviceToken ,err := getDeviceTokenByKey(key)
if err != nil {
log.Println("找不到key对应的DeviceToken key: " + key)
fmt.Fprint(w, responseString(400,"找不到key对应的DeviceToken, 请确保Key正确! Key可在App端注册获得。"))
return
}
r.ParseForm()
if len(title) <= 0 && len(body) <= 0 {
//url中不包含 title body,则从Form里取
for key,value := range r.Form{
if strings.ToLower(key) == "title" {
title = value[0]
} else if strings.ToLower(key) == "body"{
body = value[0]
}
}
}
if len(body) <= 0 {
body = "无推送文字内容"
}
params := make(map[string]interface{})
for key,value := range r.Form {
params[strings.ToLower(key)] = value[0]
}
log.Println(" ========================== ")
log.Println("key: ", key)
log.Println("category: ", category)
log.Println("title: ", title)
log.Println("body: ", body)
log.Println("params: ", params)
log.Println(" ========================== ")
err = postPush(category,title,body,deviceToken,params)
if err != nil {
fmt.Fprint(w, responseString(400, err.Error()))
} else{
fmt.Fprint(w, responseString(200, ""))
}
}
func register(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
r.ParseForm()
key := shortuuid.New()
var deviceToken string
for key,value := range r.Form {
if strings.ToLower(key) == "devicetoken" {
deviceToken = value[0]
break
}
}
if len(deviceToken) <= 0 {
fmt.Fprint(w, responseString(400, "deviceToken 不能为空"))
return
}
oldKey := r.FormValue("key")
boltDB.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte("device"))
if err != nil {
return err
}
if len(oldKey) >0 {
//如果已经注册,则更新DeviceToken的值
val := bucket.Get([]byte(oldKey))
if val != nil {
key = oldKey
}
}
bucket.Put([]byte(key), []byte(deviceToken))
return nil
})
log.Println("注册设备成功")
log.Println("key: ", key)
log.Println("deviceToken: ", deviceToken)
fmt.Fprint(w, responseData(200, map[string]interface{}{"key":key}, "注册成功"))
}
func getDeviceTokenByKey(key string) (string,error){
var deviceTokenBytes []byte
err := boltDB.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte("device"))
deviceTokenBytes = bucket.Get([]byte(key))
if deviceTokenBytes == nil {
return errors.New("没找到DeviceToken")
}
return nil
})
if err != nil {
return "", err
}
return string(deviceTokenBytes), nil
}
func getb() []byte {
//测试证书
if IsDev{
return []byte{}
}
//线上证书
return []byte{48, 130, 12, 161, 2, 1, 3, 48, 130, 12, 104, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 1, 160, 130, 12, 89, 4, 130, 12, 85, 48, 130, 12, 81, 48, 130, 6, 231, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 6, 160, 130, 6, 216, 48, 130, 6, 212, 2, 1, 0, 48, 130, 6, 205, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 1, 48, 28, 6, 10, 42, 134, 72, 134, 247, 13, 1, 12, 1, 6, 48, 14, 4, 8, 34, 11, 31, 234, 17, 222, 233, 133, 2, 2, 8, 0, 128, 130, 6, 160, 23, 176, 48, 129, 183, 141, 55, 111, 167, 183, 140, 29, 140, 31, 137, 155, 62, 161, 215, 132, 49, 236, 211, 179, 26, 103, 219, 113, 90, 242, 141, 228, 204, 23, 101, 205, 2, 185, 228, 122, 98, 19, 224, 190, 240, 183, 152, 18, 248, 102, 89, 128, 123, 12, 183, 106, 204, 87, 212, 193, 109, 39, 26, 157, 66, 105, 48, 12, 28, 153, 106, 103, 175, 0, 92, 49, 92, 117, 221, 118, 34, 151, 253, 6, 237, 231, 90, 168, 130, 145, 153, 184, 192, 26, 64, 13, 253, 177, 83, 81, 27, 62, 152, 106, 222, 187, 108, 201, 213, 4, 153, 26, 227, 1, 245, 243, 234, 106, 115, 21, 69, 240, 147, 22, 36, 254, 155, 114, 203, 118, 102, 146, 227, 64, 24, 190, 68, 58, 164, 186, 38, 110, 183, 89, 83, 217, 78, 216, 181, 11, 42, 180, 125, 194, 204, 235, 76, 234, 57, 195, 23, 93, 143, 70, 188, 159, 179, 121, 68, 184, 172, 161, 169, 151, 185, 202, 34, 125, 165, 164, 223, 0, 78, 66, 248, 171, 48, 113, 52, 2, 148, 177, 87, 220, 14, 221, 144, 126, 131, 216, 8, 58, 10, 3, 213, 53, 72, 170, 0, 36, 173, 26, 194, 134, 65, 151, 253, 219, 150, 24, 27, 56, 184, 133, 187, 128, 70, 15, 134, 90, 128, 26, 22, 25, 171, 152, 214, 216, 210, 124, 65, 2, 131, 249, 233, 95, 96, 18, 213, 202, 255, 3, 224, 235, 143, 36, 213, 38, 51, 240, 157, 30, 216, 173, 240, 9, 200, 102, 138, 227, 86, 209, 178, 180, 19, 129, 4, 70, 198, 110, 172, 153, 240, 223, 153, 26, 97, 253, 107, 71, 192, 248, 109, 240, 102, 156, 53, 93, 56, 178, 94, 67, 20, 141, 165, 12, 83, 4, 74, 26, 225, 224, 182, 46, 136, 197, 191, 230, 58, 29, 5, 105, 164, 108, 213, 212, 188, 157, 130, 97, 7, 185, 56, 51, 61, 230, 52, 129, 244, 148, 71, 193, 90, 162, 1, 142, 224, 206, 168, 244, 240, 228, 252, 141, 116, 190, 248, 107, 253, 13, 16, 164, 44, 161, 129, 197, 106, 103, 40, 251, 73, 70, 82, 138, 247, 244, 236, 92, 34, 179, 28, 171, 218, 216, 80, 56, 103, 45, 210, 156, 108, 20, 141, 236, 251, 146, 250, 79, 66, 181, 97, 190, 38, 21, 94, 104, 16, 27, 18, 42, 230, 73, 85, 25, 160, 172, 8, 28, 3, 82, 201, 233, 178, 42, 112, 1, 86, 22, 111, 177, 191, 125, 216, 221, 86, 16, 211, 128, 149, 5, 248, 188, 62, 44, 37, 81, 193, 157, 212, 250, 254, 191, 234, 179, 7, 84, 73, 161, 242, 152, 217, 106, 208, 44, 87, 150, 222, 104, 83, 235, 205, 126, 83, 179, 40, 176, 163, 68, 8, 250, 129, 79, 201, 194, 99, 68, 195, 157, 139, 166, 39, 30, 120, 242, 130, 64, 137, 109, 59, 54, 239, 145, 238, 172, 202, 2, 75, 95, 49, 67, 133, 77, 126, 73, 63, 7, 155, 146, 195, 87, 148, 147, 124, 146, 130, 15, 1, 187, 104, 32, 174, 161, 168, 253, 162, 66, 170, 41, 160, 213, 110, 143, 156, 75, 80, 132, 102, 234, 185, 178, 189, 192, 177, 136, 54, 38, 26, 154, 24, 5, 164, 179, 230, 172, 46, 201, 10, 108, 50, 87, 80, 240, 78, 224, 87, 17, 127, 176, 229, 17, 128, 140, 132, 104, 4, 91, 197, 53, 112, 238, 105, 201, 5, 123, 60, 157, 105, 10, 131, 139, 108, 13, 206, 186, 41, 14, 137, 206, 178, 46, 21, 128, 202, 101, 255, 88, 173, 172, 165, 162, 239, 131, 147, 96, 252, 125, 204, 152, 160, 255, 228, 209, 138, 53, 199, 178, 14, 213, 113, 222, 108, 44, 243, 197, 187, 93, 224, 167, 231, 136, 135, 169, 53, 186, 243, 45, 66, 38, 211, 232, 223, 79, 234, 40, 214, 231, 151, 51, 121, 209, 69, 2, 89, 202, 94, 200, 51, 121, 207, 172, 229, 155, 60, 180, 90, 132, 17, 158, 190, 220, 104, 251, 3, 66, 51, 200, 30, 71, 52, 165, 178, 83, 233, 185, 226, 91, 12, 18, 247, 49, 21, 183, 100, 92, 252, 233, 200, 15, 206, 168, 24, 110, 83, 60, 8, 244, 11, 101, 138, 159, 77, 113, 167, 250, 76, 207, 29, 46, 45, 141, 168, 188, 213, 38, 73, 188, 207, 48, 21, 227, 177, 7, 95, 44, 238, 138, 254, 59, 226, 231, 206, 250, 34, 109, 184, 238, 101, 70, 230, 125, 162, 78, 112, 254, 206, 17, 56, 102, 181, 246, 133, 248, 125, 229, 203, 169, 193, 195, 84, 64, 104, 129, 82, 242, 209, 83, 5, 141, 36, 221, 106, 104, 118, 184, 25, 67, 82, 7, 118, 60, 69, 247, 250, 213, 147, 245, 63, 139, 201, 31, 121, 184, 67, 105, 67, 31, 188, 2, 191, 142, 142, 180, 227, 134, 115, 212, 129, 35, 60, 5, 119, 246, 48, 108, 44, 67, 254, 55, 185, 23, 180, 104, 60, 255, 139, 59, 237, 4, 180, 87, 233, 7, 16, 240, 20, 50, 161, 205, 214, 198, 249, 169, 208, 240, 80, 38, 181, 47, 233, 215, 124, 206, 144, 236, 135, 193, 143, 222, 154, 173, 100, 91, 219, 146, 205, 225, 93, 236, 4, 10, 38, 16, 96, 100, 82, 35, 202, 54, 8, 133, 152, 1, 122, 235, 113, 131, 233, 133, 35, 22, 140, 194, 227, 25, 197, 37, 175, 225, 76, 100, 212, 109, 227, 45, 53, 120, 91, 18, 128, 98, 54, 150, 218, 163, 154, 236, 102, 12, 47, 158, 100, 237, 43, 4, 7, 191, 211, 19, 160, 158, 78, 133, 134, 50, 47, 51, 192, 129, 40, 133, 198, 84, 126, 65, 230, 205, 29, 97, 40, 2, 85, 117, 70, 15, 178, 141, 29, 50, 31, 17, 224, 249, 166, 144, 148, 11, 107, 138, 115, 6, 133, 162, 35, 79, 18, 240, 220, 91, 238, 47, 87, 249, 119, 73, 221, 166, 95, 71, 141, 124, 222, 69, 50, 143, 126, 133, 116, 252, 136, 99, 61, 87, 237, 89, 171, 183, 196, 157, 0, 27, 15, 11, 72, 21, 180, 186, 137, 8, 75, 144, 23, 109, 230, 144, 160, 227, 187, 139, 134, 104, 237, 107, 55, 59, 217, 52, 126, 81, 183, 36, 16, 177, 204, 201, 147, 222, 143, 157, 68, 145, 177, 230, 70, 196, 74, 132, 119, 238, 229, 90, 22, 234, 47, 28, 170, 18, 255, 226, 94, 197, 239, 76, 0, 56, 205, 230, 231, 58, 136, 30, 175, 64, 154, 20, 23, 171, 140, 211, 130, 91, 193, 78, 59, 203, 221, 55, 126, 11, 69, 19, 158, 67, 80, 242, 195, 76, 157, 118, 161, 237, 203, 255, 82, 238, 161, 114, 137, 110, 164, 155, 188, 183, 136, 188, 218, 138, 147, 245, 231, 199, 243, 83, 39, 237, 77, 21, 247, 2, 191, 140, 135, 203, 248, 122, 212, 153, 144, 76, 43, 10, 129, 136, 59, 170, 101, 32, 2, 180, 98, 213, 149, 185, 244, 176, 162, 152, 64, 91, 50, 82, 112, 91, 58, 16, 255, 51, 40, 158, 17, 68, 99, 241, 224, 40, 194, 128, 142, 186, 113, 15, 142, 167, 125, 69, 67, 87, 190, 54, 245, 33, 116, 7, 108, 192, 237, 251, 52, 61, 250, 147, 20, 254, 158, 235, 19, 133, 199, 249, 37, 17, 189, 159, 6, 228, 44, 249, 190, 147, 12, 33, 127, 43, 98, 32, 177, 185, 134, 145, 110, 170, 101, 205, 42, 250, 16, 120, 57, 45, 247, 91, 120, 33, 199, 85, 240, 230, 55, 171, 69, 212, 139, 229, 35, 139, 140, 162, 34, 142, 21, 105, 121, 53, 72, 190, 24, 89, 32, 149, 7, 198, 249, 103, 249, 141, 20, 160, 172, 135, 0, 174, 54, 132, 183, 47, 167, 206, 252, 190, 126, 204, 15, 23, 11, 59, 175, 113, 11, 219, 129, 189, 95, 250, 252, 113, 69, 173, 60, 204, 226, 19, 188, 181, 207, 195, 150, 215, 150, 171, 216, 197, 101, 255, 159, 206, 60, 63, 96, 49, 240, 109, 115, 106, 85, 83, 64, 179, 180, 75, 202, 192, 197, 105, 230, 245, 3, 30, 214, 232, 203, 7, 76, 27, 100, 39, 18, 149, 238, 248, 117, 87, 168, 200, 21, 224, 93, 156, 149, 10, 191, 145, 109, 181, 230, 74, 242, 94, 249, 239, 208, 21, 62, 47, 36, 217, 116, 120, 58, 49, 116, 197, 134, 203, 4, 128, 9, 214, 195, 114, 4, 10, 54, 240, 136, 62, 181, 55, 30, 69, 81, 68, 93, 193, 23, 239, 55, 141, 49, 115, 45, 91, 151, 61, 138, 51, 203, 197, 42, 45, 118, 175, 79, 244, 38, 150, 94, 202, 157, 207, 166, 146, 107, 203, 39, 234, 164, 88, 246, 92, 237, 102, 127, 193, 102, 58, 136, 113, 187, 118, 206, 193, 72, 115, 151, 243, 143, 16, 126, 170, 104, 25, 195, 90, 179, 58, 94, 162, 135, 126, 70, 103, 196, 182, 127, 23, 226, 247, 155, 80, 155, 250, 175, 179, 55, 56, 8, 247, 201, 205, 153, 161, 40, 184, 69, 152, 106, 255, 35, 245, 96, 125, 240, 237, 1, 111, 217, 1, 12, 220, 244, 181, 136, 45, 204, 17, 8, 152, 201, 188, 58, 60, 75, 155, 220, 221, 142, 186, 73, 116, 175, 172, 199, 121, 254, 71, 195, 16, 3, 93, 36, 173, 29, 172, 29, 193, 106, 52, 38, 183, 186, 53, 217, 169, 192, 82, 13, 127, 82, 37, 90, 99, 188, 114, 68, 16, 174, 112, 169, 92, 53, 195, 12, 109, 154, 139, 103, 215, 189, 18, 17, 225, 183, 140, 149, 187, 208, 37, 6, 90, 87, 70, 151, 25, 183, 31, 51, 244, 80, 185, 155, 22, 23, 110, 231, 251, 59, 172, 197, 45, 233, 61, 168, 233, 245, 217, 253, 228, 16, 61, 244, 4, 157, 203, 18, 128, 171, 206, 91, 63, 145, 214, 19, 71, 86, 145, 198, 109, 149, 196, 215, 158, 128, 86, 220, 241, 80, 192, 48, 130, 5, 98, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 1, 160, 130, 5, 83, 4, 130, 5, 79, 48, 130, 5, 75, 48, 130, 5, 71, 6, 11, 42, 134, 72, 134, 247, 13, 1, 12, 10, 1, 2, 160, 130, 4, 238, 48, 130, 4, 234, 48, 28, 6, 10, 42, 134, 72, 134, 247, 13, 1, 12, 1, 3, 48, 14, 4, 8, 225, 187, 108, 131, 1, 103, 57, 216, 2, 2, 8, 0, 4, 130, 4, 200, 49, 185, 84, 177, 202, 77, 73, 96, 46, 115, 118, 209, 103, 123, 194, 127, 124, 95, 203, 213, 167, 31, 152, 115, 47, 133, 186, 31, 175, 73, 161, 24, 239, 175, 94, 158, 234, 43, 220, 129, 131, 25, 205, 254, 66, 66, 246, 206, 150, 223, 236, 41, 79, 41, 96, 74, 91, 19, 152, 242, 87, 199, 215, 45, 122, 155, 64, 176, 5, 16, 45, 25, 75, 111, 180, 249, 97, 193, 151, 23, 174, 179, 102, 151, 110, 218, 245, 90, 235, 223, 220, 225, 156, 212, 240, 34, 40, 5, 21, 201, 22, 201, 226, 89, 183, 161, 136, 159, 190, 171, 216, 191, 87, 9, 250, 247, 219, 112, 93, 211, 111, 7, 54, 114, 75, 1, 114, 155, 205, 203, 237, 89, 246, 194, 162, 73, 172, 117, 1, 171, 58, 130, 140, 238, 20, 153, 16, 3, 79, 56, 177, 72, 18, 97, 108, 68, 37, 244, 49, 113, 189, 211, 241, 161, 27, 248, 198, 101, 225, 150, 129, 218, 220, 121, 124, 34, 139, 133, 164, 48, 156, 63, 244, 26, 132, 190, 103, 244, 25, 116, 215, 197, 204, 131, 207, 154, 31, 192, 255, 5, 158, 9, 181, 242, 5, 50, 70, 243, 69, 252, 34, 243, 170, 14, 134, 176, 226, 123, 115, 108, 195, 77, 185, 171, 164, 55, 241, 39, 185, 158, 75, 47, 218, 142, 244, 181, 64, 221, 25, 88, 119, 131, 249, 79, 73, 75, 137, 233, 125, 187, 100, 51, 47, 120, 232, 99, 40, 74, 211, 129, 78, 136, 66, 205, 175, 6, 79, 140, 52, 46, 140, 248, 248, 236, 165, 44, 244, 209, 175, 56, 81, 206, 107, 15, 250, 87, 37, 81, 111, 26, 235, 219, 99, 212, 202, 54, 127, 227, 194, 97, 220, 62, 238, 252, 145, 14, 198, 103, 60, 122, 67, 31, 119, 83, 135, 150, 218, 11, 150, 190, 202, 67, 175, 113, 97, 250, 192, 177, 237, 68, 123, 102, 12, 65, 237, 169, 107, 180, 166, 10, 102, 125, 194, 216, 94, 217, 126, 48, 240, 55, 225, 117, 161, 168, 187, 58, 129, 74, 143, 187, 160, 9, 1, 45, 206, 249, 222, 2, 68, 91, 255, 32, 94, 217, 6, 122, 9, 160, 93, 135, 37, 194, 129, 168, 59, 136, 123, 165, 189, 104, 16, 31, 109, 8, 10, 11, 2, 199, 121, 97, 183, 172, 1, 172, 180, 71, 228, 96, 85, 0, 216, 32, 235, 12, 233, 132, 77, 230, 225, 10, 31, 171, 161, 248, 187, 26, 187, 128, 152, 208, 244, 13, 55, 214, 113, 214, 34, 23, 34, 148, 18, 121, 213, 169, 200, 66, 249, 207, 197, 37, 75, 131, 198, 157, 254, 155, 50, 40, 128, 33, 24, 8, 74, 135, 46, 112, 209, 155, 55, 252, 114, 112, 0, 52, 12, 59, 231, 191, 140, 174, 18, 102, 155, 161, 224, 226, 47, 59, 95, 57, 18, 69, 245, 149, 141, 131, 127, 58, 52, 93, 152, 177, 233, 112, 57, 69, 79, 249, 159, 246, 90, 63, 19, 165, 115, 208, 72, 113, 95, 136, 191, 162, 73, 239, 187, 244, 225, 42, 104, 39, 153, 212, 78, 38, 85, 37, 88, 244, 160, 198, 223, 144, 1, 238, 183, 130, 50, 68, 2, 223, 48, 107, 138, 241, 9, 166, 51, 142, 234, 13, 188, 31, 61, 97, 150, 253, 24, 81, 174, 131, 173, 227, 133, 1, 98, 159, 65, 93, 117, 45, 198, 192, 250, 108, 161, 136, 26, 37, 1, 14, 168, 187, 34, 160, 131, 67, 128, 0, 148, 175, 143, 191, 126, 127, 23, 23, 100, 183, 72, 147, 190, 182, 210, 175, 176, 183, 148, 31, 216, 34, 177, 212, 41, 113, 241, 5, 150, 9, 74, 40, 125, 176, 241, 125, 235, 246, 34, 194, 73, 182, 40, 14, 170, 135, 96, 100, 230, 187, 47, 210, 231, 122, 152, 81, 153, 254, 69, 116, 166, 243, 138, 247, 32, 221, 156, 202, 67, 218, 45, 139, 218, 123, 248, 80, 154, 116, 143, 128, 239, 194, 243, 102, 61, 42, 155, 54, 164, 120, 205, 6, 68, 242, 185, 64, 5, 85, 62, 138, 27, 228, 204, 133, 127, 132, 210, 21, 239, 229, 216, 147, 61, 102, 110, 176, 62, 211, 20, 208, 132, 108, 43, 68, 222, 177, 162, 138, 254, 57, 93, 26, 113, 178, 7, 121, 9, 96, 235, 8, 141, 223, 86, 72, 207, 39, 205, 179, 215, 28, 90, 33, 175, 202, 4, 206, 174, 195, 44, 57, 219, 143, 249, 28, 59, 209, 135, 184, 186, 240, 119, 216, 41, 179, 1, 211, 181, 30, 236, 42, 123, 96, 213, 130, 244, 222, 197, 207, 84, 180, 187, 95, 199, 64, 89, 254, 161, 24, 133, 134, 68, 13, 50, 65, 166, 22, 140, 138, 101, 253, 152, 7, 29, 26, 143, 103, 253, 47, 233, 13, 209, 110, 169, 232, 249, 189, 80, 24, 165, 157, 226, 171, 89, 137, 201, 20, 27, 228, 211, 21, 37, 71, 115, 199, 81, 15, 110, 153, 53, 11, 179, 113, 86, 65, 57, 12, 185, 143, 145, 67, 248, 159, 63, 98, 15, 197, 201, 228, 254, 122, 125, 246, 224, 221, 168, 137, 76, 186, 218, 172, 32, 13, 62, 251, 60, 114, 86, 62, 41, 92, 17, 62, 126, 214, 59, 200, 185, 219, 209, 82, 65, 153, 93, 6, 60, 58, 81, 70, 201, 43, 179, 211, 243, 204, 189, 174, 98, 202, 8, 88, 222, 129, 237, 123, 205, 116, 15, 131, 0, 168, 251, 34, 226, 209, 219, 222, 120, 159, 218, 11, 187, 45, 31, 118, 223, 152, 20, 99, 127, 222, 96, 92, 18, 46, 50, 7, 189, 88, 81, 92, 210, 50, 73, 117, 227, 254, 61, 144, 188, 174, 209, 101, 209, 208, 76, 143, 241, 170, 158, 69, 243, 119, 236, 127, 0, 50, 244, 149, 194, 138, 178, 106, 156, 70, 78, 176, 35, 173, 59, 24, 109, 223, 87, 62, 46, 199, 168, 67, 238, 25, 211, 67, 221, 161, 250, 6, 124, 194, 184, 139, 194, 48, 38, 173, 224, 245, 204, 155, 76, 65, 46, 138, 162, 21, 15, 246, 155, 93, 173, 39, 163, 179, 17, 61, 143, 171, 133, 181, 25, 175, 9, 169, 23, 193, 113, 241, 202, 74, 58, 197, 189, 141, 90, 160, 189, 248, 80, 183, 66, 97, 144, 95, 125, 83, 241, 52, 33, 127, 114, 233, 162, 0, 124, 85, 201, 164, 206, 10, 98, 144, 89, 174, 135, 29, 246, 53, 35, 42, 183, 200, 215, 124, 11, 186, 146, 93, 17, 37, 122, 182, 26, 138, 88, 75, 169, 192, 33, 194, 15, 38, 30, 8, 180, 242, 210, 161, 214, 83, 46, 87, 198, 203, 41, 104, 94, 193, 241, 65, 255, 33, 200, 51, 142, 83, 189, 248, 169, 162, 3, 170, 184, 247, 21, 205, 140, 94, 225, 176, 113, 251, 118, 182, 221, 190, 61, 173, 100, 47, 217, 186, 121, 235, 50, 127, 144, 164, 9, 60, 218, 169, 249, 82, 47, 3, 186, 209, 132, 122, 40, 107, 68, 70, 110, 254, 87, 222, 118, 255, 30, 142, 46, 4, 89, 174, 118, 235, 59, 29, 190, 151, 75, 115, 144, 200, 191, 142, 141, 150, 60, 242, 231, 7, 66, 137, 120, 144, 49, 70, 48, 31, 6, 9, 42, 134, 72, 134, 247, 13, 1, 9, 20, 49, 18, 30, 16, 0, 66, 0, 97, 0, 114, 0, 107, 0, 80, 0, 117, 0, 115, 0, 104, 48, 35, 6, 9, 42, 134, 72, 134, 247, 13, 1, 9, 21, 49, 22, 4, 20, 206, 241, 31, 180, 60, 189, 54, 61, 207, 254, 234, 236, 195, 214, 138, 23, 108, 207, 31, 97, 48, 48, 48, 33, 48, 9, 6, 5, 43, 14, 3, 2, 26, 5, 0, 4, 20, 241, 195, 202, 243, 191, 180, 20, 176, 187, 228, 42, 58, 125, 238, 212, 149, 210, 143, 124, 153, 4, 8, 156, 27, 11, 86, 128, 35, 225, 219, 2, 1, 1}
}
func postPush(category string, title string, body string, deviceToken string, params map[string]interface{}) error{
notification := &apns2.Notification{}
notification.DeviceToken = deviceToken
payload := payload.NewPayload().Sound("1107").Category("myNotificationCategory")
badge := params["badge"]
if badge != nil {
badgeStr, pass := badge.(string)
if pass {
badgeNum, err := strconv.Atoi(badgeStr)
if err == nil {
payload = payload.Badge(badgeNum)
}
}
}
for key, value := range params {
payload = payload.Custom(key, value)
}
if len(title) > 0 {
payload.AlertTitle(title)
}
if len(body) > 0 {
payload.AlertBody(body)
}
notification.Payload = payload
notification.Topic = "me.fin.bark"
res, err := apnsClient.Push(notification)
if err != nil {
log.Println("Error:", err)
return errors.New("与苹果推送服务器传输数据失败")
}
log.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
if res.StatusCode == 200 {
return nil
}else{
return errors.New("推送发送失败 " + res.Reason)
}
}
var IsDev bool = false
var boltDB *bolt.DB
var apnsClient *apns2.Client
func main() {
//f,_:= os.Open("./BarkPush.p12")
//t,_ := ioutil.ReadAll(f)
//
//str := ""
//for _,val := range t {
// str += ", "
// str += strconv.Itoa(int(val))
//}
//
//fmt.Printf(string(t))
ip := flag.String("ip", "0.0.0.0", "http listen ip")
port := flag.Int("port", 8080, "http listen port")
dev := flag.Bool("dev", false, "develop推送,请忽略此参数,设置此参数为True会导致推送失败")
flag.Parse()
IsDev = *dev
db, err := bolt.Open("bark.db", 0600, nil)
if err != nil {
log.Fatalln(err)
}
defer db.Close()
boltDB = db
boltDB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("device"))
if err != nil {
log.Fatalln(err)
}
return err
})
cert, err := certificate.FromP12Bytes(getb(),"bp")
if err != nil {
log.Fatalln("cer error")
}
apnsClient = apns2.NewClient(cert).Production()
addr := *ip + ":" + strconv.Itoa(*port)
log.Println("Serving HTTP on " + addr)
r := bone.New()
r.Get("/ping", http.HandlerFunc(ping))
r.Post("/ping", http.HandlerFunc(ping))
r.Get("/register", http.HandlerFunc(register))
r.Post("/register", http.HandlerFunc(register))
r.Get("/:key/:body", http.HandlerFunc(Index))
r.Post("/:key/:body", http.HandlerFunc(Index))
r.Get("/:key/:title/:body", http.HandlerFunc(Index))
r.Post("/:key/:title/:body", http.HandlerFunc(Index))
r.Get("/:key/:category/:title/:body", http.HandlerFunc(Index))
r.Post("/:key/:category/:title/:body", http.HandlerFunc(Index))
log.Fatal(http.ListenAndServe(addr, r))
}