2018-06-10 14:50:03 +00:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2018-06-16 13:34:13 +00:00
|
|
|
"time"
|
2018-06-10 14:50:03 +00:00
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
InboundAdapter "github.com/Dreamacro/clash/adapters/inbound"
|
2018-06-10 14:50:03 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-11-21 05:47:46 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
"gopkg.in/eapache/channels.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tunnel *Tunnel
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
// Tunnel handle proxy socket and HTTP/SOCKS socket
|
2018-06-10 14:50:03 +00:00
|
|
|
type Tunnel struct {
|
|
|
|
queue *channels.InfiniteChannel
|
|
|
|
rules []C.Rule
|
2018-07-18 13:50:16 +00:00
|
|
|
proxies map[string]C.Proxy
|
2018-06-14 16:49:52 +00:00
|
|
|
configLock *sync.RWMutex
|
2018-06-17 14:41:32 +00:00
|
|
|
traffic *C.Traffic
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
// Outbound Rule
|
2018-11-21 05:47:46 +00:00
|
|
|
mode Mode
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
// Add request to queue
|
2018-06-10 14:50:03 +00:00
|
|
|
func (t *Tunnel) Add(req C.ServerAdapter) {
|
|
|
|
t.queue.In() <- req
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
// Traffic return traffic of all connections
|
2018-06-18 03:31:49 +00:00
|
|
|
func (t *Tunnel) Traffic() *C.Traffic {
|
|
|
|
return t.traffic
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
// Rules return all rules
|
|
|
|
func (t *Tunnel) Rules() []C.Rule {
|
|
|
|
return t.rules
|
2018-06-18 03:31:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
// UpdateRules handle update rules
|
|
|
|
func (t *Tunnel) UpdateRules(rules []C.Rule) {
|
|
|
|
t.configLock.Lock()
|
|
|
|
t.rules = rules
|
|
|
|
t.configLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxies return all proxies
|
|
|
|
func (t *Tunnel) Proxies() map[string]C.Proxy {
|
|
|
|
return t.proxies
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProxies handle update proxies
|
|
|
|
func (t *Tunnel) UpdateProxies(proxies map[string]C.Proxy) {
|
|
|
|
t.configLock.Lock()
|
|
|
|
t.proxies = proxies
|
|
|
|
t.configLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode return current mode
|
|
|
|
func (t *Tunnel) Mode() Mode {
|
|
|
|
return t.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMode change the mode of tunnel
|
|
|
|
func (t *Tunnel) SetMode(mode Mode) {
|
|
|
|
t.mode = mode
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tunnel) process() {
|
|
|
|
queue := t.queue.Out()
|
|
|
|
for {
|
|
|
|
elm := <-queue
|
|
|
|
conn := elm.(C.ServerAdapter)
|
|
|
|
go t.handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
|
|
|
defer localConn.Close()
|
2018-09-30 04:25:52 +00:00
|
|
|
metadata := localConn.Metadata()
|
2018-07-12 15:28:38 +00:00
|
|
|
|
|
|
|
var proxy C.Proxy
|
|
|
|
switch t.mode {
|
2018-11-21 05:47:46 +00:00
|
|
|
case Direct:
|
2018-07-18 13:50:16 +00:00
|
|
|
proxy = t.proxies["DIRECT"]
|
2018-11-21 05:47:46 +00:00
|
|
|
case Global:
|
2018-08-04 15:04:16 +00:00
|
|
|
proxy = t.proxies["GLOBAL"]
|
2018-07-12 15:28:38 +00:00
|
|
|
// Rule
|
|
|
|
default:
|
2018-09-30 04:25:52 +00:00
|
|
|
proxy = t.match(metadata)
|
2018-07-12 15:28:38 +00:00
|
|
|
}
|
2018-09-30 04:25:52 +00:00
|
|
|
remoConn, err := proxy.Generator(metadata)
|
2018-06-10 14:50:03 +00:00
|
|
|
if err != nil {
|
2018-11-21 05:47:46 +00:00
|
|
|
log.Warnln("Proxy connect error: %s", err.Error())
|
2018-06-10 14:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer remoConn.Close()
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
switch adapter := localConn.(type) {
|
2018-09-30 04:25:52 +00:00
|
|
|
case *InboundAdapter.HTTPAdapter:
|
2018-07-25 16:04:59 +00:00
|
|
|
t.handleHTTP(adapter, remoConn)
|
2018-09-30 04:25:52 +00:00
|
|
|
case *InboundAdapter.SocketAdapter:
|
2018-07-25 16:04:59 +00:00
|
|
|
t.handleSOCKS(adapter, remoConn)
|
|
|
|
}
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
func (t *Tunnel) match(metadata *C.Metadata) C.Proxy {
|
2018-06-14 16:49:52 +00:00
|
|
|
t.configLock.RLock()
|
|
|
|
defer t.configLock.RUnlock()
|
|
|
|
|
2018-06-10 14:50:03 +00:00
|
|
|
for _, rule := range t.rules {
|
2018-09-30 04:25:52 +00:00
|
|
|
if rule.IsMatch(metadata) {
|
2018-07-18 13:50:16 +00:00
|
|
|
a, ok := t.proxies[rule.Adapter()]
|
2018-06-10 14:50:03 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-21 05:47:46 +00:00
|
|
|
log.Infoln("%v match %s using %s", metadata.String(), rule.RuleType().String(), rule.Adapter())
|
2018-06-10 14:50:03 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
2018-11-21 05:47:46 +00:00
|
|
|
log.Infoln("%v doesn't match any rule using DIRECT", metadata.String())
|
2018-07-18 13:50:16 +00:00
|
|
|
return t.proxies["DIRECT"]
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTunnel() *Tunnel {
|
2018-07-25 16:04:59 +00:00
|
|
|
return &Tunnel{
|
2018-06-10 14:50:03 +00:00
|
|
|
queue: channels.NewInfiniteChannel(),
|
2018-07-18 13:50:16 +00:00
|
|
|
proxies: make(map[string]C.Proxy),
|
2018-06-14 16:49:52 +00:00
|
|
|
configLock: &sync.RWMutex{},
|
2018-06-17 14:41:32 +00:00
|
|
|
traffic: C.NewTraffic(time.Second),
|
2018-11-21 05:47:46 +00:00
|
|
|
mode: Rule,
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
// Instance return singleton instance of Tunnel
|
|
|
|
func Instance() *Tunnel {
|
2018-06-10 14:50:03 +00:00
|
|
|
once.Do(func() {
|
|
|
|
tunnel = newTunnel()
|
2018-11-21 05:47:46 +00:00
|
|
|
go tunnel.process()
|
2018-06-10 14:50:03 +00:00
|
|
|
})
|
|
|
|
return tunnel
|
|
|
|
}
|