1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00

utp header

This commit is contained in:
v2ray 2016-08-08 21:23:07 +02:00
parent 86490e884c
commit b38137bd13
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 68 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
_ "github.com/v2ray/v2ray-core/transport/internet/authenticators/noop" _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/noop"
_ "github.com/v2ray/v2ray-core/transport/internet/authenticators/srtp" _ "github.com/v2ray/v2ray-core/transport/internet/authenticators/srtp"
_ "github.com/v2ray/v2ray-core/transport/internet/authenticators/utp"
) )
var ( var (

View File

@ -0,0 +1,46 @@
package utp
import (
"math/rand"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/transport/internet"
)
type Config struct {
Version byte
}
type UTP struct {
header byte
extension byte
connectionId uint16
}
func (this *UTP) Overhead() int {
return 4
}
func (this *UTP) Open(payload *alloc.Buffer) bool {
payload.SliceFrom(this.Overhead())
return true
}
func (this *UTP) Seal(payload *alloc.Buffer) {
payload.PrependUint16(this.connectionId)
payload.PrependBytes(this.header, this.extension)
}
type UTPFactory struct{}
func (this UTPFactory) Create(rawSettings internet.AuthenticatorConfig) internet.Authenticator {
return &UTP{
header: 1,
extension: 0,
connectionId: uint16(rand.Intn(65536)),
}
}
func init() {
internet.RegisterAuthenticator("utp", UTPFactory{}, func() interface{} { return new(Config) })
}

View File

@ -0,0 +1,21 @@
package utp_test
import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/assert"
. "github.com/v2ray/v2ray-core/transport/internet/authenticators/utp"
)
func TestUTPOpenSeal(t *testing.T) {
assert := assert.On(t)
content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
payload := alloc.NewLocalBuffer(2048).Clear().Append(content)
utp := UTP{}
utp.Seal(payload)
assert.Int(payload.Len()).GreaterThan(len(content))
assert.Bool(utp.Open(payload)).IsTrue()
assert.Bytes(content).Equals(payload.Bytes())
}