mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-04 05:40:43 +00:00
修改
This commit is contained in:
parent
03aa58cd77
commit
55b0c4b98b
@ -12,7 +12,6 @@ var (
|
|||||||
verifyKey = flag.String("vkey", "", "验证密钥")
|
verifyKey = flag.String("vkey", "", "验证密钥")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
//go func() {
|
//go func() {
|
@ -18,25 +18,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const cryptKey = "1234567812345678"
|
const cryptKey = "1234567812345678"
|
||||||
|
const poolSize = 64 * 1024
|
||||||
|
|
||||||
type CryptConn struct {
|
type CryptConn struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
crypt bool
|
crypt bool
|
||||||
|
rb []byte
|
||||||
|
rn int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCryptConn(conn net.Conn, crypt bool) *CryptConn {
|
func NewCryptConn(conn net.Conn, crypt bool) *CryptConn {
|
||||||
c := new(CryptConn)
|
c := new(CryptConn)
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
c.crypt = crypt
|
c.crypt = crypt
|
||||||
|
c.rb = make([]byte, poolSize)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
//加密写
|
//加密写
|
||||||
func (s *CryptConn) Write(b []byte) (n int, err error) {
|
func (s *CryptConn) Write(b []byte) (n int, err error) {
|
||||||
n = len(b)
|
n = len(b)
|
||||||
if n == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.crypt {
|
if s.crypt {
|
||||||
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
||||||
return
|
return
|
||||||
@ -51,6 +52,21 @@ func (s *CryptConn) Write(b []byte) (n int, err error) {
|
|||||||
|
|
||||||
//解密读
|
//解密读
|
||||||
func (s *CryptConn) Read(b []byte) (n int, err error) {
|
func (s *CryptConn) Read(b []byte) (n int, err error) {
|
||||||
|
read:
|
||||||
|
if len(s.rb) > 0 {
|
||||||
|
if len(b) >= s.rn {
|
||||||
|
n = s.rn
|
||||||
|
copy(b, s.rb[:s.rn])
|
||||||
|
s.rn = 0
|
||||||
|
s.rb = s.rb[:0]
|
||||||
|
} else {
|
||||||
|
n = len(b)
|
||||||
|
copy(b, s.rb[:len(b)])
|
||||||
|
s.rn = n - len(b)
|
||||||
|
s.rb = s.rb[len(b):]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err == nil && n == len(IO_EOF) && string(b[:n]) == IO_EOF {
|
if err == nil && n == len(IO_EOF) && string(b[:n]) == IO_EOF {
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
@ -58,7 +74,7 @@ func (s *CryptConn) Read(b []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
var lens int
|
var lens int
|
||||||
var buf, bs []byte
|
var buf []byte
|
||||||
c := NewConn(s.conn)
|
c := NewConn(s.conn)
|
||||||
if lens, err = c.GetLen(); err != nil {
|
if lens, err = c.GetLen(); err != nil {
|
||||||
return
|
return
|
||||||
@ -67,15 +83,14 @@ func (s *CryptConn) Read(b []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.crypt {
|
if s.crypt {
|
||||||
if bs, err = AesDecrypt(buf, []byte(cryptKey)); err != nil {
|
if s.rb, err = AesDecrypt(buf, []byte(cryptKey)); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bs = buf
|
s.rb = buf
|
||||||
}
|
}
|
||||||
n = len(bs)
|
s.rn = len(s.rb)
|
||||||
copy(b, bs)
|
goto read
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SnappyConn struct {
|
type SnappyConn struct {
|
||||||
@ -95,9 +110,6 @@ func NewSnappyConn(conn net.Conn, crypt bool) *SnappyConn {
|
|||||||
//snappy压缩写 包含加密
|
//snappy压缩写 包含加密
|
||||||
func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
func (s *SnappyConn) Write(b []byte) (n int, err error) {
|
||||||
n = len(b)
|
n = len(b)
|
||||||
if n == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if s.crypt {
|
if s.crypt {
|
||||||
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
if b, err = AesEncrypt(b, []byte(cryptKey)); err != nil {
|
||||||
log.Println("encode crypt error:", err)
|
log.Println("encode crypt error:", err)
|
||||||
@ -119,18 +131,21 @@ func (s *SnappyConn) Read(b []byte) (n int, err error) {
|
|||||||
n = 0
|
n = 0
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if n, err = s.r.Read(b); err != nil || err == io.EOF {
|
buf := bufPool.Get().([]byte)
|
||||||
|
if n, err = s.r.Read(buf); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.crypt {
|
|
||||||
var bs []byte
|
var bs []byte
|
||||||
if bs, err = AesDecrypt(b[:n], []byte(cryptKey)); err != nil {
|
if s.crypt {
|
||||||
|
if bs, err = AesDecrypt(buf[:n], []byte(cryptKey)); err != nil {
|
||||||
log.Println("decode crypt error:", err)
|
log.Println("decode crypt error:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
bs = buf[:n]
|
||||||
|
}
|
||||||
n = len(bs)
|
n = len(bs)
|
||||||
copy(b, bs)
|
copy(b, bs)
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,8 +162,8 @@ func NewConn(conn net.Conn) *Conn {
|
|||||||
|
|
||||||
//读取指定长度内容
|
//读取指定长度内容
|
||||||
func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
func (s *Conn) ReadLen(cLen int) ([]byte, error) {
|
||||||
if cLen > 65536 {
|
if cLen > poolSize {
|
||||||
return nil, errors.New("长度错误")
|
return nil, errors.New("长度错误" + strconv.Itoa(cLen))
|
||||||
}
|
}
|
||||||
buf := bufPool.Get().([]byte)[:cLen]
|
buf := bufPool.Get().([]byte)[:cLen]
|
||||||
if n, err := io.ReadFull(s, buf); err != nil || n != cLen {
|
if n, err := io.ReadFull(s, buf); err != nil || n != cLen {
|
||||||
|
@ -40,21 +40,21 @@ WWW-Authenticate: Basic realm="easyProxy"
|
|||||||
func Relay(in, out net.Conn, compressType int, crypt, mux bool) {
|
func Relay(in, out net.Conn, compressType int, crypt, mux bool) {
|
||||||
switch compressType {
|
switch compressType {
|
||||||
case COMPRESS_SNAPY_ENCODE:
|
case COMPRESS_SNAPY_ENCODE:
|
||||||
copyBuffer(NewSnappyConn(in, crypt), out)
|
io.Copy(NewSnappyConn(in, crypt), out)
|
||||||
out.Close()
|
out.Close()
|
||||||
NewSnappyConn(in, crypt).Write([]byte(IO_EOF))
|
NewSnappyConn(in, crypt).Write([]byte(IO_EOF))
|
||||||
case COMPRESS_SNAPY_DECODE:
|
case COMPRESS_SNAPY_DECODE:
|
||||||
copyBuffer(in, NewSnappyConn(out, crypt))
|
io.Copy(in, NewSnappyConn(out, crypt))
|
||||||
in.Close()
|
in.Close()
|
||||||
if !mux {
|
if !mux {
|
||||||
out.Close()
|
out.Close()
|
||||||
}
|
}
|
||||||
case COMPRESS_NONE_ENCODE:
|
case COMPRESS_NONE_ENCODE:
|
||||||
copyBuffer(NewCryptConn(in, crypt), out)
|
io.Copy(NewCryptConn(in, crypt), out)
|
||||||
out.Close()
|
out.Close()
|
||||||
NewCryptConn(in, crypt).Write([]byte(IO_EOF))
|
NewCryptConn(in, crypt).Write([]byte(IO_EOF))
|
||||||
case COMPRESS_NONE_DECODE:
|
case COMPRESS_NONE_DECODE:
|
||||||
copyBuffer(in, NewCryptConn(out, crypt))
|
io.Copy(in, NewCryptConn(out, crypt))
|
||||||
in.Close()
|
in.Close()
|
||||||
if !mux {
|
if !mux {
|
||||||
out.Close()
|
out.Close()
|
||||||
@ -147,10 +147,11 @@ func GetIntNoerrByStr(str string) int {
|
|||||||
|
|
||||||
var bufPool = sync.Pool{
|
var bufPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return make([]byte, 65536)
|
return make([]byte, poolSize)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// io.copy的优化版,读取buffer长度原为32*1024,与snappy不同,导致读取出的内容存在差异,不利于解密,特此修改
|
// io.copy的优化版,读取buffer长度原为32*1024,与snappy不同,导致读取出的内容存在差异,不利于解密,特此修改
|
||||||
|
//废除
|
||||||
func copyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
func copyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||||
//TODO 回收问题
|
//TODO 回收问题
|
||||||
buf := bufPool.Get().([]byte)
|
buf := bufPool.Get().([]byte)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user