| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Dev is a simple development server. |
| 18 |
package main |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/base64" |
| 22 |
"encoding/json" |
| 23 |
"net" |
| 24 |
"net/http" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
"strings" |
| 28 |
|
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/config" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/server" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/api" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker/simple" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/listener" |
| 35 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/session" |
| 36 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/statistics" |
| 37 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/store" |
| 38 |
) |
| 39 |
|
| 40 |
type configuration struct { |
| 41 |
// device server configuration |
| 42 |
server.DevicesParsedConfig |
| 43 |
// api http server configuration |
| 44 |
server.HTTPServeParsedConfig |
| 45 |
// user credentials to be used for access to the stats endpoint |
| 46 |
StatisticsAuthUser string `json:"statistics_auth_user"` |
| 47 |
// password credentials to be used for access to the stats endpoint |
| 48 |
StatisticsAuthPassword string `json:"statistics_auth_password"` |
| 49 |
// delivery domain |
| 50 |
DeliveryDomain string `json:"delivery_domain"` |
| 51 |
// max notifications per application |
| 52 |
MaxNotificationsPerApplication int `json:"max_notifications_per_app"` |
| 53 |
} |
| 54 |
|
| 55 |
type Storage struct { |
| 56 |
sto store.PendingStore |
| 57 |
maxNotificationsPerApplication int |
| 58 |
} |
| 59 |
|
| 60 |
func (storage *Storage) StoreForRequest(http.ResponseWriter, *http.Request) (store.PendingStore, error) { |
| 61 |
return storage.sto, nil |
| 62 |
} |
| 63 |
|
| 64 |
func (storage *Storage) GetMaxNotificationsPerApplication() int { |
| 65 |
return storage.maxNotificationsPerApplication |
| 66 |
} |
| 67 |
|
| 68 |
func main() { |
| 69 |
cfgFpaths := os.Args[1:] |
| 70 |
cfg := &configuration{} |
| 71 |
err := config.ReadFiles(cfg, cfgFpaths...) |
| 72 |
if err != nil { |
| 73 |
server.BootLogFatalf("reading config: %v", err) |
| 74 |
} |
| 75 |
err = cfg.DevicesParsedConfig.LoadPEMs(filepath.Dir(cfgFpaths[len(cfgFpaths)-1])) |
| 76 |
if err != nil { |
| 77 |
server.BootLogFatalf("reading config: %v", err) |
| 78 |
} |
| 79 |
logger := logger.NewSimpleLogger(os.Stderr, "info") |
| 80 |
// Setup statistics |
| 81 |
currentStats := statistics.NewStatistics(logger) |
| 82 |
// setup a pending store and start the broker |
| 83 |
sto := store.NewInMemoryPendingStore() |
| 84 |
broker := simple.NewSimpleBroker(sto, cfg, logger, currentStats) |
| 85 |
broker.Start() |
| 86 |
defer broker.Stop() |
| 87 |
// serve the http api |
| 88 |
storage := &Storage{ |
| 89 |
sto: sto, |
| 90 |
maxNotificationsPerApplication: cfg.MaxNotificationsPerApplication, |
| 91 |
} |
| 92 |
lst, err := net.Listen("tcp", cfg.Addr()) |
| 93 |
if err != nil { |
| 94 |
server.BootLogFatalf("start device listening: %v", err) |
| 95 |
} |
| 96 |
mux := api.MakeHandlersMux(storage, broker, logger) |
| 97 |
// & /delivery-hosts |
| 98 |
mux.HandleFunc("/delivery-hosts", func(w http.ResponseWriter, req *http.Request) { |
| 99 |
w.Header().Set("Cache-Control", "no-cache") |
| 100 |
w.Header().Set("Content-Type", "application/json") |
| 101 |
enc := json.NewEncoder(w) |
| 102 |
enc.Encode(map[string]interface{}{ |
| 103 |
"hosts": []string{lst.Addr().String()}, |
| 104 |
"domain": cfg.DeliveryDomain, |
| 105 |
}) |
| 106 |
}) |
| 107 |
// /stats |
| 108 |
mux.HandleFunc("/stats", func(w http.ResponseWriter, req *http.Request) { |
| 109 |
var err error |
| 110 |
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) |
| 111 |
s := strings.SplitN(req.Header.Get("Authorization"), " ", 2) |
| 112 |
if len(s) != 2 { |
| 113 |
http.Error(w, "Not authorized", 401) |
| 114 |
return |
| 115 |
} |
| 116 |
|
| 117 |
b, err := base64.StdEncoding.DecodeString(s[1]) |
| 118 |
if err != nil { |
| 119 |
http.Error(w, err.Error(), 401) |
| 120 |
return |
| 121 |
} |
| 122 |
|
| 123 |
pair := strings.SplitN(string(b), ":", 2) |
| 124 |
if len(pair) != 2 { |
| 125 |
http.Error(w, "Not authorized", 401) |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
if pair[0] != cfg.StatisticsAuthUser || pair[1] != cfg.StatisticsAuthPassword { |
| 130 |
http.Error(w, "Not authorized", 401) |
| 131 |
return |
| 132 |
} |
| 133 |
|
| 134 |
statsData := currentStats.GetStats() |
| 135 |
statsJSON := &[]byte{} |
| 136 |
*statsJSON, err = json.Marshal(statsData) |
| 137 |
if err != nil { |
| 138 |
server.BootLogFatalf("Error marshal stats json: %v", err) |
| 139 |
} |
| 140 |
w.Header().Set("Cache-Control", "no-cache") |
| 141 |
w.Header().Set("Content-Type", "application/json") |
| 142 |
w.Header().Set("Access-Control-Allow-Origin", "*") |
| 143 |
w.Write(*statsJSON) |
| 144 |
}) |
| 145 |
handler := api.PanicTo500Handler(mux, logger) |
| 146 |
go server.HTTPServeRunner(nil, handler, &cfg.HTTPServeParsedConfig, cfg.DevicesParsedConfig.TLSServerConfig())() |
| 147 |
// listen for device connections |
| 148 |
resource := &listener.NopSessionResourceManager{} |
| 149 |
server.DevicesRunner(lst, func(conn net.Conn) error { |
| 150 |
track := session.NewTracker(logger) |
| 151 |
return session.Session(conn, broker, cfg, track) |
| 152 |
}, logger, resource, &cfg.DevicesParsedConfig)() |
| 153 |
} |