2022-12-04 05:37:14 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/listener/http"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPOption struct {
|
|
|
|
BaseOption
|
|
|
|
}
|
2022-12-04 13:53:13 +00:00
|
|
|
|
|
|
|
func (o HTTPOption) Equal(config C.InboundConfig) bool {
|
|
|
|
return optionToString(o) == optionToString(config)
|
|
|
|
}
|
|
|
|
|
2022-12-04 05:37:14 +00:00
|
|
|
type HTTP struct {
|
|
|
|
*Base
|
2022-12-04 09:20:24 +00:00
|
|
|
config *HTTPOption
|
|
|
|
l *http.Listener
|
2022-12-04 05:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTP(options *HTTPOption) (*HTTP, error) {
|
|
|
|
base, err := NewBase(&options.BaseOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &HTTP{
|
2022-12-04 09:20:24 +00:00
|
|
|
Base: base,
|
|
|
|
config: options,
|
2022-12-04 05:37:14 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:53:13 +00:00
|
|
|
// Config implements constant.InboundListener
|
|
|
|
func (h *HTTP) Config() C.InboundConfig {
|
|
|
|
return h.config
|
2022-12-04 09:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 13:53:13 +00:00
|
|
|
// Address implements constant.InboundListener
|
2022-12-04 05:37:14 +00:00
|
|
|
func (h *HTTP) Address() string {
|
|
|
|
return h.l.Address()
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:53:13 +00:00
|
|
|
// Listen implements constant.InboundListener
|
2022-12-04 07:15:23 +00:00
|
|
|
func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
2022-12-04 05:37:14 +00:00
|
|
|
var err error
|
2022-12-04 16:20:50 +00:00
|
|
|
h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...)
|
2022-12-04 05:37:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infoln("HTTP[%s] proxy listening at: %s", h.Name(), h.Address())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:53:13 +00:00
|
|
|
// Close implements constant.InboundListener
|
2022-12-04 05:37:14 +00:00
|
|
|
func (h *HTTP) Close() error {
|
|
|
|
if h.l != nil {
|
|
|
|
return h.l.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:53:13 +00:00
|
|
|
var _ C.InboundListener = (*HTTP)(nil)
|