2022-12-04 13:37:14 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
2025-02-27 09:59:09 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2025-02-28 13:13:53 +08:00
|
|
|
LC "github.com/metacubex/mihomo/listener/config"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/listener/http"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-12-04 13:37:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPOption struct {
|
|
|
|
BaseOption
|
2025-02-28 13:13:53 +08:00
|
|
|
Users AuthUsers `inbound:"users,omitempty"`
|
|
|
|
Certificate string `inbound:"certificate,omitempty"`
|
|
|
|
PrivateKey string `inbound:"private-key,omitempty"`
|
|
|
|
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
2022-12-04 21:53:13 +08:00
|
|
|
|
|
|
|
func (o HTTPOption) Equal(config C.InboundConfig) bool {
|
|
|
|
return optionToString(o) == optionToString(config)
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
type HTTP struct {
|
|
|
|
*Base
|
2022-12-04 17:20:24 +08:00
|
|
|
config *HTTPOption
|
2025-02-27 09:59:09 +08:00
|
|
|
l []*http.Listener
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTP(options *HTTPOption) (*HTTP, error) {
|
|
|
|
base, err := NewBase(&options.BaseOption)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &HTTP{
|
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 (h *HTTP) Config() C.InboundConfig {
|
|
|
|
return h.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 (h *HTTP) Address() string {
|
2025-02-27 09:59:09 +08:00
|
|
|
var addrList []string
|
|
|
|
for _, l := range h.l {
|
|
|
|
addrList = append(addrList, l.Address())
|
|
|
|
}
|
|
|
|
return strings.Join(addrList, ",")
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Listen implements constant.InboundListener
|
2023-09-28 18:59:31 +08:00
|
|
|
func (h *HTTP) Listen(tunnel C.Tunnel) error {
|
2025-02-27 09:59:09 +08:00
|
|
|
for _, addr := range strings.Split(h.RawAddress(), ",") {
|
2025-02-28 13:13:53 +08:00
|
|
|
l, err := http.NewWithConfig(
|
|
|
|
LC.AuthServer{
|
|
|
|
Enable: true,
|
|
|
|
Listen: addr,
|
|
|
|
AuthStore: h.config.Users.GetAuthStore(),
|
|
|
|
Certificate: h.config.Certificate,
|
|
|
|
PrivateKey: h.config.PrivateKey,
|
|
|
|
RealityConfig: h.config.RealityConfig.Build(),
|
|
|
|
},
|
|
|
|
tunnel,
|
|
|
|
h.Additions()...,
|
|
|
|
)
|
2025-02-27 09:59:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.l = append(h.l, l)
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
log.Infoln("HTTP[%s] proxy listening at: %s", h.Name(), h.Address())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
// Close implements constant.InboundListener
|
2022-12-04 13:37:14 +08:00
|
|
|
func (h *HTTP) Close() error {
|
2025-02-27 09:59:09 +08:00
|
|
|
var errs []error
|
|
|
|
for _, l := range h.l {
|
|
|
|
err := l.Close()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("close tcp listener %s err: %w", l.Address(), err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.Join(errs...)
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:53:13 +08:00
|
|
|
var _ C.InboundListener = (*HTTP)(nil)
|