2022-12-04 13:37:14 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/listener/redir"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-12-04 13:37:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedirOption struct {
|
|
|
|
BaseOption
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
func (o RedirOption) Equal(config C.InboundConfig) bool {
|
|
|
|
return optionToString(o) == optionToString(config)
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
type Redir struct {
|
|
|
|
*Base
|
2022-12-04 17:20:24 +08:00
|
|
|
config *RedirOption
|
|
|
|
l *redir.Listener
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedir(options *RedirOption) (*Redir, error) {
|
|
|
|
base, err := NewBase(&options.BaseOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Redir{
|
2022-12-04 17:20:24 +08:00
|
|
|
Base: base,
|
|
|
|
config: options,
|
2022-12-04 13:37:14 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Config implements constant.InboundListener
|
|
|
|
func (r *Redir) Config() C.InboundConfig {
|
|
|
|
return r.config
|
2022-12-04 17:20:24 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Address implements constant.InboundListener
|
2022-12-04 13:37:14 +08:00
|
|
|
func (r *Redir) Address() string {
|
|
|
|
return r.l.Address()
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Listen implements constant.InboundListener
|
2023-09-28 18:59:31 +08:00
|
|
|
func (r *Redir) Listen(tunnel C.Tunnel) error {
|
2022-12-04 13:37:14 +08:00
|
|
|
var err error
|
2023-09-28 18:59:31 +08:00
|
|
|
r.l, err = redir.New(r.RawAddress(), tunnel, r.Additions()...)
|
2022-12-04 13:37:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infoln("Redir[%s] proxy listening at: %s", r.Name(), r.Address())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Close implements constant.InboundListener
|
2022-12-04 13:37:14 +08:00
|
|
|
func (r *Redir) Close() error {
|
|
|
|
if r.l != nil {
|
|
|
|
r.l.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
var _ C.InboundListener = (*Redir)(nil)
|