2018-06-10 22:50:03 +08:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-02-02 20:47:38 +08:00
|
|
|
"fmt"
|
2018-12-05 21:13:29 +08:00
|
|
|
"net"
|
2018-06-10 22:50:03 +08:00
|
|
|
"sync"
|
2018-06-16 21:34:13 +08:00
|
|
|
"time"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
InboundAdapter "github.com/Dreamacro/clash/adapters/inbound"
|
2019-10-11 20:11:18 +08:00
|
|
|
"github.com/Dreamacro/clash/component/nat"
|
2018-06-10 22:50:03 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-12-05 21:13:29 +08:00
|
|
|
"github.com/Dreamacro/clash/dns"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2019-02-02 20:47:38 +08:00
|
|
|
channels "gopkg.in/eapache/channels.v1"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tunnel *Tunnel
|
|
|
|
once sync.Once
|
2019-10-11 20:11:18 +08:00
|
|
|
|
|
|
|
// default timeout for UDP session
|
|
|
|
udpTimeout = 60 * time.Second
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2019-04-23 23:29:36 +08:00
|
|
|
// Tunnel handle relay inbound proxy and outbound proxy
|
2018-06-10 22:50:03 +08:00
|
|
|
type Tunnel struct {
|
2019-10-11 20:11:18 +08:00
|
|
|
tcpQueue *channels.InfiniteChannel
|
|
|
|
udpQueue *channels.InfiniteChannel
|
|
|
|
natTable *nat.Table
|
2019-04-24 12:02:52 +08:00
|
|
|
rules []C.Rule
|
|
|
|
proxies map[string]C.Proxy
|
2019-10-27 21:44:07 +08:00
|
|
|
configMux sync.RWMutex
|
2019-04-24 12:02:52 +08:00
|
|
|
|
|
|
|
// experimental features
|
|
|
|
ignoreResolveFail bool
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
// Outbound Rule
|
2018-11-21 13:47:46 +08:00
|
|
|
mode Mode
|
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) {
|
2019-10-11 20:11:18 +08:00
|
|
|
switch req.Metadata().NetWork {
|
|
|
|
case C.TCP:
|
|
|
|
t.tcpQueue.In() <- req
|
|
|
|
case C.UDP:
|
|
|
|
t.udpQueue.In() <- req
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// Rules return all rules
|
|
|
|
func (t *Tunnel) Rules() []C.Rule {
|
|
|
|
return t.rules
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// UpdateRules handle update rules
|
|
|
|
func (t *Tunnel) UpdateRules(rules []C.Rule) {
|
2019-04-24 12:02:52 +08:00
|
|
|
t.configMux.Lock()
|
2018-11-21 13:47:46 +08:00
|
|
|
t.rules = rules
|
2019-04-24 12:02:52 +08:00
|
|
|
t.configMux.Unlock()
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-04-24 12:02:52 +08:00
|
|
|
t.configMux.Lock()
|
2018-11-21 13:47:46 +08:00
|
|
|
t.proxies = proxies
|
2019-04-24 12:02:52 +08:00
|
|
|
t.configMux.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateExperimental handle update experimental config
|
|
|
|
func (t *Tunnel) UpdateExperimental(ignoreResolveFail bool) {
|
|
|
|
t.configMux.Lock()
|
|
|
|
t.ignoreResolveFail = ignoreResolveFail
|
|
|
|
t.configMux.Unlock()
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tunnel) process() {
|
2019-10-11 20:11:18 +08:00
|
|
|
go func() {
|
|
|
|
queue := t.udpQueue.Out()
|
|
|
|
for elm := range queue {
|
|
|
|
conn := elm.(C.ServerAdapter)
|
|
|
|
t.handleUDPConn(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
queue := t.tcpQueue.Out()
|
|
|
|
for elm := range queue {
|
2018-06-10 22:50:03 +08:00
|
|
|
conn := elm.(C.ServerAdapter)
|
2019-10-11 20:11:18 +08:00
|
|
|
go t.handleTCPConn(conn)
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
func (t *Tunnel) resolveIP(host string) (net.IP, error) {
|
2019-06-29 00:58:59 +08:00
|
|
|
return dns.ResolveIP(host)
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:20:42 +08:00
|
|
|
func (t *Tunnel) needLookupIP(metadata *C.Metadata) bool {
|
2019-06-29 00:58:59 +08:00
|
|
|
return dns.DefaultResolver != nil && (dns.DefaultResolver.IsMapping() || dns.DefaultResolver.IsFakeIP()) && metadata.Host == "" && metadata.DstIP != nil
|
2019-02-11 15:44:42 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
func (t *Tunnel) resolveMetadata(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
2019-10-28 00:02:23 +08:00
|
|
|
// handle host equal IP string
|
|
|
|
if ip := net.ParseIP(metadata.Host); ip != nil {
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:05:14 +08:00
|
|
|
// preprocess enhanced-mode metadata
|
2019-02-11 17:20:42 +08:00
|
|
|
if t.needLookupIP(metadata) {
|
2019-10-27 21:44:07 +08:00
|
|
|
host, exist := dns.DefaultResolver.IPToHost(metadata.DstIP)
|
2018-12-05 21:13:29 +08:00
|
|
|
if exist {
|
|
|
|
metadata.Host = host
|
|
|
|
metadata.AddrType = C.AtypDomainName
|
2019-06-29 00:58:59 +08:00
|
|
|
if dns.DefaultResolver.IsFakeIP() {
|
2019-05-09 21:00:29 +08:00
|
|
|
metadata.DstIP = nil
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
var proxy C.Proxy
|
2019-08-09 01:28:37 +08:00
|
|
|
var rule C.Rule
|
2018-07-12 23:28:38 +08:00
|
|
|
switch t.mode {
|
2018-11-21 13:47:46 +08:00
|
|
|
case Direct:
|
2018-07-18 21:50:16 +08:00
|
|
|
proxy = t.proxies["DIRECT"]
|
2018-11-21 13:47:46 +08:00
|
|
|
case Global:
|
2018-08-04 23:04:16 +08:00
|
|
|
proxy = t.proxies["GLOBAL"]
|
2018-07-12 23:28:38 +08:00
|
|
|
// Rule
|
|
|
|
default:
|
2019-02-02 20:47:38 +08:00
|
|
|
var err error
|
2019-08-09 01:28:37 +08:00
|
|
|
proxy, rule, err = t.match(metadata)
|
2019-02-02 20:47:38 +08:00
|
|
|
if err != nil {
|
2019-10-11 20:11:18 +08:00
|
|
|
return nil, nil, err
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
2019-10-11 20:11:18 +08:00
|
|
|
return proxy, rule, nil
|
|
|
|
}
|
2019-02-02 20:47:38 +08:00
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
func (t *Tunnel) handleUDPConn(localConn C.ServerAdapter) {
|
|
|
|
metadata := localConn.Metadata()
|
|
|
|
if !metadata.Valid() {
|
|
|
|
log.Warnln("[Metadata] not valid: %#v", metadata)
|
|
|
|
return
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
src := localConn.RemoteAddr().String()
|
|
|
|
dst := metadata.RemoteAddress()
|
|
|
|
key := src + "-" + dst
|
|
|
|
|
|
|
|
pc, addr := t.natTable.Get(key)
|
|
|
|
if pc != nil {
|
|
|
|
t.handleUDPToRemote(localConn, pc, addr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lockKey := key + "-lock"
|
|
|
|
wg, loaded := t.natTable.GetOrCreateLock(lockKey)
|
|
|
|
go func() {
|
|
|
|
if !loaded {
|
|
|
|
wg.Add(1)
|
|
|
|
proxy, rule, err := t.resolveMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("Parse metadata failed: %s", err.Error())
|
|
|
|
t.natTable.Delete(lockKey)
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPc, nAddr, err := proxy.DialUDP(metadata)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
|
|
|
t.natTable.Delete(lockKey)
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addr = nAddr
|
2019-10-27 21:44:07 +08:00
|
|
|
pc = newUDPTracker(rawPc, DefaultManager, metadata, rule)
|
2019-10-11 20:11:18 +08:00
|
|
|
|
|
|
|
if rule != nil {
|
|
|
|
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rawPc.Chains().String())
|
|
|
|
} else {
|
|
|
|
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
t.natTable.Set(key, pc, addr)
|
|
|
|
t.natTable.Delete(lockKey)
|
|
|
|
wg.Done()
|
|
|
|
go t.handleUDPToLocal(localConn, pc, key, udpTimeout)
|
2019-04-24 10:29:29 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
wg.Wait()
|
|
|
|
pc, addr := t.natTable.Get(key)
|
|
|
|
if pc != nil {
|
|
|
|
t.handleUDPToRemote(localConn, pc, addr)
|
2019-08-09 01:28:37 +08:00
|
|
|
}
|
2019-10-11 20:11:18 +08:00
|
|
|
}()
|
|
|
|
}
|
2019-08-09 01:28:37 +08:00
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
func (t *Tunnel) handleTCPConn(localConn C.ServerAdapter) {
|
|
|
|
defer localConn.Close()
|
|
|
|
|
|
|
|
metadata := localConn.Metadata()
|
|
|
|
if !metadata.Valid() {
|
|
|
|
log.Warnln("[Metadata] not valid: %#v", metadata)
|
|
|
|
return
|
2019-04-23 23:29:36 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
proxy, rule, err := t.resolveMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("Parse metadata failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-07-25 17:47:39 +08:00
|
|
|
|
2019-08-12 14:01:32 +08:00
|
|
|
remoteConn, err := proxy.Dial(metadata)
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
2019-08-10 20:14:24 +08:00
|
|
|
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
2018-06-10 22:50:03 +08:00
|
|
|
return
|
|
|
|
}
|
2019-10-27 21:44:07 +08:00
|
|
|
remoteConn = newTCPTracker(remoteConn, DefaultManager, metadata, rule)
|
2019-08-12 14:01:32 +08:00
|
|
|
defer remoteConn.Close()
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
if rule != nil {
|
2019-08-12 14:01:32 +08:00
|
|
|
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), remoteConn.Chains().String())
|
2019-08-09 01:28:37 +08:00
|
|
|
} else {
|
|
|
|
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
switch adapter := localConn.(type) {
|
2018-09-30 12:25:52 +08:00
|
|
|
case *InboundAdapter.HTTPAdapter:
|
2019-08-12 14:01:32 +08:00
|
|
|
t.handleHTTP(adapter, remoteConn)
|
2018-09-30 12:25:52 +08:00
|
|
|
case *InboundAdapter.SocketAdapter:
|
2019-08-12 14:01:32 +08:00
|
|
|
t.handleSocket(adapter, remoteConn)
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 20:47:38 +08:00
|
|
|
func (t *Tunnel) shouldResolveIP(rule C.Rule, metadata *C.Metadata) bool {
|
2019-10-28 00:02:23 +08:00
|
|
|
return !rule.NoResolveIP() && metadata.Host != "" && metadata.DstIP == nil
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (t *Tunnel) match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
2019-04-24 12:02:52 +08:00
|
|
|
t.configMux.RLock()
|
|
|
|
defer t.configMux.RUnlock()
|
2018-06-15 00:49:52 +08:00
|
|
|
|
2019-04-24 12:02:52 +08:00
|
|
|
var resolved bool
|
2019-09-11 17:00:55 +08:00
|
|
|
|
|
|
|
if node := dns.DefaultHosts.Search(metadata.Host); node != nil {
|
|
|
|
ip := node.Data.(net.IP)
|
2019-10-27 21:44:07 +08:00
|
|
|
metadata.DstIP = ip
|
2019-09-11 17:00:55 +08:00
|
|
|
resolved = true
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
for _, rule := range t.rules {
|
2019-04-24 12:02:52 +08:00
|
|
|
if !resolved && t.shouldResolveIP(rule, metadata) {
|
2019-02-02 20:47:38 +08:00
|
|
|
ip, err := t.resolveIP(metadata.Host)
|
|
|
|
if err != nil {
|
2019-04-24 12:02:52 +08:00
|
|
|
if !t.ignoreResolveFail {
|
2019-08-09 01:28:37 +08:00
|
|
|
return nil, nil, fmt.Errorf("[DNS] resolve %s error: %s", metadata.Host, err.Error())
|
2019-04-24 12:02:52 +08:00
|
|
|
}
|
|
|
|
log.Debugln("[DNS] resolve %s error: %s", metadata.Host, err.Error())
|
|
|
|
} else {
|
|
|
|
log.Debugln("[DNS] %s --> %s", metadata.Host, ip.String())
|
2019-10-27 21:44:07 +08:00
|
|
|
metadata.DstIP = ip
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
2019-04-24 12:02:52 +08:00
|
|
|
resolved = true
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
|
|
|
|
2019-10-28 00:02:23 +08:00
|
|
|
if rule.Match(metadata) {
|
2019-04-23 23:29:36 +08:00
|
|
|
adapter, ok := t.proxies[rule.Adapter()]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2019-04-23 23:29:36 +08:00
|
|
|
|
|
|
|
if metadata.NetWork == C.UDP && !adapter.SupportUDP() {
|
2019-07-25 17:47:39 +08:00
|
|
|
log.Debugln("%v UDP is not supported", adapter.Name())
|
2019-04-23 23:29:36 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-08-09 01:28:37 +08:00
|
|
|
return adapter, rule, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-09 01:28:37 +08:00
|
|
|
return t.proxies["DIRECT"], nil, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTunnel() *Tunnel {
|
2018-07-26 00:04:59 +08:00
|
|
|
return &Tunnel{
|
2019-10-27 21:44:07 +08:00
|
|
|
tcpQueue: channels.NewInfiniteChannel(),
|
|
|
|
udpQueue: channels.NewInfiniteChannel(),
|
|
|
|
natTable: nat.New(),
|
|
|
|
proxies: make(map[string]C.Proxy),
|
|
|
|
mode: Rule,
|
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()
|
2018-11-21 13:47:46 +08:00
|
|
|
go tunnel.process()
|
2018-06-10 22:50:03 +08:00
|
|
|
})
|
|
|
|
return tunnel
|
|
|
|
}
|