optimisations

This commit is contained in:
Qian Wang 2018-10-20 17:03:39 +01:00
parent 7487600434
commit bd69784443
5 changed files with 43 additions and 26 deletions

View File

@ -142,8 +142,8 @@ func main() {
log.Fatalf("Failed to establish connection to remote: %v\n", err) log.Fatalf("Failed to establish connection to remote: %v\n", err)
} }
obfs := util.MakeObfs(sta.SID) obfs := util.MakeObfs(sta.SID[:16])
deobfs := util.MakeDeobfs(sta.SID) deobfs := util.MakeDeobfs(sta.SID[:16])
// TODO: where to put obfs deobfs and rtd? // TODO: where to put obfs deobfs and rtd?
sesh := mux.MakeSession(0, initRemoteConn, obfs, deobfs, util.ReadTillDrain) sesh := mux.MakeSession(0, initRemoteConn, obfs, deobfs, util.ReadTillDrain)

View File

@ -9,7 +9,7 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"runtime" //"runtime"
"strings" "strings"
"time" "time"
@ -105,7 +105,7 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
if sesh = sta.GetSession(arrSID); sesh != nil { if sesh = sta.GetSession(arrSID); sesh != nil {
sesh.AddConnection(conn) sesh.AddConnection(conn)
} else { } else {
sesh = mux.MakeSession(0, conn, util.MakeObfs(SID), util.MakeDeobfs(SID), util.ReadTillDrain) sesh = mux.MakeSession(0, conn, util.MakeObfs(SID[:16]), util.MakeDeobfs(SID[:16]), util.ReadTillDrain)
sta.PutSession(arrSID, sesh) sta.PutSession(arrSID, sesh)
} }
go func() { go func() {
@ -129,7 +129,6 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
} }
func main() { func main() {
runtime.SetBlockProfileRate(2)
go func() { go func() {
log.Println(http.ListenAndServe("0.0.0.0:8001", nil)) log.Println(http.ListenAndServe("0.0.0.0:8001", nil))
}() }()

View File

@ -62,6 +62,18 @@ func (s *Stream) recvNewFrame() {
continue continue
} }
if len(s.sh) == 0 && f.Seq == s.nextRecvSeq {
s.sortedBufCh <- f.Payload
s.nextRecvSeq += 1
if s.nextRecvSeq == 0 {
// when nextN is wrapped, wrapMode becomes false and rev+1
s.rev += 1
s.wrapMode = false
}
continue
}
// For the ease of demonstration, assume seq is uint8, i.e. it wraps around after 255 // For the ease of demonstration, assume seq is uint8, i.e. it wraps around after 255
fs := &frameNode{ fs := &frameNode{
f.Seq, f.Seq,

View File

@ -3,7 +3,7 @@ package multiplex
import ( import (
"log" "log"
"net" "net"
//"sort" "sort"
) )
const ( const (
@ -79,18 +79,18 @@ type sentNotifier struct {
func (ce *connEnclave) send(data []byte) { func (ce *connEnclave) send(data []byte) {
// TODO: error handling // TODO: error handling
_, err := ce.remoteConn.Write(data) n, err := ce.remoteConn.Write(data)
if err != nil { if err != nil {
ce.sb.closingCECh <- ce ce.sb.closingCECh <- ce
log.Println(err) log.Println(err)
} }
/*
sn := &sentNotifier{ sn := &sentNotifier{
ce, ce,
n, n,
} }
ce.sb.sentNotifyCh <- sn ce.sb.sentNotifyCh <- sn
*/
} }
// Dispatcher sends data coming from a stream to a remote connection // Dispatcher sends data coming from a stream to a remote connection
@ -102,11 +102,11 @@ func (sb *switchboard) dispatch() {
// dispatCh receives data from stream.Write // dispatCh receives data from stream.Write
case data := <-sb.dispatCh: case data := <-sb.dispatCh:
go sb.ces[nextCE%len(sb.ces)].send(data) go sb.ces[nextCE%len(sb.ces)].send(data)
//sb.ces[0].sendQueue += len(data) sb.ces[0].sendQueue += len(data)
nextCE += 1 nextCE += 1
/*case notified := <-sb.sentNotifyCh: case notified := <-sb.sentNotifyCh:
notified.ce.sendQueue -= notified.sent notified.ce.sendQueue -= notified.sent
sort.Sort(byQ(sb.ces))*/ sort.Sort(byQ(sb.ces))
case conn := <-sb.newConnCh: case conn := <-sb.newConnCh:
log.Println("newConn") log.Println("newConn")
newCe := &connEnclave{ newCe := &connEnclave{

View File

@ -37,20 +37,26 @@ func MakeObfs(key []byte) func(*mux.Frame) []byte {
iv := make([]byte, 16) iv := make([]byte, 16)
io.ReadFull(rand.Reader, iv) io.ReadFull(rand.Reader, iv)
cipherheader := AESEncrypt(iv, key, header) cipherheader := AESEncrypt(iv, key, header)
obfsed := make([]byte, len(f.Payload)+12+16)
copy(obfsed[0:16], iv) // Composing final obfsed message
copy(obfsed[16:28], cipherheader) // We don't use util.AddRecordLayer here to avoid unnecessary malloc
copy(obfsed[28:], f.Payload) obfsed := make([]byte, 5+16+12+len(f.Payload))
// obfsed: [iv 16 bytes][cipherheader 12 bytes][payload] obfsed[0] = 0x17
ret := AddRecordLayer(obfsed, []byte{0x17}, []byte{0x03, 0x03}) obfsed[1] = 0x03
return ret obfsed[2] = 0x03
binary.BigEndian.PutUint16(obfsed[3:5], uint16(16+12+len(f.Payload)))
copy(obfsed[5:21], iv)
copy(obfsed[21:33], cipherheader)
copy(obfsed[33:], f.Payload)
// obfsed: [record layer 5 bytes][iv 16 bytes][cipherheader 12 bytes][payload]
return obfsed
} }
return obfs return obfs
} }
func MakeDeobfs(key []byte) func([]byte) *mux.Frame { func MakeDeobfs(key []byte) func([]byte) *mux.Frame {
deobfs := func(in []byte) *mux.Frame { deobfs := func(in []byte) *mux.Frame {
peeled := PeelRecordLayer(in) peeled := in[5:]
header := AESDecrypt(peeled[0:16], key, peeled[16:28]) header := AESDecrypt(peeled[0:16], key, peeled[16:28])
streamID := binary.BigEndian.Uint32(header[0:4]) streamID := binary.BigEndian.Uint32(header[0:4])
seq := binary.BigEndian.Uint32(header[4:8]) seq := binary.BigEndian.Uint32(header[4:8])