types.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package types
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "strconv"
  20. "strings"
  21. )
  22. const (
  23. MB = 1024 * 1024
  24. KB = 1024
  25. BandwidthLimitModeClient = "client"
  26. BandwidthLimitModeServer = "server"
  27. )
  28. type BandwidthQuantity struct {
  29. s string // MB or KB
  30. i int64 // bytes
  31. }
  32. func NewBandwidthQuantity(s string) (BandwidthQuantity, error) {
  33. q := BandwidthQuantity{}
  34. err := q.UnmarshalString(s)
  35. if err != nil {
  36. return q, err
  37. }
  38. return q, nil
  39. }
  40. func MustBandwidthQuantity(s string) BandwidthQuantity {
  41. q := BandwidthQuantity{}
  42. err := q.UnmarshalString(s)
  43. if err != nil {
  44. panic(err)
  45. }
  46. return q
  47. }
  48. func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
  49. if q == nil && u == nil {
  50. return true
  51. }
  52. if q != nil && u != nil {
  53. return q.i == u.i
  54. }
  55. return false
  56. }
  57. func (q *BandwidthQuantity) String() string {
  58. return q.s
  59. }
  60. func (q *BandwidthQuantity) UnmarshalString(s string) error {
  61. s = strings.TrimSpace(s)
  62. if s == "" {
  63. return nil
  64. }
  65. var (
  66. base int64
  67. f float64
  68. err error
  69. )
  70. switch {
  71. case strings.HasSuffix(s, "MB"):
  72. base = MB
  73. fstr := strings.TrimSuffix(s, "MB")
  74. f, err = strconv.ParseFloat(fstr, 64)
  75. if err != nil {
  76. return err
  77. }
  78. case strings.HasSuffix(s, "KB"):
  79. base = KB
  80. fstr := strings.TrimSuffix(s, "KB")
  81. f, err = strconv.ParseFloat(fstr, 64)
  82. if err != nil {
  83. return err
  84. }
  85. default:
  86. return errors.New("unit not support")
  87. }
  88. q.s = s
  89. q.i = int64(f * float64(base))
  90. return nil
  91. }
  92. func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
  93. if len(b) == 4 && string(b) == "null" {
  94. return nil
  95. }
  96. var str string
  97. err := json.Unmarshal(b, &str)
  98. if err != nil {
  99. return err
  100. }
  101. return q.UnmarshalString(str)
  102. }
  103. func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
  104. return []byte("\"" + q.s + "\""), nil
  105. }
  106. func (q *BandwidthQuantity) Bytes() int64 {
  107. return q.i
  108. }
  109. type PortsRange struct {
  110. Start int `json:"start,omitempty"`
  111. End int `json:"end,omitempty"`
  112. Single int `json:"single,omitempty"`
  113. }
  114. type PortsRangeSlice []PortsRange
  115. func (p PortsRangeSlice) String() string {
  116. if len(p) == 0 {
  117. return ""
  118. }
  119. strs := []string{}
  120. for _, v := range p {
  121. if v.Single > 0 {
  122. strs = append(strs, strconv.Itoa(v.Single))
  123. } else {
  124. strs = append(strs, strconv.Itoa(v.Start)+"-"+strconv.Itoa(v.End))
  125. }
  126. }
  127. return strings.Join(strs, ",")
  128. }
  129. // the format of str is like "1000-2000,3000,4000-5000"
  130. func NewPortsRangeSliceFromString(str string) ([]PortsRange, error) {
  131. str = strings.TrimSpace(str)
  132. out := []PortsRange{}
  133. numRanges := strings.Split(str, ",")
  134. for _, numRangeStr := range numRanges {
  135. // 1000-2000 or 2001
  136. numArray := strings.Split(numRangeStr, "-")
  137. // length: only 1 or 2 is correct
  138. rangeType := len(numArray)
  139. switch rangeType {
  140. case 1:
  141. // single number
  142. singleNum, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  143. if err != nil {
  144. return nil, fmt.Errorf("range number is invalid, %v", err)
  145. }
  146. out = append(out, PortsRange{Single: int(singleNum)})
  147. case 2:
  148. // range numbers
  149. min, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  150. if err != nil {
  151. return nil, fmt.Errorf("range number is invalid, %v", err)
  152. }
  153. max, err := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  154. if err != nil {
  155. return nil, fmt.Errorf("range number is invalid, %v", err)
  156. }
  157. if max < min {
  158. return nil, fmt.Errorf("range number is invalid")
  159. }
  160. out = append(out, PortsRange{Start: int(min), End: int(max)})
  161. default:
  162. return nil, fmt.Errorf("range number is invalid")
  163. }
  164. }
  165. return out, nil
  166. }