mirror of
https://github.com/anyproto/anytype-ts.git
synced 2025-06-07 21:47:02 +09:00
nmh: add debug log
in case we failed to detect mw services ports
This commit is contained in:
parent
9018b00a86
commit
aeea95ecaf
3 changed files with 40 additions and 15 deletions
7
go.mod
7
go.mod
|
@ -1,3 +1,8 @@
|
|||
module nativeMessagingHost
|
||||
|
||||
go 1.21
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
)
|
||||
|
|
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -0,0 +1,4 @@
|
|||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
|
@ -13,6 +13,7 @@ import (
|
|||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
@ -24,6 +25,8 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
)
|
||||
|
||||
// UTILITY FUNCTIONS
|
||||
|
@ -126,13 +129,13 @@ func getOpenPortsWindows() (map[string][]string, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func isFileGateway(port string) bool {
|
||||
func isFileGateway(port string) (bool, error) {
|
||||
client := &http.Client{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", "http://127.0.0.1:"+port+"/file", nil)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
// disable follow redirect
|
||||
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||
|
@ -141,7 +144,7 @@ func isFileGateway(port string) bool {
|
|||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
|
||||
bu := bytes.NewBuffer(nil)
|
||||
|
@ -151,35 +154,35 @@ func isFileGateway(port string) bool {
|
|||
defer resp.Body.Close()
|
||||
// should return 301 redirect Location: /file/
|
||||
if resp.StatusCode == 301 {
|
||||
return true
|
||||
return true, err
|
||||
}
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
|
||||
func isGrpcWebServer(port string) bool {
|
||||
func isGrpcWebServer(port string) (bool, error) {
|
||||
client := &http.Client{}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
var data = strings.NewReader(`AAAAAAIQFA==`)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", "http://127.0.0.1:"+port+"/anytype.ClientCommands/AppGetVersion", data)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, err
|
||||
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/grpc-web-text")
|
||||
req.Header.Set("X-Grpc-Web", "1")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// should has Content-Type: application/grpc-web-text
|
||||
if resp.Header.Get("Content-Type") == "application/grpc-web-text" {
|
||||
return true
|
||||
return true, err
|
||||
}
|
||||
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
|
||||
// MacOS and Linux: returns a list of all open ports for all instances of anytype found using cli utilities lsof and grep
|
||||
|
@ -247,23 +250,36 @@ func getOpenPorts() (map[string][]string, error) {
|
|||
} else {
|
||||
return nil, errors.New("unsupported platform")
|
||||
}
|
||||
totalPids := len(ports)
|
||||
for pid, pidports := range ports {
|
||||
var gatewayPort, grpcWebPort string
|
||||
var merr multierror.Error
|
||||
for _, port := range pidports {
|
||||
if isFileGateway(port) {
|
||||
var (
|
||||
errDetectGateway, errDetectGrpcWeb error
|
||||
v bool
|
||||
)
|
||||
if v, errDetectGateway = isFileGateway(port); v {
|
||||
gatewayPort = port
|
||||
} else if isGrpcWebServer(port) {
|
||||
} else if v, errDetectGrpcWeb = isGrpcWebServer(port); v {
|
||||
grpcWebPort = port
|
||||
} else {
|
||||
merr.Errors = append(merr.Errors, fmt.Errorf("pid %s; port: %s: gateway: %v; grpcweb: %v", pid, port, errDetectGateway, errDetectGrpcWeb))
|
||||
}
|
||||
|
||||
}
|
||||
if gatewayPort != "" && grpcWebPort != "" {
|
||||
ports[pid] = []string{grpcWebPort, gatewayPort}
|
||||
} else {
|
||||
Trace.Printf("can't detect ports. grpc: '%s'; gateway: '%s'; error: %v;", grpcWebPort, gatewayPort, merr.ErrorOrNil())
|
||||
delete(ports, pid)
|
||||
}
|
||||
}
|
||||
Trace.Printf("found ports: %v", ports)
|
||||
|
||||
if len(ports) > 0 {
|
||||
Trace.Printf("found ports: %v", ports)
|
||||
} else {
|
||||
Trace.Printf("ports no able to detect for %d pids", totalPids)
|
||||
}
|
||||
return ports, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue