Skip to content

Commit 4236c3f

Browse files
committed
Fix macOS tray browser URL
1 parent 9a66371 commit 4236c3f

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

cmd/comigo/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func main() {
7676
startServer,
7777
shutdownServer,
7878
getServerURL,
79+
getBrowserURL,
7980
config.GetConfigDir,
8081
getStoreUrls,
8182
toggleTailscale,
@@ -117,6 +118,11 @@ func getServerURL() string {
117118
return config.GetQrcodeURL()
118119
}
119120

121+
// getBrowserURL 获取托盘打开浏览器使用的本机 URL,避免局域网地址或自定义 Host 在本机不可访问。
122+
func getBrowserURL() string {
123+
return config.GetLocalBrowserURL()
124+
}
125+
120126
// getStoreUrls 获取书库URL列表
121127
func getStoreUrls() []string {
122128
return config.GetCfg().StoreUrls

config/common.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,17 @@ func GetQrcodeURL() string {
299299
return protocol + OutIP + ":" + strconv.Itoa(int(cfg.Port)) + PrefixPath("/")
300300
}
301301

302+
// GetLocalBrowserURL 返回本机浏览器入口,避免托盘打开时受局域网 IP、Host 或 0.0.0.0 影响。
303+
func GetLocalBrowserURL() string {
304+
protocol := "http://"
305+
if cfg.EnableTLS {
306+
protocol = "https://"
307+
}
308+
return protocol + "127.0.0.1:" + strconv.Itoa(cfg.Port) + PrefixPath("/")
309+
}
310+
302311
func OpenBrowserIfNeeded() {
303312
if cfg.OpenBrowser == true {
304-
protocol := "http://"
305-
if cfg.EnableTLS {
306-
protocol = "https://"
307-
}
308-
go tools.OpenBrowserByURL(protocol + "127.0.0.1:" + strconv.Itoa(cfg.Port) + PrefixPath("/"))
313+
go tools.OpenBrowserByURL(GetLocalBrowserURL())
309314
}
310315
}

tools/open_url_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// openURL 在 macOS 上打开 URL。
1313
func openURL(uri string) error {
14-
cmd := exec.Command("open", uri)
14+
cmd := exec.Command("/usr/bin/open", uri)
1515
if err := cmd.Start(); err != nil {
1616
return fmt.Errorf("open url: %w", err)
1717
}

tools/system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ func OpenBrowserByURL(uri string) {
171171
)
172172
if err != nil {
173173
logger.Infof(locale.GetString("log_api_health_check_failed"), err)
174-
return
174+
} else {
175+
logger.Info(locale.GetString("log_api_healthy_ready"))
175176
}
176-
logger.Info(locale.GetString("log_api_healthy_ready"))
177177

178178
// 打开浏览器(Windows 使用 ShellExecute,避免闪黑框)
179179
logger.Infof(locale.GetString("log_opening_browser"), uri)

tools/system_tray/systray.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
startServerFunc func()
2828
shutdownServerFunc func()
2929
getURLFunc func() string
30+
getBrowserURLFunc func() string
3031
getConfigDirFunc func() (string, error)
3132
getStoreUrlsFunc func() []string
3233
toggleTailscaleFunc func() error
@@ -62,6 +63,7 @@ const noRequestedExitCode int32 = -1
6263
// startServer: 启动服务器的函数
6364
// shutdownServer: 清理服务器的函数
6465
// getURL: 获取服务器URL的函数
66+
// getBrowserURL: 获取本机浏览器URL的函数
6567
// getConfigDir: 获取配置目录的函数
6668
// getStoreUrls: 获取书库URL列表的函数
6769
// toggleTailscale: 切换Tailscale状态的函数
@@ -72,6 +74,7 @@ const noRequestedExitCode int32 = -1
7274
func SetupSystray(
7375
startServer, shutdownServer func(),
7476
getURL func() string,
77+
getBrowserURL func() string,
7578
getConfigDir func() (string, error),
7679
getStoreUrls func() []string,
7780
toggleTailscale func() error,
@@ -82,6 +85,7 @@ func SetupSystray(
8285
startServerFunc = startServer
8386
shutdownServerFunc = shutdownServer
8487
getURLFunc = getURL
88+
getBrowserURLFunc = getBrowserURL
8589
getConfigDirFunc = getConfigDir
8690
getStoreUrlsFunc = getStoreUrls
8791
toggleTailscaleFunc = toggleTailscale
@@ -142,8 +146,8 @@ func initMenuItems() {
142146
// 创建菜单项
143147
menuItems.mOpenBrowser = systray.AddMenuItem(locale.GetString("systray_open_browser"), locale.GetString("systray_open_browser_tooltip"))
144148
menuItems.mOpenBrowser.Click(func() {
145-
if getURLFunc != nil {
146-
url := getURLFunc()
149+
if getBrowserURLFunc != nil {
150+
url := getBrowserURLFunc()
147151
go tools.OpenBrowserByURL(url)
148152
logger.Infof(locale.GetString("log_opening_browser"), url)
149153
}

0 commit comments

Comments
 (0)