2017-02-07 20:11:47 +00:00
|
|
|
package mux
|
|
|
|
|
2017-03-31 22:53:01 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
|
|
|
"v2ray.com/core/app/log"
|
2017-03-31 22:53:01 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-04-02 07:48:30 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2017-03-31 22:53:01 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/ray"
|
|
|
|
)
|
2017-02-07 20:11:47 +00:00
|
|
|
|
2017-03-26 23:47:01 +00:00
|
|
|
const (
|
|
|
|
maxParallel = 8
|
|
|
|
maxTotal = 128
|
|
|
|
)
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
type manager interface {
|
|
|
|
remove(id uint16)
|
|
|
|
}
|
|
|
|
|
|
|
|
type session struct {
|
2017-03-31 22:53:01 +00:00
|
|
|
sync.Mutex
|
2017-04-02 07:48:30 +00:00
|
|
|
input ray.InputStream
|
|
|
|
output ray.OutputStream
|
|
|
|
parent manager
|
2017-03-31 22:53:01 +00:00
|
|
|
id uint16
|
|
|
|
uplinkClosed bool
|
|
|
|
downlinkClosed bool
|
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (s *session) checkAndRemove() {
|
2017-03-31 22:53:01 +00:00
|
|
|
s.Lock()
|
|
|
|
if s.uplinkClosed && s.downlinkClosed {
|
|
|
|
s.parent.remove(s.id)
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (s *session) closeUplink() {
|
2017-03-31 22:53:01 +00:00
|
|
|
s.Lock()
|
|
|
|
s.uplinkClosed = true
|
|
|
|
s.Unlock()
|
|
|
|
s.checkAndRemove()
|
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (s *session) closeDownlink() {
|
2017-03-31 22:53:01 +00:00
|
|
|
s.Lock()
|
|
|
|
s.downlinkClosed = true
|
|
|
|
s.Unlock()
|
|
|
|
s.checkAndRemove()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
access sync.RWMutex
|
|
|
|
count uint16
|
2017-04-02 07:48:30 +00:00
|
|
|
sessions map[uint16]*session
|
2017-03-31 22:53:01 +00:00
|
|
|
inboundRay ray.InboundRay
|
2017-04-02 07:48:30 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
var muxCoolDestination = net.TCPDestination(net.DomainAddress("v1.mux.cool"), net.Port(9527))
|
2017-03-31 22:53:01 +00:00
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func NewClient(p proxy.Outbound, dialer proxy.Dialer) (*Client, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ctx = proxy.ContextWithTarget(ctx, muxCoolDestination)
|
|
|
|
pipe := ray.NewRay(ctx)
|
|
|
|
err := p.Process(ctx, pipe, dialer)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{
|
|
|
|
sessions: make(map[uint16]*session, 256),
|
|
|
|
inboundRay: pipe,
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
}, nil
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (m *Client) isFullyOccupied() bool {
|
2017-03-31 22:53:01 +00:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
return len(m.sessions) >= maxParallel
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 22:53:01 +00:00
|
|
|
func (m *Client) remove(id uint16) {
|
|
|
|
m.access.Lock()
|
2017-04-02 07:48:30 +00:00
|
|
|
defer m.access.Unlock()
|
|
|
|
|
2017-03-31 22:53:01 +00:00
|
|
|
delete(m.sessions, id)
|
2017-04-02 07:48:30 +00:00
|
|
|
|
|
|
|
if len(m.sessions) == 0 {
|
|
|
|
m.cancel()
|
|
|
|
m.inboundRay.InboundInput().Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Client) Closed() bool {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2017-03-26 23:47:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (m *Client) fetchInput(ctx context.Context, s *session) {
|
2017-03-31 22:53:01 +00:00
|
|
|
dest, _ := proxy.TargetFromContext(ctx)
|
2017-04-02 11:43:24 +00:00
|
|
|
writer := &Writer{
|
2017-03-31 22:53:01 +00:00
|
|
|
dest: dest,
|
2017-04-02 07:48:30 +00:00
|
|
|
id: s.id,
|
2017-03-31 22:53:01 +00:00
|
|
|
writer: m.inboundRay.InboundInput(),
|
|
|
|
}
|
|
|
|
_, timer := signal.CancelAfterInactivity(ctx, time.Minute*5)
|
2017-04-02 07:48:30 +00:00
|
|
|
buf.PipeUntilEOF(timer, s.input, writer)
|
2017-03-31 22:53:01 +00:00
|
|
|
writer.Close()
|
2017-04-02 07:48:30 +00:00
|
|
|
s.closeUplink()
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
func (m *Client) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) bool {
|
2017-03-31 22:53:01 +00:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
if len(m.sessions) >= maxParallel {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.count >= maxTotal {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-03-31 22:53:01 +00:00
|
|
|
m.count++
|
|
|
|
id := m.count
|
2017-04-02 07:48:30 +00:00
|
|
|
s := &session{
|
|
|
|
input: outboundRay.OutboundInput(),
|
|
|
|
output: outboundRay.OutboundOutput(),
|
|
|
|
parent: m,
|
|
|
|
id: id,
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
2017-04-02 07:48:30 +00:00
|
|
|
m.sessions[id] = s
|
|
|
|
go m.fetchInput(ctx, s)
|
|
|
|
return true
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Client) fetchOutput() {
|
|
|
|
reader := NewReader(m.inboundRay.InboundOutput())
|
|
|
|
for {
|
|
|
|
meta, err := reader.ReadMetadata()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m.access.RLock()
|
2017-04-02 07:48:30 +00:00
|
|
|
s, found := m.sessions[meta.SessionID]
|
2017-03-31 22:53:01 +00:00
|
|
|
m.access.RUnlock()
|
|
|
|
if found && meta.SessionStatus == SessionStatusEnd {
|
2017-04-02 07:48:30 +00:00
|
|
|
s.closeDownlink()
|
|
|
|
s.output.Close()
|
2017-03-31 22:53:01 +00:00
|
|
|
}
|
|
|
|
if !meta.Option.Has(OptionData) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
data, more, err := reader.Read()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if found {
|
2017-04-02 07:48:30 +00:00
|
|
|
if err := s.output.Write(data); err != nil {
|
2017-03-31 22:53:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !more {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
2017-04-02 07:48:30 +00:00
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
dispatcher dispatcher.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
|
|
|
|
if dest != muxCoolDestination {
|
|
|
|
return s.dispatcher.Dispatch(ctx, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
ray := ray.NewRay(ctx)
|
|
|
|
|
|
|
|
return ray, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerWorker struct {
|
|
|
|
dispatcher dispatcher.Interface
|
|
|
|
outboundRay ray.OutboundRay
|
|
|
|
sessions map[uint16]*session
|
|
|
|
access sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ServerWorker) remove(id uint16) {
|
|
|
|
w.access.Lock()
|
|
|
|
delete(w.sessions, id)
|
|
|
|
w.access.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ServerWorker) handle(ctx context.Context, s *session) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
data, err := s.input.Read()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.outboundRay.OutboundOutput().Write(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *ServerWorker) run(ctx context.Context) {
|
|
|
|
input := w.outboundRay.OutboundInput()
|
|
|
|
reader := NewReader(input)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := reader.ReadMetadata()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.access.RLock()
|
|
|
|
s, found := w.sessions[meta.SessionID]
|
|
|
|
w.access.RUnlock()
|
|
|
|
|
|
|
|
if found && meta.SessionStatus == SessionStatusEnd {
|
|
|
|
s.closeUplink()
|
|
|
|
s.output.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.SessionStatus == SessionStatusNew {
|
|
|
|
inboundRay, err := w.dispatcher.Dispatch(ctx, meta.Target)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Proxyman|Mux: Failed to dispatch request: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s = &session{
|
|
|
|
input: inboundRay.InboundOutput(),
|
|
|
|
output: inboundRay.InboundInput(),
|
|
|
|
parent: w,
|
|
|
|
id: meta.SessionID,
|
|
|
|
}
|
|
|
|
w.access.Lock()
|
|
|
|
w.sessions[meta.SessionID] = s
|
|
|
|
w.access.Unlock()
|
|
|
|
go w.handle(ctx, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Option.Has(OptionData) {
|
|
|
|
for {
|
|
|
|
data, more, err := reader.Read()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
s.output.Write(data)
|
|
|
|
}
|
|
|
|
if !more {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|