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"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
2022-12-04 13:37:14 +08:00
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/listener/mixed"
"github.com/metacubex/mihomo/listener/socks"
2022-12-04 13:37:14 +08:00
)
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
func (m *Mixed) Listen(tunnel C.Tunnel) error {
2022-12-04 13:37:14 +08:00
var err error
m.l, err = mixed.New(m.RawAddress(), tunnel, m.Additions()...)
2022-12-04 13:37:14 +08:00
if err != nil {
return err
}
if m.udp {
m.lUDP, err = socks.NewUDP(m.RawAddress(), tunnel, 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)