2020-11-25 14:01:53 +03:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2020-12-27 22:20:39 +03:00
|
|
|
"github.com/xtls/xray-core/common/net/cnc"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common/signal/done"
|
|
|
|
"github.com/xtls/xray-core/transport"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// OutboundListener is a net.Listener for listening gRPC connections.
|
|
|
|
type OutboundListener struct {
|
|
|
|
buffer chan net.Conn
|
|
|
|
done *done.Instance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *OutboundListener) add(conn net.Conn) {
|
|
|
|
select {
|
|
|
|
case l.buffer <- conn:
|
|
|
|
case <-l.done.Wait():
|
|
|
|
conn.Close()
|
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements net.Listener.
|
|
|
|
func (l *OutboundListener) Accept() (net.Conn, error) {
|
|
|
|
select {
|
|
|
|
case <-l.done.Wait():
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("listen closed")
|
2020-11-25 14:01:53 +03:00
|
|
|
case c := <-l.buffer:
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 07:39:58 +03:00
|
|
|
// Close implements net.Listener.
|
2020-11-25 14:01:53 +03:00
|
|
|
func (l *OutboundListener) Close() error {
|
|
|
|
common.Must(l.done.Close())
|
|
|
|
L:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-l.buffer:
|
|
|
|
c.Close()
|
|
|
|
default:
|
|
|
|
break L
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr implements net.Listener.
|
|
|
|
func (l *OutboundListener) Addr() net.Addr {
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.IP{0, 0, 0, 0},
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outbound is a outbound.Handler that handles gRPC connections.
|
|
|
|
type Outbound struct {
|
|
|
|
tag string
|
|
|
|
listener *OutboundListener
|
|
|
|
access sync.RWMutex
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch implements outbound.Handler.
|
|
|
|
func (co *Outbound) Dispatch(ctx context.Context, link *transport.Link) {
|
|
|
|
co.access.RLock()
|
|
|
|
|
|
|
|
if co.closed {
|
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
|
|
|
co.access.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
closeSignal := done.New()
|
2020-12-27 22:20:39 +03:00
|
|
|
c := cnc.NewConnection(cnc.ConnectionInputMulti(link.Writer), cnc.ConnectionOutputMulti(link.Reader), cnc.ConnectionOnClose(closeSignal))
|
2020-11-25 14:01:53 +03:00
|
|
|
co.listener.add(c)
|
|
|
|
co.access.RUnlock()
|
|
|
|
<-closeSignal.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag implements outbound.Handler.
|
|
|
|
func (co *Outbound) Tag() string {
|
|
|
|
return co.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements common.Runnable.
|
|
|
|
func (co *Outbound) Start() error {
|
|
|
|
co.access.Lock()
|
|
|
|
co.closed = false
|
|
|
|
co.access.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements common.Closable.
|
|
|
|
func (co *Outbound) Close() error {
|
|
|
|
co.access.Lock()
|
|
|
|
defer co.access.Unlock()
|
|
|
|
|
|
|
|
co.closed = true
|
|
|
|
return co.listener.Close()
|
|
|
|
}
|