Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

manage the source code of buford myself #125

Merged
merged 3 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.vendor
_vendor
vendor
bin/gaurun
bin/gaurun_client
bin/gaurun_recover
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312
go.uber.org/atomic v1.1.0
go.uber.org/atomic v1.1.0 // indirect
go.uber.org/zap v0.0.0-20170224221842-12592ca48efc
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
)

replace github.com/RobotsAndPencils/buford => github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e
replace github.com/RobotsAndPencils/buford => ./vendor/buford
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
github.com/BurntSushi/toml v0.2.0 h1:OthAm9ZSUx4uAmn3WbPwc06nowWrByRwBsYRhbmFjBs=
github.com/BurntSushi/toml v0.2.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/RobotsAndPencils/buford v0.12.0 h1:2nfOk+N/QVoQHwXIS0m5TFdvlUjEnqAj/0yXKR75azY=
github.com/RobotsAndPencils/buford v0.12.0/go.mod h1:27KhJZ/wLQHRnsZF+mTWKvF5w8U4dVl4Nh+BfQem4Lo=
github.com/client9/reopen v0.0.0-20160619053521-4b86f9c0ead5 h1:46QA9E5dIKm6lNygBd+eSiLF32HsgjJwbA1IELhs5Vo=
github.com/client9/reopen v0.0.0-20160619053521-4b86f9c0ead5/go.mod h1:caXVCEr+lUtoN1FlsRiOWdfQtdRHIYfcb0ai8qKWtkQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e h1:6pcQA9RJaK0CtoW/BB58Je4BT916gyH5hCWSH2s+B7o=
github.com/flexfrank/buford v0.13.1-0.20190906024551-21672ff2794e/go.mod h1:9w6wdgYczkqCGQWRaV0BB7ygAqNgcnRTHX+1w4E3OWc=
github.com/fukata/golang-stats-api-handler v0.0.0-20160325105040-ab9f90f16caa h1:YxLexpeQS0Fz/sNa3QQEgLWyVhgNhEB1GQUi+c45nFs=
github.com/fukata/golang-stats-api-handler v0.0.0-20160325105040-ab9f90f16caa/go.mod h1:1sIi4/rHq6s/ednWMZqTmRq3765qTUSs/c3xF6lj8J8=
github.com/lestrrat/go-server-starter v0.0.0-20151125041704-901cec093d58 h1:9/ngkqJb42WLtYR6EFRnImztkmF+n5EhwKBxVOj2B3o=
Expand All @@ -25,8 +21,6 @@ go.uber.org/zap v0.0.0-20170224221842-12592ca48efc/go.mod h1:vwi/ZaCAaUcBkycHslx
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 h1:0hQKqeLdqlt5iIwVOBErRisrHJAN57yOiPRQItI20fU=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20161116075034-4971afdc2f16 h1:x2xFZACPoDbV+g+48fDH/4EQTTNPgHTRko7g0JQiZws=
golang.org/x/net v0.0.0-20161116075034-4971afdc2f16/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
9 changes: 9 additions & 0 deletions vendor/buford/.github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Please ensure that you are using the latest version of Buford and its dependencies before reporting an issue. Use the `-u` flag with `go get` as described in the [README](https://github.com/RobotsAndPencils/buford/blob/master/README.md#installation).

NOTE: please don't include your device token or certificate password in issues.

1. What version of Go are you using (`go version`)?
2. What operating system (GOOS) are you using (`go env`) and what version?
3. What did you do? (steps to reproduce or a code sample is helpful)
4. What did you expect to see?
5. What did you see instead?
9 changes: 9 additions & 0 deletions vendor/buford/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.p12
*.pem
*.cer
c.out
*.pass
*.pkpass
!testdata/*
*.sublime-project
coverage.txt
33 changes: 33 additions & 0 deletions vendor/buford/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
sudo: false
language: go

go:
- 1.7.1
- 1.6.3
- "1.10"
- tip

matrix:
allow_failures:
- go: tip
fast_finish: true

before_script:
- go get -u golang.org/x/lint/golint

script:
- ./test.sh

after_script:
- test -z "$(gofmt -s -l -w . | tee /dev/stderr)"
- test -z "$(golint ./... | tee /dev/stderr)"
- go vet ./...

after_success:
- bash <(curl -s https://codecov.io/bash)

os:
- linux

notifications:
email: false
21 changes: 21 additions & 0 deletions vendor/buford/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Robots and Pencils

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
234 changes: 234 additions & 0 deletions vendor/buford/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
# Buford

Apple Push Notification (APN) Provider library for Go 1.6 and HTTP/2. Send remote notifications to iOS, macOS, tvOS and watchOS. Buford can also sign push packages for Safari notifications and Wallet passes.

Please see [releases](https://github.com/RobotsAndPencils/buford/releases) for updates.

[![GoDoc](https://godoc.org/github.com/RobotsAndPencils/buford?status.svg)](https://godoc.org/github.com/RobotsAndPencils/buford) [![Build Status](https://travis-ci.org/RobotsAndPencils/buford.svg?branch=ci)](https://travis-ci.org/RobotsAndPencils/buford) ![MIT](https://img.shields.io/badge/license-MIT-blue.svg) [![codecov](https://codecov.io/gh/RobotsAndPencils/buford/branch/master/graph/badge.svg)](https://codecov.io/gh/RobotsAndPencils/buford)

### Documentation

Buford uses Apple's new HTTP/2 Notification API that was announced at WWDC 2015 and [released on December 17, 2015](https://developer.apple.com/news/?id=12172015b).

[API documentation](https://godoc.org/github.com/RobotsAndPencils/buford/) is available from GoDoc.

Also see Apple's [Local and Remote Notification Programming Guide][notification], especially the sections on the JSON [payload][] and the [Notification API][notification-api].

[notification]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/Introduction.html
[payload]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1
[notification-api]: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/APNsProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH101-SW1

#### Terminology

**APN** Apple Push Notification

**Provider** The Buford library is used to create a _provider_ of push notifications.

**Service** Apple's push notification service that Buford communicates with.

**Client** An `http.Client` provides an HTTP/2 client to communicate with the APN Service.

**Notification** A payload, device token, and headers.

**Device Token** An identifier for an application on a given device.

**Payload** The JSON sent to a device.

**Headers** HTTP/2 headers are used to set priority and expiration.

### Installation

This library requires [Go 1.6.3](https://golang.org/dl/) or better.

```
go get -u -d github.com/RobotsAndPencils/buford
```

Buford depends on several packages outside of the standard library, including the http2 package. Its certificate package depends on the pkcs12 and pushpackage depends on pkcs7. They can be retrieved or updated with:

```
go get -u golang.org/x/net/http2
go get -u golang.org/x/crypto/pkcs12
go get -u github.com/aai/gocrypto/pkcs7
```

I am still looking for feedback on the API so it may change. Please copy Buford and its dependencies into a `vendor/` folder at the root of your project.

### Examples

```go
package main

import (
"encoding/json"
"fmt"

"github.com/RobotsAndPencils/buford/certificate"
"github.com/RobotsAndPencils/buford/payload"
"github.com/RobotsAndPencils/buford/payload/badge"
"github.com/RobotsAndPencils/buford/push"
)

// set these variables appropriately
const (
filename = "/path/to/certificate.p12"
password = ""
host = push.Development
deviceToken = "c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433"
)

func main() {
// load a certificate and use it to connect to the APN service:
cert, err := certificate.Load(filename, password)
exitOnError(err)

client, err := push.NewClient(cert)
exitOnError(err)

service := push.NewService(client, host)

// construct a payload to send to the device:
p := payload.APS{
Alert: payload.Alert{Body: "Hello HTTP/2"},
Badge: badge.New(42),
}
b, err := json.Marshal(p)
exitOnError(err)

// push the notification:
id, err := service.Push(deviceToken, nil, b)
exitOnError(err)

fmt.Println("apns-id:", id)
}
```

See `example/push` for the complete listing.

#### Concurrent use

HTTP/2 can send multiple requests over a single connection, but `service.Push` waits for a response before returning. Instead, you can wrap a `Service` in a queue to handle responses independently, allowing you to send multiple notifications at once.

```go
var wg sync.WaitGroup
queue := push.NewQueue(service, numWorkers)

// process responses (responses may be received in any order)
go func() {
for resp := range queue.Responses {
log.Println(resp)
// done receiving and processing one response
wg.Done()
}
}()

// send the notifications
for i := 0; i < 100; i++ {
// increment count of notifications sent and queue it
wg.Add(1)
queue.Push(deviceToken, nil, b)
}

// wait for all responses to be processed
wg.Wait()
// shutdown the channels and workers for the queue
queue.Close()
```

It's important to set up a goroutine to handle responses before sending any notifications, otherwise Push will block waiting for room to return a Response.

You can configure the number of workers used to send notifications concurrently, but be aware that a larger number isn't necessarily better, as Apple limits the number of concurrent streams. From the Apple Push Notification documentation:

> "The APNs server allows multiple concurrent streams for each connection. The exact number of streams is based on server load, so do not assume a specific number of streams."

See `example/concurrent/` for a complete listing.

#### Headers

You can specify an ID, expiration, priority, and other parameters via the Headers struct.

```go
headers := &push.Headers{
ID: "922D9F1F-B82E-B337-EDC9-DB4FC8527676",
Expiration: time.Now().Add(time.Hour),
LowPriority: true,
}

id, err := service.Push(deviceToken, headers, b)
```

If no ID is specified APNS will generate and return a unique ID. When an expiration is specified, APNS will store and retry sending the notification until that time, otherwise APNS will not store or retry the notification. LowPriority should always be set when sending a ContentAvailable payload.

#### Custom values

To add custom values to an APS payload, use the Map method as follows:

```go
p := payload.APS{
Alert: payload.Alert{Body: "Message received from Bob"},
}
pm := p.Map()
pm["acme2"] = []string{"bang", "whiz"}

b, err := json.Marshal(pm)
if err != nil {
log.Fatal(b)
}

id, err := service.Push(deviceToken, nil, b)
```

#### Error responses

Errors from `service.Push` or `queue.Response` could be HTTP errors or an error response from Apple. To access the Reason and HTTP Status code, you must convert the `error` to a `push.Error` as follows:

```go
if e, ok := err.(*push.Error); ok {
switch e.Reason {
case push.ErrBadDeviceToken:
// handle error
}
}
```

### Website Push

Before you can send push notifications through Safari and the Notification Center, you must provide a push package, which is a signed zip file containing some JSON and icons.

Use `pushpackage` to write a zip to a `http.ResponseWriter` or to a file. It will create the `manifest.json` and `signature` files for you.

```go
pkg := pushpackage.New(w)
pkg.EncodeJSON("website.json", website)
pkg.File("icon.iconset/[email protected]", "static/[email protected]")
// other icons... (required)
if err := pkg.Sign(cert, nil); err != nil {
log.Fatal(err)
}
```

NOTE: The filenames added to the zip may contain forward slashes but not back slashes or drive letters.

See `example/website/` and the [Safari Push Notifications][safari] documentation.

[safari]: https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html#//apple_ref/doc/uid/TP40013225-CH3-SW12

### Wallet (Passbook) Pass

A pass is a signed zip file with a .pkpass extension and a `application/vnd.apple.pkpass` MIME type. You can use `pushpackage` to write a .pkpass that contains a `pass.json` file.

See `example/wallet/` and the [Wallet Developer Guide][wallet].

[wallet]: https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/PassKit_PG/index.html

### Related Projects

* [apns2](https://github.com/sideshow/apns2) Alternative HTTP/2 APN provider library (Go)
* [go-apns-server](https://github.com/CleverTap/go-apns-server) Mock APN server (Go)
* [gorush](https://github.com/appleboy/gorush) A push notification server (Go)
* [Push Encryption](https://github.com/GoogleChrome/push-encryption-go) Web Push for Chrome and Firefox (Go)
* [micromdm](https://micromdm.io/) Mobile Device Management server (Go)
* [Lowdown](https://github.com/alloy/lowdown) (Ruby)
* [Apnotic](https://github.com/ostinelli/apnotic) (Ruby)
* [Pigeon](https://github.com/codedge-llc/pigeon) (Elixir, iOS and Android)
* [APNSwift](https://github.com/kaunteya/APNSwift) (Swift)
Loading