Skip to content

Commit

Permalink
Remake desktop
Browse files Browse the repository at this point in the history
Optimize change proxy

Optimize network check

Fix fallback issues

Optimize lots of details
  • Loading branch information
chen08209 committed Dec 6, 2024
1 parent 4b32a09 commit ece8a48
Show file tree
Hide file tree
Showing 96 changed files with 5,782 additions and 2,291 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,6 @@ jobs:
arch: arm64

steps:
- name: Setup Mingw64
if: startsWith(matrix.platform,'windows')
uses: msys2/setup-msys2@v2
with:
msystem: mingw64
install: mingw-w64-x86_64-gcc
update: true

- name: Set Mingw64 Env
if: startsWith(matrix.platform,'windows')
run: |
echo "${{ runner.temp }}\msys64\mingw64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Check Matrix
run: |
echo "Running on ${{ matrix.os }}"
echo "Arch: ${{ runner.arch }}"
gcc --version
- name: Checkout
uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -103,7 +84,6 @@ jobs:
path: ./dist
overwrite: true


upload:
permissions: write-all
needs: [ build ]
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/change.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
name: change
name: changelog

on:
push:
branches:
- 'main'
tags:
- 'v*'

jobs:
changelog:
if: ${{ !contains(github.ref, '+') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -61,6 +62,5 @@ jobs:
exit 1
fi
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion core/Clash.Meta
32 changes: 32 additions & 0 deletions core/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !cgo

package main

import (
"encoding/json"
)

func (action Action) Json() ([]byte, error) {
data, err := json.Marshal(action)
return data, err
}

func (action Action) callback(data interface{}) bool {
if conn == nil {
return false
}
sendAction := Action{
Id: action.Id,
Method: action.Method,
Data: data,
}
res, err := sendAction.Json()
if err != nil {
return false
}
_, err = conn.Write(append(res, []byte("\n")...))
if err != nil {
return false
}
return true
}
156 changes: 38 additions & 118 deletions core/common.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
package main

import "C"
import (
"context"
"core/state"
"encoding/json"
"errors"
"fmt"
"github.com/metacubex/mihomo/constant/features"
"github.com/metacubex/mihomo/hub/route"
"github.com/samber/lo"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"time"

"github.com/metacubex/mihomo/adapter"
"github.com/metacubex/mihomo/adapter/inbound"
"github.com/metacubex/mihomo/adapter/outboundgroup"
Expand All @@ -27,84 +14,38 @@ import (
"github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/config"
"github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/constant/features"
cp "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/hub"
"github.com/metacubex/mihomo/hub/executor"
"github.com/metacubex/mihomo/hub/route"
"github.com/metacubex/mihomo/listener"
"github.com/metacubex/mihomo/log"
rp "github.com/metacubex/mihomo/rules/provider"
"github.com/metacubex/mihomo/tunnel"
"github.com/samber/lo"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
)

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"`
OverrideDns bool `json:"override-dns"`
}

type GenerateConfigParams struct {
ProfileId string `json:"profile-id"`
Config config.RawConfig `json:"config" `
Params ConfigExtendedParams `json:"params"`
}

type ChangeProxyParams struct {
GroupName *string `json:"group-name"`
ProxyName *string `json:"proxy-name"`
}

type TestDelayParams struct {
ProxyName string `json:"proxy-name"`
Timeout int64 `json:"timeout"`
}

type ProcessMapItem struct {
Id int64 `json:"id"`
Value string `json:"value"`
}

type ExternalProvider struct {
Name string `json:"name"`
Type string `json:"type"`
VehicleType string `json:"vehicle-type"`
Count int `json:"count"`
Path string `json:"path"`
UpdateAt time.Time `json:"update-at"`
SubscriptionInfo *provider.SubscriptionInfo `json:"subscription-info"`
}
var (
isRunning = false
runLock sync.Mutex
ips = []string{"ipinfo.io", "ipapi.co", "api.ip.sb", "ipwho.is"}
b, _ = batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](50))
)

type ExternalProviders []ExternalProvider

