function.go 3.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5
package utils

import (
	"crypto/md5"
	"encoding/hex"
Y
Your Name 已提交
6
	"fmt"
E
eoLinker API Management 已提交
7
	"math/rand"
Y
Your Name 已提交
8
	"net"
E
eoLinker API Management 已提交
9 10 11 12 13 14 15 16
	"os"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
	"time"
)

Y
Your Name 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//ConvertIntArrayToString 转换整型数组
func ConvertIntArrayToString(ids []int) string {
	idLen := len(ids)
	if idLen < 1 {
		return ""
	}
	idStr := ""
	for i, id := range ids {
		idStr += strconv.Itoa(id)
		if i < idLen-1 {
			idStr += ","
		}
	}
	return idStr
}

//ConvertArray 将[]string转为[]int
func ConvertArray(arr []string) (bool, []int) {
	result := make([]int, 0)
	for _, i := range arr {
		res, err := strconv.Atoi(i)
		if err != nil {
			return false, result
		}
		result = append(result, res)
	}
	return true, result
}

Y
Your Name 已提交
46
//ValidateRemoteAddr 判断ip端口是否合法
E
eoLinker API Management 已提交
47 48 49 50 51 52 53 54
func ValidateRemoteAddr(ip string) bool {
	match, err := regexp.MatchString(`^(?:(?:1[0-9][0-9]\.)|(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5])|(?:[1-9][0-9])|(?:[0-9]))\:(([0-9])|([1-9][0-9]{1,3})|([1-6][0-9]{0,4}))$`, ip)
	if err != nil {
		return false
	}
	return match
}

Y
Your Name 已提交
55 56 57 58 59 60 61 62 63 64 65
//ValidateURL 判断ip端口是否合法
func ValidateURL(url string) bool {
	match, err := regexp.MatchString(`^/(([a-zA-Z][0-9a-zA-Z+\-\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\.\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\.\-_!~*'()%]+)?$`, url)
	if err != nil {
		return false
	}
	return match
}

//Intercept 获取IP
func Intercept(str, substr string) (string, string) {
E
eoLinker API Management 已提交
66 67
	result := strings.Index(str, substr)
	var rs string
Y
Your Name 已提交
68
	var bs string
E
eoLinker API Management 已提交
69 70
	if result != -1 {
		rs = str[:result]
Y
Your Name 已提交
71
		bs = str[result+1:]
E
eoLinker API Management 已提交
72 73
	} else {
		rs = str
Y
Your Name 已提交
74
		bs = str
E
eoLinker API Management 已提交
75
	}
Y
Your Name 已提交
76
	return rs, bs
E
eoLinker API Management 已提交
77 78
}

Y
Your Name 已提交
79
//Md5 md5加密
E
eoLinker API Management 已提交
80 81 82 83 84 85
func Md5(encodeString string) string {
	h := md5.New()
	h.Write([]byte(encodeString))
	return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
}

Y
Your Name 已提交
86
//GetRandomString 生成随机字符串
E
eoLinker API Management 已提交
87 88 89 90 91 92 93 94 95 96 97
func GetRandomString(num int) string {
	str := "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < num; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

Y
Your Name 已提交
98
//CheckFileIsExist 判断文件是否存在  存在返回 true 不存在返回false
E
eoLinker API Management 已提交
99 100 101 102
func CheckFileIsExist(filename string) bool {
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		return false
	}
Y
Your Name 已提交
103
	return true
E
eoLinker API Management 已提交
104 105
}

Y
Your Name 已提交
106
//Stop 关闭网关服务,重启读取配置文件
E
eoLinker API Management 已提交
107 108 109 110 111 112
func Stop() bool {
	id := os.Getpid()
	cmd := exec.Command("/bin/bash", "-c", "kill -HUP "+strconv.Itoa(id))
	if _, err := cmd.Output(); err != nil {
		return false
	}
Y
Your Name 已提交
113
	return true
E
eoLinker API Management 已提交
114
}
Y
Your Name 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

//GetMac 获取MAC地址
func GetMac() (bool, string) {
	interfaces, err := net.Interfaces()
	if err != nil {
		return false, "Poor soul, here is what you got: " + err.Error()
	}
	for _, inter := range interfaces {
		mac := inter.HardwareAddr //获取本机MAC地址
		m := fmt.Sprintf("%s", mac)
		match, err := regexp.MatchString(`[0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f]`, m)
		if err != nil {
			return false, ""
		}
		if match {
			return true, string(m)
		}
	}
	return false, ""
}