Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Ntfy_archive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Ntfy_archive
Commits
63206f85
Commit
63206f85
authored
3 years ago
by
Philipp Heckel
Browse files
Options
Downloads
Patches
Plain Diff
Firebase keepalive, supports #56
parent
de0c41ec
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
config/config.go
+11
-8
11 additions, 8 deletions
config/config.go
server/server.go
+31
-4
31 additions, 4 deletions
server/server.go
tools/fbsend/main.go
+50
-0
50 additions, 0 deletions
tools/fbsend/main.go
with
93 additions
and
12 deletions
.gitignore
+
1
−
0
View file @
63206f85
...
...
@@ -2,4 +2,5 @@ dist/
build/
.idea/
server/docs/
tools/fbsend/fbsend
*.iml
This diff is collapsed.
Click to expand it.
config/config.go
+
11
−
8
View file @
63206f85
...
...
@@ -7,14 +7,15 @@ import (
// Defines default config settings
const
(
DefaultListenHTTP
=
":80"
DefaultCacheDuration
=
12
*
time
.
Hour
DefaultKeepaliveInterval
=
30
*
time
.
Second
DefaultManagerInterval
=
time
.
Minute
DefaultAtSenderInterval
=
10
*
time
.
Second
DefaultMinDelay
=
10
*
time
.
Second
DefaultMaxDelay
=
3
*
24
*
time
.
Hour
DefaultMessageLimit
=
512
DefaultListenHTTP
=
":80"
DefaultCacheDuration
=
12
*
time
.
Hour
DefaultKeepaliveInterval
=
30
*
time
.
Second
DefaultManagerInterval
=
time
.
Minute
DefaultAtSenderInterval
=
10
*
time
.
Second
DefaultMinDelay
=
10
*
time
.
Second
DefaultMaxDelay
=
3
*
24
*
time
.
Hour
DefaultMessageLimit
=
512
DefaultFirebaseKeepaliveInterval
=
time
.
Hour
)
// Defines all the limits
...
...
@@ -40,6 +41,7 @@ type Config struct {
KeepaliveInterval
time
.
Duration
ManagerInterval
time
.
Duration
AtSenderInterval
time
.
Duration
FirebaseKeepaliveInterval
time
.
Duration
MessageLimit
int
MinDelay
time
.
Duration
MaxDelay
time
.
Duration
...
...
@@ -66,6 +68,7 @@ func New(listenHTTP string) *Config {
MinDelay
:
DefaultMinDelay
,
MaxDelay
:
DefaultMaxDelay
,
AtSenderInterval
:
DefaultAtSenderInterval
,
FirebaseKeepaliveInterval
:
DefaultFirebaseKeepaliveInterval
,
GlobalTopicLimit
:
DefaultGlobalTopicLimit
,
VisitorRequestLimitBurst
:
DefaultVisitorRequestLimitBurst
,
VisitorRequestLimitReplenish
:
DefaultVisitorRequestLimitReplenish
,
...
...
This diff is collapsed.
Click to expand it.
server/server.go
+
31
−
4
View file @
63206f85
...
...
@@ -105,6 +105,10 @@ var (
errHTTPTooManyRequests
=
&
errHTTP
{
http
.
StatusTooManyRequests
,
http
.
StatusText
(
http
.
StatusTooManyRequests
)}
)
const
(
firebaseControlTopic
=
"~control"
// See Android if changed
)
// New instantiates a new Server. It creates the cache and adds a Firebase
// subscriber (if configured).
func
New
(
conf
*
config
.
Config
)
(
*
Server
,
error
)
{
...
...
@@ -152,9 +156,17 @@ func createFirebaseSubscriber(conf *config.Config) (subscriber, error) {
return
nil
,
err
}
return
func
(
m
*
message
)
error
{
_
,
err
:=
msg
.
Send
(
context
.
Background
(),
&
messaging
.
Message
{
Topic
:
m
.
Topic
,
Data
:
map
[
string
]
string
{
var
data
map
[
string
]
string
// Matches https://ntfy.sh/docs/subscribe/api/#json-message-format
switch
m
.
Event
{
case
keepaliveEvent
,
openEvent
:
data
=
map
[
string
]
string
{
"id"
:
m
.
ID
,
"time"
:
fmt
.
Sprintf
(
"%d"
,
m
.
Time
),
"event"
:
m
.
Event
,
"topic"
:
m
.
Topic
,
}
case
messageEvent
:
data
=
map
[
string
]
string
{
"id"
:
m
.
ID
,
"time"
:
fmt
.
Sprintf
(
"%d"
,
m
.
Time
),
"event"
:
m
.
Event
,
...
...
@@ -163,7 +175,11 @@ func createFirebaseSubscriber(conf *config.Config) (subscriber, error) {
"tags"
:
strings
.
Join
(
m
.
Tags
,
","
),
"title"
:
m
.
Title
,
"message"
:
m
.
Message
,
},
}
}
_
,
err
:=
msg
.
Send
(
context
.
Background
(),
&
messaging
.
Message
{
Topic
:
m
.
Topic
,
Data
:
data
,
})
return
err
},
nil
...
...
@@ -188,6 +204,17 @@ func (s *Server) Run() error {
}
}
}()
if
s
.
firebase
!=
nil
{
go
func
()
{
ticker
:=
time
.
NewTicker
(
s
.
config
.
FirebaseKeepaliveInterval
)
for
{
<-
ticker
.
C
if
err
:=
s
.
firebase
(
newKeepaliveMessage
(
firebaseControlTopic
));
err
!=
nil
{
log
.
Printf
(
"error sending Firebase keepalive message: %s"
,
err
.
Error
())
}
}
}()
}
listenStr
:=
fmt
.
Sprintf
(
"%s/http"
,
s
.
config
.
ListenHTTP
)
if
s
.
config
.
ListenHTTPS
!=
""
{
listenStr
+=
fmt
.
Sprintf
(
" %s/https"
,
s
.
config
.
ListenHTTPS
)
...
...
This diff is collapsed.
Click to expand it.
tools/fbsend/main.go
0 → 100644
+
50
−
0
View file @
63206f85
package
main
import
(
"context"
firebase
"firebase.google.com/go"
"firebase.google.com/go/messaging"
"flag"
"fmt"
"google.golang.org/api/option"
"os"
"strings"
)
func
main
()
{
conffile
:=
flag
.
String
(
"config"
,
"/etc/fbsend/fbsend.json"
,
"config file"
)
flag
.
Parse
()
if
flag
.
NArg
()
<
2
{
fail
(
"Syntax: fbsend [-config FILE] topic key=value ..."
)
}
topic
:=
flag
.
Arg
(
0
)
data
:=
make
(
map
[
string
]
string
)
for
i
:=
1
;
i
<
flag
.
NArg
();
i
++
{
kv
:=
strings
.
SplitN
(
flag
.
Arg
(
i
),
"="
,
2
)
if
len
(
kv
)
!=
2
{
fail
(
fmt
.
Sprintf
(
"Invalid argument: %s (%v)"
,
flag
.
Arg
(
i
),
kv
))
}
data
[
kv
[
0
]]
=
kv
[
1
]
}
fb
,
err
:=
firebase
.
NewApp
(
context
.
Background
(),
nil
,
option
.
WithCredentialsFile
(
*
conffile
))
if
err
!=
nil
{
fail
(
err
.
Error
())
}
msg
,
err
:=
fb
.
Messaging
(
context
.
Background
())
if
err
!=
nil
{
fail
(
err
.
Error
())
}
_
,
err
=
msg
.
Send
(
context
.
Background
(),
&
messaging
.
Message
{
Topic
:
topic
,
Data
:
data
,
})
if
err
!=
nil
{
fail
(
err
.
Error
())
}
fmt
.
Println
(
"Sent successfully"
)
}
func
fail
(
s
string
)
{
fmt
.
Println
(
s
)
os
.
Exit
(
1
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment