util.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package utils
  2. import (
  3. "encoding/base64"
  4. "io/ioutil"
  5. "net"
  6. "net/http"
  7. "os"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. )
  12. const (
  13. COMPRESS_NONE_ENCODE = iota
  14. COMPRESS_NONE_DECODE
  15. COMPRESS_SNAPY_ENCODE
  16. COMPRESS_SNAPY_DECODE
  17. VERIFY_EER = "vkey"
  18. WORK_MAIN = "main"
  19. WORK_CHAN = "chan"
  20. RES_SIGN = "sign"
  21. RES_MSG = "msg0"
  22. RES_CLOSE = "clse"
  23. NEW_CONN = "conn" //新连接标志
  24. CONN_SUCCESS = "sucs"
  25. CONN_TCP = "tcp"
  26. CONN_UDP = "udp"
  27. UnauthorizedBytes = `HTTP/1.1 401 Unauthorized
  28. Content-Type: text/plain; charset=utf-8
  29. WWW-Authenticate: Basic realm="easyProxy"
  30. 401 Unauthorized`
  31. IO_EOF = "PROXYEOF"
  32. ConnectionFailBytes = `HTTP/1.1 404 Not Found
  33. `
  34. )
  35. //判断压缩方式
  36. func GetCompressType(compress string) (int, int) {
  37. switch compress {
  38. case "":
  39. return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
  40. case "snappy":
  41. return COMPRESS_SNAPY_DECODE, COMPRESS_SNAPY_ENCODE
  42. default:
  43. Fatalln("数据压缩格式错误")
  44. }
  45. return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
  46. }
  47. //通过host获取对应的ip地址
  48. func GetHostByName(hostname string) string {
  49. if !DomainCheck(hostname) {
  50. return hostname
  51. }
  52. ips, _ := net.LookupIP(hostname)
  53. if ips != nil {
  54. for _, v := range ips {
  55. if v.To4() != nil {
  56. return v.String()
  57. }
  58. }
  59. }
  60. return ""
  61. }
  62. //检查是否是域名
  63. func DomainCheck(domain string) bool {
  64. var match bool
  65. IsLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)"
  66. NotLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}"
  67. match, _ = regexp.MatchString(IsLine, domain)
  68. if !match {
  69. match, _ = regexp.MatchString(NotLine, domain)
  70. }
  71. return match
  72. }
  73. //检查basic认证
  74. func CheckAuth(r *http.Request, user, passwd string) bool {
  75. s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
  76. if len(s) != 2 {
  77. return false
  78. }
  79. b, err := base64.StdEncoding.DecodeString(s[1])
  80. if err != nil {
  81. return false
  82. }
  83. pair := strings.SplitN(string(b), ":", 2)
  84. if len(pair) != 2 {
  85. return false
  86. }
  87. return pair[0] == user && pair[1] == passwd
  88. }
  89. //get bool by str
  90. func GetBoolByStr(s string) bool {
  91. switch s {
  92. case "1", "true":
  93. return true
  94. }
  95. return false
  96. }
  97. //get str by bool
  98. func GetStrByBool(b bool) string {
  99. if b {
  100. return "1"
  101. }
  102. return "0"
  103. }
  104. //int
  105. func GetIntNoErrByStr(str string) int {
  106. i, _ := strconv.Atoi(str)
  107. return i
  108. }
  109. //简单的一个校验值
  110. func Getverifyval(vkey string) string {
  111. return Md5(vkey)
  112. }
  113. func ChangeHostAndHeader(r *http.Request, host string, header string, addr string) {
  114. if host != "" {
  115. r.Host = host
  116. }
  117. if header != "" {
  118. h := strings.Split(header, "\n")
  119. for _, v := range h {
  120. hd := strings.Split(v, ":")
  121. if len(hd) == 2 {
  122. r.Header.Set(hd[0], hd[1])
  123. }
  124. }
  125. }
  126. addr = strings.Split(addr, ":")[0]
  127. r.Header.Set("X-Forwarded-For", addr)
  128. r.Header.Set("X-Real-IP", addr)
  129. }
  130. func ReadAllFromFile(filePath string) ([]byte, error) {
  131. f, err := os.Open(filePath)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return ioutil.ReadAll(f)
  136. }
  137. // FileExists reports whether the named file or directory exists.
  138. func FileExists(name string) bool {
  139. if _, err := os.Stat(name); err != nil {
  140. if os.IsNotExist(err) {
  141. return false
  142. }
  143. }
  144. return true
  145. }