Add ua selector

Support modify test url

Optimize android proxy

Fix the error that async proxy provider could not selected the proxy
This commit is contained in:
chen08209
2024-07-02 08:08:31 +08:00
parent 6dcb466fd3
commit 5c3a0c576d
43 changed files with 1247 additions and 301 deletions

View File

@@ -4,6 +4,7 @@ import "C"
import (
"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/adapter/outboundgroup"
ap "github.com/metacubex/mihomo/adapter/provider"
"github.com/metacubex/mihomo/component/dialer"
"github.com/metacubex/mihomo/component/resolver"
@@ -59,11 +60,17 @@ type ruleProviderSchema struct {
Interval int `provider:"interval,omitempty"`
}
type ConfigExtendedParams struct {
IsPatch bool `json:"is-patch"`
IsCompatible bool `json:"is-compatible"`
SelectedMap map[string]string `json:"selected-map"`
TestURL *string `json:"test-url"`
}
type GenerateConfigParams struct {
ProfilePath *string `json:"profile-path"`
Config *config.RawConfig `json:"config" `
IsPatch *bool `json:"is-patch"`
IsCompatible *bool `json:"is-compatible"`
ProfilePath *string `json:"profile-path"`
Config config.RawConfig `json:"config" `
Params ConfigExtendedParams `json:"params"`
}
type ChangeProxyParams struct {
@@ -170,9 +177,9 @@ func getRawConfigWithPath(path *string) *config.RawConfig {
}
}
func decorationConfig(profilePath *string, cfg config.RawConfig, compatible bool) *config.RawConfig {
func decorationConfig(profilePath *string, cfg config.RawConfig) *config.RawConfig {
prof := getRawConfigWithPath(profilePath)
overwriteConfig(prof, cfg, compatible)
overwriteConfig(prof, cfg)
return prof
}
@@ -322,7 +329,7 @@ func generateProxyGroupAndRule(proxyGroup *[]map[string]any, rule *[]string) {
*rule = computedRule
}
func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfig, compatible bool) {
func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfig) {
targetConfig.ExternalController = patchConfig.ExternalController
targetConfig.ExternalUI = ""
targetConfig.Interface = ""
@@ -352,7 +359,7 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
//} else if runtime.GOOS == "windows" {
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, dns.SystemDNSPlaceholder)
//}
if compatible == false {
if configParams.IsCompatible == false {
targetConfig.ProxyProvider = make(map[string]map[string]any)
targetConfig.RuleProvider = make(map[string]map[string]any)
generateProxyGroupAndRule(&targetConfig.ProxyGroup, &targetConfig.Rule)
@@ -388,15 +395,44 @@ func patchConfig(general *config.General) {
resolver.DisableIPv6 = !general.IPv6
}
func applyConfig(isPatch bool) {
func patchSelectGroup() {
mapping := configParams.SelectedMap
if mapping == nil {
return
}
for name, proxy := range tunnel.ProxiesWithProviders() {
outbound, ok := proxy.(*adapter.Proxy)
if !ok {
continue
}
selector, ok := outbound.ProxyAdapter.(outboundgroup.SelectAble)
if !ok {
continue
}
selected, exist := mapping[name]
if !exist {
continue
}
selector.ForceSet(selected)
}
}
func applyConfig() {
cfg, err := config.ParseRawConfig(currentConfig)
if err != nil {
cfg, _ = config.ParseRawConfig(config.DefaultRawConfig())
}
if isPatch {
if configParams.TestURL != nil {
constant.DefaultTestURL = *configParams.TestURL
}
if configParams.IsPatch {
patchConfig(cfg.General)
} else {
runtime.GC()
hub.UltraApplyConfig(cfg, true)
patchSelectGroup()
}
}

View File

@@ -14,6 +14,7 @@ const (
Process MessageType = "process"
Request MessageType = "request"
Run MessageType = "run"
Loaded MessageType = "loaded"
)
type Message struct {

View File

@@ -31,6 +31,8 @@ import (
var currentConfig = config.DefaultRawConfig()
var configParams = ConfigExtendedParams{}
var isInit = false
//export initClash
@@ -92,17 +94,14 @@ func updateConfig(s *C.char, port C.longlong) {
go func() {
var params = &GenerateConfigParams{}
err := json.Unmarshal([]byte(paramsString), params)
configParams = params.Params
if err != nil {
bridge.SendToPort(i, err.Error())
return
}
prof := decorationConfig(params.ProfilePath, *params.Config, *params.IsCompatible)
prof := decorationConfig(params.ProfilePath, params.Config)
currentConfig = prof
if *params.IsPatch {
applyConfig(true)
} else {
applyConfig(false)
}
applyConfig()
bridge.SendToPort(i, "")
}()
}
@@ -161,8 +160,8 @@ func changeProxy(s *C.char) {
groupName := *params.GroupName
proxyName := *params.ProxyName
proxies := tunnel.ProxiesWithProviders()
group := proxies[groupName]
if group == nil {
group, ok := proxies[groupName]
if !ok {
return
}
adapterProxy := group.(*adapter.Proxy)
@@ -441,4 +440,10 @@ func init() {
Data: c,
})
}
executor.DefaultProxyProviderLoadedHook = func(providerName string) {
bridge.SendMessage(bridge.Message{
Type: bridge.Loaded,
Data: providerName,
})
}
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/metacubex/mihomo/log"
"golang.org/x/sync/semaphore"
"sync"
"sync/atomic"
"syscall"
"time"
)
@@ -22,11 +23,11 @@ type FdMap struct {
m sync.Map
}
func (cm *FdMap) Store(key int) {
func (cm *FdMap) Store(key int64) {
cm.m.Store(key, struct{}{})
}
func (cm *FdMap) Load(key int) bool {
func (cm *FdMap) Load(key int64) bool {
_, ok := cm.m.Load(key)
return ok
}
@@ -80,36 +81,44 @@ var errBlocked = errors.New("blocked")
//export setFdMap
func setFdMap(fd C.long) {
fdInt := int(fd)
fdInt := int64(fd)
go func() {
fdMap.Store(fdInt)
}()
}
var fdCounter int64 = 0
func init() {
dialer.DefaultSocketHook = func(network, address string, conn syscall.RawConn) error {
if platform.ShouldBlockConnection() {
return errBlocked
}
return conn.Control(func(fd uintptr) {
fdInt := int(fd)
//timeout := time.After(100 * time.Millisecond)
if tun != nil {
tun.MarkSocket(fdInt)
time.Sleep(100 * time.Millisecond)
if tun == nil {
return
}
fdInt := int64(fd)
timeout := time.After(100 * time.Millisecond)
id := atomic.AddInt64(&fdCounter, 1)
tun.MarkSocket(t.Fd{
Id: id,
Value: fdInt,
})
for {
select {
case <-timeout:
return
default:
exists := fdMap.Load(id)
if exists {
return
}
time.Sleep(10 * time.Millisecond)
}
}
//for {
// select {
// case <-timeout:
// return
// default:
// exists := fdMap.Load(fdInt)
// if exists {
// return
// }
// time.Sleep(20 * time.Millisecond)
// }
//}
})
}
}

View File

@@ -19,7 +19,6 @@ import (
"io"
"net"
"os"
"strconv"
"time"
)
@@ -187,7 +186,12 @@ func Start(fd int, gateway, portal, dns string) (io.Closer, error) {
return stack, nil
}
func (t *Tun) MarkSocket(fd int) {
type Fd struct {
Id int64 `json:"id"`
Value int64 `json:"value"`
}
func (t *Tun) MarkSocket(fd Fd) {
_ = t.Limit.Acquire(context.Background(), 1)
defer t.Limit.Release(1)
@@ -197,7 +201,7 @@ func (t *Tun) MarkSocket(fd int) {
message := &bridge.Message{
Type: bridge.Tun,
Data: strconv.Itoa(fd),
Data: fd,
}
bridge.SendMessage(*message)