mirror of https://github.com/cbeuw/Cloak
Redo the header obfuscation. Fix hiccups caused by short packets
This commit is contained in:
parent
fb12f096d1
commit
d906541497
|
|
@ -23,7 +23,7 @@ import (
|
||||||
var version string
|
var version string
|
||||||
|
|
||||||
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
||||||
// The maximum size of TLS message will be 16396+12. 12 because of the stream header
|
// The maximum size of TLS message will be 16396+16. 16 because of the stream header
|
||||||
// 16408 is the max TLS message size on Firefox
|
// 16408 is the max TLS message size on Firefox
|
||||||
buf := make([]byte, 16396)
|
buf := make([]byte, 16396)
|
||||||
for {
|
for {
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import (
|
||||||
var version string
|
var version string
|
||||||
|
|
||||||
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
func pipe(dst io.ReadWriteCloser, src io.ReadWriteCloser) {
|
||||||
// The maximum size of TLS message will be 16396+12. 12 because of the stream header
|
// The maximum size of TLS message will be 16396+16. 16 because of the stream header
|
||||||
// 16408 is the max TLS message size on Firefox
|
// 16408 is the max TLS message size on Firefox
|
||||||
buf := make([]byte, 16396)
|
buf := make([]byte, 16396)
|
||||||
for {
|
for {
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,46 @@
|
||||||
package multiplex
|
package multiplex
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
xxhash "github.com/OneOfOne/xxhash"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Obfser func(*Frame) ([]byte, error)
|
type Obfser func(*Frame) ([]byte, error)
|
||||||
type Deobfser func([]byte) (*Frame, error)
|
type Deobfser func([]byte) (*Frame, error)
|
||||||
|
|
||||||
|
var u32 = binary.BigEndian.Uint32
|
||||||
|
|
||||||
// For each frame, the three parts of the header is xored with three keys.
|
// For each frame, the three parts of the header is xored with three keys.
|
||||||
// The keys are generated from the SID and the payload of the frame.
|
// The keys are generated from the SID and the payload of the frame.
|
||||||
func genXorKeys(secret []byte, data []byte) (i uint32, ii uint32, iii uint32) {
|
func genXorKeys(key, nonce []byte) (i uint32, ii uint32, iii uint32) {
|
||||||
h := xxhash.New32()
|
h := sha1.New()
|
||||||
ret := make([]uint32, 3)
|
hashed := h.Sum(append(key, nonce...))
|
||||||
preHash := make([]byte, 16)
|
return u32(hashed[0:4]), u32(hashed[4:8]), u32(hashed[8:12])
|
||||||
for j := 0; j < 3; j++ {
|
|
||||||
copy(preHash[0:10], secret[j*10:j*10+10])
|
|
||||||
copy(preHash[10:16], data[j*6:j*6+6])
|
|
||||||
h.Write(preHash)
|
|
||||||
ret[j] = h.Sum32()
|
|
||||||
}
|
|
||||||
return ret[0], ret[1], ret[2]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeObfs(key []byte) Obfser {
|
func MakeObfs(key []byte) Obfser {
|
||||||
obfs := func(f *Frame) ([]byte, error) {
|
obfs := func(f *Frame) ([]byte, error) {
|
||||||
if len(f.Payload) < 18 {
|
obfsedHeader := make([]byte, 16)
|
||||||
return nil, errors.New("Payload cannot be shorter than 18 bytes")
|
// header: [StreamID 4 bytes][Seq 4 bytes][Closing 4 bytes][Nonce 4 bytes]
|
||||||
}
|
io.ReadFull(rand.Reader, obfsedHeader[12:16])
|
||||||
obfsedHeader := make([]byte, 12)
|
i, ii, iii := genXorKeys(key, obfsedHeader[12:16])
|
||||||
// header: [StreamID 4 bytes][Seq 4 bytes][Closing 4 bytes]
|
|
||||||
i, ii, iii := genXorKeys(key, f.Payload[0:18])
|
|
||||||
binary.BigEndian.PutUint32(obfsedHeader[0:4], f.StreamID^i)
|
binary.BigEndian.PutUint32(obfsedHeader[0:4], f.StreamID^i)
|
||||||
binary.BigEndian.PutUint32(obfsedHeader[4:8], f.Seq^ii)
|
binary.BigEndian.PutUint32(obfsedHeader[4:8], f.Seq^ii)
|
||||||
binary.BigEndian.PutUint32(obfsedHeader[8:12], f.Closing^iii)
|
binary.BigEndian.PutUint32(obfsedHeader[8:12], f.Closing^iii)
|
||||||
|
|
||||||
// Composing final obfsed message
|
// Composing final obfsed message
|
||||||
// We don't use util.AddRecordLayer here to avoid unnecessary malloc
|
// We don't use util.AddRecordLayer here to avoid unnecessary malloc
|
||||||
obfsed := make([]byte, 5+12+len(f.Payload))
|
obfsed := make([]byte, 5+16+len(f.Payload))
|
||||||
obfsed[0] = 0x17
|
obfsed[0] = 0x17
|
||||||
obfsed[1] = 0x03
|
obfsed[1] = 0x03
|
||||||
obfsed[2] = 0x03
|
obfsed[2] = 0x03
|
||||||
binary.BigEndian.PutUint16(obfsed[3:5], uint16(12+len(f.Payload)))
|
binary.BigEndian.PutUint16(obfsed[3:5], uint16(16+len(f.Payload)))
|
||||||
copy(obfsed[5:17], obfsedHeader)
|
copy(obfsed[5:21], obfsedHeader)
|
||||||
copy(obfsed[17:], f.Payload)
|
copy(obfsed[21:], f.Payload)
|
||||||
// obfsed: [record layer 5 bytes][cipherheader 12 bytes][payload]
|
// obfsed: [record layer 5 bytes][cipherheader 16 bytes][payload]
|
||||||
return obfsed, nil
|
return obfsed, nil
|
||||||
}
|
}
|
||||||
return obfs
|
return obfs
|
||||||
|
|
@ -54,16 +48,16 @@ func MakeObfs(key []byte) Obfser {
|
||||||
|
|
||||||
func MakeDeobfs(key []byte) Deobfser {
|
func MakeDeobfs(key []byte) Deobfser {
|
||||||
deobfs := func(in []byte) (*Frame, error) {
|
deobfs := func(in []byte) (*Frame, error) {
|
||||||
if len(in) < 30 {
|
if len(in) < 21 {
|
||||||
return nil, errors.New("Input cannot be shorter than 30 bytes")
|
return nil, errors.New("Input cannot be shorter than 21 bytes")
|
||||||
}
|
}
|
||||||
peeled := in[5:]
|
peeled := in[5:]
|
||||||
i, ii, iii := genXorKeys(key, peeled[12:30])
|
i, ii, iii := genXorKeys(key, peeled[12:16])
|
||||||
streamID := binary.BigEndian.Uint32(peeled[0:4]) ^ i
|
streamID := u32(peeled[0:4]) ^ i
|
||||||
seq := binary.BigEndian.Uint32(peeled[4:8]) ^ ii
|
seq := u32(peeled[4:8]) ^ ii
|
||||||
closing := binary.BigEndian.Uint32(peeled[8:12]) ^ iii
|
closing := u32(peeled[8:12]) ^ iii
|
||||||
payload := make([]byte, len(peeled)-12)
|
payload := make([]byte, len(peeled)-16)
|
||||||
copy(payload, peeled[12:])
|
copy(payload, peeled[16:])
|
||||||
ret := &Frame{
|
ret := &Frame{
|
||||||
StreamID: streamID,
|
StreamID: streamID,
|
||||||
Seq: seq,
|
Seq: seq,
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errBrokenStream = errors.New("broken stream")
|
var ErrBrokenStream = errors.New("broken stream")
|
||||||
|
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
id uint32
|
id uint32
|
||||||
|
|
@ -18,9 +18,9 @@ type Stream struct {
|
||||||
|
|
||||||
// Explanations of the following 4 fields can be found in frameSorter.go
|
// Explanations of the following 4 fields can be found in frameSorter.go
|
||||||
nextRecvSeq uint32
|
nextRecvSeq uint32
|
||||||
rev int
|
//rev int
|
||||||
sh sorterHeap
|
sh sorterHeap
|
||||||
wrapMode bool
|
//wrapMode bool
|
||||||
|
|
||||||
// New frames are received through newFrameCh by frameSorter
|
// New frames are received through newFrameCh by frameSorter
|
||||||
newFrameCh chan *Frame
|
newFrameCh chan *Frame
|
||||||
|
|
@ -54,18 +54,18 @@ func (stream *Stream) Read(buf []byte) (n int, err error) {
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
select {
|
select {
|
||||||
case <-stream.die:
|
case <-stream.die:
|
||||||
return 0, errBrokenStream
|
return 0, ErrBrokenStream
|
||||||
default:
|
default:
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-stream.die:
|
case <-stream.die:
|
||||||
return 0, errBrokenStream
|
return 0, ErrBrokenStream
|
||||||
case data := <-stream.sortedBufCh:
|
case data := <-stream.sortedBufCh:
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
stream.passiveClose()
|
stream.passiveClose()
|
||||||
return 0, errBrokenStream
|
return 0, ErrBrokenStream
|
||||||
}
|
}
|
||||||
if len(buf) < len(data) {
|
if len(buf) < len(data) {
|
||||||
log.Println(len(data))
|
log.Println(len(data))
|
||||||
|
|
@ -87,7 +87,7 @@ func (stream *Stream) Write(in []byte) (n int, err error) {
|
||||||
select {
|
select {
|
||||||
case <-stream.die:
|
case <-stream.die:
|
||||||
stream.writingM.RUnlock()
|
stream.writingM.RUnlock()
|
||||||
return 0, errBrokenStream
|
return 0, ErrBrokenStream
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,6 @@ type switchboard struct {
|
||||||
optimum atomic.Value // *connEnclave
|
optimum atomic.Value // *connEnclave
|
||||||
cesM sync.RWMutex
|
cesM sync.RWMutex
|
||||||
ces []*connEnclave
|
ces []*connEnclave
|
||||||
|
|
||||||
/*
|
|
||||||
//debug
|
|
||||||
hM sync.Mutex
|
|
||||||
used map[uint32]bool
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *switchboard) getOptimum() *connEnclave {
|
func (sb *switchboard) getOptimum() *connEnclave {
|
||||||
|
|
@ -34,10 +28,6 @@ func (sb *switchboard) getOptimum() *connEnclave {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *switchboard) setOptimum(ce *connEnclave) {
|
|
||||||
sb.optimum.Store(ce)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some data comes from a Stream to be sent through one of the many
|
// Some data comes from a Stream to be sent through one of the many
|
||||||
// remoteConn, but which remoteConn should we use to send the data?
|
// remoteConn, but which remoteConn should we use to send the data?
|
||||||
//
|
//
|
||||||
|
|
@ -54,8 +44,6 @@ func makeSwitchboard(sesh *Session, valve *Valve) *switchboard {
|
||||||
session: sesh,
|
session: sesh,
|
||||||
Valve: valve,
|
Valve: valve,
|
||||||
ces: []*connEnclave{},
|
ces: []*connEnclave{},
|
||||||
//debug
|
|
||||||
// used: make(map[uint32]bool),
|
|
||||||
}
|
}
|
||||||
return sb
|
return sb
|
||||||
}
|
}
|
||||||
|
|
@ -99,18 +87,19 @@ func (sb *switchboard) updateOptimum() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.cesM.RUnlock()
|
sb.cesM.RUnlock()
|
||||||
sb.setOptimum(currentOpti)
|
sb.optimum.Store(currentOpti)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *switchboard) addConn(conn net.Conn) {
|
func (sb *switchboard) addConn(conn net.Conn) {
|
||||||
|
var sendQueue uint32
|
||||||
newCe := &connEnclave{
|
newCe := &connEnclave{
|
||||||
remoteConn: conn,
|
remoteConn: conn,
|
||||||
sendQueue: 0,
|
sendQueue: sendQueue,
|
||||||
}
|
}
|
||||||
sb.cesM.Lock()
|
sb.cesM.Lock()
|
||||||
sb.ces = append(sb.ces, newCe)
|
sb.ces = append(sb.ces, newCe)
|
||||||
sb.cesM.Unlock()
|
sb.cesM.Unlock()
|
||||||
sb.setOptimum(newCe)
|
sb.optimum.Store(newCe)
|
||||||
go sb.deplex(newCe)
|
go sb.deplex(newCe)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -177,14 +166,5 @@ func (sb *switchboard) deplex(ce *connEnclave) {
|
||||||
if stream != nil {
|
if stream != nil {
|
||||||
stream.writeNewFrame(frame)
|
stream.writeNewFrame(frame)
|
||||||
}
|
}
|
||||||
//debug
|
|
||||||
/*
|
|
||||||
sb.hM.Lock()
|
|
||||||
if sb.used[frame.StreamID] {
|
|
||||||
log.Printf("%v lost!\n", frame.StreamID)
|
|
||||||
}
|
|
||||||
sb.used[frame.StreamID] = true
|
|
||||||
sb.hM.Unlock()
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue