github.com/metacubex/mihomo@v1.18.5/listener/inbound/http.go (about)

     1  package inbound
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  	"github.com/metacubex/mihomo/listener/http"
     6  	"github.com/metacubex/mihomo/log"
     7  )
     8  
     9  type HTTPOption struct {
    10  	BaseOption
    11  }
    12  
    13  func (o HTTPOption) Equal(config C.InboundConfig) bool {
    14  	return optionToString(o) == optionToString(config)
    15  }
    16  
    17  type HTTP struct {
    18  	*Base
    19  	config *HTTPOption
    20  	l      *http.Listener
    21  }
    22  
    23  func NewHTTP(options *HTTPOption) (*HTTP, error) {
    24  	base, err := NewBase(&options.BaseOption)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return &HTTP{
    29  		Base:   base,
    30  		config: options,
    31  	}, nil
    32  }
    33  
    34  // Config implements constant.InboundListener
    35  func (h *HTTP) Config() C.InboundConfig {
    36  	return h.config
    37  }
    38  
    39  // Address implements constant.InboundListener
    40  func (h *HTTP) Address() string {
    41  	return h.l.Address()
    42  }
    43  
    44  // Listen implements constant.InboundListener
    45  func (h *HTTP) Listen(tunnel C.Tunnel) error {
    46  	var err error
    47  	h.l, err = http.New(h.RawAddress(), tunnel, h.Additions()...)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	log.Infoln("HTTP[%s] proxy listening at: %s", h.Name(), h.Address())
    52  	return nil
    53  }
    54  
    55  // Close implements constant.InboundListener
    56  func (h *HTTP) Close() error {
    57  	if h.l != nil {
    58  		return h.l.Close()
    59  	}
    60  	return nil
    61  }
    62  
    63  var _ C.InboundListener = (*HTTP)(nil)