Skip to content

Commit ece8a48

Browse files
committed
Remake desktop
Optimize change proxy Optimize network check Fix fallback issues Optimize lots of details
1 parent 4b32a09 commit ece8a48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+5782
-2291
lines changed

.github/workflows/build.yaml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,6 @@ jobs:
2727
arch: arm64
2828

2929
steps:
30-
- name: Setup Mingw64
31-
if: startsWith(matrix.platform,'windows')
32-
uses: msys2/setup-msys2@v2
33-
with:
34-
msystem: mingw64
35-
install: mingw-w64-x86_64-gcc
36-
update: true
37-
38-
- name: Set Mingw64 Env
39-
if: startsWith(matrix.platform,'windows')
40-
run: |
41-
echo "${{ runner.temp }}\msys64\mingw64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
42-
43-
- name: Check Matrix
44-
run: |
45-
echo "Running on ${{ matrix.os }}"
46-
echo "Arch: ${{ runner.arch }}"
47-
gcc --version
48-
4930
- name: Checkout
5031
uses: actions/checkout@v4
5132
with:
@@ -103,7 +84,6 @@ jobs:
10384
path: ./dist
10485
overwrite: true
10586

106-
10787
upload:
10888
permissions: write-all
10989
needs: [ build ]

.github/workflows/change.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
name: change
1+
name: changelog
22

33
on:
44
push:
5-
branches:
6-
- 'main'
5+
tags:
6+
- 'v*'
77

88
jobs:
99
changelog:
10+
if: ${{ !contains(github.ref, '+') }}
1011
runs-on: ubuntu-latest
1112
steps:
1213
- name: Checkout
@@ -61,6 +62,5 @@ jobs:
6162
exit 1
6263
fi
6364
fi
64-
6565
env:
6666
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

core/action.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build !cgo
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
)
8+
9+
func (action Action) Json() ([]byte, error) {
10+
data, err := json.Marshal(action)
11+
return data, err
12+
}
13+
14+
func (action Action) callback(data interface{}) bool {
15+
if conn == nil {
16+
return false
17+
}
18+
sendAction := Action{
19+
Id: action.Id,
20+
Method: action.Method,
21+
Data: data,
22+
}
23+
res, err := sendAction.Json()
24+
if err != nil {
25+
return false
26+
}
27+
_, err = conn.Write(append(res, []byte("\n")...))
28+
if err != nil {
29+
return false
30+
}
31+
return true
32+
}

core/common.go

Lines changed: 38 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
package main
22

3-
import "C"
43
import (
54
"context"
6-
"core/state"
5+
"encoding/json"
76
"errors"
87
"fmt"
9-
"github.com/metacubex/mihomo/constant/features"
10-
"github.com/metacubex/mihomo/hub/route"
11-
"github.com/samber/lo"
12-
"os"
13-
"os/exec"
14-
"path/filepath"
15-
"runtime"
16-
"strings"
17-
"sync"
18-
"syscall"
19-
"time"
20-
218
"github.com/metacubex/mihomo/adapter"
229
"github.com/metacubex/mihomo/adapter/inbound"
2310
"github.com/metacubex/mihomo/adapter/outboundgroup"
@@ -27,84 +14,38 @@ import (
2714
"github.com/metacubex/mihomo/component/resolver"
2815
"github.com/metacubex/mihomo/config"
2916
"github.com/metacubex/mihomo/constant"
17+
"github.com/metacubex/mihomo/constant/features"
3018
cp "github.com/metacubex/mihomo/constant/provider"
3119
"github.com/metacubex/mihomo/hub"
32-
"github.com/metacubex/mihomo/hub/executor"
20+
"github.com/metacubex/mihomo/hub/route"
3321
"github.com/metacubex/mihomo/listener"
3422
"github.com/metacubex/mihomo/log"
3523
rp "github.com/metacubex/mihomo/rules/provider"
3624
"github.com/metacubex/mihomo/tunnel"
25+
"github.com/samber/lo"
26+
"os"
27+
"path/filepath"
28+
"runtime"
29+
"strings"
30+
"sync"
3731
)
3832

39-
type ConfigExtendedParams struct {
40-
IsPatch bool `json:"is-patch"`
41-
IsCompatible bool `json:"is-compatible"`
42-
SelectedMap map[string]string `json:"selected-map"`
43-
TestURL *string `json:"test-url"`
44-
OverrideDns bool `json:"override-dns"`
45-
}
46-
47-
type GenerateConfigParams struct {
48-
ProfileId string `json:"profile-id"`
49-
Config config.RawConfig `json:"config" `
50-
Params ConfigExtendedParams `json:"params"`
51-
}
52-
53-
type ChangeProxyParams struct {
54-
GroupName *string `json:"group-name"`
55-
ProxyName *string `json:"proxy-name"`
56-
}
57-
58-
type TestDelayParams struct {
59-
ProxyName string `json:"proxy-name"`
60-
Timeout int64 `json:"timeout"`
61-
}
62-
63-
type ProcessMapItem struct {
64-
Id int64 `json:"id"`
65-
Value string `json:"value"`
66-
}
67-
68-
type ExternalProvider struct {
69-
Name string `json:"name"`
70-
Type string `json:"type"`
71-
VehicleType string `json:"vehicle-type"`
72-
Count int `json:"count"`
73-
Path string `json:"path"`
74-
UpdateAt time.Time `json:"update-at"`
75-
SubscriptionInfo *provider.SubscriptionInfo `json:"subscription-info"`
76-
}
33+
var (
34+
isRunning = false
35+
runLock sync.Mutex
36+
ips = []string{"ipinfo.io", "ipapi.co", "api.ip.sb", "ipwho.is"}
37+
b, _ = batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](50))
38+
)
7739

