types.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
  41. if q == nil && u == nil {
  42. return true
  43. }
  44. if q != nil && u != nil {
  45. return q.i == u.i
  46. }
  47. return false
  48. }
  49. func (q *BandwidthQuantity) String() string {
  50. return q.s
  51. }
  52. func (q *BandwidthQuantity) UnmarshalString(s string) error {
  53. s = strings.TrimSpace(s)
  54. if s == "" {
  55. return nil
  56. }
  57. var (
  58. base int64
  59. f float64
  60. err error
  61. )
  62. switch {
  63. case strings.HasSuffix(s, "MB"):
  64. base = MB
  65. fstr := strings.TrimSuffix(s, "MB")
  66. f, err = strconv.ParseFloat(fstr, 64)
  67. if err != nil {
  68. return err
  69. }
  70. case strings.HasSuffix(s, "KB"):
  71. base = KB
  72. fstr := strings.TrimSuffix(s, "KB")
  73. f, err = strconv.ParseFloat(fstr, 64)
  74. if err != nil {
  75. return err
  76. }
  77. default:
  78. return errors.New("unit not support")
  79. }
  80. q.s = s
  81. q.i = int64(f * float64(base))
  82. return nil
  83. }
  84. func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
  85. if len(b) == 4 && string(b) == "null" {
  86. return nil
  87. }
  88. var str string
  89. err := json.Unmarshal(b, &str)
  90. if err != nil {
  91. return err
  92. }
  93. return q.UnmarshalString(str)
  94. }
  95. func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
  96. return []byte("\"" + q.s + "\""), nil
  97. }
  98. func (q *BandwidthQuantity) Bytes() int64 {
  99. return q.i
  100. }
  101. type PortsRange struct {
  102. Start int `json:"start,omitempty"`
  103. End int `json:"end,omitempty"`
  104. Single int `json:"single,omitempty"`
  105. }
  106. type PortsRangeSlice []PortsRange
  107. func (p PortsRangeSlice) String() string {
  108. if len(p) == 0 {
  109. return ""
  110. }
  111. strs := []string{}
  112. for _, v := range p {
  113. if v.Single > 0 {
  114. strs = append(strs, strconv.Itoa(v.Single))
  115. } else {
  116. strs = append(strs, strconv.Itoa(v.Start)+"-"+strconv.Itoa(v.End))
  117. }
  118. }
  119. return strings.Join(strs, ",")
  120. }
  121. // the format of str is like "1000-2000,3000,4000-5000"
  122. func NewPortsRangeSliceFromString(str string) ([]PortsRange, error) {
  123. str = strings.TrimSpace(str)
  124. out := []PortsRange{}
  125. numRanges := strings.Split(str, ",")
  126. for _, numRangeStr := range numRanges {
  127. // 1000-2000 or 2001
  128. numArray := strings.Split(numRangeStr, "-")
  129. // length: only 1 or 2 is correct
  130. rangeType := len(numArray)
  131. switch rangeType {
  132. case 1:
  133. // single number
  134. singleNum, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  135. if err != nil {
  136. return nil, fmt.Errorf("range number is invalid, %v", err)
  137. }
  138. out = append(out, PortsRange{Single: int(singleNum)})
  139. case 2:
  140. // range numbers
  141. minNum, err := strconv.ParseInt(strings.TrimSpace(numArray[0]), 10, 64)
  142. if err != nil {
  143. return nil, fmt.Errorf("range number is invalid, %v", err)
  144. }
  145. maxNum, err := strconv.ParseInt(strings.TrimSpace(numArray[1]), 10, 64)
  146. if err != nil {
  147. return nil, fmt.Errorf("range number is invalid, %v", err)
  148. }
  149. if maxNum < minNum {
  150. return nil, fmt.Errorf("range number is invalid")
  151. }
  152. out = append(out, PortsRange{Start: int(minNum), End: int(maxNum)})
  153. default:
  154. return nil, fmt.Errorf("range number is invalid")
  155. }
  156. }
  157. return out, nil
  158. }