2121// configuration from one version to another
2222type convArray func ([]string ) []string
2323
24+ // convAddrs does an inplace conversion of the swarm, announce
25+ // and noAnnounce arrays of strings from one version to another
26+ type convAddrs func ([]string , []string , []string ) ([]string , []string , []string )
27+
2428// convertFile converts a config file from one version to another
25- func convertFile (path string , enableQuic bool , convBootstrap convArray , convSwarm convArray ) error {
29+ func convertFile (path string , convBootstrap convArray , convAddresses convAddrs ) error {
2630 in , err := os .Open (path )
2731 if err != nil {
2832 return err
@@ -35,7 +39,7 @@ func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwar
3539 return err
3640 }
3741
38- err = convert (in , out , enableQuic , convBootstrap , convSwarm )
42+ err = convert (in , out , convBootstrap , convAddresses )
3943
4044 in .Close ()
4145
@@ -51,7 +55,7 @@ func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwar
5155}
5256
5357// convert converts the config from one version to another
54- func convert (in io.Reader , out io.Writer , enableQuic bool , convBootstrap convArray , convSwarm convArray ) error {
58+ func convert (in io.Reader , out io.Writer , convBootstrap convArray , convAddresses convAddrs ) error {
5559 data , err := ioutil .ReadAll (in )
5660 if err != nil {
5761 return err
@@ -64,19 +68,8 @@ func convert(in io.Reader, out io.Writer, enableQuic bool, convBootstrap convArr
6468 // Convert bootstrap config
6569 convertBootstrap (confMap , convBootstrap )
6670
67- // Convert swarm config
68- if enableQuic {
69- // When enabling quic
70- // - Remove experimental option from config
71- confEnabled , ok := removeExperimentalQuic (confMap )
72- // - Only convert swarm config if experimental quic option was present
73- // and not enabled
74- if ok && ! confEnabled {
75- convertSwarm (confMap , convSwarm )
76- }
77- } else {
78- convertSwarm (confMap , convSwarm )
79- }
71+ // Convert addresses config
72+ convertAddresses (confMap , convAddresses )
8073
8174 fixed , err := json .MarshalIndent (confMap , "" , " " )
8275 if err != nil {
@@ -90,57 +83,45 @@ func convert(in io.Reader, out io.Writer, enableQuic bool, convBootstrap convArr
9083 return err
9184}
9285
93- // Remove Experimental.QUIC flag
94- func removeExperimentalQuic (confMap map [string ]interface {}) (bool , bool ) {
95- confExpi := confMap ["Experimental" ].(map [string ]interface {})
96- if confExpi == nil {
97- return false , false
98- }
99- enabledi , ok := confExpi ["QUIC" ]
100- if ! ok {
101- return false , false
102- }
103-
104- enabled , ok := enabledi .(bool )
105-
106- delete (confExpi , "QUIC" )
107-
108- return enabled , ok
109- }
110-
11186// Convert Bootstrap addresses to/from QUIC
11287func convertBootstrap (confMap map [string ]interface {}, conv convArray ) {
11388 bootstrapi , _ := confMap ["Bootstrap" ].([]interface {})
11489 if bootstrapi == nil {
11590 log .Log ("No Bootstrap field in config, skipping" )
11691 return
11792 }
118- bootstrap := make ([]string , len (bootstrapi ))
119- for i := range bootstrapi {
120- bootstrap [i ] = bootstrapi [i ].(string )
121- }
122- confMap ["Bootstrap" ] = conv (bootstrap )
93+ confMap ["Bootstrap" ] = conv (toStringArray (bootstrapi ))
12394}
12495
125- // Convert Addresses.Swarm to/from QUIC
126- func convertSwarm (confMap map [string ]interface {}, conv convArray ) {
96+ // Convert Addresses.Swarm, Addresses.Announce, Addresses.NoAnnounce to/from QUIC
97+ func convertAddresses (confMap map [string ]interface {}, conv convAddrs ) {
12798 addressesi , _ := confMap ["Addresses" ].(map [string ]interface {})
12899 if addressesi == nil {
129100 log .Log ("Addresses field missing or of the wrong type" )
130101 return
131102 }
132103
133- swarmi , _ := addressesi ["Swarm" ].([]interface {})
134- if swarmi == nil {
135- log .Log ("Addresses.Swarm field missing or of the wrong type" )
136- return
104+ swarm := toStringArray (addressesi ["Swarm" ])
105+ announce := toStringArray (addressesi ["Announce" ])
106+ noAnnounce := toStringArray (addressesi ["NoAnnounce" ])
107+
108+ s , a , na := conv (swarm , announce , noAnnounce )
109+ addressesi ["Swarm" ] = s
110+ addressesi ["Announce" ] = a
111+ addressesi ["NoAnnounce" ] = na
112+ }
113+
114+ func toStringArray (el interface {}) []string {
115+ listi , _ := el .([]interface {})
116+ if listi == nil {
117+ return []string {}
137118 }
138119
139- swarm := make ([]string , len (swarmi ))
140- for i := range swarmi {
141- swarm [i ] = swarmi [i ].(string )
120+ list := make ([]string , len (listi ))
121+ for i := range listi {
122+ list [i ] = listi [i ].(string )
142123 }
143- addressesi [ "Swarm" ] = conv ( swarm )
124+ return list
144125}
145126
146127// Add QUIC Bootstrap address
@@ -166,48 +147,34 @@ func ver9to10Bootstrap(bootstrap []string) []string {
166147 return res
167148}
168149
169- func ver10to9Bootstrap (bootstrap []string ) []string {
170- // No need to remove the QUIC bootstrapper, just leave things as they are
171- return bootstrap
150+ // For each TCP address, add a QUIC address
151+ func ver9to10Addresses (swarm , announce , noAnnounce []string ) ([]string , []string , []string ) {
152+ for _ , addr := range append (swarm , append (announce , noAnnounce ... )... ) {
153+ // If the old configuration already has a quic address in it, assume
154+ // the user has already set up their addresses for quic and leave
155+ // things as they are
156+ if strings .Contains (addr , "/udp/quic" ) {
157+ return swarm , announce , noAnnounce
158+ }
159+ }
160+
161+ return addQuic (swarm ), addQuic (announce ), addQuic (noAnnounce )
172162}
173163
174164var tcpRegexp = regexp .MustCompile (`/tcp/([0-9]+)` )
175165
176- // For each TCP listener, add a QUIC listener
177- func ver9to10Swarm (swarm []string ) []string {
178- res := make ([]string , 0 , len (swarm )* 2 )
179- for _ , addr := range swarm {
166+ func addQuic (addrs []string ) []string {
167+ res := make ([]string , 0 , len (addrs )* 2 )
168+ for _ , addr := range addrs {
180169 res = append (res , addr )
181-
182- // If the old configuration already has a quic address in it, assume
183- // the user has already set up their swarm addresses for quic and leave
184- // things as they are
185- if strings .Contains (addr , "/udp/quic" ) {
186- return swarm
187- }
188170 }
189171
190172 // For each tcp address, add a corresponding quic address
191- for _ , addr := range swarm {
173+ for _ , addr := range addrs {
192174 if tcpRegexp .MatchString (addr ) {
193175 res = append (res , tcpRegexp .ReplaceAllString (addr , `/udp/$1/quic` ))
194176 }
195177 }
196178
197179 return res
198180}
199-
200- var quicRegexp = regexp .MustCompile (`/udp/[0-9]+/quic` )
201-
202- // Remove QUIC listeners
203- func ver10to9Swarm (swarm []string ) []string {
204- // Remove quic addresses
205- res := make ([]string , 0 , len (swarm ))
206- for _ , addr := range swarm {
207- if ! quicRegexp .MatchString (addr ) {
208- res = append (res , addr )
209- }
210- }
211-
212- return res
213- }
0 commit comments