Clash.Meta/tunnel/tunnel.go

164 lines
3.3 KiB
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package tunnel
import (
"fmt"
"strings"
"sync"
"github.com/Dreamacro/clash/adapters"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/observable"
R "github.com/Dreamacro/clash/rules"
"gopkg.in/eapache/channels.v1"
)
var (
tunnel *Tunnel
once sync.Once
)
type Tunnel struct {
queue *channels.InfiniteChannel
rules []C.Rule
proxys map[string]C.Proxy
observable *observable.Observable
logCh chan interface{}
2018-06-15 00:49:52 +08:00
configLock *sync.RWMutex
2018-06-10 22:50:03 +08:00
}
func (t *Tunnel) Add(req C.ServerAdapter) {
t.queue.In() <- req
}
func (t *Tunnel) UpdateConfig() (err error) {
cfg, err := C.GetConfig()
if err != nil {
return
}
// clear proxys and rules
proxys := make(map[string]C.Proxy)
rules := []C.Rule{}
proxysConfig := cfg.Section("Proxy")
rulesConfig := cfg.Section("Rule")
2018-06-10 22:50:03 +08:00
// parse proxy
for _, key := range proxysConfig.Keys() {
2018-06-10 22:50:03 +08:00
proxy := strings.Split(key.Value(), ",")
if len(proxy) == 0 {
continue
}
proxy = trimArr(proxy)
switch proxy[0] {
// ss, server, port, cipter, password
case "ss":
if len(proxy) < 5 {
continue
}
ssURL := fmt.Sprintf("ss://%s:%s@%s:%s", proxy[3], proxy[4], proxy[1], proxy[2])
ss, err := adapters.NewShadowSocks(ssURL)
if err != nil {
return err
}
proxys[key.Name()] = ss
2018-06-10 22:50:03 +08:00
}
}
// init proxy
proxys["DIRECT"] = adapters.NewDirect()
proxys["REJECT"] = adapters.NewReject()
2018-06-10 22:50:03 +08:00
// parse rules
for _, key := range rulesConfig.Keys() {
2018-06-10 22:50:03 +08:00
rule := strings.Split(key.Name(), ",")
if len(rule) < 3 {
continue
}
rule = trimArr(rule)
switch rule[0] {
case "DOMAIN-SUFFIX":
rules = append(rules, R.NewDomainSuffix(rule[1], rule[2]))
2018-06-10 22:50:03 +08:00
case "DOMAIN-KEYWORD":
rules = append(rules, R.NewDomainKeyword(rule[1], rule[2]))
2018-06-10 22:50:03 +08:00
case "GEOIP":
rules = append(rules, R.NewGEOIP(rule[1], rule[2]))
2018-06-10 22:50:03 +08:00
case "IP-CIDR", "IP-CIDR6":
rules = append(rules, R.NewIPCIDR(rule[1], rule[2]))
2018-06-10 22:50:03 +08:00
case "FINAL":
rules = append(rules, R.NewFinal(rule[2]))
2018-06-10 22:50:03 +08:00
}
}
t.configLock.Lock()
defer t.configLock.Unlock()
t.proxys = proxys
t.rules = rules
2018-06-10 22:50:03 +08:00
return nil
}
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()
proxy := t.match(addr)
remoConn, err := proxy.Generator(addr)
if err != nil {
t.logCh <- newLog(WARNING, "Proxy connect error: %s", err.Error())
return
}
defer remoConn.Close()
2018-06-14 01:00:58 +08:00
localConn.Connect(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) {
a, ok := t.proxys[rule.Adapter()]
if !ok {
continue
}
2018-06-11 18:36:39 +08:00
t.logCh <- newLog(INFO, "%v match %d using %s", addr.String(), rule.RuleType(), rule.Adapter())
2018-06-10 22:50:03 +08:00
return a
}
}
t.logCh <- newLog(INFO, "don't find, direct")
return t.proxys["DIRECT"]
}
func newTunnel() *Tunnel {
logCh := make(chan interface{})
tunnel := &Tunnel{
queue: channels.NewInfiniteChannel(),
proxys: make(map[string]C.Proxy),
observable: observable.NewObservable(logCh),
logCh: logCh,
2018-06-15 00:49:52 +08:00
configLock: &sync.RWMutex{},
2018-06-10 22:50:03 +08:00
}
go tunnel.process()
go tunnel.subscribeLogs()
return tunnel
}
func GetInstance() *Tunnel {
once.Do(func() {
tunnel = newTunnel()
})
return tunnel
}