chore: optimize code
Build / Build (push) Has been cancelled

This commit is contained in:
nadoo committed 2026-11-16 21:08:27 +08:00
1 parent 9eadb8d9f4
commit a7d0421c93
15 files changed
+422 -171

No files matched your search

+38 -24
View File
@@ -25,6 +25,20 @@ const (
chunkSize = 1 << 13 // 8192
)
// putBE16 writes v to buf as a big-endian uint16.
func putBE16(buf *bytes.Buffer, v uint16) {
var b [2]byte
binary.BigEndian.PutUint16(b[:], v)
buf.Write(b[:])
}
// putBE32 writes v to buf as a big-endian uint32.
func putBE32(buf *bytes.Buffer, v uint32) {
var b [4]byte
binary.BigEndian.PutUint32(b[:], v)
buf.Write(b[:])
}
// TLSObfs struct
type TLSObfs struct {
obfsHost string
@@ -67,7 +81,7 @@ func (c *TLSObfsConn) Write(b []byte) (int, error) {
end := min(i+chunkSize, n)
buf.Write([]byte{0x17, 0x03, 0x03})
binary.Write(buf, binary.BigEndian, uint16(len(b[i:end])))
putBE16(buf, uint16(len(b[i:end])))
buf.Write(b[i:end])
_, err := c.Conn.Write(buf.Bytes())
@@ -143,7 +157,7 @@ func (c *TLSObfsConn) handshake(b []byte) (int, error) {
buf.Write([]byte{0x03, 0x01})
// length
binary.Write(buf, binary.BigEndian, uint16(handshakeLen))
putBE16(buf, uint16(handshakeLen))
// Handshake Begin
// Handshake Type: Client Hello (1)
@@ -157,7 +171,7 @@ func (c *TLSObfsConn) handshake(b []byte) (int, error) {
// Extension Begin
// ext content length
binary.Write(buf, binary.BigEndian, uint16(extLen))
putBE16(buf, uint16(extLen))
// ext content
buf.Write(bufExt.Bytes())
@@ -182,7 +196,7 @@ func clientHello(buf *bytes.Buffer) {
// clients do not send current time, and server do not check it,
// golang tls client and chrome browser send random bytes instead.
//
binary.Write(buf, binary.BigEndian, uint32(time.Now().Unix()))
putBE32(buf, uint32(time.Now().Unix()))
random := make([]byte, 28)
// The above 2 lines of codes was added to make it compatible with some server implementation,
// if we don't need the compatibility, just use the following code instead.
@@ -200,7 +214,7 @@ func clientHello(buf *bytes.Buffer) {
// https://github.com/shadowsocks/simple-obfs/blob/7659eeccf473aa41eb294e92c32f8f60a8747325/src/obfs_tls.c#L57
// Cipher Suites Length: 56
binary.Write(buf, binary.BigEndian, uint16(56))
putBE16(buf, uint16(56))
// Cipher Suites (28 suites)
buf.Write([]byte{
0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f,
@@ -219,44 +233,44 @@ func extension(b []byte, server string, buf *bytes.Buffer) {
// Extension: SessionTicket TLS
buf.Write([]byte{0x00, 0x23}) // type
// NOTE: send some data in sessionticket, the server will treat it as data too
binary.Write(buf, binary.BigEndian, uint16(len(b))) // length
putBE16(buf, uint16(len(b))) // length
buf.Write(b)
// Extension: server_name
buf.Write([]byte{0x00, 0x00}) // type
binary.Write(buf, binary.BigEndian, uint16(len(server)+5)) // length
binary.Write(buf, binary.BigEndian, uint16(len(server)+3)) // Server Name list length
buf.WriteByte(0x00) // Server Name Type: host_name (0)
binary.Write(buf, binary.BigEndian, uint16(len(server))) // Server Name length
buf.Write([]byte{0x00, 0x00}) // type
putBE16(buf, uint16(len(server)+5)) // length
putBE16(buf, uint16(len(server)+3)) // Server Name list length
buf.WriteByte(0x00) // Server Name Type: host_name (0)
putBE16(buf, uint16(len(server))) // Server Name length
buf.WriteString(server)
// https://github.com/shadowsocks/simple-obfs/blob/7659eeccf473aa41eb294e92c32f8f60a8747325/src/obfs_tls.c#L88
// Extension: ec_point_formats (len=4)
buf.Write([]byte{0x00, 0x0b}) // type
binary.Write(buf, binary.BigEndian, uint16(4)) // length
buf.WriteByte(0x03) // format length
buf.Write([]byte{0x00, 0x0b}) // type
putBE16(buf, uint16(4)) // length
buf.WriteByte(0x03) // format length
buf.Write([]byte{0x01, 0x00, 0x02})
// Extension: supported_groups (len=10)
buf.Write([]byte{0x00, 0x0a}) // type
binary.Write(buf, binary.BigEndian, uint16(10)) // length
binary.Write(buf, binary.BigEndian, uint16(8)) // Supported Groups List Length: 8
buf.Write([]byte{0x00, 0x0a}) // type
putBE16(buf, uint16(10)) // length
putBE16(buf, uint16(8)) // Supported Groups List Length: 8
buf.Write([]byte{0x00, 0x1d, 0x00, 0x17, 0x00, 0x19, 0x00, 0x18})
// Extension: signature_algorithms (len=32)
buf.Write([]byte{0x00, 0x0d}) // type
binary.Write(buf, binary.BigEndian, uint16(32)) // length
binary.Write(buf, binary.BigEndian, uint16(30)) // Signature Hash Algorithms Length: 30
buf.Write([]byte{0x00, 0x0d}) // type
putBE16(buf, uint16(32)) // length
putBE16(buf, uint16(30)) // Signature Hash Algorithms Length: 30
buf.Write([]byte{
0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05, 0x01, 0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02,
0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03,
})
// Extension: encrypt_then_mac (len=0)
buf.Write([]byte{0x00, 0x16}) // type
binary.Write(buf, binary.BigEndian, uint16(0)) // length
buf.Write([]byte{0x00, 0x16}) // type
putBE16(buf, uint16(0)) // length
// Extension: extended_master_secret (len=0)
buf.Write([]byte{0x00, 0x17}) // type
binary.Write(buf, binary.BigEndian, uint16(0)) // length
buf.Write([]byte{0x00, 0x17}) // type
putBE16(buf, uint16(0)) // length
}