Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

C/C++ implementation of Web Push

License

NotificationsYou must be signed in to change notification settings

rnascunha/pusha

Repository files navigation

Build

Pusha is a library implemented in C/C++ to makeWeb Push requests. The library aims to provide a easy interface that, supplying the necessary information, will deal with all the complexity, delivering the HTTP headers and payload encryptation needed to make the HTTP request. It also provides some HTTP facilities as serialize data to send.

Pusha brings 3 tools to assist:

  • genkey will generate a pair ofprivate/public keys needed to send web push requests;
  • pusha is a tool to send web push requests via command line;
  • export_key export a public key of a private key given as input.

Decendencies

It depends of:

Build

To build, first download the code:

$ git clone --recursive https://github.com/rnascunha/pusha$cd pusha

Let's create a build directory and compile:

$ mkdir build$cd build#Build and compile$ cmake ..$ cmake --build.

This will build thePusha library (libpusha.a at linux). If you want to build thetools and/or the examples,add the following arguments to thecmake command:

$ cmake -DWITH_TOOLS=1 -DWITH_EXAMPLES=1 ..

Tools

⚠️ Buildpusha with-DWITH_TOOLS=1 to compile the tools.

genkey

This tool will create a pair ofprivate/public key that is needed to make push requests. It prints the keys in base64 encoded, and also export to file in a PEM format.

$ ./genkey -hUsage:./genkey -h|[-p<private_pem_file>] [-u<public_pem_file>]Where:-hprint thishelp message-poutput private key to pem file specified-uoutput public key to pem file specified

The private key is used withPusha at the backend itself, and the public key pair at your website. As example:

$ ./genkey -p priv.pemPrivate: 3zDn2khtNBpZCAUjwBepiaVy3u6bbVKZwlFP3d3nUboPublic: BKE67tSj-yFp3ZsRruJnEwiGxj8KMUkC_5gk_tjRtoVDBHBhvpPX8DgOSVZXkey2AM1pk1vzEd7hlk_-KOqV_Yw

This command will also create apriv.pem with the private key.

pusha tool

This tool allows to send push notification at command line.

$ ./pusha -hUsage:./pusha -h|(-p<pem_priv_file>|-b<base64_priv_key>) [-v][-m<message>] [-e<expire_time_seconds>][-o send|curl|print] [-l<ttl>]<subscriber><p256dh><auth><endpoint>Where:<subscriber>vapid subscriber (e.g. mainto:email@company.com)<p256dh>public server key (received at push subscription)<auth>authentication secret (received at push subscription)<endpoint>endpoint (received at push subscription)-vverbose mode-hthishelp message-ppem file with EC private key (don't use with'-b')-bbase64 encoded private key (don't use with'-p')-eseconds to expiretime (default 12h, i.e, 12* 60* 60)-oset output type. Options:'send' (default),'curl' or'print'-lset http ttl value (default = 0)-mmessage payload to send

Five arguments are mandatory:

  • -p <pem_priv_file>|-b <base64_priv_key>: the private key (can be generate with thegenkey tool above);
  • subscriber: a information of contact. A URL or a email, e.gmailto:email@company.com;
  • p256dh,auth andendpoint: this information is received when the user allows to receive a push notification. Your application is responsible to keep this information for each user. When the user subscribes, it will present the information in a JSON format, like this:
{"endpoint":"https://fcm.googleapis.com/fcm/send/eAIof_7CKT0:APA91bGtHiknduwFFRTTHF59vT05bsduAR_uAhWCGSxU-D8O3wg7Km0cRF246956jg-DPTlUj8xgAJP1I6VJU_xJipbpGg6rS4_B8qC5yKhqalDbkSDPwZ87ki_P3RlskUb1BEKY6wI8","expirationTime":null,"keys":{"p256dh":"BMkGGRuBBhQf8H2s_I2Xz2487IaKqmP9WW3YRbgfi7MS4HkgLo73ZnbVOe5OLNL7judxPtElktgCLwOMWxRDLyo","auth":"0cFEpUxPVUWT8NlnKF5xSQ"}}

