nps/utils/util.go

246 lines
5.1 KiB
Go
Raw Normal View History

2019-01-09 12:33:00 +00:00
package utils
import (
"encoding/base64"
"io"
"io/ioutil"
2019-01-09 12:33:00 +00:00
"log"
"net"
"net/http"
"os"
2019-01-09 12:33:00 +00:00
"regexp"
"strconv"
"strings"
"sync"
"time"
)
const (
COMPRESS_NONE_ENCODE = iota
COMPRESS_NONE_DECODE
COMPRESS_SNAPY_ENCODE
COMPRESS_SNAPY_DECODE
VERIFY_EER = "vkey"
WORK_MAIN = "main"
WORK_CHAN = "chan"
RES_SIGN = "sign"
RES_MSG = "msg0"
2019-01-28 06:45:55 +00:00
RES_CLOSE = "clse"
CONN_SUCCESS = "sucs"
CONN_ERROR = "fail"
TEST_FLAG = "tst"
CONN_TCP = "tcp"
CONN_UDP = "udp"
UnauthorizedBytes = `HTTP/1.1 401 Unauthorized
2019-01-09 12:33:00 +00:00
Content-Type: text/plain; charset=utf-8
WWW-Authenticate: Basic realm="easyProxy"
401 Unauthorized`
IO_EOF = "PROXYEOF"
ConnectionFailBytes = `HTTP/1.1 404 Not Found
`
2019-01-09 12:33:00 +00:00
)
//copy
2019-01-28 06:45:55 +00:00
func Relay(in, out net.Conn, compressType int, crypt, mux bool, rate *Rate) (n int64, err error) {
2019-01-09 12:33:00 +00:00
switch compressType {
case COMPRESS_SNAPY_ENCODE:
2019-01-28 06:45:55 +00:00
n, err = copyBuffer(NewSnappyConn(in, crypt, rate), out)
2019-01-10 15:25:56 +00:00
out.Close()
2019-01-28 06:45:55 +00:00
NewSnappyConn(in, crypt, rate).Write([]byte(IO_EOF))
2019-01-09 12:33:00 +00:00
case COMPRESS_SNAPY_DECODE:
2019-01-28 06:45:55 +00:00
n, err = copyBuffer(in, NewSnappyConn(out, crypt, rate))
2019-01-10 15:25:56 +00:00
in.Close()
if !mux {
out.Close()
2019-01-09 12:33:00 +00:00
}
case COMPRESS_NONE_ENCODE:
2019-01-28 06:45:55 +00:00
n, err = copyBuffer(NewCryptConn(in, crypt, rate), out)
2019-01-10 15:25:56 +00:00
out.Close()
2019-01-28 06:45:55 +00:00
NewCryptConn(in, crypt, rate).Write([]byte(IO_EOF))
2019-01-09 12:33:00 +00:00
case COMPRESS_NONE_DECODE:
2019-01-28 06:45:55 +00:00
n, err = copyBuffer(in, NewCryptConn(out, crypt, rate))
2019-01-09 12:33:00 +00:00
in.Close()
2019-01-10 15:25:56 +00:00
if !mux {
out.Close()
}
2019-01-09 12:33:00 +00:00
}
return
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:
log.Fatalln("数据压缩格式错误")
}
return COMPRESS_NONE_DECODE, COMPRESS_NONE_ENCODE
}
//通过host获取对应的ip地址
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 ""
}
//检查是否是域名
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
}
//检查basic认证
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-01-15 12:59:50 +00:00
// io.copy的优化版读取buffer长度原为32*1024与snappy不同导致读取出的内容存在差异不利于解密
2019-01-13 14:02:58 +00:00
//内存优化 用到pool快速回收
2019-01-09 12:33:00 +00:00
func copyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
for {
2019-01-15 12:59:50 +00:00
//放在里面是为了加快回收和重利用
2019-01-13 14:02:58 +00:00
buf := bufPoolCopy.Get().([]byte)
2019-01-09 12:33:00 +00:00
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
2019-01-13 14:02:58 +00:00
bufPoolCopy.Put(buf)
2019-01-09 12:33:00 +00:00
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
} else {
2019-01-13 14:02:58 +00:00
bufPoolCopy.Put(buf)
2019-01-09 12:33:00 +00:00
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}
//连接重置 清空缓存区
func FlushConn(c net.Conn) {
c.SetReadDeadline(time.Now().Add(time.Second * 3))
buf := bufPool.Get().([]byte)
2019-01-13 12:49:45 +00:00
defer bufPool.Put(buf)
2019-01-09 12:33:00 +00:00
for {
if _, err := c.Read(buf); err != nil {
break
}
}
c.SetReadDeadline(time.Time{})
}
//简单的一个校验值
func Getverifyval(vkey string) string {
return Md5(vkey)
}
2019-01-12 16:09:12 +00:00
//wait replay group
//conn1 网桥 conn2
2019-01-28 06:45:55 +00:00
func ReplayWaitGroup(conn1 net.Conn, conn2 net.Conn, compressEncode, compressDecode int, crypt, mux bool, rate *Rate) (out int64, in int64) {
2019-01-12 16:09:12 +00:00
var wg sync.WaitGroup
wg.Add(1)
go func() {
2019-01-28 06:45:55 +00:00
in, _ = Relay(conn1, conn2, compressEncode, crypt, mux, rate)
2019-01-12 16:09:12 +00:00
wg.Done()
}()
2019-01-28 06:45:55 +00:00
out, _ = Relay(conn2, conn1, compressDecode, crypt, mux, rate)
2019-01-12 16:09:12 +00:00
wg.Wait()
return
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)
}
func ReadAllFromFile(filePth string) ([]byte, error) {
f, err := os.Open(filePth)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}