2022-12-05 17:03:12 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
LT "github.com/metacubex/mihomo/listener/tunnel"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-12-05 17:03:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TunnelOption struct {
|
|
|
|
BaseOption
|
|
|
|
Network []string `inbound:"network"`
|
|
|
|
Target string `inbound:"target"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o TunnelOption) Equal(config C.InboundConfig) bool {
|
|
|
|
return optionToString(o) == optionToString(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tunnel struct {
|
|
|
|
*Base
|
|
|
|
config *TunnelOption
|
2023-09-28 18:59:31 +08:00
|
|
|
ttl *LT.Listener
|
|
|
|
tul *LT.PacketConn
|
2022-12-05 17:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTunnel(options *TunnelOption) (*Tunnel, error) {
|
|
|
|
base, err := NewBase(&options.BaseOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Tunnel{
|
|
|
|
Base: base,
|
|
|
|
config: options,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config implements constant.InboundListener
|
|
|
|
func (t *Tunnel) Config() C.InboundConfig {
|
|
|
|
return t.config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements constant.InboundListener
|
|
|
|
func (t *Tunnel) Close() error {
|
|
|
|
var err error
|
|
|
|
if t.ttl != nil {
|
|
|
|
if tcpErr := t.ttl.Close(); tcpErr != nil {
|
|
|
|
err = tcpErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.tul != nil {
|
|
|
|
if udpErr := t.tul.Close(); udpErr != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = udpErr
|
|
|
|
} else {
|
2022-12-05 17:43:50 +08:00
|
|
|
return fmt.Errorf("close tcp err: %s, close udp err: %s", err.Error(), udpErr.Error())
|
2022-12-05 17:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements constant.InboundListener
|
|
|
|
func (t *Tunnel) Address() string {
|
2022-12-11 08:59:57 +08:00
|
|
|
if t.ttl != nil {
|
|
|
|
return t.ttl.Address()
|
|
|
|
}
|
|
|
|
if t.tul != nil {
|
|
|
|
return t.tul.Address()
|
|
|
|
}
|
|
|
|
return ""
|
2022-12-05 17:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listen implements constant.InboundListener
|
2023-09-28 18:59:31 +08:00
|
|
|
func (t *Tunnel) Listen(tunnel C.Tunnel) error {
|
2022-12-05 17:03:12 +08:00
|
|
|
var err error
|
|
|
|
for _, network := range t.config.Network {
|
|
|
|
switch network {
|
|
|
|
case "tcp":
|
2023-09-28 18:59:31 +08:00
|
|
|
if t.ttl, err = LT.New(t.RawAddress(), t.config.Target, t.config.SpecialProxy, tunnel, t.Additions()...); err != nil {
|
2022-12-05 17:03:12 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "udp":
|
2023-09-28 18:59:31 +08:00
|
|
|
if t.tul, err = LT.NewUDP(t.RawAddress(), t.config.Target, t.config.SpecialProxy, tunnel, t.Additions()...); err != nil {
|
2022-12-05 17:03:12 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2022-12-05 17:43:50 +08:00
|
|
|
log.Warnln("unknown network type: %s, passed", network)
|
|
|
|
continue
|
2022-12-05 17:03:12 +08:00
|
|
|
}
|
|
|
|
log.Infoln("Tunnel[%s](%s/%s)proxy listening at: %s", t.Name(), network, t.config.Target, t.Address())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ C.InboundListener = (*Tunnel)(nil)
|