mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-10-10 04:03:11 +08:00
chore: allow passing tunnel when creating the proxy
This commit is contained in:
@@ -202,6 +202,7 @@ type BasicOption struct {
|
||||
// The following parameters are used internally, assign value by the structure decoder are disallowed
|
||||
//
|
||||
DialerForAPI C.Dialer `proxy:"-"` // the dialer used for API usage has higher priority than all the above configurations.
|
||||
TunnelForAPI C.Tunnel `proxy:"-"`
|
||||
ProviderName string `proxy:"-"`
|
||||
}
|
||||
|
||||
@@ -217,6 +218,10 @@ func (b *BasicOption) NewDialer(opts []dialer.Option) C.Dialer {
|
||||
return cDialer
|
||||
}
|
||||
|
||||
func (b *BasicOption) NewTunnel() C.Tunnel {
|
||||
return b.TunnelForAPI
|
||||
}
|
||||
|
||||
type conn struct {
|
||||
N.ExtendedConn
|
||||
chain C.Chain
|
||||
|
||||
@@ -18,6 +18,7 @@ func ParseProxy(mapping map[string]any, options ...ProxyOption) (C.Proxy, error)
|
||||
opt := applyProxyOptions(options...)
|
||||
basicOption := outbound.BasicOption{
|
||||
DialerForAPI: opt.DialerForAPI,
|
||||
TunnelForAPI: opt.TunnelForAPI,
|
||||
ProviderName: opt.ProviderName,
|
||||
}
|
||||
|
||||
@@ -215,6 +216,7 @@ func ParseProxy(mapping map[string]any, options ...ProxyOption) (C.Proxy, error)
|
||||
|
||||
type proxyOption struct {
|
||||
DialerForAPI C.Dialer
|
||||
TunnelForAPI C.Tunnel
|
||||
ProviderName string
|
||||
}
|
||||
|
||||
@@ -234,6 +236,12 @@ func WithDialerForAPI(dialer C.Dialer) ProxyOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithTunnelForAPI(tunnel C.Tunnel) ProxyOption {
|
||||
return func(opt *proxyOption) {
|
||||
opt.TunnelForAPI = tunnel
|
||||
}
|
||||
}
|
||||
|
||||
func WithProviderName(name string) ProxyOption {
|
||||
return func(opt *proxyOption) {
|
||||
opt.ProviderName = name
|
||||
|
||||
@@ -43,7 +43,7 @@ type proxyProviderSchema struct {
|
||||
Header map[string][]string `provider:"header,omitempty"`
|
||||
}
|
||||
|
||||
func ParseProxyProvider(name string, mapping map[string]any) (P.ProxyProvider, error) {
|
||||
func ParseProxyProvider(name string, mapping map[string]any, tunnel C.Tunnel) (P.ProxyProvider, error) {
|
||||
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
||||
|
||||
schema := &proxyProviderSchema{
|
||||
@@ -69,7 +69,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (P.ProxyProvider, e
|
||||
}
|
||||
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
||||
|
||||
parser, err := NewProxiesParser(name, schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
|
||||
parser, err := NewProxiesParser(name, tunnel, schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -339,7 +339,7 @@ func (cp *CompatibleProvider) Close() error {
|
||||
return cp.compatibleProvider.Close()
|
||||
}
|
||||
|
||||
func NewProxiesParser(pdName string, filter string, excludeFilter string, excludeType string, dialerProxy string, override overrideSchema) (resource.Parser[[]C.Proxy], error) {
|
||||
func NewProxiesParser(pdName string, tunnel C.Tunnel, filter string, excludeFilter string, excludeType string, dialerProxy string, override overrideSchema) (resource.Parser[[]C.Proxy], error) {
|
||||
var excludeTypeArray []string
|
||||
if excludeType != "" {
|
||||
excludeTypeArray = strings.Split(excludeType, "|")
|
||||
@@ -433,7 +433,7 @@ func NewProxiesParser(pdName string, filter string, excludeFilter string, exclud
|
||||
return nil, fmt.Errorf("proxy %d override error: %w", idx, err)
|
||||
}
|
||||
|
||||
proxy, err := adapter.ParseProxy(mapping, adapter.WithProviderName(pdName))
|
||||
proxy, err := adapter.ParseProxy(mapping, adapter.WithTunnelForAPI(tunnel), adapter.WithProviderName(pdName))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
||||
}
|
||||
|
||||
+2
-2
@@ -876,7 +876,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
||||
|
||||
// parse proxy
|
||||
for idx, mapping := range proxiesConfig {
|
||||
proxy, err := adapter.ParseProxy(mapping)
|
||||
proxy, err := adapter.ParseProxy(mapping, adapter.WithTunnelForAPI(T.Tunnel))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("proxy %d: %w", idx, err)
|
||||
}
|
||||
@@ -913,7 +913,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
||||
return nil, nil, fmt.Errorf("can not defined a provider called `%s`", provider.ReservedName)
|
||||
}
|
||||
|
||||
pd, err := provider.ParseProxyProvider(name, mapping)
|
||||
pd, err := provider.ParseProxyProvider(name, mapping, T.Tunnel)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("parse proxy provider %s error: %w", name, err)
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ func testInboundAnyTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outbou
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.Password = userUUID
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewAnyTLS(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -354,6 +354,9 @@ func NewHttpTestTunnel() *TestTunnel {
|
||||
}
|
||||
<-c.ch
|
||||
},
|
||||
HandleUDPPacketFn: func(packet C.UDPPacket, metadata *C.Metadata) {
|
||||
// TODO
|
||||
},
|
||||
CloseFn: ln.Close,
|
||||
DoSequentialTestFn: sequentialTestFn,
|
||||
DoConcurrentTestFn: concurrentTestFn,
|
||||
|
||||
@@ -42,6 +42,7 @@ func testInboundHysteria2(t *testing.T, inboundOptions inbound.Hysteria2Option,
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.Password = userUUID
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewHysteria2(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -238,6 +238,7 @@ func testInboundMieruTCP(t *testing.T, handshakeMode string) {
|
||||
HandshakeMode: handshakeMode,
|
||||
}
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
out, err := outbound.NewMieru(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
|
||||
@@ -86,6 +86,7 @@ func testInboundShadowSocks0(t *testing.T, inboundOptions inbound.ShadowSocksOpt
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.Password = password
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewShadowSocks(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -44,6 +44,7 @@ func testInboundSudoku(t *testing.T, inboundOptions inbound.SudokuOption, outbou
|
||||
outboundOptions.Server = addrPort.Addr().String()
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewSudoku(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -44,6 +44,7 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.Password = userUUID
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewTrojan(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -43,6 +43,7 @@ func testInboundTrustTunnel(t *testing.T, inboundOptions inbound.TrustTunnelOpti
|
||||
outboundOptions.UserName = "test"
|
||||
outboundOptions.Password = userUUID
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewTrustTunnel(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -70,6 +70,7 @@ func testInboundTuic0(t *testing.T, inboundOptions inbound.TuicOption, outboundO
|
||||
outboundOptions.Server = addrPort.Addr().String()
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewTuic(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -45,6 +45,7 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound
|
||||
outboundOptions.Port = int(addrPort.Port())
|
||||
outboundOptions.UUID = userUUID
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewVless(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
@@ -46,6 +46,7 @@ func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outbound
|
||||
outboundOptions.AlterID = 0
|
||||
outboundOptions.Cipher = "auto"
|
||||
outboundOptions.DialerForAPI = tunnel.NewDialer()
|
||||
outboundOptions.TunnelForAPI = tunnel
|
||||
|
||||
out, err := outbound.NewVmess(outboundOptions)
|
||||
if !assert.NoError(t, err) {
|
||||
|
||||
Reference in New Issue
Block a user