7840
type ExternalProviders []ExternalProvider
7941

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

84-
var b, _ = batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](50))
85-
86-
func restartExecutable(execPath string) {
87-
var err error
88-
executor.Shutdown()
89-
if runtime.GOOS == "windows" {
90-
cmd := exec.Command(execPath, os.Args[1:]...)
91-
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
92-
cmd.Stdin = os.Stdin
93-
cmd.Stdout = os.Stdout
94-
cmd.Stderr = os.Stderr
95-
err = cmd.Start()
96-
if err != nil {
97-
log.Fatalln("restarting: %s", err)
98-
}
99-
100-
os.Exit(0)
101-
}
102-
103-
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
104-
err = syscall.Exec(execPath, os.Args, os.Environ())
105-
if err != nil {
106-
log.Fatalln("restarting: %s", err)
107-
}
46+
func (message *Message) Json() (string, error) {
47+
data, err := json.Marshal(message)
48+
return string(data), err
10849
}
10950

11051
func readFile(path string) ([]byte, error) {
@@ -119,19 +60,6 @@ func readFile(path string) ([]byte, error) {
11960
return data, err
12061
}
12162

122-
func removeFile(path string) error {
123-
absPath, err := filepath.Abs(path)
124-
if err != nil {
125-
return err
126-
}
127-
err = os.Remove(absPath)
128-
if err != nil {
129-
return err
130-
}
131-
132-
return nil
133-
}
134-
13563
func getProfilePath(id string) string {
13664
return filepath.Join(constant.Path.HomeDir(), "profiles", id+".yaml")
13765
}
@@ -262,8 +190,6 @@ func trimArr(arr []string) (r []string) {
262190
return
263191
}
264192

265-
var ips = []string{"ipinfo.io", "ipapi.co", "api.ip.sb", "ipwho.is"}
266-
267193
func overrideRules(rules *[]string) {
268194
var target = ""
269195
for _, line := range *rules {
@@ -325,20 +251,13 @@ func overwriteConfig(targetConfig *config.RawConfig, patchConfig config.RawConfi
325251
}
326252
}
327253
overrideRules(&targetConfig.Rule)
328-
//if runtime.GOOS == "android" {
329-
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, "dhcp://"+dns.SystemDNSPlaceholder)
330-
//} else if runtime.GOOS == "windows" {
331-
// targetConfig.DNS.NameServer = append(targetConfig.DNS.NameServer, dns.SystemDNSPlaceholder)
332-
//}
333-
//if configParams.IsCompatible == false {
334-
// targetConfig.ProxyProvider = make(map[string]map[string]any)
335-
// targetConfig.RuleProvider = make(map[string]map[string]any)
336-
// generateProxyGroupAndRule(&targetConfig.ProxyGroup, &targetConfig.Rule)
337-
//}
338254
}
339255

340-
func patchConfig(general *config.General, controller *config.Controller, tls *config.TLS) {
256+
func patchConfig() {
341257
log.Infoln("[Apply] patch")
258+
general := currentConfig.General
259+
controller := currentConfig.Controller
260+
tls := currentConfig.TLS
342261
tunnel.SetSniffing(general.Sniffing)
343262
tunnel.SetFindProcessMode(general.FindProcessMode)
344263
dialer.SetTcpConcurrent(general.TCPConcurrent)
@@ -365,17 +284,15 @@ func patchConfig(general *config.General, controller *config.Controller, tls *co
365284
})
366285
}
367286

368-
var isRunning = false
369-
370-
var runLock sync.Mutex
371-
372-
func updateListeners(general *config.General, listeners map[string]constant.InboundListener) {
287+
func updateListeners(force bool) {
373288
if !isRunning {
374289
return
375290
}
376-
runLock.Lock()
377-
defer runLock.Unlock()
378-
stopListeners()
291+
general := currentConfig.General
292+
listeners := currentConfig.Listeners
293+
if force == true {
294+
stopListeners()
295+
}
379296
listener.PatchInboundListeners(listeners, tunnel.Tunnel, true)
380297
listener.SetAllowLan(general.AllowLan)
381298
inbound.SetSkipAuthPrefixes(general.SkipAuthPrefixes)
@@ -424,19 +341,22 @@ func patchSelectGroup() {
424341
}
425342
}
426343

427-
func applyConfig() error {
428-
cfg, err := config.ParseRawConfig(state.CurrentRawConfig)
344+
func applyConfig(rawConfig *config.RawConfig) error {
345+
runLock.Lock()
346+
defer runLock.Unlock()
347+
var err error
348+
currentConfig, err = config.ParseRawConfig(rawConfig)
429349
if err != nil {
430-
cfg, _ = config.ParseRawConfig(config.DefaultRawConfig())
350+
currentConfig, _ = config.ParseRawConfig(config.DefaultRawConfig())
431351
}
432352
if configParams.IsPatch {
433-
patchConfig(cfg.General, cfg.Controller, cfg.TLS)
353+
patchConfig()
434354
} else {
435-
closeConnections()
355+
handleCloseConnectionsUnLock()
436356
runtime.GC()
437-
hub.ApplyConfig(cfg)
357+
hub.ApplyConfig(currentConfig)
438358
patchSelectGroup()
439359
}
440-
updateListeners(cfg.General, cfg.Listeners)
360+
updateListeners(false)
441361
return err
442362
}

0 commit comments

Comments
 (0)