2018-06-10 22:50:03 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2018-06-16 21:34:13 +08:00
|
|
|
"time"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
LocalAdapter "github.com/Dreamacro/clash/adapters/local"
|
|
|
|
cfg "github.com/Dreamacro/clash/config"
|
2018-06-10 22:50:03 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/observable"
|
|
|
|
|
|
|
|
"gopkg.in/eapache/channels.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tunnel *Tunnel
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Tunnel handle proxy socket and HTTP/SOCKS socket
|
2018-06-10 22:50:03 +08:00
|
|
|
type Tunnel struct {
|
|
|
|
queue *channels.InfiniteChannel
|
|
|
|
rules []C.Rule
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies map[string]C.Proxy
|
2018-06-15 00:49:52 +08:00
|
|
|
configLock *sync.RWMutex
|
2018-06-17 22:41:32 +08:00
|
|
|
traffic *C.Traffic
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
// Outbound Rule
|
2018-08-04 23:04:16 +08:00
|
|
|
mode cfg.Mode
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
// Log
|
|
|
|
logCh chan interface{}
|
|
|
|
observable *observable.Observable
|
|
|
|
logLevel C.LogLevel
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Add request to queue
|
2018-06-10 22:50:03 +08:00
|
|
|
func (t *Tunnel) Add(req C.ServerAdapter) {
|
|
|
|
t.queue.In() <- req
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Traffic return traffic of all connections
|
2018-06-18 11:31:49 +08:00
|
|
|
func (t *Tunnel) Traffic() *C.Traffic {
|
|
|
|
return t.traffic
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Log return clash log stream
|
2018-06-18 11:31:49 +08:00
|
|
|
func (t *Tunnel) Log() *observable.Observable {
|
|
|
|
return t.observable
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func (t *Tunnel) configMonitor(signal chan<- struct{}) {
|
|
|
|
sub := cfg.Instance().Subscribe()
|
|
|
|
signal <- struct{}{}
|
|
|
|
for elm := range sub {
|
|
|
|
event := elm.(*cfg.Event)
|
|
|
|
switch event.Type {
|
|
|
|
case "proxies":
|
|
|
|
proxies := event.Payload.(map[string]C.Proxy)
|
|
|
|
t.configLock.Lock()
|
|
|
|
t.proxies = proxies
|
|
|
|
t.configLock.Unlock()
|
|
|
|
case "rules":
|
|
|
|
rules := event.Payload.([]C.Rule)
|
|
|
|
t.configLock.Lock()
|
|
|
|
t.rules = rules
|
|
|
|
t.configLock.Unlock()
|
|
|
|
case "mode":
|
|
|
|
t.mode = event.Payload.(cfg.Mode)
|
|
|
|
case "log-level":
|
|
|
|
t.logLevel = event.Payload.(C.LogLevel)
|
2018-06-10 22:50:03 +08: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()
|
|
|
|
addr := localConn.Addr()
|
2018-07-12 23:28:38 +08:00
|
|
|
|
|
|
|
var proxy C.Proxy
|
|
|
|
switch t.mode {
|
2018-07-26 00:04:59 +08:00
|
|
|
case cfg.Direct:
|
2018-07-18 21:50:16 +08:00
|
|
|
proxy = t.proxies["DIRECT"]
|
2018-07-26 00:04:59 +08:00
|
|
|
case cfg.Global:
|
2018-08-04 23:04:16 +08:00
|
|
|
proxy = t.proxies["GLOBAL"]
|
2018-07-12 23:28:38 +08:00
|
|
|
// Rule
|
|
|
|
default:
|
|
|
|
proxy = t.match(addr)
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
remoConn, err := proxy.Generator(addr)
|
|
|
|
if err != nil {
|
2018-07-26 00:04:59 +08:00
|
|
|
t.logCh <- newLog(C.WARNING, "Proxy connect error: %s", err.Error())
|
2018-06-10 22:50:03 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer remoConn.Close()
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
switch adapter := localConn.(type) {
|
2018-08-12 16:18:58 +08:00
|
|
|
case *LocalAdapter.HTTPAdapter:
|
2018-07-26 00:04:59 +08:00
|
|
|
t.handleHTTP(adapter, remoConn)
|
|
|
|
case *LocalAdapter.SocksAdapter:
|
|
|
|
t.handleSOCKS(adapter, remoConn)
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tunnel) match(addr *C.Addr) C.Proxy {
|
2018-06-15 00:49:52 +08:00
|
|
|
t.configLock.RLock()
|
|
|
|
defer t.configLock.RUnlock()
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
for _, rule := range t.rules {
|
|
|
|
if rule.IsMatch(addr) {
|
2018-07-18 21:50:16 +08:00
|
|
|
a, ok := t.proxies[rule.Adapter()]
|
2018-06-10 22:50:03 +08:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
t.logCh <- newLog(C.INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter())
|
2018-06-10 22:50:03 +08:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", addr.String())
|
2018-07-18 21:50:16 +08:00
|
|
|
return t.proxies["DIRECT"]
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Run initial task
|
|
|
|
func (t *Tunnel) Run() {
|
|
|
|
go t.process()
|
|
|
|
go t.subscribeLogs()
|
|
|
|
signal := make(chan struct{})
|
|
|
|
go t.configMonitor(signal)
|
|
|
|
<-signal
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func newTunnel() *Tunnel {
|
|
|
|
logCh := make(chan interface{})
|
2018-07-26 00:04:59 +08:00
|
|
|
return &Tunnel{
|
2018-06-10 22:50:03 +08:00
|
|
|
queue: channels.NewInfiniteChannel(),
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies: make(map[string]C.Proxy),
|
2018-06-10 22:50:03 +08:00
|
|
|
observable: observable.NewObservable(logCh),
|
|
|
|
logCh: logCh,
|
2018-06-15 00:49:52 +08:00
|
|
|
configLock: &sync.RWMutex{},
|
2018-06-17 22:41:32 +08:00
|
|
|
traffic: C.NewTraffic(time.Second),
|
2018-07-26 00:04:59 +08:00
|
|
|
mode: cfg.Rule,
|
|
|
|
logLevel: C.INFO,
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
// Instance return singleton instance of Tunnel
|
|
|
|
func Instance() *Tunnel {
|
2018-06-10 22:50:03 +08:00
|
|
|
once.Do(func() {
|
|
|
|
tunnel = newTunnel()
|
|
|
|
})
|
|
|
|
return tunnel
|
|
|
|
}
|