2018-06-10 22:50:03 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-06-16 21:34:13 +08:00
|
|
|
"strconv"
|
2018-06-10 22:50:03 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-06-16 21:34:13 +08:00
|
|
|
"time"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
"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
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies map[string]C.Proxy
|
2018-06-10 22:50:03 +08:00
|
|
|
observable *observable.Observable
|
|
|
|
logCh chan interface{}
|
2018-06-15 00:49:52 +08:00
|
|
|
configLock *sync.RWMutex
|
2018-06-17 22:41:32 +08:00
|
|
|
traffic *C.Traffic
|
2018-07-12 23:28:38 +08:00
|
|
|
mode Mode
|
|
|
|
selector *adapters.Selector
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tunnel) Add(req C.ServerAdapter) {
|
|
|
|
t.queue.In() <- req
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
func (t *Tunnel) Traffic() *C.Traffic {
|
|
|
|
return t.traffic
|
|
|
|
}
|
|
|
|
|
2018-06-20 22:41:02 +08:00
|
|
|
func (t *Tunnel) Config() ([]C.Rule, map[string]C.Proxy) {
|
2018-07-18 21:50:16 +08:00
|
|
|
return t.rules, t.proxies
|
2018-06-20 22:41:02 +08:00
|
|
|
}
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
func (t *Tunnel) Log() *observable.Observable {
|
|
|
|
return t.observable
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
func (t *Tunnel) SetMode(mode Mode) {
|
|
|
|
t.mode = mode
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
func (t *Tunnel) GetMode() Mode {
|
|
|
|
return t.mode
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func (t *Tunnel) UpdateConfig() (err error) {
|
|
|
|
cfg, err := C.GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:50:16 +08:00
|
|
|
// empty proxies and rules
|
|
|
|
proxies := make(map[string]C.Proxy)
|
2018-06-16 15:41:26 +08:00
|
|
|
rules := []C.Rule{}
|
2018-06-13 13:38:46 +08:00
|
|
|
|
2018-07-18 21:50:16 +08:00
|
|
|
proxiesConfig := cfg.Section("Proxy")
|
2018-06-16 15:41:26 +08:00
|
|
|
rulesConfig := cfg.Section("Rule")
|
2018-06-16 21:34:13 +08:00
|
|
|
groupsConfig := cfg.Section("Proxy Group")
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
// parse proxy
|
2018-07-18 21:50:16 +08:00
|
|
|
for _, key := range proxiesConfig.Keys() {
|
2018-07-12 23:28:38 +08:00
|
|
|
proxy := key.Strings(",")
|
2018-06-10 22:50:03 +08:00
|
|
|
if len(proxy) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
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])
|
2018-06-17 22:41:32 +08:00
|
|
|
ss, err := adapters.NewShadowSocks(key.Name(), ssURL, t.traffic)
|
2018-06-16 15:41:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies[key.Name()] = ss
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse rules
|
2018-06-16 15:41:26 +08:00
|
|
|
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":
|
2018-06-16 15:41:26 +08:00
|
|
|
rules = append(rules, R.NewDomainSuffix(rule[1], rule[2]))
|
2018-06-10 22:50:03 +08:00
|
|
|
case "DOMAIN-KEYWORD":
|
2018-06-16 15:41:26 +08:00
|
|
|
rules = append(rules, R.NewDomainKeyword(rule[1], rule[2]))
|
2018-06-10 22:50:03 +08:00
|
|
|
case "GEOIP":
|
2018-06-16 15:41:26 +08:00
|
|
|
rules = append(rules, R.NewGEOIP(rule[1], rule[2]))
|
2018-06-10 22:50:03 +08:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
2018-06-16 15:41:26 +08:00
|
|
|
rules = append(rules, R.NewIPCIDR(rule[1], rule[2]))
|
2018-06-10 22:50:03 +08:00
|
|
|
case "FINAL":
|
2018-06-16 15:41:26 +08:00
|
|
|
rules = append(rules, R.NewFinal(rule[2]))
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
// parse proxy groups
|
|
|
|
for _, key := range groupsConfig.Keys() {
|
|
|
|
rule := strings.Split(key.Value(), ",")
|
|
|
|
rule = trimArr(rule)
|
|
|
|
switch rule[0] {
|
|
|
|
case "url-test":
|
2018-07-12 23:28:38 +08:00
|
|
|
if len(rule) < 4 {
|
|
|
|
return fmt.Errorf("URLTest need more than 4 param")
|
|
|
|
}
|
2018-06-16 21:34:13 +08:00
|
|
|
proxyNames := rule[1 : len(rule)-2]
|
|
|
|
delay, _ := strconv.Atoi(rule[len(rule)-1])
|
|
|
|
url := rule[len(rule)-2]
|
|
|
|
var ps []C.Proxy
|
|
|
|
for _, name := range proxyNames {
|
2018-07-18 21:50:16 +08:00
|
|
|
if p, ok := proxies[name]; ok {
|
2018-06-16 21:34:13 +08:00
|
|
|
ps = append(ps, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter, err := adapters.NewURLTest(key.Name(), ps, url, time.Duration(delay)*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Config error: %s", err.Error())
|
|
|
|
}
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies[key.Name()] = adapter
|
2018-07-12 23:28:38 +08:00
|
|
|
case "select":
|
|
|
|
if len(rule) < 3 {
|
|
|
|
return fmt.Errorf("Selector need more than 3 param")
|
|
|
|
}
|
|
|
|
proxyNames := rule[1:]
|
|
|
|
selectProxy := make(map[string]C.Proxy)
|
|
|
|
for _, name := range proxyNames {
|
2018-07-18 21:50:16 +08:00
|
|
|
proxy, exist := proxies[name]
|
2018-07-12 23:28:38 +08:00
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf("Proxy %s not exist", name)
|
|
|
|
}
|
|
|
|
selectProxy[name] = proxy
|
|
|
|
}
|
|
|
|
selector, err := adapters.NewSelector(key.Name(), selectProxy)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Selector create error: %s", err.Error())
|
|
|
|
}
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies[key.Name()] = selector
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:31:36 +08:00
|
|
|
// init proxy
|
2018-07-18 21:50:16 +08:00
|
|
|
proxies["DIRECT"] = adapters.NewDirect(t.traffic)
|
|
|
|
proxies["REJECT"] = adapters.NewReject()
|
2018-06-19 20:31:36 +08:00
|
|
|
|
2018-06-16 15:41:26 +08:00
|
|
|
t.configLock.Lock()
|
|
|
|
defer t.configLock.Unlock()
|
|
|
|
|
2018-06-19 20:31:36 +08:00
|
|
|
// stop url-test
|
2018-07-18 21:50:16 +08:00
|
|
|
for _, elm := range t.proxies {
|
2018-06-19 20:31:36 +08:00
|
|
|
urlTest, ok := elm.(*adapters.URLTest)
|
|
|
|
if ok {
|
|
|
|
urlTest.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:50:16 +08:00
|
|
|
s, err := adapters.NewSelector("Proxy", proxies)
|
2018-07-12 23:28:38 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:50:16 +08:00
|
|
|
t.proxies = proxies
|
2018-06-16 15:41:26 +08:00
|
|
|
t.rules = rules
|
2018-07-12 23:28:38 +08:00
|
|
|
t.selector = s
|
2018-06-16 15:41:26 +08:00
|
|
|
|
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()
|
2018-07-12 23:28:38 +08:00
|
|
|
|
|
|
|
var proxy C.Proxy
|
|
|
|
switch t.mode {
|
|
|
|
case Direct:
|
2018-07-18 21:50:16 +08:00
|
|
|
proxy = t.proxies["DIRECT"]
|
2018-07-12 23:28:38 +08:00
|
|
|
case Global:
|
|
|
|
proxy = t.selector
|
|
|
|
// Rule
|
|
|
|
default:
|
|
|
|
proxy = t.match(addr)
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
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) {
|
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-06-22 13:15:20 +08:00
|
|
|
t.logCh <- newLog(INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter())
|
2018-06-10 22:50:03 +08:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
2018-06-17 22:41:32 +08:00
|
|
|
t.logCh <- newLog(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
|
|
|
}
|
|
|
|
|
|
|
|
func newTunnel() *Tunnel {
|
|
|
|
logCh := make(chan interface{})
|
|
|
|
tunnel := &Tunnel{
|
|
|
|
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-12 23:28:38 +08:00
|
|
|
mode: Rule,
|
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
|
|
|
|
}
|