1
1
package lib
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"io/ioutil"
6
7
"net"
7
8
"net/http"
8
9
"os"
9
10
"path"
11
+ "path/filepath"
12
+ "regexp"
10
13
"sort"
11
14
"strings"
12
15
16
+ log "github.com/inconshreveable/log15"
13
17
. "github.com/netsec-ethz/scion-apps/webapp/util"
14
18
)
15
19
@@ -23,7 +27,6 @@ var LABROOT = "src/github.com/netsec-ethz/scion-apps"
23
27
var GOPATH = os .Getenv ("GOPATH" )
24
28
25
29
// default params for localhost testing
26
- var cliIaDef = "1-ff00:0:111"
27
30
var serIaDef = "1-ff00:0:112"
28
31
var cliPortDef = "30001"
29
32
var serPortDefBwt = "30100"
@@ -36,18 +39,66 @@ var cfgFileSerUser = "config/servers_user.json"
36
39
var cfgFileCliDef = "config/clients_default.json"
37
40
var cfgFileSerDef = "config/servers_default.json"
38
41
39
- // GetLocalIa reads locally generated file for this IA's name, if written
40
- func GetLocalIa () string {
41
- filepath := path .Join (GOPATH , SCIONROOT , "gen/ia" )
42
- b , err := ioutil .ReadFile (filepath )
43
- if CheckError (err ) {
44
- return ""
42
+ // UserSetting holds the serialized structure for persistent user settings
43
+ type UserSetting struct {
44
+ MyIA string `json:"myIa"`
45
+ }
46
+
47
+ // WriteUserSetting writes the settings to disk.
48
+ func WriteUserSetting (srcpath string , settings UserSetting ) {
49
+ cliUserFp := path .Join (srcpath , cfgFileCliUser )
50
+ settingsJSON , _ := json .Marshal (settings )
51
+
52
+ err := ioutil .WriteFile (cliUserFp , settingsJSON , 0644 )
53
+ CheckError (err )
54
+ }
55
+
56
+ // ReadUserSetting reads the settings from disk.
57
+ func ReadUserSetting (srcpath string ) UserSetting {
58
+ var settings UserSetting
59
+ cliUserFp := path .Join (srcpath , cfgFileCliUser )
60
+
61
+ // no problem when user settings not set yet
62
+ raw , err := ioutil .ReadFile (cliUserFp )
63
+ log .Debug ("ReadClientsUser" , "settings" , string (raw ))
64
+ if ! CheckError (err ) {
65
+ json .Unmarshal ([]byte (raw ), & settings )
45
66
}
46
- return strings . Replace ( strings . TrimSpace ( string ( b )), "_" , ":" , - 1 )
67
+ return settings
47
68
}
48
69
49
- func GetCliIaDef () string {
50
- return cliIaDef
70
+ // ScanLocalIAs will load list of locally available IAs
71
+ func ScanLocalIAs () []string {
72
+ var localIAs []string
73
+ var reIaFilePathCap = `\/ISD([0-9]+)\/AS(\w+)`
74
+ re := regexp .MustCompile (reIaFilePathCap )
75
+ var searchPath = path .Join (GOPATH , SCIONROOT , "gen" )
76
+ filepath .Walk (searchPath , func (path string , f os.FileInfo , _ error ) error {
77
+ if f != nil && f .IsDir () {
78
+ capture := re .FindStringSubmatch (path )
79
+ if len (capture ) > 0 {
80
+ ia := capture [1 ] + "-" + capture [2 ]
81
+ ia = strings .Replace (ia , "_" , ":" , - 1 ) // convert once
82
+ if ! StringInSlice (localIAs , ia ) {
83
+ log .Debug ("Local IA Found:" , "ia" , ia )
84
+ localIAs = append (localIAs , ia )
85
+ }
86
+ }
87
+ }
88
+ return nil
89
+ })
90
+ sort .Strings (localIAs )
91
+ return localIAs
92
+ }
93
+
94
+ // StringInSlice can check a slice for a unique string
95
+ func StringInSlice (arr []string , i string ) bool {
96
+ for _ , v := range arr {
97
+ if v == i {
98
+ return true
99
+ }
100
+ }
101
+ return false
51
102
}
52
103
53
104
// Makes interfaces sortable, by preferred name
@@ -87,39 +138,31 @@ func (c byPrefInterface) Less(i, j int) bool {
87
138
}
88
139
89
140
// GenServerNodeDefaults creates server defaults for localhost testing
90
- func GenServerNodeDefaults (srcpath string ) {
141
+ func GenServerNodeDefaults (srcpath string , localIAs []string ) {
142
+ // reverse sort so that the default server will oppose the default client
143
+ sort .Sort (sort .Reverse (sort .StringSlice (localIAs )))
144
+
91
145
serFp := path .Join (srcpath , cfgFileSerUser )
92
- jsonBuf := []byte (`{ ` )
93
- json := []byte (`"bwtester": [{"name":"lo ` + serIaDef + `","isdas":"` +
94
- serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefBwt +
95
- `},{"name":"lo 2-ff00:0:222","isdas":"2-ff00:0:222", "addr":"127.0.0.22","port":30101}], ` )
96
- jsonBuf = append (jsonBuf , json ... )
97
- json = []byte (`"camerapp": [{"name":"localhost","isdas":"` +
98
- serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefImg + `}], ` )
99
- jsonBuf = append (jsonBuf , json ... )
100
- json = []byte (`"sensorapp": [{"name":"localhost","isdas":"` +
101
- serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}], ` )
102
- jsonBuf = append (jsonBuf , json ... )
103
- json = []byte (`"echo": [{"name":"localhost","isdas":"` +
104
- serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}], ` )
105
- jsonBuf = append (jsonBuf , json ... )
106
- json = []byte (`"traceroute": [{"name":"localhost","isdas":"` +
107
- serIaDef + `", "addr":"` + serDefAddr + `","port":` + serPortDefSen + `}]` )
108
- jsonBuf = append (jsonBuf , json ... )
109
-
110
- jsonBuf = append (jsonBuf , []byte (` }` )... )
146
+ jsonBuf := []byte (`{ "all": [` )
147
+ for i := 0 ; i < len (localIAs ); i ++ {
148
+ // use all localhost endpoints as possible servers for bwtester as least
149
+ ia := strings .Replace (localIAs [i ], "_" , ":" , - 1 )
150
+ json := []byte (`{"name":"lo ` + ia + `","isdas":"` + ia +
151
+ `", "addr":"` + serDefAddr + `","port":` + serPortDefBwt + `}` )
152
+ jsonBuf = append (jsonBuf , json ... )
153
+ if i < (len (localIAs ) - 1 ) {
154
+ jsonBuf = append (jsonBuf , []byte (`,` )... )
155
+ }
156
+ }
157
+ jsonBuf = append (jsonBuf , []byte (`] }` )... )
111
158
err := ioutil .WriteFile (serFp , jsonBuf , 0644 )
112
159
CheckError (err )
113
160
}
114
161
115
162
// GenClientNodeDefaults queries network interfaces and writes local client
116
163
// SCION addresses as json
117
- func GenClientNodeDefaults (srcpath string ) {
164
+ func GenClientNodeDefaults (srcpath , cisdas string ) {
118
165
cliFp := path .Join (srcpath , cfgFileCliDef )
119
- cisdas := GetLocalIa ()
120
- if len (cisdas ) == 0 {
121
- cisdas = cliIaDef
122
- }
123
166
cport := cliPortDef
124
167
125
168
// find interface addresses
0 commit comments