mihomo/tunnel/tunnel.go

266 lines
6.3 KiB
Go
Raw Normal View History

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"
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-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 {
queue *channels.InfiniteChannel
rules []C.Rule
proxies map[string]C.Proxy
configMux *sync.RWMutex
traffic *C.Traffic
// experimental features
ignoreResolveFail bool
// Outbound Rule
2018-11-21 13:47:46 +08:00
mode Mode
2018-06-10 22:50:03 +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
}
// Traffic return traffic of all connections
2018-06-18 11:31:49 +08:00
func (t *Tunnel) Traffic() *C.Traffic {
return t.traffic
}
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) {
t.configMux.Lock()
2018-11-21 13:47:46 +08:00
t.rules = rules
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) {
t.configMux.Lock()
2018-11-21 13:47:46 +08:00
t.proxies = proxies
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() {
queue := t.queue.Out()
for {
elm := <-queue
conn := elm.(C.ServerAdapter)
go t.handleConn(conn)
}
}
2018-12-05 21:13:29 +08:00
func (t *Tunnel) resolveIP(host string) (net.IP, error) {
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 {
return dns.DefaultResolver != nil && (dns.DefaultResolver.IsMapping() || dns.DefaultResolver.IsFakeIP()) && metadata.Host == "" && metadata.DstIP != nil
2019-02-11 15:44:42 +08:00
}
2018-06-10 22:50:03 +08:00
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
defer func() {
var conn net.Conn
switch adapter := localConn.(type) {
case *InboundAdapter.HTTPAdapter:
conn = adapter.Conn
case *InboundAdapter.SocketAdapter:
conn = adapter.Conn
}
if _, ok := conn.(*net.TCPConn); ok {
localConn.Close()
}
}()
metadata := localConn.Metadata()
if !metadata.Valid() {
log.Warnln("[Metadata] not valid: %#v", metadata)
return
}
2019-05-03 00:05:14 +08:00
// preprocess enhanced-mode metadata
2019-02-11 17:20:42 +08:00
if t.needLookupIP(metadata) {
host, exist := dns.DefaultResolver.IPToHost(*metadata.DstIP)
2018-12-05 21:13:29 +08:00
if exist {
metadata.Host = host
metadata.AddrType = C.AtypDomainName
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
}
}
var proxy C.Proxy
var rule C.Rule
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:
proxy = t.proxies["GLOBAL"]
// Rule
default:
2019-02-02 20:47:38 +08:00
var err error
proxy, rule, err = t.match(metadata)
2019-02-02 20:47:38 +08:00
if err != nil {
return
}
}
2019-02-02 20:47:38 +08:00
switch metadata.NetWork {
case C.TCP:
t.handleTCPConn(localConn, metadata, proxy, rule)
case C.UDP:
t.handleUDPConn(localConn, metadata, proxy, rule)
}
}
func (t *Tunnel) handleUDPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
pc, addr := natTable.Get(localConn.RemoteAddr())
if pc == nil {
rawpc, naddr, err := proxy.DialUDP(metadata)
addr = naddr
pc = rawpc
2019-04-24 10:29:29 +08:00
if err != nil {
log.Warnln("%s --> %v match %s using %s error: %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rule.Adapter(), err.Error())
2019-05-16 18:40:20 +08:00
return
2019-04-24 10:29:29 +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())
}
natTable.Set(localConn.RemoteAddr(), pc, addr)
go t.handleUDPToLocal(localConn, pc)
2019-04-23 23:29:36 +08:00
}
t.handleUDPToRemote(localConn, pc, addr)
}
func (t *Tunnel) handleTCPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
2019-03-03 11:59:07 +08:00
remoConn, err := proxy.Dial(metadata)
2018-06-10 22:50:03 +08:00
if err != nil {
log.Warnln("%s --> %v match %s using %s error: %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rule.Adapter(), err.Error())
2018-06-10 22:50:03 +08:00
return
}
defer remoConn.Close()
if rule != nil {
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), remoConn.Chains().String())
} else {
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
}
switch adapter := localConn.(type) {
2018-09-30 12:25:52 +08:00
case *InboundAdapter.HTTPAdapter:
t.handleHTTP(adapter, remoConn)
2018-09-30 12:25:52 +08:00
case *InboundAdapter.SocketAdapter:
2019-04-23 23:29:36 +08:00
t.handleSocket(adapter, remoConn)
}
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-05-09 21:00:29 +08:00
return (rule.RuleType() == C.GEOIP || rule.RuleType() == C.IPCIDR) && metadata.Host != "" && metadata.DstIP == nil
2019-02-02 20:47:38 +08:00
}
func (t *Tunnel) match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
t.configMux.RLock()
defer t.configMux.RUnlock()
2018-06-15 00:49:52 +08:00
var resolved bool
2018-06-10 22:50:03 +08:00
for _, rule := range t.rules {
if !resolved && t.shouldResolveIP(rule, metadata) {
2019-02-02 20:47:38 +08:00
ip, err := t.resolveIP(metadata.Host)
if err != nil {
if !t.ignoreResolveFail {
return nil, nil, fmt.Errorf("[DNS] resolve %s error: %s", metadata.Host, err.Error())
}
log.Debugln("[DNS] resolve %s error: %s", metadata.Host, err.Error())
} else {
log.Debugln("[DNS] %s --> %s", metadata.Host, ip.String())
2019-05-09 21:00:29 +08:00
metadata.DstIP = &ip
2019-02-02 20:47:38 +08:00
}
resolved = true
2019-02-02 20:47:38 +08:00
}
2018-09-30 12:25:52 +08:00
if rule.IsMatch(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() {
log.Debugln("%v UDP is not supported", adapter.Name())
2019-04-23 23:29:36 +08:00
continue
}
return adapter, rule, nil
2018-06-10 22:50:03 +08:00
}
}
return t.proxies["DIRECT"], nil, nil
2018-06-10 22:50:03 +08:00
}
func newTunnel() *Tunnel {
return &Tunnel{
queue: channels.NewInfiniteChannel(),
proxies: make(map[string]C.Proxy),
configMux: &sync.RWMutex{},
traffic: C.NewTraffic(time.Second),
mode: Rule,
2018-06-10 22:50:03 +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
}