Migrate VMessOut into protocol

This commit is contained in:
v2ray committed 2016-02-27 16:41:21 +01:00
1 parent ef51c600fb
commit 2144c47102
7 files changed
+94 -112

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
package raw
import (
"hash/fnv"
)
func Authenticate(b []byte) uint32 {
fnv1hash := fnv.New32a()
fnv1hash.Write(b)
return fnv1hash.Sum32()
}
+5 -10
View File
@@ -51,15 +51,13 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
}
func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
buffer := alloc.NewSmallBuffer().Clear()
defer buffer.Release()
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
idHash := this.idHash(header.User.AnyValidID().Bytes())
idHash.Write(timestamp.Bytes())
idHash.Sum(buffer.Value)
writer.Write(idHash.Sum(nil))
encryptionBegin := buffer.Len()
buffer := alloc.NewSmallBuffer().Clear()
defer buffer.Release()
buffer.AppendBytes(Version)
buffer.Append(this.requestBodyIV)
@@ -80,20 +78,17 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
buffer.Append([]byte(header.Address.Domain()))
}
encryptionEnd := buffer.Len()
fnv1a := fnv.New32a()
fnv1a.Write(buffer.Value[encryptionBegin:encryptionEnd])
fnv1a.Write(buffer.Value)
fnvHash := fnv1a.Sum32()
buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
encryptionEnd += 4
timestampHash := md5.New()
timestampHash.Write(hashTimestamp(timestamp))
iv := timestampHash.Sum(nil)
aesStream := crypto.NewAesEncryptionStream(header.User.ID.CmdKey(), iv)
aesStream.XORKeyStream(buffer.Value[encryptionBegin:encryptionEnd], buffer.Value[encryptionBegin:encryptionEnd])
aesStream.XORKeyStream(buffer.Value, buffer.Value)
writer.Write(buffer.Value)
return
+30 -2
View File
@@ -4,6 +4,7 @@ import (
"errors"
"io"
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/serial"
@@ -14,20 +15,47 @@ import (
var (
ErrorCommandTypeMismatch = errors.New("Command type mismatch.")
ErrorUnknownCommand = errors.New("Unknown command.")
ErrorCommandTooLarge = errors.New("Command too large.")
)
func MarshalCommand(command interface{}, writer io.Writer) error {
var cmdId byte
var factory CommandFactory
switch command.(type) {
case *protocol.CommandSwitchAccount:
factory = new(CommandSwitchAccountFactory)
cmdId = 1
default:
return ErrorUnknownCommand
}
return factory.Marshal(command, writer)
buffer := alloc.NewSmallBuffer()
err := factory.Marshal(command, buffer)
if err != nil {
return err
}
auth := Authenticate(buffer.Value)
len := buffer.Len() + 4
if len > 255 {
return ErrorCommandTooLarge
}
writer.Write([]byte{cmdId, byte(len), byte(auth >> 24), byte(auth >> 16), byte(auth >> 8), byte(auth)})
writer.Write(buffer.Value)
return nil
}
func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error) {
if len(data) <= 4 {
return nil, transport.ErrorCorruptedPacket
}
expectedAuth := Authenticate(data[4:])
actualAuth := serial.BytesLiteral(data[:4]).Uint32Value()
if expectedAuth != actualAuth {
return nil, transport.ErrorCorruptedPacket
}
var factory CommandFactory
switch cmdId {
case 1:
@@ -35,7 +63,7 @@ func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error)
default:
return nil, ErrorUnknownCommand
}
return factory.Unmarshal(data)
return factory.Unmarshal(data[4:])
}
type CommandFactory interface {
+6
View File
@@ -140,4 +140,10 @@ func (this *ServerSession) EncodeResponseHeader(header *protocol.ResponseHeader,
encryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
this.responseWriter = encryptionWriter
encryptionWriter.Write([]byte{this.responseHeader, 0x00})
MarshalCommand(header.Command, encryptionWriter)
}
func (this *ServerSession) EncodeResponseBody(writer io.Writer) io.Writer {
return this.responseWriter
}