mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-20 08:16:55 -05:00
refactor
This commit is contained in:
parent
94405dd467
commit
c9c2338f05
@ -12,6 +12,7 @@ import (
|
|||||||
"v2ray.com/core/app/proxyman"
|
"v2ray.com/core/app/proxyman"
|
||||||
"v2ray.com/core/app/router"
|
"v2ray.com/core/app/router"
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
"v2ray.com/core/proxy"
|
"v2ray.com/core/proxy"
|
||||||
"v2ray.com/core/transport/ray"
|
"v2ray.com/core/transport/ray"
|
||||||
@ -21,11 +22,13 @@ var (
|
|||||||
errSniffingTimeout = newError("timeout on sniffing")
|
errSniffingTimeout = newError("timeout on sniffing")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultDispatcher is a default implementation of Dispatcher.
|
||||||
type DefaultDispatcher struct {
|
type DefaultDispatcher struct {
|
||||||
ohm proxyman.OutboundHandlerManager
|
ohm proxyman.OutboundHandlerManager
|
||||||
router *router.Router
|
router *router.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewDefaultDispatcher create a new DefaultDispatcher.
|
||||||
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
|
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
|
||||||
space := app.SpaceFromContext(ctx)
|
space := app.SpaceFromContext(ctx)
|
||||||
if space == nil {
|
if space == nil {
|
||||||
@ -43,21 +46,17 @@ func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*Defa
|
|||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (DefaultDispatcher) Start() error {
|
func (*DefaultDispatcher) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (DefaultDispatcher) Close() {}
|
func (*DefaultDispatcher) Close() {}
|
||||||
|
|
||||||
func (DefaultDispatcher) Interface() interface{} {
|
func (*DefaultDispatcher) Interface() interface{} {
|
||||||
return (*dispatcher.Interface)(nil)
|
return (*dispatcher.Interface)(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
type domainOrError struct {
|
// Dispatch implements Dispatcher.Interface.
|
||||||
domain string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
|
func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destination) (ray.InboundRay, error) {
|
||||||
if !destination.IsValid() {
|
if !destination.IsValid() {
|
||||||
panic("Dispatcher: Invalid destination.")
|
panic("Dispatcher: Invalid destination.")
|
||||||
@ -70,15 +69,13 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
|
|||||||
go d.routedDispatch(ctx, outbound, destination)
|
go d.routedDispatch(ctx, outbound, destination)
|
||||||
} else {
|
} else {
|
||||||
go func() {
|
go func() {
|
||||||
done := make(chan domainOrError)
|
domain, err := snifer(ctx, sniferList, outbound)
|
||||||
go snifer(ctx, sniferList, outbound, done)
|
if err != nil {
|
||||||
de := <-done
|
log.Trace(newError("failed to snif").Base(err))
|
||||||
if de.err != nil {
|
|
||||||
log.Trace(newError("failed to snif").Base(de.err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Trace(newError("sniffed domain: ", de.domain))
|
log.Trace(newError("sniffed domain: ", domain))
|
||||||
destination.Address = net.ParseAddress(de.domain)
|
destination.Address = net.ParseAddress(domain)
|
||||||
ctx = proxy.ContextWithTarget(ctx, destination)
|
ctx = proxy.ContextWithTarget(ctx, destination)
|
||||||
d.routedDispatch(ctx, outbound, destination)
|
d.routedDispatch(ctx, outbound, destination)
|
||||||
}()
|
}()
|
||||||
@ -86,31 +83,24 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
|
|||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay, done chan<- domainOrError) {
|
func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound ray.OutboundRay) (string, error) {
|
||||||
payload := make([]byte, 2048)
|
payload := buf.New()
|
||||||
|
defer payload.Release()
|
||||||
|
|
||||||
totalAttempt := 0
|
totalAttempt := 0
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
done <- domainOrError{
|
return "", ctx.Err()
|
||||||
domain: "",
|
|
||||||
err: ctx.Err(),
|
|
||||||
}
|
|
||||||
return
|
|
||||||
case <-time.After(time.Millisecond * 100):
|
case <-time.After(time.Millisecond * 100):
|
||||||
totalAttempt++
|
totalAttempt++
|
||||||
if totalAttempt > 5 {
|
if totalAttempt > 5 {
|
||||||
done <- domainOrError{
|
return "", errSniffingTimeout
|
||||||
domain: "",
|
|
||||||
err: errSniffingTimeout,
|
|
||||||
}
|
}
|
||||||
return
|
outbound.OutboundInput().Peek(payload)
|
||||||
}
|
if payload.IsEmpty() {
|
||||||
mb := outbound.OutboundInput().Peek()
|
|
||||||
if mb.IsEmpty() {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
nBytes := mb.Copy(payload)
|
|
||||||
for _, protocol := range sniferList {
|
for _, protocol := range sniferList {
|
||||||
var f func([]byte) (string, error)
|
var f func([]byte) (string, error)
|
||||||
switch protocol {
|
switch protocol {
|
||||||
@ -122,21 +112,13 @@ func snifer(ctx context.Context, sniferList []proxyman.KnownProtocols, outbound
|
|||||||
panic("Unsupported protocol")
|
panic("Unsupported protocol")
|
||||||
}
|
}
|
||||||
|
|
||||||
domain, err := f(payload[:nBytes])
|
domain, err := f(payload.Bytes())
|
||||||
if err != ErrMoreData {
|
if err != ErrMoreData {
|
||||||
done <- domainOrError{
|
return domain, err
|
||||||
domain: domain,
|
|
||||||
err: err,
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nBytes == 2048 {
|
if payload.IsFull() {
|
||||||
done <- domainOrError{
|
return "", ErrInvalidData
|
||||||
domain: "",
|
|
||||||
err: ErrInvalidData,
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,8 @@ func SniffHTTP(b []byte) (string, error) {
|
|||||||
key := strings.ToLower(string(parts[0]))
|
key := strings.ToLower(string(parts[0]))
|
||||||
value := strings.ToLower(string(bytes.Trim(parts[1], " ")))
|
value := strings.ToLower(string(bytes.Trim(parts[1], " ")))
|
||||||
if key == "host" {
|
if key == "host" {
|
||||||
return value, nil
|
domain := strings.Split(value, ":")
|
||||||
|
return domain[0], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", ErrMoreData
|
return "", ErrMoreData
|
||||||
@ -60,11 +61,11 @@ func ReadClientHello(data []byte) (string, error) {
|
|||||||
if len(data) < 42 {
|
if len(data) < 42 {
|
||||||
return "", ErrMoreData
|
return "", ErrMoreData
|
||||||
}
|
}
|
||||||
sessionIdLen := int(data[38])
|
sessionIDLen := int(data[38])
|
||||||
if sessionIdLen > 32 || len(data) < 39+sessionIdLen {
|
if sessionIDLen > 32 || len(data) < 39+sessionIDLen {
|
||||||
return "", ErrInvalidData
|
return "", ErrInvalidData
|
||||||
}
|
}
|
||||||
data = data[39+sessionIdLen:]
|
data = data[39+sessionIDLen:]
|
||||||
if len(data) < 2 {
|
if len(data) < 2 {
|
||||||
return "", ErrMoreData
|
return "", ErrMoreData
|
||||||
}
|
}
|
||||||
|
@ -75,11 +75,13 @@ func (s *Stream) getData() (buf.MultiBuffer, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stream) Peek() buf.MultiBuffer {
|
func (s *Stream) Peek(b *buf.Buffer) {
|
||||||
s.access.RLock()
|
s.access.RLock()
|
||||||
defer s.access.RUnlock()
|
defer s.access.RUnlock()
|
||||||
|
|
||||||
return s.data
|
b.Reset(func(data []byte) (int, error) {
|
||||||
|
return s.data.Copy(data), nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stream) Read() (buf.MultiBuffer, error) {
|
func (s *Stream) Read() (buf.MultiBuffer, error) {
|
||||||
|
@ -42,7 +42,7 @@ type InputStream interface {
|
|||||||
buf.Reader
|
buf.Reader
|
||||||
buf.TimeoutReader
|
buf.TimeoutReader
|
||||||
RayStream
|
RayStream
|
||||||
Peek() buf.MultiBuffer
|
Peek(*buf.Buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutputStream interface {
|
type OutputStream interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user