clash/listener/inbound/tuic.go

88 lines
2.4 KiB
Go
Raw Normal View History

2022-12-04 15:05:13 +00:00
package inbound
import (
C "github.com/Dreamacro/clash/constant"
LC "github.com/Dreamacro/clash/listener/config"
"github.com/Dreamacro/clash/listener/tuic"
"github.com/Dreamacro/clash/log"
)
type TuicOption struct {
BaseOption
Token []string `inbound:"token"`
Certificate string `inbound:"certificate"`
PrivateKey string `inbound:"private-key"`
2023-02-02 13:29:12 +00:00
CongestionController string `inbound:"congestion-controller,omitempty"`
2023-02-02 13:48:20 +00:00
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"`
2022-12-04 15:05:13 +00: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 09:43:50 +00:00
ts LC.TuicServer
2022-12-04 15:05:13 +00: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 09:43:50 +00:00
ts: LC.TuicServer{
Enable: true,
Listen: base.RawAddress(),
Token: options.Token,
Certificate: options.Certificate,
PrivateKey: options.PrivateKey,
CongestionController: options.CongestionController,
MaxIdleTime: options.MaxIdleTime,
AuthenticationTimeout: options.AuthenticationTimeout,
ALPN: options.ALPN,
MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize,
},
2022-12-04 15:05:13 +00: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
2023-02-18 05:16:07 +00:00
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error {
2022-12-04 15:05:13 +00:00
var err error
2022-12-05 09:43:50 +00:00
t.l, err = tuic.New(t.ts, tcpIn, udpIn, t.Additions()...)
2022-12-04 15:05:13 +00: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)