mihomo/listener/inbound/mixed.go

90 lines
1.8 KiB
Go
Raw Normal View History

2022-12-04 13:37:14 +08:00
package inbound
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/socks"
)
type MixedOption struct {
BaseOption
2022-12-04 21:53:13 +08:00
UDP bool `inbound:"udp,omitempty"`
}
func (o MixedOption) Equal(config C.InboundConfig) bool {
return optionToString(o) == optionToString(config)
2022-12-04 13:37:14 +08:00
}
type Mixed struct {
*Base
config *MixedOption
l *mixed.Listener
lUDP *socks.UDPListener
udp bool
2022-12-04 13:37:14 +08:00
}
func NewMixed(options *MixedOption) (*Mixed, error) {
base, err := NewBase(&options.BaseOption)
if err != nil {
return nil, err
}
return &Mixed{
Base: base,
config: options,
2022-12-04 21:53:13 +08:00
udp: options.UDP,
2022-12-04 13:37:14 +08:00
}, nil
}
2022-12-04 21:53:13 +08:00
// Config implements constant.InboundListener
func (m *Mixed) Config() C.InboundConfig {
return m.config
}
2022-12-04 21:53:13 +08:00
// Address implements constant.InboundListener
2022-12-04 13:37:14 +08:00
func (m *Mixed) Address() string {
return m.l.Address()
}
2022-12-04 21:53:13 +08:00
// Listen implements constant.InboundListener
2022-12-04 15:15:23 +08:00
func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
2022-12-04 13:37:14 +08:00
var err error
m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...)
2022-12-04 13:37:14 +08:00
if err != nil {
return err
}
if m.udp {
m.lUDP, err = socks.NewUDP(m.Address(), udpIn, m.Additions()...)
2022-12-04 13:37:14 +08:00
if err != nil {
return err
}
}
log.Infoln("Mixed(http+socks)[%s] proxy listening at: %s", m.Name(), m.Address())
return nil
}
2022-12-04 21:53:13 +08:00
// Close implements constant.InboundListener
2022-12-04 13:37:14 +08:00
func (m *Mixed) Close() error {
var err error
if m.l != nil {
if tcpErr := m.l.Close(); tcpErr != nil {
err = tcpErr
}
}
if m.udp && m.lUDP != nil {
if udpErr := m.lUDP.Close(); udpErr != nil {
if err == nil {
err = udpErr
} else {
return fmt.Errorf("close tcp err: %s, close udp err: %s", err.Error(), udpErr.Error())
}
}
}
return err
}
2022-12-04 21:53:13 +08:00
var _ C.InboundListener = (*Mixed)(nil)