|
| 1 | +package mg9 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ipfs/fs-repo-migrations/ipfs-6-to-7/gx/ipfs/QmdYwCmx8pZRkzdcd8MhmLJqYVoVTC1aGsy5Q4reMGLNLg/atomicfile" |
| 12 | + log "github.com/ipfs/fs-repo-migrations/stump" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + ip4BootstrapAddr = "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" |
| 17 | + quicBootstrapAddr = "/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ" |
| 18 | +) |
| 19 | + |
| 20 | +// convArray does an inplace conversion of an array of strings |
| 21 | +// configuration from one version to another |
| 22 | +type convArray func([]string) []string |
| 23 | + |
| 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 | + |
| 28 | +// convertFile converts a config file from one version to another |
| 29 | +func convertFile(path string, convBootstrap convArray, convAddresses convAddrs) error { |
| 30 | + in, err := os.Open(path) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + // Create a temp file to write the output to on success |
| 36 | + out, err := atomicfile.New(path, 0600) |
| 37 | + if err != nil { |
| 38 | + in.Close() |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + err = convert(in, out, convBootstrap, convAddresses) |
| 43 | + |
| 44 | + in.Close() |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + // There was an error so abort writing the output and clean up temp file |
| 48 | + out.Abort() |
| 49 | + } else { |
| 50 | + // Write the output and clean up temp file |
| 51 | + out.Close() |
| 52 | + } |
| 53 | + |
| 54 | + return err |
| 55 | +} |
| 56 | + |
| 57 | +// convert converts the config from one version to another |
| 58 | +func convert(in io.Reader, out io.Writer, convBootstrap convArray, convAddresses convAddrs) error { |
| 59 | + data, err := ioutil.ReadAll(in) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + confMap := make(map[string]interface{}) |
| 64 | + if err = json.Unmarshal(data, &confMap); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + // Convert bootstrap config |
| 69 | + convertBootstrap(confMap, convBootstrap) |
| 70 | + |
| 71 | + // Convert addresses config |
| 72 | + convertAddresses(confMap, convAddresses) |
| 73 | + |
| 74 | + fixed, err := json.MarshalIndent(confMap, "", " ") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + if _, err := out.Write(fixed); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + _, err = out.Write([]byte("\n")) |
| 83 | + return err |
| 84 | +} |
| 85 | + |
| 86 | +// Convert Bootstrap addresses to/from QUIC |
| 87 | +func convertBootstrap(confMap map[string]interface{}, conv convArray) { |
| 88 | + bootstrapi, _ := confMap["Bootstrap"].([]interface{}) |
| 89 | + if bootstrapi == nil { |
| 90 | + log.Log("No Bootstrap field in config, skipping") |
| 91 | + return |
| 92 | + } |
| 93 | + confMap["Bootstrap"] = conv(toStringArray(bootstrapi)) |
| 94 | +} |
| 95 | + |
| 96 | +// Convert Addresses.Swarm, Addresses.Announce, Addresses.NoAnnounce to/from QUIC |
| 97 | +func convertAddresses(confMap map[string]interface{}, conv convAddrs) { |
| 98 | + addressesi, _ := confMap["Addresses"].(map[string]interface{}) |
| 99 | + if addressesi == nil { |
| 100 | + log.Log("Addresses field missing or of the wrong type") |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 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{} |
| 118 | + } |
| 119 | + |
| 120 | + list := make([]string, len(listi)) |
| 121 | + for i := range listi { |
| 122 | + list[i] = listi[i].(string) |
| 123 | + } |
| 124 | + return list |
| 125 | +} |
| 126 | + |
| 127 | +// Add QUIC Bootstrap address |
| 128 | +func ver9to10Bootstrap(bootstrap []string) []string { |
| 129 | + hasOld := false |
| 130 | + hasNew := false |
| 131 | + res := make([]string, 0, len(bootstrap)+1) |
| 132 | + for _, addr := range bootstrap { |
| 133 | + res = append(res, addr) |
| 134 | + if addr == ip4BootstrapAddr { |
| 135 | + hasOld = true |
| 136 | + } else if addr == quicBootstrapAddr { |
| 137 | + hasNew = true |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // If the config has the old IP v4 bootstrapper, add the new QUIC |
| 142 | + // bootstrapper |
| 143 | + if hasOld && !hasNew { |
| 144 | + res = append(res, quicBootstrapAddr) |
| 145 | + } |
| 146 | + |
| 147 | + return res |
| 148 | +} |
| 149 | + |
| 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) |
| 162 | +} |
| 163 | + |
| 164 | +var tcpRegexp = regexp.MustCompile(`/tcp/([0-9]+)`) |
| 165 | + |
| 166 | +func addQuic(addrs []string) []string { |
| 167 | + res := make([]string, 0, len(addrs)*2) |
| 168 | + for _, addr := range addrs { |
| 169 | + res = append(res, addr) |
| 170 | + } |
| 171 | + |
| 172 | + // For each tcp address, add a corresponding quic address |
| 173 | + for _, addr := range addrs { |
| 174 | + if tcpRegexp.MatchString(addr) { |
| 175 | + res = append(res, tcpRegexp.ReplaceAllString(addr, `/udp/$1/quic`)) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return res |
| 180 | +} |
0 commit comments