mihomo/listener/inbound/tuic.go

94 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-12-04 23:05:13 +08:00
package inbound
import (
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
LC "github.com/metacubex/mihomo/listener/config"
"github.com/metacubex/mihomo/listener/tuic"
"github.com/metacubex/mihomo/log"
2022-12-04 23:05:13 +08:00
)
type TuicOption struct {
BaseOption
2023-06-12 17:44:22 +08:00
Token []string `inbound:"token,omitempty"`
Users map[string]string `inbound:"users,omitempty"`
Certificate string `inbound:"certificate"`
PrivateKey string `inbound:"private-key"`
CongestionController string `inbound:"congestion-controller,omitempty"`
MaxIdleTime int `inbound:"max-idle-time,omitempty"`
AuthenticationTimeout int `inbound:"authentication-timeout,omitempty"`
ALPN []string `inbound:"alpn,omitempty"`
MaxUdpRelayPacketSize int `inbound:"max-udp-relay-packet-size,omitempty"`
2023-06-21 14:00:49 +08:00
CWND int `inbound:"cwnd,omitempty"`
2023-11-23 08:20:26 +08:00
MuxOption MuxOption `inbound:"mux-option,omitempty"`
2022-12-04 23:05:13 +08:00
}
func (o TuicOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
}
type Tuic struct {
*Base
config *TuicOption
l *tuic.Listener
2022-12-05 17:43:50 +08:00
ts LC.TuicServer
2022-12-04 23:05:13 +08:00
}
func NewTuic(options *TuicOption) (*Tuic, error) {
base, err := NewBase(&options.BaseOption)
if err != nil {
return nil, err
}
return &Tuic{
Base: base,
config: options,
2022-12-05 17:43:50 +08:00
ts: LC.TuicServer{
Enable: true,
Listen: base.RawAddress(),
Token: options.Token,
2023-06-12 17:44:22 +08:00
Users: options.Users,
2022-12-05 17:43:50 +08:00
Certificate: options.Certificate,
PrivateKey: options.PrivateKey,
CongestionController: options.CongestionController,
MaxIdleTime: options.MaxIdleTime,
AuthenticationTimeout: options.AuthenticationTimeout,
ALPN: options.ALPN,
MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize,
2023-06-21 14:00:49 +08:00
CWND: options.CWND,
2023-11-23 08:20:26 +08:00
MuxOption: options.MuxOption.Build(),
2022-12-05 17:43:50 +08:00
},
2022-12-04 23:05:13 +08:00
}, nil
}
// Config implements constant.InboundListener
func (t *Tuic) Config() C.InboundConfig {
return t.config
}
// Address implements constant.InboundListener
func (t *Tuic) Address() string {
if t.l != nil {
for _, addr := range t.l.AddrList() {
return addr.String()
}
}
return ""
}
// Listen implements constant.InboundListener
func (t *Tuic) Listen(tunnel C.Tunnel) error {
2022-12-04 23:05:13 +08:00
var err error
t.l, err = tuic.New(t.ts, tunnel, t.Additions()...)
2022-12-04 23:05:13 +08:00
if err != nil {
return err
}
log.Infoln("Tuic[%s] proxy listening at: %s", t.Name(), t.Address())
return nil
}
// Close implements constant.InboundListener
func (t *Tuic) Close() error {
return t.l.Close()
}
var _ C.InboundListener = (*Tuic)(nil)