mirror of https://github.com/v2ray/v2ray-core
				
				
				
			refactor bytes functions
							parent
							
								
									d12d5b0593
								
							
						
					
					
						commit
						67ac925ee7
					
				| 
						 | 
				
			
			@ -3,6 +3,8 @@ package alloc
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"io"
 | 
			
		||||
 | 
			
		||||
	"github.com/v2ray/v2ray-core/common/serial"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
| 
						 | 
				
			
			@ -56,6 +58,16 @@ func (b *Buffer) AppendString(s string) *Buffer {
 | 
			
		|||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Buffer) AppendUint16(v uint16) *Buffer {
 | 
			
		||||
	b.Value = serial.Uint16ToBytes(v, b.Value)
 | 
			
		||||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Buffer) AppendUint32(v uint32) *Buffer {
 | 
			
		||||
	b.Value = serial.Uint32ToBytes(v, b.Value)
 | 
			
		||||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
 | 
			
		||||
// no more than 16 bytes.
 | 
			
		||||
func (b *Buffer) Prepend(data []byte) *Buffer {
 | 
			
		||||
| 
						 | 
				
			
			@ -64,6 +76,22 @@ func (b *Buffer) Prepend(data []byte) *Buffer {
 | 
			
		|||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Buffer) PrependBytes(data ...byte) *Buffer {
 | 
			
		||||
	return b.Prepend(data)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Buffer) PrependUint16(v uint16) *Buffer {
 | 
			
		||||
	b.SliceBack(2)
 | 
			
		||||
	serial.Uint16ToBytes(v, b.Value[:0])
 | 
			
		||||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Buffer) PrependUint32(v uint32) *Buffer {
 | 
			
		||||
	b.SliceBack(4)
 | 
			
		||||
	serial.Uint32ToBytes(v, b.Value[:0])
 | 
			
		||||
	return b
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Bytes returns the content bytes of this Buffer.
 | 
			
		||||
func (b *Buffer) Bytes() []byte {
 | 
			
		||||
	return b.Value
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,8 +46,8 @@ func (this Port) Value() uint16 {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Bytes returns the correspoding bytes of this Port, in big endian order.
 | 
			
		||||
func (this Port) Bytes() []byte {
 | 
			
		||||
	return serial.Uint16ToBytes(this.Value())
 | 
			
		||||
func (this Port) Bytes(b []byte) []byte {
 | 
			
		||||
	return serial.Uint16ToBytes(this.Value(), b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// String returns the string presentation of this Port.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,12 +14,11 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
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...)
 | 
			
		||||
	t.Bytes(bytes)
 | 
			
		||||
	t.Bytes(bytes)
 | 
			
		||||
	t.Bytes(bytes)
 | 
			
		||||
	t.Bytes(bytes)
 | 
			
		||||
	return bytes
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -53,7 +52,7 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession {
 | 
			
		|||
func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
 | 
			
		||||
	timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
 | 
			
		||||
	idHash := this.idHash(header.User.Account.(*protocol.VMessAccount).AnyValidID().Bytes())
 | 
			
		||||
	idHash.Write(timestamp.Bytes())
 | 
			
		||||
	idHash.Write(timestamp.Bytes(nil))
 | 
			
		||||
	writer.Write(idHash.Sum(nil))
 | 
			
		||||
 | 
			
		||||
	buffer := alloc.NewSmallBuffer().Clear()
 | 
			
		||||
| 
						 | 
				
			
			@ -64,7 +63,7 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
 | 
			
		|||
	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())
 | 
			
		||||
	buffer.AppendUint16(header.Port.Value())
 | 
			
		||||
 | 
			
		||||
	switch {
 | 
			
		||||
	case header.Address.IsIPv4():
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -94,12 +94,12 @@ func (this *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.
 | 
			
		|||
		writer.Write([]byte(hostStr))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	writer.Write(cmd.Port.Bytes())
 | 
			
		||||
	writer.Write(cmd.Port.Bytes(nil))
 | 
			
		||||
 | 
			
		||||
	idBytes := cmd.ID.Bytes()
 | 
			
		||||
	writer.Write(idBytes)
 | 
			
		||||
 | 
			
		||||
	writer.Write(serial.Uint16ToBytes(cmd.AlterIds))
 | 
			
		||||
	writer.Write(serial.Uint16ToBytes(cmd.AlterIds, nil))
 | 
			
		||||
	writer.Write([]byte{byte(cmd.Level)})
 | 
			
		||||
 | 
			
		||||
	writer.Write([]byte{cmd.ValidMin})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,8 +9,8 @@ import (
 | 
			
		|||
 | 
			
		||||
type Timestamp int64
 | 
			
		||||
 | 
			
		||||
func (this Timestamp) Bytes() []byte {
 | 
			
		||||
	return serial.Int64ToBytes(int64(this))
 | 
			
		||||
func (this Timestamp) Bytes(b []byte) []byte {
 | 
			
		||||
	return serial.Int64ToBytes(int64(this), b)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type TimestampGenerator func() Timestamp
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -83,11 +83,11 @@ func (this *TimedUserValidator) generateNewHashes(nowSec Timestamp, idx int, ent
 | 
			
		|||
	var hashValueRemoval [16]byte
 | 
			
		||||
	idHash := this.hasher(entry.id.Bytes())
 | 
			
		||||
	for entry.lastSec <= nowSec {
 | 
			
		||||
		idHash.Write(entry.lastSec.Bytes())
 | 
			
		||||
		idHash.Write(entry.lastSec.Bytes(nil))
 | 
			
		||||
		idHash.Sum(hashValue[:0])
 | 
			
		||||
		idHash.Reset()
 | 
			
		||||
 | 
			
		||||
		idHash.Write(entry.lastSecRemoval.Bytes())
 | 
			
		||||
		idHash.Write(entry.lastSecRemoval.Bytes(nil))
 | 
			
		||||
		idHash.Sum(hashValueRemoval[:0])
 | 
			
		||||
		idHash.Reset()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,24 +10,24 @@ func ByteToHexString(value byte) string {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func BytesToUint16(value []byte) uint16 {
 | 
			
		||||
	return uint16(value[0])<<8 + uint16(value[1])
 | 
			
		||||
	return uint16(value[0])<<8 | uint16(value[1])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BytesToUint32(value []byte) uint32 {
 | 
			
		||||
	return uint32(value[0])<<24 +
 | 
			
		||||
		uint32(value[1])<<16 +
 | 
			
		||||
		uint32(value[2])<<8 +
 | 
			
		||||
	return uint32(value[0])<<24 |
 | 
			
		||||
		uint32(value[1])<<16 |
 | 
			
		||||
		uint32(value[2])<<8 |
 | 
			
		||||
		uint32(value[3])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BytesToInt64(value []byte) int64 {
 | 
			
		||||
	return int64(value[0])<<56 +
 | 
			
		||||
		int64(value[1])<<48 +
 | 
			
		||||
		int64(value[2])<<40 +
 | 
			
		||||
		int64(value[3])<<32 +
 | 
			
		||||
		int64(value[4])<<24 +
 | 
			
		||||
		int64(value[5])<<16 +
 | 
			
		||||
		int64(value[6])<<8 +
 | 
			
		||||
	return int64(value[0])<<56 |
 | 
			
		||||
		int64(value[1])<<48 |
 | 
			
		||||
		int64(value[2])<<40 |
 | 
			
		||||
		int64(value[3])<<32 |
 | 
			
		||||
		int64(value[4])<<24 |
 | 
			
		||||
		int64(value[5])<<16 |
 | 
			
		||||
		int64(value[6])<<8 |
 | 
			
		||||
		int64(value[7])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,47 +4,36 @@ import (
 | 
			
		|||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Uint16ToBytes(value uint16) []byte {
 | 
			
		||||
	return []byte{byte(value >> 8), byte(value)}
 | 
			
		||||
func Uint16ToBytes(value uint16, b []byte) []byte {
 | 
			
		||||
	return append(b, byte(value>>8), byte(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Uint16ToString(value uint16) string {
 | 
			
		||||
	return strconv.Itoa(int(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Uint32ToBytes(value uint32) []byte {
 | 
			
		||||
	return []byte{
 | 
			
		||||
		byte(value >> 24),
 | 
			
		||||
		byte(value >> 16),
 | 
			
		||||
		byte(value >> 8),
 | 
			
		||||
		byte(value),
 | 
			
		||||
	}
 | 
			
		||||
func Uint32ToBytes(value uint32, b []byte) []byte {
 | 
			
		||||
	return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IntToBytes(value int) []byte {
 | 
			
		||||
	return []byte{
 | 
			
		||||
		byte(value >> 24),
 | 
			
		||||
		byte(value >> 16),
 | 
			
		||||
		byte(value >> 8),
 | 
			
		||||
		byte(value),
 | 
			
		||||
	}
 | 
			
		||||
func IntToBytes(value int, b []byte) []byte {
 | 
			
		||||
	return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IntToString(value int) string {
 | 
			
		||||
	return Int64ToString(int64(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Int64ToBytes(value int64) []byte {
 | 
			
		||||
	return []byte{
 | 
			
		||||
		byte(value >> 56),
 | 
			
		||||
		byte(value >> 48),
 | 
			
		||||
		byte(value >> 40),
 | 
			
		||||
		byte(value >> 32),
 | 
			
		||||
		byte(value >> 24),
 | 
			
		||||
		byte(value >> 16),
 | 
			
		||||
		byte(value >> 8),
 | 
			
		||||
		byte(value),
 | 
			
		||||
	}
 | 
			
		||||
func Int64ToBytes(value int64, b []byte) []byte {
 | 
			
		||||
	return append(b,
 | 
			
		||||
		byte(value>>56),
 | 
			
		||||
		byte(value>>48),
 | 
			
		||||
		byte(value>>40),
 | 
			
		||||
		byte(value>>32),
 | 
			
		||||
		byte(value>>24),
 | 
			
		||||
		byte(value>>16),
 | 
			
		||||
		byte(value>>8),
 | 
			
		||||
		byte(value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Int64ToString(value int64) string {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -49,7 +49,7 @@ func ChunkKeyGenerator(iv []byte) func() []byte {
 | 
			
		|||
	return func() []byte {
 | 
			
		||||
		newKey := make([]byte, 0, len(iv)+4)
 | 
			
		||||
		newKey = append(newKey, iv...)
 | 
			
		||||
		newKey = append(newKey, serial.IntToBytes(chunkId)...)
 | 
			
		||||
		serial.IntToBytes(chunkId, newKey)
 | 
			
		||||
		chunkId++
 | 
			
		||||
		return newKey
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -143,7 +143,7 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin
 | 
			
		|||
			writer.Write([]byte(request.Address.Domain()))
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		writer.Write(request.Port.Bytes())
 | 
			
		||||
		writer.Write(request.Port.Bytes(nil))
 | 
			
		||||
		writer.Write(payload.Value)
 | 
			
		||||
 | 
			
		||||
		if request.OTA {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -306,5 +306,5 @@ func (r *Socks5Response) Write(writer io.Writer) {
 | 
			
		|||
	case 0x04:
 | 
			
		||||
		writer.Write(r.IPv6[:])
 | 
			
		||||
	}
 | 
			
		||||
	writer.Write(r.Port.Bytes())
 | 
			
		||||
	writer.Write(r.Port.Bytes(nil))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,7 +34,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
 | 
			
		|||
	case request.Address.IsDomain():
 | 
			
		||||
		buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
 | 
			
		||||
	}
 | 
			
		||||
	buffer.Append(request.Port.Bytes())
 | 
			
		||||
	buffer.AppendUint16(request.Port.Value())
 | 
			
		||||
	buffer.Append(request.Data.Value)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,6 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/v2ray/v2ray-core/common/alloc"
 | 
			
		||||
	v2io "github.com/v2ray/v2ray-core/common/io"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/common/serial"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type AuthChunkWriter struct {
 | 
			
		||||
| 
						 | 
				
			
			@ -35,5 +34,5 @@ func Authenticate(buffer *alloc.Buffer) {
 | 
			
		|||
	buffer.SliceBack(4)
 | 
			
		||||
	fnvHash.Sum(buffer.Value[:0])
 | 
			
		||||
 | 
			
		||||
	buffer.Prepend(serial.Uint16ToBytes(uint16(buffer.Len())))
 | 
			
		||||
	buffer.PrependUint16(uint16(buffer.Len()))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,7 +32,7 @@ func (this *Receiver) UnmarshalJSON(data []byte) error {
 | 
			
		|||
		return internal.ErrorBadConfiguration
 | 
			
		||||
	}
 | 
			
		||||
	if rawConfig.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
 | 
			
		||||
		rawConfig.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854))
 | 
			
		||||
		rawConfig.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854, nil))
 | 
			
		||||
	}
 | 
			
		||||
	this.Destination = v2net.TCPDestination(rawConfig.Address.Address, rawConfig.Port)
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,7 @@ func appendAddress(request []byte, address v2net.Address) []byte {
 | 
			
		|||
func socks5Request(command byte, address v2net.Destination) []byte {
 | 
			
		||||
	request := []byte{socks5Version, command, 0}
 | 
			
		||||
	request = appendAddress(request, address.Address())
 | 
			
		||||
	request = append(request, address.Port().Bytes()...)
 | 
			
		||||
	request = address.Port().Bytes(request)
 | 
			
		||||
	return request
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +43,7 @@ func socks5UDPRequest(address v2net.Destination, payload []byte) []byte {
 | 
			
		|||
	request := make([]byte, 0, 1024)
 | 
			
		||||
	request = append(request, 0, 0, 0)
 | 
			
		||||
	request = appendAddress(request, address.Address())
 | 
			
		||||
	request = append(request, address.Port().Bytes()...)
 | 
			
		||||
	request = address.Port().Bytes(request)
 | 
			
		||||
	request = append(request, payload...)
 | 
			
		||||
	return request
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,8 +29,7 @@ func (this *SimpleAuthenticator) HeaderSize() int {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer) {
 | 
			
		||||
	var length uint16 = uint16(buffer.Len())
 | 
			
		||||
	buffer.Prepend(serial.Uint16ToBytes(length))
 | 
			
		||||
	buffer.PrependUint16(uint16(buffer.Len()))
 | 
			
		||||
	fnvHash := fnv.New32a()
 | 
			
		||||
	fnvHash.Write(buffer.Value)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue