mirror of https://github.com/v2ray/v2ray-core
refactor
parent
a4d66be9fa
commit
bae5639dfd
|
@ -21,33 +21,33 @@ func NewBufferedReader(rawReader io.Reader) *BufferedReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsBuffered returns true if the internal cache is effective.
|
// IsBuffered returns true if the internal cache is effective.
|
||||||
func (v *BufferedReader) IsBuffered() bool {
|
func (r *BufferedReader) IsBuffered() bool {
|
||||||
return v.buffered
|
return r.buffered
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBuffered is to enable or disable internal cache. If cache is disabled,
|
// SetBuffered is to enable or disable internal cache. If cache is disabled,
|
||||||
// Read() calls will be delegated to the underlying io.Reader directly.
|
// Read() calls will be delegated to the underlying io.Reader directly.
|
||||||
func (v *BufferedReader) SetBuffered(cached bool) {
|
func (r *BufferedReader) SetBuffered(cached bool) {
|
||||||
v.buffered = cached
|
r.buffered = cached
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read implements io.Reader.Read().
|
// Read implements io.Reader.Read().
|
||||||
func (v *BufferedReader) Read(b []byte) (int, error) {
|
func (r *BufferedReader) Read(b []byte) (int, error) {
|
||||||
if !v.buffered || v.buffer == nil {
|
if !r.buffered || r.buffer == nil {
|
||||||
if !v.buffer.IsEmpty() {
|
if !r.buffer.IsEmpty() {
|
||||||
return v.buffer.Read(b)
|
return r.buffer.Read(b)
|
||||||
}
|
}
|
||||||
return v.reader.Read(b)
|
return r.reader.Read(b)
|
||||||
}
|
}
|
||||||
if v.buffer.IsEmpty() {
|
if r.buffer.IsEmpty() {
|
||||||
if err := v.buffer.Reset(ReadFrom(v.reader)); err != nil {
|
if err := r.buffer.Reset(ReadFrom(r.reader)); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.buffer.IsEmpty() {
|
if r.buffer.IsEmpty() {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return v.buffer.Read(b)
|
return r.buffer.Read(b)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,22 +37,22 @@ const (
|
||||||
RequestOptionChunkMasking = RequestOption(0x04)
|
RequestOptionChunkMasking = RequestOption(0x04)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v RequestOption) Has(option RequestOption) bool {
|
func (o RequestOption) Has(option RequestOption) bool {
|
||||||
return (v & option) == option
|
return (o & option) == option
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *RequestOption) Set(option RequestOption) {
|
func (o *RequestOption) Set(option RequestOption) {
|
||||||
*v = (*v | option)
|
*o = (*o | option)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *RequestOption) Clear(option RequestOption) {
|
func (o *RequestOption) Clear(option RequestOption) {
|
||||||
*v = (*v & (^option))
|
*o = (*o & (^option))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Security byte
|
type Security byte
|
||||||
|
|
||||||
func (v Security) Is(t SecurityType) bool {
|
func (s Security) Is(t SecurityType) bool {
|
||||||
return v == Security(t)
|
return s == Security(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NormSecurity(s Security) Security {
|
func NormSecurity(s Security) Security {
|
||||||
|
@ -72,11 +72,11 @@ type RequestHeader struct {
|
||||||
User *User
|
User *User
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *RequestHeader) Destination() net.Destination {
|
func (h *RequestHeader) Destination() net.Destination {
|
||||||
if v.Command == RequestCommandUDP {
|
if h.Command == RequestCommandUDP {
|
||||||
return net.UDPDestination(v.Address, v.Port)
|
return net.UDPDestination(h.Address, h.Port)
|
||||||
}
|
}
|
||||||
return net.TCPDestination(v.Address, v.Port)
|
return net.TCPDestination(h.Address, h.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseOption byte
|
type ResponseOption byte
|
||||||
|
@ -85,16 +85,16 @@ const (
|
||||||
ResponseOptionConnectionReuse = ResponseOption(0x01)
|
ResponseOptionConnectionReuse = ResponseOption(0x01)
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v *ResponseOption) Set(option ResponseOption) {
|
func (o *ResponseOption) Set(option ResponseOption) {
|
||||||
*v = (*v | option)
|
*o = (*o | option)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v ResponseOption) Has(option ResponseOption) bool {
|
func (o ResponseOption) Has(option ResponseOption) bool {
|
||||||
return (v & option) == option
|
return (o & option) == option
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ResponseOption) Clear(option ResponseOption) {
|
func (o *ResponseOption) Clear(option ResponseOption) {
|
||||||
*v = (*v & (^option))
|
*o = (*o & (^option))
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseCommand interface{}
|
type ResponseCommand interface{}
|
||||||
|
@ -113,15 +113,15 @@ type CommandSwitchAccount struct {
|
||||||
ValidMin byte
|
ValidMin byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *SecurityConfig) AsSecurity() Security {
|
func (sc *SecurityConfig) AsSecurity() Security {
|
||||||
if v == nil {
|
if sc == nil {
|
||||||
return Security(SecurityType_LEGACY)
|
return Security(SecurityType_LEGACY)
|
||||||
}
|
}
|
||||||
if v.Type == SecurityType_AUTO {
|
if sc.Type == SecurityType_AUTO {
|
||||||
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
|
||||||
return Security(SecurityType_AES128_GCM)
|
return Security(SecurityType_AES128_GCM)
|
||||||
}
|
}
|
||||||
return Security(SecurityType_CHACHA20_POLY1305)
|
return Security(SecurityType_CHACHA20_POLY1305)
|
||||||
}
|
}
|
||||||
return NormSecurity(Security(v.Type))
|
return NormSecurity(Security(sc.Type))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue