nps/lib/common/util.go

177 lines
3.7 KiB
Go
Raw Normal View History

2019-02-09 09:07:47 +00:00
package common
2019-01-09 12:33:00 +00:00
import (
2019-02-09 09:07:47 +00:00
"bytes"
2019-01-09 12:33:00 +00:00
"encoding/base64"
2019-02-09 09:07:47 +00:00
"encoding/binary"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/lg"
"io/ioutil"
2019-01-09 12:33:00 +00:00
"net"
"net/http"
"os"
2019-01-09 12:33:00 +00:00
"regexp"
"strconv"
"strings"
)
2019-02-09 09:07:47 +00:00
//Judging Compression Mode
2019-01-09 12:33:00 +00:00
func GetCompressType(compress string) (int, int) {
switch compress {
case "":
return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
case "snappy":
return COMPRESS_SNAPY_DECODE, COMPRESS_SNAPY_ENCODE
default:
2019-02-09 09:07:47 +00:00
lg.Fatalln("数据压缩格式错误")
2019-01-09 12:33:00 +00:00
}
return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
}
2019-02-09 09:07:47 +00:00
//Get the corresponding IP address through domain name
2019-01-15 12:59:50 +00:00
func GetHostByName(hostname string) string {
2019-01-09 12:33:00 +00:00
if !DomainCheck(hostname) {
return hostname
}
ips, _ := net.LookupIP(hostname)
if ips != nil {
for _, v := range ips {
if v.To4() != nil {
return v.String()
}
}
}
return ""
}
2019-02-09 09:07:47 +00:00
//Check the legality of domain
2019-01-09 12:33:00 +00:00
func DomainCheck(domain string) bool {
var match bool
IsLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)"
NotLine := "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}"
match, _ = regexp.MatchString(IsLine, domain)
if !match {
match, _ = regexp.MatchString(NotLine, domain)
}
return match
}
2019-02-09 09:07:47 +00:00
//Check if the Request request is validated
2019-01-09 12:33:00 +00:00
func CheckAuth(r *http.Request, user, passwd string) bool {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 {
return false
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return false
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return false
}
return pair[0] == user && pair[1] == passwd
}
//get bool by str
func GetBoolByStr(s string) bool {
switch s {
case "1", "true":
return true
}
return false
}
//get str by bool
func GetStrByBool(b bool) string {
if b {
return "1"
}
return "0"
}
//int
2019-01-15 12:59:50 +00:00
func GetIntNoErrByStr(str string) int {
2019-01-09 12:33:00 +00:00
i, _ := strconv.Atoi(str)
return i
}
2019-02-09 09:07:47 +00:00
//Get verify value
2019-01-09 12:33:00 +00:00
func Getverifyval(vkey string) string {
2019-02-09 09:07:47 +00:00
return crypt.Md5(vkey)
2019-01-09 12:33:00 +00:00
}
2019-01-12 16:09:12 +00:00
2019-02-09 09:07:47 +00:00
//Change headers and host of request
2019-01-12 16:09:12 +00:00
func ChangeHostAndHeader(r *http.Request, host string, header string, addr string) {
if host != "" {
r.Host = host
}
if header != "" {
h := strings.Split(header, "\n")
for _, v := range h {
hd := strings.Split(v, ":")
if len(hd) == 2 {
r.Header.Set(hd[0], hd[1])
}
}
}
addr = strings.Split(addr, ":")[0]
r.Header.Set("X-Forwarded-For", addr)
r.Header.Set("X-Real-IP", addr)
}
2019-02-09 09:07:47 +00:00
//Read file content by file path
func ReadAllFromFile(filePath string) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
2019-02-02 16:54:43 +00:00
// FileExists reports whether the named file or directory exists.
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
2019-02-05 16:35:23 +00:00
}
2019-02-09 09:07:47 +00:00
//Judge whether the TCP port can open normally
func TestTcpPort(port int) bool {
l, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), port, ""})
defer l.Close()
if err != nil {
return false
2019-02-05 16:35:23 +00:00
}
2019-02-09 09:07:47 +00:00
return true
2019-02-05 16:35:23 +00:00
}
2019-02-09 09:07:47 +00:00
//Judge whether the UDP port can open normally
func TestUdpPort(port int) bool {
l, err := net.ListenUDP("udp", &net.UDPAddr{net.ParseIP("0.0.0.0"), port, ""})
2019-02-05 16:35:23 +00:00
defer l.Close()
if err != nil {
return false
}
return true
}
2019-02-09 09:07:47 +00:00
//Write length and individual byte data
//Length prevents sticking
//# Characters are used to separate data
func BinaryWrite(raw *bytes.Buffer, v ...string) {
buffer := new(bytes.Buffer)
var l int32
for _, v := range v {
l += int32(len([]byte(v))) + int32(len([]byte("#")))
binary.Write(buffer, binary.LittleEndian, []byte(v))
binary.Write(buffer, binary.LittleEndian, []byte("#"))
}
binary.Write(raw, binary.LittleEndian, buffer.Bytes())
}