Skip to content

feat: nativeMessagingHost read ports from listening.json #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions go/nativeMessagingHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -221,14 +222,64 @@ func getOpenPortsUnix() (map[string][]string, error) {
return result, nil
}

func extractPort(s string) string {
arr := strings.Split(s, ":")
if len(arr) == 2 {
return arr[1]
}
return ""
}

func getOpenPortsNew() map[string][]string {
var dataPath string
if env := os.Getenv("ANYTYPE_DATA_PATH"); env != "" {
dataPath = env
} else {
if appData, err := os.UserConfigDir(); err == nil {
dataPath = path.Join(appData, "anytype")
}
}
if dataPath == "" {
return nil
}

var s struct {
GRPC string `json:"grpc"`
Web string `json:"web"`
}

f, err := os.Open(path.Join(dataPath, "listening.json"))
if err != nil {
return nil
}
defer f.Close()

err = json.NewDecoder(f).Decode(&s)
if err != nil {
return nil
}

grpcWebPort := extractPort(s.Web)
gatewayPort := extractPort(s.GRPC)
if grpcWebPort == "" || gatewayPort == "" {
return nil
}

return map[string][]string{"0": {grpcWebPort, gatewayPort}}
}

// Windows, MacOS and Linux: returns a list of all open ports for all instances of anytype found using cli utilities
func getOpenPorts() (map[string][]string, error) {
// Get Platform
platform := runtime.GOOS
var (
ports map[string][]string
err error
)
ports = getOpenPortsNew()
if ports != nil {
return ports, nil
}
// Get Platform
platform := runtime.GOOS
// Platform specific functions
if platform == "windows" {
ports, err = getOpenPortsWindows()
Expand Down
Loading