Feature: add inbounds for flexible binding inbound (#2818)

This commit is contained in:
fuyun
2023-08-03 22:30:08 +08:00
committed by GitHub
parent 10f4d5375a
commit 9e78137768
25 changed files with 552 additions and 361 deletions

View File

@ -73,6 +73,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateHosts(cfg.Hosts)
updateProfile(cfg)
updateGeneral(cfg.General, force)
updateInbounds(cfg.Inbounds, force)
updateDNS(cfg.DNS)
updateExperimental(cfg)
updateTunnels(cfg.Tunnels)
@ -86,19 +87,19 @@ func GetGeneral() *config.General {
}
general := &config.General{
Inbound: config.Inbound{
Port: ports.Port,
SocksPort: ports.SocksPort,
RedirPort: ports.RedirPort,
TProxyPort: ports.TProxyPort,
MixedPort: ports.MixedPort,
Authentication: authenticator,
AllowLan: listener.AllowLan(),
BindAddress: listener.BindAddress(),
LagecyInbound: config.LagecyInbound{
Port: ports.Port,
SocksPort: ports.SocksPort,
RedirPort: ports.RedirPort,
TProxyPort: ports.TProxyPort,
MixedPort: ports.MixedPort,
AllowLan: listener.AllowLan(),
BindAddress: listener.BindAddress(),
},
Mode: tunnel.Mode(),
LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6,
Authentication: authenticator,
Mode: tunnel.Mode(),
LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6,
}
return general
@ -164,6 +165,16 @@ func updateTunnels(tunnels []config.Tunnel) {
listener.PatchTunnel(tunnels, tunnel.TCPIn(), tunnel.UDPIn())
}
func updateInbounds(inbounds []C.Inbound, force bool) {
if !force {
return
}
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
listener.ReCreateListeners(inbounds, tcpIn, udpIn)
}
func updateGeneral(general *config.General, force bool) {
log.SetLevel(general.LogLevel)
tunnel.SetMode(general.Mode)
@ -184,14 +195,14 @@ func updateGeneral(general *config.General, force bool) {
bindAddress := general.BindAddress
listener.SetBindAddress(bindAddress)
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
listener.ReCreateHTTP(general.Port, tcpIn)
listener.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn)
listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
ports := listener.Ports{
Port: general.Port,
SocksPort: general.SocksPort,
RedirPort: general.RedirPort,
TProxyPort: general.TProxyPort,
MixedPort: general.MixedPort,
}
listener.ReCreatePortsListeners(ports, tunnel.TCPIn(), tunnel.UDPIn())
}
func updateUsers(users []auth.AuthUser) {

View File

@ -6,14 +6,15 @@ import (
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/constant"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor"
P "github.com/Dreamacro/clash/listener"
"github.com/Dreamacro/clash/listener"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/samber/lo"
)
func configRouter() http.Handler {
@ -29,14 +30,6 @@ func getConfigs(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, general)
}
func pointerOrDefault(p *int, def int) int {
if p != nil {
return *p
}
return def
}
func patchConfigs(w http.ResponseWriter, r *http.Request) {
general := struct {
Port *int `json:"port"`
@ -56,25 +49,6 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
return
}
if general.AllowLan != nil {
P.SetAllowLan(*general.AllowLan)
}
if general.BindAddress != nil {
P.SetBindAddress(*general.BindAddress)
}
ports := P.GetPorts()
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn)
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn)
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn)
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn)
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
if general.Mode != nil {
tunnel.SetMode(*general.Mode)
}
@ -87,6 +61,23 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
resolver.DisableIPv6 = !*general.IPv6
}
if general.AllowLan != nil {
listener.SetAllowLan(*general.AllowLan)
}
if general.BindAddress != nil {
listener.SetBindAddress(*general.BindAddress)
}
ports := listener.GetPorts()
ports.Port = lo.FromPtrOr(general.Port, ports.Port)
ports.SocksPort = lo.FromPtrOr(general.SocksPort, ports.SocksPort)
ports.RedirPort = lo.FromPtrOr(general.RedirPort, ports.RedirPort)
ports.TProxyPort = lo.FromPtrOr(general.TProxyPort, ports.TProxyPort)
ports.MixedPort = lo.FromPtrOr(general.MixedPort, ports.MixedPort)
listener.ReCreatePortsListeners(*ports, tunnel.TCPIn(), tunnel.UDPIn())
render.NoContent(w, r)
}
@ -114,7 +105,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
}
} else {
if req.Path == "" {
req.Path = constant.Path.Config()
req.Path = C.Path.Config()
}
if !filepath.IsAbs(req.Path) {
render.Status(r, http.StatusBadRequest)

39
hub/route/inbounds.go Normal file
View File

@ -0,0 +1,39 @@
package route
import (
"net/http"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func inboundRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getInbounds)
r.Put("/", updateInbounds)
return r
}
func getInbounds(w http.ResponseWriter, r *http.Request) {
inbounds := listener.GetInbounds()
render.JSON(w, r, render.M{
"inbounds": inbounds,
})
}
func updateInbounds(w http.ResponseWriter, r *http.Request) {
var req []C.Inbound
if err := render.DecodeJSON(r.Body, &req); err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, ErrBadRequest)
return
}
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
listener.ReCreateListeners(req, tcpIn, udpIn)
render.NoContent(w, r)
}

View File

@ -69,6 +69,7 @@ func Start(addr string, secret string) {
r.Get("/traffic", traffic)
r.Get("/version", version)
r.Mount("/configs", configRouter())
r.Mount("/inbounds", inboundRouter())
r.Mount("/proxies", proxyRouter())
r.Mount("/rules", ruleRouter())
r.Mount("/connections", connectionRouter())