修订代码

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 0ed6621bf6
commit 0bb3e23908
4 changed files with 64 additions and 35 deletions

View File

@@ -47,12 +47,10 @@ var (
)
type exitCmd struct {
enable, defaultBoolValue bool
name string
isStr bool
desc, defaultStringValue, strValue string
f func()
fs func(string)
enable, defaultBoolValue, isStr bool
name, desc, defaultStringValue, strValue string
f func()
fs func(string)
}
func init() {

View File

@@ -1,6 +1,7 @@
package tlsLayer
import (
"bytes"
"crypto/tls"
"encoding/binary"
"io"
@@ -29,7 +30,7 @@ func (c *FakeAppDataConn) Read(p []byte) (n int, err error) {
if err != nil {
return
}
length := int(binary.BigEndian.Uint16(tlsHeader[3:5]))
length := int(binary.BigEndian.Uint16(tlsHeader[3:]))
if tlsHeader[0] != 23 {
return 0, utils.ErrInErr{ErrDesc: "unexpected TLS record type: ", Data: tlsHeader[0]}
}
@@ -45,41 +46,67 @@ func (c *FakeAppDataConn) Read(p []byte) (n int, err error) {
return
}
func (c *FakeAppDataConn) Write(p []byte) (n int, err error) {
var header [5]byte
func WriteAppData(conn io.Writer, buf *bytes.Buffer, d []byte) (n int, err error) {
var h [5]byte
h[0] = 23
binary.BigEndian.PutUint16(h[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(h[3:], uint16(len(d)))
header[0] = 23
const maxlen = 16384
for len(p) > maxlen {
binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(header[3:5], uint16(maxlen))
shouldPut := false
buf := utils.GetBuf()
buf.Write(header[:])
buf.Write(p[:maxlen])
if buf == nil {
buf = utils.GetBuf()
shouldPut = true
}
buf.Write(h[:])
buf.Write(d)
c.Conn.Write(buf.Bytes())
n, err = conn.Write(buf.Bytes())
if shouldPut {
utils.PutBuf(buf)
}
return
}
// 一般conn直接为tcp连接而它是有系统缓存的因此我们一般不需要特地创建一个缓存
// 写两遍之后在发出
func WriteAppDataNoBuf(conn io.Writer, d []byte) (n int, err error) {
var h [5]byte
h[0] = 23
binary.BigEndian.PutUint16(h[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(h[3:], uint16(len(d)))
_, err = conn.Write(h[:])
if err != nil {
return
}
return conn.Write(d)
}
func (c *FakeAppDataConn) Write(p []byte) (n int, err error) {
const maxlen = 1 << 14
var nn int
for len(p) > maxlen {
nn, err = WriteAppDataNoBuf(c.Conn, p[:maxlen])
n += nn
if err != nil {
return
}
n += maxlen
p = p[maxlen:]
}
binary.BigEndian.PutUint16(header[1:3], tls.VersionTLS12)
binary.BigEndian.PutUint16(header[3:5], uint16(len(p)))
buf := utils.GetBuf()
buf.Write(header[:])
buf.Write(p)
nn, err = WriteAppDataNoBuf(c.Conn, p)
c.Conn.Write(buf.Bytes())
utils.PutBuf(buf)
n += nn
if err == nil {
n += len(p)
}
return
}

View File

@@ -141,6 +141,8 @@ func shadowCopyHandshakeClientToFake(fakeConn, clientConn net.Conn, hashW *utils
step := 0
var applicationDataCount int
buf := utils.GetBuf()
for {
if ce := utils.CanLogDebug("shadowTls2 copy "); ce != nil {
ce.Write(zap.Int("step", step))
@@ -167,7 +169,6 @@ func shadowCopyHandshakeClientToFake(fakeConn, clientConn net.Conn, hashW *utils
}
if contentType == 23 {
buf := utils.GetBuf()
netLayer.SetCommonReadTimeout(clientConn)
@@ -183,16 +184,16 @@ func shadowCopyHandshakeClientToFake(fakeConn, clientConn net.Conn, hashW *utils
if hashW.Written() && length >= 8 {
checksum := hashW.Sum()
bs := buf.Bytes()
first8 := buf.Bytes()[:8]
if ce := utils.CanLogDebug("shadowTls2 check "); ce != nil {
ce.Write(zap.Int("step", step),
zap.String("checksum", fmt.Sprintf("%v", checksum)),
zap.String("real8", fmt.Sprintf("%v", bs[:8])),
zap.String("real8", fmt.Sprintf("%v", first8)),
)
}
if bytes.Equal(bs[:8], checksum) {
if bytes.Equal(first8, checksum) {
buf.Next(8)
return buf, nil
}
@@ -201,13 +202,16 @@ func shadowCopyHandshakeClientToFake(fakeConn, clientConn net.Conn, hashW *utils
netLayer.SetCommonWriteTimeout(fakeConn)
_, err = io.Copy(fakeConn, io.MultiReader(bytes.NewReader(header[:]), buf))
utils.PutBuf(buf)
netLayer.PersistWrite(fakeConn)
if err != nil {
utils.PutBuf(buf)
return nil, utils.ErrInErr{ErrDetail: err, ErrDesc: "shadowTls2, copy err2"}
}
buf.Reset()
applicationDataCount++
} else {

View File

@@ -1,5 +1,5 @@
/*
Package tlsLayer provides facilities for tls, including uTls, sniffing and random certificate.
Package tlsLayer provides facilities for tls, including uTls,shadowTls, sniffing and random certificate.
Sniffing can be a part of Tls Lazy Encrypt tech.
*/