1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

protocol and api

This commit is contained in:
v2ray 2016-02-25 21:50:38 +01:00
parent 76ca9de25f
commit 7b79a768ef
7 changed files with 145 additions and 13 deletions

12
app/api/api.go Normal file
View File

@ -0,0 +1,12 @@
package api
import (
"github.com/v2ray/v2ray-core/app"
)
const (
APP_ID = app.ID(5)
)
type ApiServer struct {
}

9
app/api/config.go Normal file
View File

@ -0,0 +1,9 @@
package api
import (
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Config struct {
DirectPort v2net.Port
}

View File

@ -0,0 +1,99 @@
package raw
import (
"crypto/md5"
"crypto/rand"
"hash/fnv"
"io"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/common/protocol"
)
func hashTimestamp(t protocol.Timestamp) []byte {
once := t.Bytes()
bytes := make([]byte, 0, 32)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
return bytes
}
type ClientSession struct {
requestBodyKey []byte
requestBodyIV []byte
responseHeader byte
responseBodyKey []byte
responseBodyIV []byte
idHash protocol.IDHash
}
func NewClientSession(idHash protocol.IDHash) *ClientSession {
randomBytes := make([]byte, 33) // 16 + 16 + 1
rand.Read(randomBytes)
session := &ClientSession{}
session.requestBodyKey = randomBytes[:16]
session.requestBodyIV = randomBytes[16:32]
session.responseHeader = randomBytes[32]
session.idHash = idHash
return session
}
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)
encryptionBegin := buffer.Len()
buffer.AppendBytes(Version)
buffer.Append(this.requestBodyIV)
buffer.Append(this.requestBodyKey)
buffer.AppendBytes(this.responseHeader, byte(header.Option), byte(0), byte(0))
buffer.AppendBytes(byte(header.Command))
buffer.Append(header.Port.Bytes())
switch {
case header.Address.IsIPv4():
buffer.AppendBytes(AddrTypeIPv4)
buffer.Append(header.Address.IP())
case header.Address.IsIPv6():
buffer.AppendBytes(AddrTypeIPv6)
buffer.Append(header.Address.IP())
case header.Address.IsDomain():
buffer.AppendBytes(AddrTypeDomain, byte(len(header.Address.Domain())))
buffer.Append([]byte(header.Address.Domain()))
}
encryptionEnd := buffer.Len()
fnv1a := fnv.New32a()
fnv1a.Write(buffer.Value[encryptionBegin:encryptionEnd])
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])
writer.Write(buffer.Value)
return
}
func (this *ClientSession) EncodeRequestBody(writer io.Writer) io.Writer {
aesStream := crypto.NewAesEncryptionStream(this.requestBodyKey, this.requestBodyIV)
return crypto.NewCryptionWriter(aesStream, writer)
}

View File

@ -0,0 +1,9 @@
package raw
const (
Version = byte(1)
AddrTypeIPv4 = byte(0x01)
AddrTypeIPv6 = byte(0x03)
AddrTypeDomain = byte(0x02)
)

View File

@ -1,13 +0,0 @@
package raw
type RequestEncoder struct {
}
type RequestDecoder struct {
}
type ResponseEncoder struct {
}
type ResponseDecoder struct {
}

View File

@ -0,0 +1,6 @@
package hooks
type Input struct {
Action string
Value string
}

10
common/web/service.go Normal file
View File

@ -0,0 +1,10 @@
package web
import (
"github.com/v2ray/v2ray-core/common/protocol"
)
type Authenciation struct {
Required bool
User *protocol.User
}