This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
310 lines (263 loc) · 11.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2016 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/astaxie/beego/orm"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/gorilla/mux"
"github.com/netsec-ethz/scion-coord/config"
"github.com/netsec-ethz/scion-coord/controllers"
"github.com/netsec-ethz/scion-coord/controllers/api"
"github.com/netsec-ethz/scion-coord/controllers/middleware"
"github.com/netsec-ethz/scion-coord/models"
"github.com/scionproto/scion/go/lib/addr"
"golang.org/x/crypto/acme/autocert"
)
// initialize ISD location mapping
func initializeISD() error {
raw, err := ioutil.ReadFile(config.ISDLocationMapping)
if err != nil {
return fmt.Errorf("ERROR: Cannot access ISD location mapping json file: %v", err)
}
var isdLocs []struct {
ISD addr.ISD
Country string
Continent string
}
json.Unmarshal(raw, &isdLocs)
for _, ISD := range isdLocs {
_, err = models.FindISDbyID(ISD.ISD)
if err == orm.ErrNoRows {
isd := models.ISDLocation{
ISD: ISD.ISD,
Country: ISD.Country,
Continent: ISD.Continent,
}
err = isd.Insert()
}
if err != nil {
return fmt.Errorf("ERROR: Cannot insert ISD location mapping into database:"+
" %v", err)
}
}
return nil
}
// check if credential files exist and create necessary directories
func checkCredentialsDirectories() error {
aps, err := models.GetAllAPs()
if err != nil {
return err
}
for _, ap := range aps {
isd := ap.ISD
for _, f := range []string{api.TrcFile(isd), api.CoreCertFile(isd),
api.CoreSigKey(isd)} {
if _, err := os.Stat(f); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("ERROR: Credential file %s does not exist. Please make "+
"sure that the necessary credential files exist.\n"+
"Consult the README.md for further details.", f)
}
return fmt.Errorf("An error occurred when accessing " + f + ".")
}
}
}
os.MkdirAll(api.TempPath, os.ModePerm)
os.MkdirAll(api.PackagePath, os.ModePerm)
return nil
}
func main() {
for _, v := range os.Args {
if v == "--help" {
// also used to initialize the DB schema
fmt.Printf("Usage: %s\n", os.Args[0])
return
}
}
if err := initializeISD(); err != nil {
fmt.Printf("There was an error updating"+
" the ISD location mapping in the database: %v", err)
return
}
// check if credential files exist and create necessary directories
if err := checkCredentialsDirectories(); err != nil {
fmt.Printf("There was an error checking credential files: %v", err)
return
}
// controllers
registrationController := api.RegistrationController{}
loginController := api.LoginController{}
userController := api.UserController{}
adminController := api.AdminController{}
asController := api.ASInfoController{}
scionLabASController := api.SCIONLabASController{}
scionBoxController := api.SCIONBoxController{}
scionImageBuildController := api.CreateSCIONImgBuildController()
// rate limitation
resendLimit := tollbooth.NewLimiter(1, time.Minute*10,
&limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour})
resendLimit.SetOnLimitReached(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Blocked %v from accessing '/api/resendLink' because of reached rate limit",
w.Header().Get("X-Rate-Limit-Request-Remote-Addr"))
})
resendLimit.SetMessage("You can request an email every 10 minutes")
// router
router := mux.NewRouter()
loggingChain := middleware.NewWithLogging()
// public chain does not require authentication but serves back the XSRF Token
xsrfChain := middleware.NewWithLogging(middleware.XSRFHandler)
// Api chain goes through the authentication handler, which verifies either the session or the
// account_id.secret combination
apiChain := middleware.NewWithLogging(middleware.AuthHandler)
// User chain goes through UserHandler which checks if the user is logged in
userChain := middleware.NewWithLogging(middleware.UserHandler)
// Admin chain goes through AdminHandler which checks if user is marked as admin
adminChain := middleware.NewWithLogging(middleware.AdminHandler)
// handle favicon requests
router.Handle("/favicon.ico", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/img/favicon.ico")
}))
// index page
router.Handle("/", xsrfChain.ThenFunc(controllers.Index))
// ==========================================================
// SCION Coord API
// user registration
router.Handle("/api/register", loggingChain.ThenFunc(
registrationController.Register)).Methods(http.MethodPost)
router.Handle("/api/captchaSiteKey", loggingChain.ThenFunc(
registrationController.LoadCaptchaSiteKey))
// resend verification email
router.Handle("/api/resendLink", tollbooth.LimitHandler(resendLimit, loggingChain.ThenFunc(
registrationController.ResendActivationLink))).Methods(http.MethodPost)
// reset user password
router.Handle("/api/resetPassword", tollbooth.LimitHandler(resendLimit, loggingChain.ThenFunc(
registrationController.ResetPassword))).Methods(http.MethodPost)
// user login
router.Handle("/api/login", loggingChain.ThenFunc(loginController.Login))
// user Logout
router.Handle("/api/logout", loggingChain.ThenFunc(loginController.Logout))
// user information
router.Handle("/api/userPageData", apiChain.ThenFunc(userController.UserInformation))
// change password by logged-in users
router.Handle("/api/changePassword", userChain.ThenFunc(
userController.ChangePassword)).Methods(http.MethodPost)
// email validation
router.Handle("/api/verifyEmail/{uuid}", loggingChain.ThenFunc(
registrationController.VerifyEmail))
// set password after pre-approved registration or password reset
router.Handle("/api/setPassword", loggingChain.ThenFunc(
registrationController.SetPassword)).Methods(http.MethodPost)
// admin page
router.Handle("/api/adminPageData", adminChain.ThenFunc(adminController.AdminInformation))
router.Handle("/api/sendInvitations", adminChain.ThenFunc(
adminController.SendInvitationEmails)).Methods(http.MethodPost)
// generates a SCIONLab AS
// TODO(ercanucan): fix the authentication
router.Handle("/api/as/generateAS", userChain.ThenFunc(
scionLabASController.GenerateNewSCIONLabAS)).Methods(http.MethodPost)
router.Handle("/api/as/configureAS", userChain.ThenFunc(
scionLabASController.ConfigureSCIONLabAS)).Methods(http.MethodPost)
router.Handle("/api/as/removeAS/{as_id}", userChain.ThenFunc(
scionLabASController.RemoveSCIONLabAS))
router.Handle("/api/as/downloadTarball/{as_id}", userChain.ThenFunc(
scionLabASController.ReturnTarball))
router.Handle("/api/as/remapId/{ia}", loggingChain.ThenFunc(
scionLabASController.RemapASIdentityChallengeAndSolution)).Methods(http.MethodGet, http.MethodPost)
router.Handle("/api/as/remapIdDownloadGen/{ia}", loggingChain.ThenFunc(
scionLabASController.RemapASDownloadGen)).Methods(http.MethodPost)
router.Handle("/api/as/remapIdConfirmStatus/{ia}", loggingChain.ThenFunc(
scionLabASController.RemapASConfirmStatus)).Methods(http.MethodPost)
router.Handle("/api/as/getUpdatesForAP/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.GetUpdatesForAP))
router.Handle("/api/as/confirmUpdatesFromAP/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.ConfirmUpdatesFromAP))
// full synchronization (not only pending changes) for the APs:
router.Handle("/api/as/getConnectionsForAP/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.GetConnectionsForAP))
router.Handle("/api/as/setConnectionsForAP/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.SetConnectionsForAP))
// upgrade related calls:
router.Handle("/api/as/queryUpdateBranch/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.QueryUpdateBranch))
router.Handle("/api/as/confirmUpdate/{account_id}/{secret}",
apiChain.ThenFunc(scionLabASController.ConfirmUpdate)).Methods(http.MethodPost)
router.Handle("/api/as/getASData/{account_id}/{secret}/{ia}",
apiChain.ThenFunc(scionLabASController.GetASData))
//SCIONBox API
router.Handle("/api/as/initBox", loggingChain.ThenFunc(scionBoxController.InitializeBox))
router.Handle("/api/as/connectBox/{account_id}/{secret}", apiChain.ThenFunc(
scionBoxController.ConnectNewBox))
router.Handle("/api/as/heartbeat/{account_id}/{secret}", apiChain.ThenFunc(
scionBoxController.HeartBeatFunction))
// ==========================================================
// SCION Web API
router.Handle("/api/as/exists/{as_id}/{account_id}/{secret}", apiChain.ThenFunc(
asController.Exists))
// ISD join request
router.Handle("/api/as/uploadJoinRequest/{account_id}/{secret}", apiChain.ThenFunc(
asController.UploadJoinRequest))
router.Handle("/api/as/uploadJoinReply/{account_id}/{secret}", apiChain.ThenFunc(
asController.UploadJoinReply))
router.Handle("/api/as/pollJoinReply/{account_id}/{secret}", apiChain.ThenFunc(
asController.PollJoinReply))
// AS connection request
router.Handle("/api/as/uploadConnRequest/{account_id}/{secret}", apiChain.ThenFunc(
asController.UploadConnRequest))
router.Handle("/api/as/uploadConnReply/{account_id}/{secret}", apiChain.ThenFunc(
asController.UploadConnReply))
// show all request TO this AS
router.Handle("/api/as/pollEvents/{account_id}/{secret}", apiChain.ThenFunc(
asController.PollEvents))
// list the ASes the requesting AS can connect to
router.Handle("/api/as/listASes/{account_id}/{secret}", apiChain.ThenFunc(
asController.ListASes))
// ==========================================================
// Virtual currency API
router.Handle("/api/listASConnections/{account_id}/{secret}/{ia}",
apiChain.ThenFunc(asController.ListASesConnectionsWithCredits))
// ==========================================================
// Image building API
router.Handle("/api/imgbuild/images",
apiChain.ThenFunc(scionImageBuildController.GetAvailableDevices))
router.Handle("/api/imgbuild/create/{as_id}",
apiChain.ThenFunc(scionImageBuildController.GenerateImage)).Methods(http.MethodPost)
router.Handle("/api/imgbuild/user-images",
apiChain.ThenFunc(scionImageBuildController.GetUserImages))
// serve static files
static := http.StripPrefix("/public/", http.FileServer(http.Dir("public")))
router.PathPrefix("/public/").Handler(xsrfChain.Then(static))
// serve website using https or standard http
if config.HTTPEnableHTTPS {
fmt.Printf("Serving website on %v over HTTPS\n", config.HTTPHostAddress)
// redirect HTTP traffic to HTTPS
go http.ListenAndServe(":80", http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusMovedPermanently)
}))
// listen to HTTPS requests
log.Fatal(http.Serve(autocert.NewListener(config.HTTPHostAddress), router))
} else {
fmt.Printf("Serving website on %v over HTTP\n", config.HTTPHostAddress)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d",
config.HTTPBindAddress, config.HTTPBindPort), router))
}
}