├── .gitignore ├── README.md └── go-apns-server.go /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.pem 3 | *.iml 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-apns-server 2 | ============== 3 | 4 | A quick and dirty script to mock the new HTTP/2 interface to Apple's push gateway. 5 | Most requests to this server will return a successful response, however, some of 6 | them will return a "BadDeviceToken" or an "Unregistered" from time to time. 7 | 8 | # Usage 9 | ``` 10 | -cert string 11 | The path to the certificate (default "cert.pem") 12 | -key string 13 | The path to the certificate key (default "key.pem") 14 | -port int 15 | The port for the mock server to listen to (default 8443) 16 | ``` 17 | 18 | # Getting started 19 | ### Install the Go programming language 20 | See [here](https://golang.org/doc/install). Be sure to install at least version 1.6. 21 | 22 | ### Generate your self signed certificate 23 | Create a self signed certificate using one of Go's scripts: 24 | ``` 25 | $ go run /usr/local/go/src/crypto/tls/generate_cert.go --host clevertap.com 26 | ``` 27 | 28 | ### Start the server 29 | If you created the certificate elsewhere, you'll need to use the 30 | options -cert and -key to specify where the certificate and its key are located. 31 | ``` 32 | $ go build go-apns-server.go 33 | $ ./go-apns-server 34 | ``` 35 | 36 | The default port is 8443, which can be changed by passing the -port argument 37 | 38 | # License 39 | Licensed under the [New 3-Clause BSD License](http://opensource.org/licenses/BSD-3-Clause). 40 | 41 | ``` 42 | Copyright (c) 2016, CleverTap 43 | All rights reserved. 44 | 45 | Redistribution and use in source and binary forms, with or without 46 | modification, are permitted provided that the following conditions are met: 47 | 48 | * Redistributions of source code must retain the above copyright notice, this 49 | list of conditions and the following disclaimer. 50 | 51 | * Redistributions in binary form must reproduce the above copyright notice, 52 | this list of conditions and the following disclaimer in the documentation 53 | and/or other materials provided with the distribution. 54 | 55 | * Neither the name of CleverTap nor the names of its 56 | contributors may be used to endorse or promote products derived from 57 | this software without specific prior written permission. 58 | 59 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 60 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 62 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 63 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 65 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 66 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 67 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 68 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 69 | ``` 70 | -------------------------------------------------------------------------------- /go-apns-server.go: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2016, CleverTap 2 | // All rights reserved. 3 | // Licensed under the New 3-Clause BSD License 4 | // 5 | // A mock server for the HTTP2 Apple push gateway 6 | 7 | package main 8 | 9 | import ( 10 | "flag" 11 | "fmt" 12 | "io/ioutil" 13 | "log" 14 | "math/rand" 15 | "net/http" 16 | "strconv" 17 | ) 18 | 19 | func main() { 20 | serverPort := flag.Int("port", 8443, "The port for the mock server to listen to") 21 | serverCert := flag.String("cert", "cert.pem", "The path to the certificate") 22 | serverKey := flag.String("key", "key.pem", "The path to the certificate key") 23 | 24 | flag.Parse() 25 | 26 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 27 | _, err := ioutil.ReadAll(r.Body) 28 | if err != nil { 29 | log.Println("Error:", err) 30 | } 31 | n := rand.Int31n(10) 32 | 33 | statusCode := 200 34 | 35 | var body string 36 | 37 | if n == 7 { 38 | statusCode = 400 39 | body = "{\"reason\": \"BadDeviceToken\"}" 40 | } else if n > 7 { 41 | statusCode = 410 42 | body = "{\"reason\": \"Unregistered\"}" 43 | } 44 | 45 | w.WriteHeader(statusCode) 46 | 47 | if statusCode != 200 { 48 | w.Header().Set("Content-Type", "application/json") 49 | fmt.Fprintf(w, body) 50 | } 51 | }) 52 | 53 | log.Printf("Using certificate %#v with key %#v", *serverCert, *serverKey) 54 | log.Println("Starting server on port", *serverPort) 55 | log.Println("Press Ctrl+C to stop...") 56 | 57 | port := ":" + strconv.Itoa(*serverPort) 58 | log.Fatal(http.ListenAndServeTLS(port, *serverCert, *serverKey, nil)) 59 | } 60 | --------------------------------------------------------------------------------