github.com/metacubex/mihomo@v1.18.5/listener/inbound/tuic.go (about)

     1  package inbound
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  	LC "github.com/metacubex/mihomo/listener/config"
     6  	"github.com/metacubex/mihomo/listener/tuic"
     7  	"github.com/metacubex/mihomo/log"
     8  )
     9  
    10  type TuicOption struct {
    11  	BaseOption
    12  	Token                 []string          `inbound:"token,omitempty"`
    13  	Users                 map[string]string `inbound:"users,omitempty"`
    14  	Certificate           string            `inbound:"certificate"`
    15  	PrivateKey            string            `inbound:"private-key"`
    16  	CongestionController  string            `inbound:"congestion-controller,omitempty"`
    17  	MaxIdleTime           int               `inbound:"max-idle-time,omitempty"`
    18  	AuthenticationTimeout int               `inbound:"authentication-timeout,omitempty"`
    19  	ALPN                  []string          `inbound:"alpn,omitempty"`
    20  	MaxUdpRelayPacketSize int               `inbound:"max-udp-relay-packet-size,omitempty"`
    21  	CWND                  int               `inbound:"cwnd,omitempty"`
    22  	MuxOption             MuxOption         `inbound:"mux-option,omitempty"`
    23  }
    24  
    25  func (o TuicOption) Equal(config C.InboundConfig) bool {
    26  	return optionToString(o) == optionToString(config)
    27  }
    28  
    29  type Tuic struct {
    30  	*Base
    31  	config *TuicOption
    32  	l      *tuic.Listener
    33  	ts     LC.TuicServer
    34  }
    35  
    36  func NewTuic(options *TuicOption) (*Tuic, error) {
    37  	base, err := NewBase(&options.BaseOption)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return &Tuic{
    42  		Base:   base,
    43  		config: options,
    44  		ts: LC.TuicServer{
    45  			Enable:                true,
    46  			Listen:                base.RawAddress(),
    47  			Token:                 options.Token,
    48  			Users:                 options.Users,
    49  			Certificate:           options.Certificate,
    50  			PrivateKey:            options.PrivateKey,
    51  			CongestionController:  options.CongestionController,
    52  			MaxIdleTime:           options.MaxIdleTime,
    53  			AuthenticationTimeout: options.AuthenticationTimeout,
    54  			ALPN:                  options.ALPN,
    55  			MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize,
    56  			CWND:                  options.CWND,
    57  			MuxOption:             options.MuxOption.Build(),
    58  		},
    59  	}, nil
    60  }
    61  
    62  // Config implements constant.InboundListener
    63  func (t *Tuic) Config() C.InboundConfig {
    64  	return t.config
    65  }
    66  
    67  // Address implements constant.InboundListener
    68  func (t *Tuic) Address() string {
    69  	if t.l != nil {
    70  		for _, addr := range t.l.AddrList() {
    71  			return addr.String()
    72  		}
    73  	}
    74  	return ""
    75  }
    76  
    77  // Listen implements constant.InboundListener
    78  func (t *Tuic) Listen(tunnel C.Tunnel) error {
    79  	var err error
    80  	t.l, err = tuic.New(t.ts, tunnel, t.Additions()...)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	log.Infoln("Tuic[%s] proxy listening at: %s", t.Name(), t.Address())
    85  	return nil
    86  }
    87  
    88  // Close implements constant.InboundListener
    89  func (t *Tuic) Close() error {
    90  	return t.l.Close()
    91  }
    92  
    93  var _ C.InboundListener = (*Tuic)(nil)