The fields correspondence is direct. To send a push request with payload to the user with the above information:

$ ./push -v -p priv.pem maito:email@company.com BMkGGRuBBhQf8H2s_I2Xz2487IaKqmP9WW3YRbgfi7MS4HkgLo73ZnbVOe5OLNL7judxPtElktgCLwOMWxRDLyo 0cFEpUxPVUWT8NlnKF5xSQ https://fcm.googleapis.com/fcm/send/eAIof_7CKT0:APA91bGtHiknduwFFRTTHF59vT05bsduAR_uAhWCGSxU-D8O3wg7Km0cRF246956jg-DPTlUj8xgAJP1I6VJU_xJipbpGg6rS4_B8qC5yKhqalDbkSDPwZ87ki_P3RlskUb1BEKY6wI8 -m'My first push message'-------Arguments------+ Subscribe: maito:email@company.com+ pd256h: BMkGGRuBBhQf8H2s_I2Xz2487IaKqmP9WW3YRbgfi7MS4HkgLo73ZnbVOe5OLNL7judxPtElktgCLwOMWxRDLyo+ auth: 0cFEpUxPVUWT8NlnKF5xSQ+ expiration time: 1628852983+ ttl: 0+ output: send+ endpoint: https://fcm.googleapis.com/fcm/send/eAIof_7CKT0:APA91bGtHiknduwFFRTTHF59vT05bsduAR_uAhWCGSxU-D8O3wg7Km0cRF246956jg-DPTlUj8xgAJP1I6VJU_xJipbpGg6rS4_B8qC5yKhqalDbkSDPwZ87ki_P3RlskUb1BEKY6wI8+ host[26]: https://fcm.googleapis.com+ payload: My first push message----------------------* Decoding subscription...*+ Subscription decoded* Generating VAPID token...*+ VAPID token generated...* Push request with payload* Encoding push payload...*+ Push payload encoded success...* Making HTTP headers...*+ HTTP headers created* Creating output...* Sending push request...* Serializing request...*+ Request serialized* Searchingfor host fcm.googleapis.com* Trying to connect to 2800:3f0:4004:809::200a*+ Connected*+ SSL connected using TLS_AES_256_GCM_SHA384* Sending SSL packet*+ SSL packet sent[853]* Wating response...*+ Received 602 bytes*+ Web push request sent successfully> HTTP response: 201 Created

Output with verbose mode ON (-v).

Thepusha tool is a good source to learn how to use thepusha library. It construct the push request step by step using some "internal" structures, learning how to manipulate the library.

export_key

This tool will export a public key in base64url encoded, and/or PEM file. The input private key can also be a PEM file format, or base64url.

./export_key -hUsage:./export_key -h|<priv_key_pem_file|base64url_priv_key> [<export_pem_file>]Where:-hThishelp message.<priv_key_pem_file>PEM file with private key to be exported.<base64url_priv_key>Base64url encoded privated key to be exported.<export_pem_file>If provided,export public key to file name.

Examples

⚠️ Buildpusha with -DWITH_EXAMPLES=1 to compile the examples.

There are two examples that shows how to use thePusha library. The examples will print the information as a HTTP request should be. Both examples begin importing theprivate key from a PEM file. The only diference is the interface used:

  • web_push: call thepusha_notify function, that populates thepusha_http_headers andpusha_payload (if any payload is present);
  • web_push_http: call thepusha_notify_http function, that populates thepusha_http_header;
  • genkey_cpp: is a implementation of thegenkey tool, but using the CPP interface;
  • notify_cpp: use the CPPpusha::notify class to create the push request.

See also

  • RFC8030 - Generic Event Delivery Using HTTP Push;
  • RFC8292 - Voluntary Application Server Identification (VAPID) for Web Push;
  • JSON Web Token;
  • Push codelabs - It provides a web page to test the push notitication (but usepusha instead to send message 😘). When it saysApplication Server Keys, is the public pair of the keys generated;
  • HTTP internals - good explanation of the how to generate/use keys and create the HTTP headers;

About

C/C++ implementation of Web Push

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2026 Movatter.jp