@@ -16,6 +16,7 @@ package lib
16
16
17
17
import (
18
18
"encoding/json"
19
+ "flag"
19
20
"fmt"
20
21
"io/ioutil"
21
22
"net"
@@ -25,37 +26,178 @@ import (
25
26
"path/filepath"
26
27
"regexp"
27
28
"sort"
29
+ "strconv"
28
30
"strings"
29
31
30
32
log "github.com/inconshreveable/log15"
31
33
. "github.com/netsec-ethz/scion-apps/webapp/util"
32
34
)
33
35
34
36
// default params for localhost testing
35
- var cliPortDef = "30001"
36
- var serPortDefBwt = "30100"
37
+ var listenAddrDef = "127.0.0.1"
38
+ var listenPortDef = 8000
39
+ var cliPortDef = 30001
40
+ var serPortDef = 30100
37
41
var serDefAddr = "127.0.0.2"
38
42
39
43
var cfgFileCliUser = "config/clients_user.json"
40
44
var cfgFileSerUser = "config/servers_user.json"
41
45
var cfgFileCliDef = "config/clients_default.json"
42
46
var cfgFileSerDef = "config/servers_default.json"
43
47
48
+ // command argument constants
49
+ var CMD_ADR = "a"
50
+ var CMD_PRT = "p"
51
+ var CMD_ART = "sabin"
52
+ var CMD_WEB = "srvroot"
53
+ var CMD_BRT = "r"
54
+ var CMD_SCN = "sroot"
55
+ var CMD_SCB = "sbin"
56
+ var CMD_SCG = "sgen"
57
+ var CMD_SCC = "sgenc"
58
+ var CMD_SCL = "slogs"
59
+
60
+ // appsRoot is the root location of scionlab apps.
61
+ var GOPATH = os .Getenv ("GOPATH" )
62
+
63
+ // scionRoot is the root location of the scion infrastructure.
64
+ var DEF_SCIONDIR = path .Join (GOPATH , "src/github.com/scionproto/scion" )
65
+
44
66
// UserSetting holds the serialized structure for persistent user settings
45
67
type UserSetting struct {
46
- MyIA string `json:"myIa"`
68
+ MyIA string `json:"myIa"`
69
+ SDAddress string `json:"sdAddress"`
47
70
}
48
71
49
72
type CmdOptions struct {
50
- StaticRoot , BrowseRoot , AppsRoot , ScionRoot , ScionBin , ScionGen , ScionGenCache , ScionLogs string
73
+ Addr string
74
+ Port int
75
+ StaticRoot string
76
+ BrowseRoot string
77
+ AppsRoot string
78
+ ScionRoot string
79
+ ScionBin string
80
+ ScionGen string
81
+ ScionGenCache string
82
+ ScionLogs string
83
+ }
84
+
85
+ func (o * CmdOptions ) AbsPathCmdOptions () {
86
+ o .StaticRoot , _ = filepath .Abs (o .StaticRoot )
87
+ o .BrowseRoot , _ = filepath .Abs (o .BrowseRoot )
88
+ o .AppsRoot , _ = filepath .Abs (o .AppsRoot )
89
+ o .ScionRoot , _ = filepath .Abs (o .ScionRoot )
90
+ o .ScionBin , _ = filepath .Abs (o .ScionBin )
91
+ o .ScionGen , _ = filepath .Abs (o .ScionGen )
92
+ o .ScionGenCache , _ = filepath .Abs (o .ScionGenCache )
93
+ o .ScionLogs , _ = filepath .Abs (o .ScionLogs )
94
+ }
95
+
96
+ func isFlagUsed (name string ) bool {
97
+ found := false
98
+ flag .Visit (func (f * flag.Flag ) {
99
+ if f .Name == name {
100
+ found = true
101
+ }
102
+ })
103
+ return found
104
+ }
105
+
106
+ // defaultAppsRoot returns the directory containing the webapp executable as
107
+ // the default base directory for the apps resources
108
+ func defaultAppsRoot () string {
109
+ exec , err := os .Executable ()
110
+ if err != nil {
111
+ return ""
112
+ }
113
+ return path .Dir (exec )
114
+ }
115
+
116
+ func defaultStaticRoot (appsRoot string ) string {
117
+ return path .Join (appsRoot , "../webapp/web" )
118
+ }
119
+
120
+ func defaultBrowseRoot (staticRoot string ) string {
121
+ return path .Join (staticRoot , "data" )
122
+ }
123
+
124
+ func defaultScionBin (scionRoot string ) string {
125
+ return path .Join (scionRoot , "bin" )
126
+ }
127
+
128
+ func defaultScionGen (scionRoot string ) string {
129
+ return path .Join (scionRoot , "gen" )
130
+ }
131
+
132
+ func defaultScionGenCache (scionRoot string ) string {
133
+ return path .Join (scionRoot , "gen-cache" )
134
+ }
135
+
136
+ func defaultScionLogs (scionRoot string ) string {
137
+ return path .Join (scionRoot , "logs" )
138
+ }
139
+
140
+ func ParseFlags () CmdOptions {
141
+ addr := flag .String (CMD_ADR , listenAddrDef , "Address of server host." )
142
+ port := flag .Int (CMD_PRT , listenPortDef , "Port of server host." )
143
+ appsRoot := flag .String (CMD_ART , defaultAppsRoot (),
144
+ "Path to execute the installed scionlab apps binaries" )
145
+ staticRoot := flag .String (CMD_WEB , defaultStaticRoot (* appsRoot ),
146
+ "Path to read/write web server files." )
147
+ browseRoot := flag .String (CMD_BRT , defaultBrowseRoot (* staticRoot ),
148
+ "Root path to read/browse from, CAUTION: read-access granted from -a and -p." )
149
+ scionRoot := flag .String (CMD_SCN , DEF_SCIONDIR ,
150
+ "Path to read SCION root directory of infrastructure" )
151
+ scionBin := flag .String (CMD_SCB , defaultScionBin (* scionRoot ),
152
+ "Path to execute SCION bin directory of infrastructure tools" )
153
+ scionGen := flag .String (CMD_SCG , defaultScionGen (* scionRoot ),
154
+ "Path to read SCION gen directory of infrastructure config" )
155
+ scionGenCache := flag .String (CMD_SCC , defaultScionGenCache (* scionRoot ),
156
+ "Path to read SCION gen-cache directory of infrastructure run-time config" )
157
+ scionLogs := flag .String (CMD_SCL , defaultScionLogs (* scionRoot ),
158
+ "Path to read SCION logs directory of infrastructure logging" )
159
+ flag .Parse ()
160
+ // recompute root args to use the proper relative defaults if undefined
161
+ if ! isFlagUsed (CMD_WEB ) {
162
+ * staticRoot = defaultStaticRoot (* appsRoot )
163
+ }
164
+ if ! isFlagUsed (CMD_BRT ) {
165
+ * browseRoot = defaultBrowseRoot (* staticRoot )
166
+ }
167
+
168
+ if isFlagUsed (CMD_SCN ) {
169
+ if ! isFlagUsed (CMD_SCB ) {
170
+ * scionBin = defaultScionBin (* scionRoot )
171
+ }
172
+ if ! isFlagUsed (CMD_SCG ) {
173
+ * scionGen = defaultScionGen (* scionRoot )
174
+ }
175
+ if ! isFlagUsed (CMD_SCC ) {
176
+ * scionGenCache = defaultScionGenCache (* scionRoot )
177
+ }
178
+ if ! isFlagUsed (CMD_SCL ) {
179
+ * scionLogs = defaultScionLogs (* scionRoot )
180
+ }
181
+ }
182
+ options := CmdOptions {* addr , * port , * staticRoot , * browseRoot , * appsRoot ,
183
+ * scionRoot , * scionBin , * scionGen , * scionGenCache , * scionLogs }
184
+ options .AbsPathCmdOptions ()
185
+ return options
51
186
}
52
187
53
188
// WriteUserSetting writes the settings to disk.
54
- func WriteUserSetting (options * CmdOptions , settings UserSetting ) {
189
+ func WriteUserSetting (options * CmdOptions , settings * UserSetting ) {
55
190
cliUserFp := path .Join (options .StaticRoot , cfgFileCliUser )
191
+
192
+ // writing myIA means we have to retrieve sciond's tcp address too
193
+ // since sciond's address may be autognerated
194
+ config , err := LoadSciondConfig (options , settings .MyIA )
195
+ CheckError (err )
196
+ settings .SDAddress = config .SD .Address
56
197
settingsJSON , _ := json .Marshal (settings )
57
198
58
- err := ioutil .WriteFile (cliUserFp , settingsJSON , 0644 )
199
+ log .Info ("Updating..." , "UserSetting" , string (settingsJSON ))
200
+ err = ioutil .WriteFile (cliUserFp , settingsJSON , 0644 )
59
201
CheckError (err )
60
202
}
61
203
@@ -153,7 +295,8 @@ func GenServerNodeDefaults(options *CmdOptions, localIAs []string) {
153
295
// use all localhost endpoints as possible servers for bwtester as least
154
296
ia := strings .Replace (localIAs [i ], "_" , ":" , - 1 )
155
297
json := []byte (`{"name":"lo ` + ia + `","isdas":"` + ia +
156
- `", "addr":"` + serDefAddr + `","port":` + serPortDefBwt + `}` )
298
+ `", "addr":"` + serDefAddr + `","port":` + strconv .Itoa (serPortDef ) +
299
+ `}` )
157
300
jsonBuf = append (jsonBuf , json ... )
158
301
if i < (len (localIAs ) - 1 ) {
159
302
jsonBuf = append (jsonBuf , []byte (`,` )... )
@@ -168,7 +311,6 @@ func GenServerNodeDefaults(options *CmdOptions, localIAs []string) {
168
311
// SCION addresses as json
169
312
func GenClientNodeDefaults (options * CmdOptions , cisdas string ) {
170
313
cliFp := path .Join (options .StaticRoot , cfgFileCliDef )
171
- cport := cliPortDef
172
314
173
315
// find interface addresses
174
316
jsonBuf := []byte (`{ "all": [ ` )
@@ -192,7 +334,8 @@ func GenClientNodeDefaults(options *CmdOptions, cisdas string) {
192
334
cname := i .Name
193
335
caddr := ipnet .IP .String ()
194
336
jsonInterface := []byte (`{"name":"` + cname + `", "isdas":"` +
195
- cisdas + `", "addr":"` + caddr + `","port":` + cport + `}` )
337
+ cisdas + `", "addr":"` + caddr + `","port":` +
338
+ strconv .Itoa (cliPortDef ) + `}` )
196
339
jsonBuf = append (jsonBuf , jsonInterface ... )
197
340
idx ++
198
341
}
@@ -223,5 +366,5 @@ func GetNodesHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions
223
366
}
224
367
raw , err := ioutil .ReadFile (fp )
225
368
CheckError (err )
226
- fmt .Fprintf (w , string (raw ))
369
+ fmt .Fprint (w , string (raw ))
227
370
}
0 commit comments