package inbound import ( "encoding/json" "net" "net/netip" "strconv" C "github.com/Dreamacro/clash/constant" ) type Base struct { config *BaseOption name string preferRulesName string listenAddr netip.Addr port int } func NewBase(options *BaseOption) (*Base, error) { if options.Listen == "" { options.Listen = "0.0.0.0" } addr, err := netip.ParseAddr(options.Listen) if err != nil { return nil, err } return &Base{ name: options.Name(), listenAddr: addr, preferRulesName: options.PreferRulesName, port: options.Port, config: options, }, nil } // Config implements constant.InboundListener func (b *Base) Config() C.InboundConfig { return b.config } // Address implements constant.InboundListener func (b *Base) Address() string { return b.RawAddress() } // Close implements constant.InboundListener func (*Base) Close() error { return nil } // Name implements constant.InboundListener func (b *Base) Name() string { return b.name } // RawAddress implements constant.InboundListener func (b *Base) RawAddress() string { return net.JoinHostPort(b.listenAddr.String(), strconv.Itoa(int(b.port))) } // Listen implements constant.InboundListener func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { return nil } var _ C.InboundListener = (*Base)(nil) type BaseOption struct { NameStr string `inbound:"name"` Listen string `inbound:"listen,omitempty"` Port int `inbound:"port"` PreferRulesName string `inbound:"rule,omitempty"` } func (o BaseOption) Name() string { return o.NameStr } func (o BaseOption) Equal(config C.InboundConfig) bool { return optionToString(o) == optionToString(config) } var _ C.InboundConfig = (*BaseOption)(nil) func optionToString(option any) string { str, _ := json.Marshal(option) return string(str) }