-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (90 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"errors"
"io"
"log"
"net/http"
"strings"
"unifiedpush.org/go/np2p_dbus/config"
"unifiedpush.org/go/np2p_dbus/distributor"
"unifiedpush.org/go/np2p_dbus/storage"
"unifiedpush.org/go/np2p_dbus/utils"
)
var store *storage.Storage
var dbus *distributor.DBus
func main() {
var err error
if store, err = storage.InitStorage(utils.StoragePath("np2p.db")); err != nil {
log.Fatalln("failed to connect database")
}
config.Init("np2p")
dbus = distributor.NewDBus("org.unifiedpush.Distributor.NP2P")
dbus.StartHandling(handler{})
go handleEndpointSettingsChanges()
http.HandleFunc("/", httpHandle)
utils.Log.Debugln("listening on", config.GetIPPort(), "with endpoints like", config.GetEndpointURL("<token>"), "...")
log.Fatal(http.ListenAndServe(config.GetIPPort(), nil))
}
func handleEndpointSettingsChanges() {
endpointFormat := config.GetEndpointURL("<token>")
for _, i := range store.GetUnequalSettings(endpointFormat) {
utils.Log.Debugln("new endpoint format for", i.AppID, i.AppToken)
//newconnection updates the endpoint settings when one already exists
n := store.NewConnection(i.AppID, i.AppToken, endpointFormat)
if n == nil || n.Settings != endpointFormat {
utils.Log.Debugln("unable to save new endpoint format for", i.AppID, i.AppToken)
continue
}
dbus.NewConnector(n.AppID).NewEndpoint(n.AppToken, config.GetEndpointURL(n.PublicToken))
}
}
func httpHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
io.WriteString(w, `{"unifiedpush" : {"version" : 1}}`)
} else if r.Method == http.MethodPost {
parts := strings.Split(r.URL.Path, "/")
utils.Log.Debugln("received request from", r.URL.Path)
var token string
if len(parts) > 0 {
token = parts[0]
} else {
w.WriteHeader(400)
return
}
conn := store.GetConnectionbyPublic(token)
if conn == nil {
w.WriteHeader(404)
return
}
body, _ := io.ReadAll(io.LimitReader(r.Body, 4005))
if len(body) > 4003 {
w.WriteHeader(413)
return
}
w.WriteHeader(202)
//implement 429 counter
_ = dbus.NewConnector(conn.AppID).Message(conn.AppToken, body, "") //TODO errors
utils.Log.Infoln("MESSAGE", conn.AppID, conn.AppToken, "from", r.RemoteAddr)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
type handler struct {
}
func (h handler) Register(appName, token string) (endpoint, refuseReason string, err error) {
conn := store.NewConnection(appName, token, config.GetEndpointURL("<token>"))
utils.Log.Debugln("registered", conn)
if conn != nil {
return config.GetEndpointURL(conn.PublicToken), "", nil
}
//np2p doesn't have a situation for refuse
return "", "", errors.New("Unknown error with NoProvider2Push")
}
func (h handler) Unregister(token string) {
deletedConn, err := store.DeleteConnection(token)
utils.Log.Debugln("deleted", deletedConn)
if err != nil {
//?????
}
_ = dbus.NewConnector(deletedConn.AppID).Unregistered(deletedConn.AppToken)
}