|
1 | 1 | package vpn
|
2 | 2 |
|
3 |
| -import "github.com/coder/coder/v2/apiversion" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
4 | 7 |
|
5 |
| -const ( |
6 |
| - CurrentMajor = 1 |
7 |
| - CurrentMinor = 0 |
| 8 | + "golang.org/x/xerrors" |
8 | 9 | )
|
9 | 10 |
|
10 |
| -var CurrentVersion = apiversion.New(CurrentMajor, CurrentMinor) |
| 11 | +// CurrentSupportedVersions is the list of versions supported by this |
| 12 | +// implementation of the VPN RPC protocol. |
| 13 | +var CurrentSupportedVersions = RPCVersionList{ |
| 14 | + Versions: []RPCVersion{ |
| 15 | + {Major: 1, Minor: 0}, |
| 16 | + }, |
| 17 | +} |
| 18 | + |
| 19 | +// RPCVersion represents a single version of the RPC protocol. Any given version |
| 20 | +// is expected to be backwards compatible with all previous minor versions on |
| 21 | +// the same major version. |
| 22 | +// |
| 23 | +// e.g. RPCVersion{2, 3} is backwards compatible with RPCVersion{2, 2} but is |
| 24 | +// not backwards compatible with RPCVersion{1, 2}. |
| 25 | +type RPCVersion struct { |
| 26 | + Major uint64 `json:"major"` |
| 27 | + Minor uint64 `json:"minor"` |
| 28 | +} |
| 29 | + |
| 30 | +// ParseRPCVersion parses a version string in the format "major.minor" into a |
| 31 | +// RPCVersion. |
| 32 | +func ParseRPCVersion(str string) (RPCVersion, error) { |
| 33 | + split := strings.Split(str, ".") |
| 34 | + if len(split) != 2 { |
| 35 | + return RPCVersion{}, xerrors.Errorf("invalid version string: %s", str) |
| 36 | + } |
| 37 | + major, err := strconv.ParseUint(split[0], 10, 64) |
| 38 | + if err != nil { |
| 39 | + return RPCVersion{}, xerrors.Errorf("invalid version string: %s", str) |
| 40 | + } |
| 41 | + if major == 0 { |
| 42 | + return RPCVersion{}, xerrors.Errorf("invalid version string: %s", str) |
| 43 | + } |
| 44 | + minor, err := strconv.ParseUint(split[1], 10, 64) |
| 45 | + if err != nil { |
| 46 | + return RPCVersion{}, xerrors.Errorf("invalid version string: %s", str) |
| 47 | + } |
| 48 | + return RPCVersion{Major: major, Minor: minor}, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (v RPCVersion) String() string { |
| 52 | + return fmt.Sprintf("%d.%d", v.Major, v.Minor) |
| 53 | +} |
| 54 | + |
| 55 | +// IsCompatibleWith returns the lowest version that is compatible with both |
| 56 | +// versions. If the versions are not compatible, the second return value will be |
| 57 | +// false. |
| 58 | +func (v RPCVersion) IsCompatibleWith(other RPCVersion) (RPCVersion, bool) { |
| 59 | + if v.Major != other.Major { |
| 60 | + return RPCVersion{}, false |
| 61 | + } |
| 62 | + // The lowest minor version from the two versions should be returned. |
| 63 | + if v.Minor < other.Minor { |
| 64 | + return v, true |
| 65 | + } |
| 66 | + return other, true |
| 67 | +} |
| 68 | + |
| 69 | +// RPCVersionList represents a list of RPC versions supported by a RPC peer. An |
| 70 | +type RPCVersionList struct { |
| 71 | + Versions []RPCVersion `json:"versions"` |
| 72 | +} |
| 73 | + |
| 74 | +// ParseRPCVersionList parses a version string in the format |
| 75 | +// "major.minor,major.minor" into a RPCVersionList. |
| 76 | +func ParseRPCVersionList(str string) (RPCVersionList, error) { |
| 77 | + split := strings.Split(str, ",") |
| 78 | + versions := make([]RPCVersion, len(split)) |
| 79 | + for i, v := range split { |
| 80 | + version, err := ParseRPCVersion(v) |
| 81 | + if err != nil { |
| 82 | + return RPCVersionList{}, xerrors.Errorf("invalid version list: %s", str) |
| 83 | + } |
| 84 | + versions[i] = version |
| 85 | + } |
| 86 | + vl := RPCVersionList{Versions: versions} |
| 87 | + err := vl.Validate() |
| 88 | + if err != nil { |
| 89 | + return RPCVersionList{}, xerrors.Errorf("invalid parsed version list %q: %w", str, err) |
| 90 | + } |
| 91 | + return vl, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (vl RPCVersionList) String() string { |
| 95 | + versionStrings := make([]string, len(vl.Versions)) |
| 96 | + for i, v := range vl.Versions { |
| 97 | + versionStrings[i] = v.String() |
| 98 | + } |
| 99 | + return strings.Join(versionStrings, ",") |
| 100 | +} |
| 101 | + |
| 102 | +// Validate returns an error if the version list is not sorted or contains |
| 103 | +// duplicate major versions. |
| 104 | +func (vl RPCVersionList) Validate() error { |
| 105 | + if len(vl.Versions) == 0 { |
| 106 | + return xerrors.New("no versions") |
| 107 | + } |
| 108 | + for i := 0; i < len(vl.Versions); i++ { |
| 109 | + if vl.Versions[i].Major == 0 { |
| 110 | + return xerrors.Errorf("invalid version: %s", vl.Versions[i].String()) |
| 111 | + } |
| 112 | + if i > 0 && vl.Versions[i-1].Major == vl.Versions[i].Major { |
| 113 | + return xerrors.Errorf("duplicate major version: %d", vl.Versions[i].Major) |
| 114 | + } |
| 115 | + if i > 0 && vl.Versions[i-1].Major > vl.Versions[i].Major { |
| 116 | + return xerrors.Errorf("versions are not sorted") |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// IsCompatibleWith returns the lowest version that is compatible with both |
| 123 | +// version lists. If the versions are not compatible, the second return value |
| 124 | +// will be false. |
| 125 | +func (vl RPCVersionList) IsCompatibleWith(other RPCVersionList) (RPCVersion, bool) { |
| 126 | + bestVersion := RPCVersion{} |
| 127 | + for _, v1 := range vl.Versions { |
| 128 | + for _, v2 := range other.Versions { |
| 129 | + if v1.Major == v2.Major && v1.Major > bestVersion.Major { |
| 130 | + v, ok := v1.IsCompatibleWith(v2) |
| 131 | + if ok { |
| 132 | + bestVersion = v |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + if bestVersion.Major == 0 { |
| 138 | + return bestVersion, false |
| 139 | + } |
| 140 | + return bestVersion, true |
| 141 | +} |
0 commit comments