func (a ExternalProviders) Len() int { return len(a) }
func (a ExternalProviders) Less(i, j int) bool { return a[i].Name < a[j].Name }
func (a ExternalProviders) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

var b, _ = batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](50))

func restartExecutable(execPath string) {
var err error
executor.Shutdown()
if runtime.GOOS == "windows" {
cmd := exec.Command(execPath, os.Args[1:]...)
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalln("restarting: %s", err)
}

os.Exit(0)
}

log.Infoln("restarting: %q %q", execPath, os.Args[1:])
err = syscall.Exec(execPath, os.Args, os.Environ())
if err != nil {
log.Fatalln("restarting: %s", err)
}
func (message *Message) Json() (string, error) {
data, err := json.Marshal(message)
return string(data), err
}

func readFile(path string) ([]byte, error) {
Expand All @@ -119,19 +60,6 @@ func readFile(path string) ([]byte, error) {
return data, err
}

func removeFile(path string) error {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
err = os.Remove(absPath)
if err != nil {
return err
}

return nil
}

func getProfilePath(id string) string {
return filepath.Join(constant.Path.HomeDir(), "profiles", id+".yaml")
}
Expand Down Expand Up @@ -262,8 +190,6 @@ func trimArr(arr []string) (r []string) {
return
}

var ips = []string{"ipinfo.io", "ipapi.co", "api.ip.sb", "ipwho.is"}

func overrideRules(rules *[]string) {
var target = ""
for _, line := range *rules {
Expand Down Expand Up @@ -325,20 +251,13 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
}
}
overrideRules(&targetConfig.Rule)
//if runtime.GOOS == "android" {
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, "dhcp://"+dns.SystemDNSPlaceholder)
//} else if runtime.GOOS == "windows" {
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, dns.SystemDNSPlaceholder)
//}
//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)
//}
}

func patchConfig(general *config.General, controller *config.Controller, tls *config.TLS) {
func patchConfig() {
log.Infoln("[Apply] patch")
general := currentConfig.General
controller := currentConfig.Controller
tls := currentConfig.TLS
tunnel.SetSniffing(general.Sniffing)
tunnel.SetFindProcessMode(general.FindProcessMode)
dialer.SetTcpConcurrent(general.TCPConcurrent)
Expand All @@ -365,17 +284,15 @@ func patchConfig(general *config.General, controller *config.Controller, tls *co
})
}

var isRunning = false

var runLock sync.Mutex

func updateListeners(general *config.General, listeners map[string]constant.InboundListener) {
func updateListeners(force bool) {
if !isRunning {
return
}
runLock.Lock()
defer runLock.Unlock()
stopListeners()
general := currentConfig.General
listeners := currentConfig.Listeners
if force == true {
stopListeners()
}
listener.PatchInboundListeners(listeners, tunnel.Tunnel, true)
listener.SetAllowLan(general.AllowLan)
inbound.SetSkipAuthPrefixes(general.SkipAuthPrefixes)
Expand Down Expand Up @@ -424,19 +341,22 @@ func patchSelectGroup() {
}
}

func applyConfig() error {
cfg, err := config.ParseRawConfig(state.CurrentRawConfig)
func applyConfig(rawConfig *config.RawConfig) error {
runLock.Lock()
defer runLock.Unlock()
var err error
currentConfig, err = config.ParseRawConfig(rawConfig)
if err != nil {
cfg, _ = config.ParseRawConfig(config.DefaultRawConfig())
currentConfig, _ = config.ParseRawConfig(config.DefaultRawConfig())
}
if configParams.IsPatch {
patchConfig(cfg.General, cfg.Controller, cfg.TLS)
patchConfig()
} else {
closeConnections()
handleCloseConnectionsUnLock()
runtime.GC()
hub.ApplyConfig(cfg)
hub.ApplyConfig(currentConfig)
patchSelectGroup()
}
updateListeners(cfg.General, cfg.Listeners)
updateListeners(false)
return err
}
Loading

0 comments on commit ece8a48

Please sign in to comment.