Compare commits
No commits in common. "main" and "v1.2.1" have entirely different histories.
|
|
@ -1,7 +1,6 @@
|
||||||
package autopush
|
package autopush
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/ecdh"
|
"crypto/ecdh"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
|
@ -19,7 +18,6 @@ const MOZILLA_PUSH_SERVICE = "wss://push.services.mozilla.com"
|
||||||
|
|
||||||
type AutoPushClient struct {
|
type AutoPushClient struct {
|
||||||
*websocket.WebSocketClient
|
*websocket.WebSocketClient
|
||||||
awaitingPong bool
|
|
||||||
ece *rfc8291.RFC8291
|
ece *rfc8291.RFC8291
|
||||||
helloChan chan HelloResponse
|
helloChan chan HelloResponse
|
||||||
notificationChan chan Notification
|
notificationChan chan Notification
|
||||||
|
|
@ -51,13 +49,8 @@ func unmarshaler[T any](payload json.RawMessage, label MessageType) (data *T) {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AutoPushClient) Ping() error {
|
|
||||||
c.awaitingPong = true
|
|
||||||
return c.SendJSON(struct{}{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *AutoPushClient) Hello(uaid string, channelIDs []string) (HelloResponse, error) {
|
func (c *AutoPushClient) Hello(uaid string, channelIDs []string) (HelloResponse, error) {
|
||||||
return request(c, c.helloChan, 5, "hello", HelloRequest{
|
return request(c, c.helloChan, 5, "hello timeout", HelloRequest{
|
||||||
Type: HELLO,
|
Type: HELLO,
|
||||||
UAID: uaid,
|
UAID: uaid,
|
||||||
ChannelIDs: channelIDs,
|
ChannelIDs: channelIDs,
|
||||||
|
|
@ -66,7 +59,7 @@ func (c *AutoPushClient) Hello(uaid string, channelIDs []string) (HelloResponse,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AutoPushClient) Register(channelID string, vapidKey string) (RegisterResponse, error) {
|
func (c *AutoPushClient) Register(channelID string, vapidKey string) (RegisterResponse, error) {
|
||||||
return request(c, c.registerChan, 5, "register", RegisterRequest{
|
return request(c, c.registerChan, 5, "register timeout", RegisterRequest{
|
||||||
Type: REGISTER,
|
Type: REGISTER,
|
||||||
ChannelID: channelID,
|
ChannelID: channelID,
|
||||||
Key: vapidKey,
|
Key: vapidKey,
|
||||||
|
|
@ -74,7 +67,7 @@ func (c *AutoPushClient) Register(channelID string, vapidKey string) (RegisterRe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AutoPushClient) Unregister(channelID string) (UnregisterResponse, error) {
|
func (c *AutoPushClient) Unregister(channelID string) (UnregisterResponse, error) {
|
||||||
return request(c, c.unregisterChan, 5, "unregister", UnregisterRequest{
|
return request(c, c.unregisterChan, 5, "unregister timeout", UnregisterRequest{
|
||||||
Type: UNREGISTER,
|
Type: UNREGISTER,
|
||||||
ChannelID: channelID,
|
ChannelID: channelID,
|
||||||
})
|
})
|
||||||
|
|
@ -109,8 +102,7 @@ func (c *AutoPushClient) Decrypt(
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAutoPushClient(ctx context.Context) (ap *AutoPushClient, ch chan Notification) {
|
func NewAutoPushClient() (ap *AutoPushClient, ch chan Notification) {
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
|
||||||
ap = &AutoPushClient{
|
ap = &AutoPushClient{
|
||||||
ece: rfc8291.NewRFC8291(sha256.New),
|
ece: rfc8291.NewRFC8291(sha256.New),
|
||||||
helloChan: make(chan HelloResponse),
|
helloChan: make(chan HelloResponse),
|
||||||
|
|
@ -118,29 +110,8 @@ func NewAutoPushClient(ctx context.Context) (ap *AutoPushClient, ch chan Notific
|
||||||
registerChan: make(chan RegisterResponse),
|
registerChan: make(chan RegisterResponse),
|
||||||
unregisterChan: make(chan UnregisterResponse),
|
unregisterChan: make(chan UnregisterResponse),
|
||||||
WebSocketClient: websocket.NewWebSocketClient(
|
WebSocketClient: websocket.NewWebSocketClient(
|
||||||
ctx,
|
nil,
|
||||||
func(ws *websocket.WebSocketClient) {
|
func(ws *websocket.WebSocketClient) {
|
||||||
// 3min: 10x more often than firefox
|
|
||||||
// if this is below 45sec, autopush will disconnect us
|
|
||||||
//
|
|
||||||
// https://github.com/mozilla-services/autopush-rs/blob/master/autoconnect/autoconnect-ws/autoconnect-ws-sm/src/identified/on_client_msg.rs#L295
|
|
||||||
// https://searchfox.org/firefox-main/source/dom/push/PushServiceWebSocket.sys.mjs#463
|
|
||||||
// https://searchfox.org/firefox-main/source/modules/libpref/init/all.js#3230
|
|
||||||
pushPing := time.NewTicker(55 * time.Second)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-pushPing.C:
|
|
||||||
ap.Ping()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
},
|
|
||||||
func(ws *websocket.WebSocketClient) {
|
|
||||||
cancel()
|
|
||||||
close(ap.helloChan)
|
close(ap.helloChan)
|
||||||
close(ap.notificationChan)
|
close(ap.notificationChan)
|
||||||
close(ap.registerChan)
|
close(ap.registerChan)
|
||||||
|
|
@ -154,17 +125,9 @@ func NewAutoPushClient(ctx context.Context) (ap *AutoPushClient, ch chan Notific
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Type == NULL {
|
|
||||||
message.Type = PING
|
|
||||||
}
|
|
||||||
|
|
||||||
switch message.Type {
|
switch message.Type {
|
||||||
case PING:
|
case PING:
|
||||||
if ap.awaitingPong {
|
ws.SendJSON("{}")
|
||||||
ap.awaitingPong = false
|
|
||||||
} else {
|
|
||||||
ap.Ping()
|
|
||||||
}
|
|
||||||
|
|
||||||
case HELLO:
|
case HELLO:
|
||||||
if data := unmarshaler[HelloResponse](payload, HELLO); data != nil {
|
if data := unmarshaler[HelloResponse](payload, HELLO); data != nil {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ const (
|
||||||
type MessageType string
|
type MessageType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NULL MessageType = ""
|
|
||||||
PING MessageType = "ping"
|
PING MessageType = "ping"
|
||||||
ACK MessageType = "ack"
|
ACK MessageType = "ack"
|
||||||
HELLO MessageType = "hello"
|
HELLO MessageType = "hello"
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/ecdh"
|
"crypto/ecdh"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
@ -102,7 +101,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
nicoPushClient, notificationChan, err := nicopush.NewNicoPushClient(
|
nicoPushClient, notificationChan, err := nicopush.NewNicoPushClient(
|
||||||
context.Background(),
|
|
||||||
config.UAID,
|
config.UAID,
|
||||||
config.ChannelIDs,
|
config.ChannelIDs,
|
||||||
config.AuthSecret,
|
config.AuthSecret,
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module git.min.rip/min/webpush-client-go
|
||||||
go 1.23.6
|
go 1.23.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.min.rip/min/websocket-client-go v1.2.0
|
git.min.rip/min/websocket-client-go v1.1.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
golang.org/x/crypto v0.35.0
|
golang.org/x/crypto v0.35.0
|
||||||
golang.org/x/net v0.35.0
|
golang.org/x/net v0.35.0
|
||||||
|
|
|
||||||
4
go.sum
4
go.sum
|
|
@ -1,5 +1,5 @@
|
||||||
git.min.rip/min/websocket-client-go v1.2.0 h1:hIwowEju/7lHHC3V6544jatB8Z9faKWbKD6gqZJwRq4=
|
git.min.rip/min/websocket-client-go v1.1.1 h1:oBIGt2HGPRpeqNY+isDagIKQUQ1W3KhVWJrp0vsMdwA=
|
||||||
git.min.rip/min/websocket-client-go v1.2.0/go.mod h1:qE2autSEJU74jUb+tET0J+Ozg2w7BVtg31Toq83kz4Q=
|
git.min.rip/min/websocket-client-go v1.1.1/go.mod h1:qE2autSEJU74jUb+tET0J+Ozg2w7BVtg31Toq83kz4Q=
|
||||||
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
|
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
|
||||||
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package nicopush
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"crypto/ecdh"
|
"crypto/ecdh"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
|
@ -33,7 +32,6 @@ type NicoPushClient struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNicoPushClient(
|
func NewNicoPushClient(
|
||||||
ctx context.Context,
|
|
||||||
uaid string,
|
uaid string,
|
||||||
channelIDs []string,
|
channelIDs []string,
|
||||||
authSecret []byte,
|
authSecret []byte,
|
||||||
|
|
@ -45,7 +43,7 @@ func NewNicoPushClient(
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ap, ch := autopush.NewAutoPushClient(ctx)
|
ap, ch := autopush.NewAutoPushClient()
|
||||||
|
|
||||||
client := &NicoPushClient{
|
client := &NicoPushClient{
|
||||||
autoPushClient: ap,
|
autoPushClient: ap,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue