| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package lsprpc |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"net" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"time" |
| 15 |
|
| 16 |
"golang.org/x/tools/internal/event" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
const autoNetwork = "auto" |
| 22 |
|
| 23 |
|
| 24 |
type autoDialer struct { |
| 25 |
network, addr string |
| 26 |
isAuto bool |
| 27 |
|
| 28 |
executable string |
| 29 |
argFunc func(network, addr string) []string |
| 30 |
} |
| 31 |
|
| 32 |
func newAutoDialer(rawAddr string, argFunc func(network, addr string) []string) (*autoDialer, error) { |
| 33 |
d := autoDialer{ |
| 34 |
argFunc: argFunc, |
| 35 |
} |
| 36 |
d.network, d.addr = ParseAddr(rawAddr) |
| 37 |
if d.network == autoNetwork { |
| 38 |
d.isAuto = true |
| 39 |
bin, err := os.Executable() |
| 40 |
if err != nil { |
| 41 |
return nil, fmt.Errorf("getting executable: %w", err) |
| 42 |
} |
| 43 |
d.executable = bin |
| 44 |
d.network, d.addr = autoNetworkAddress(bin, d.addr) |
| 45 |
} |
| 46 |
return &d, nil |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func (d *autoDialer) Dial(ctx context.Context) (io.ReadWriteCloser, error) { |
| 51 |
conn, err := d.dialNet(ctx) |
| 52 |
return conn, err |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func (d *autoDialer) dialNet(ctx context.Context) (net.Conn, error) { |
| 58 |
|
| 59 |
|
| 60 |
ok, err := verifyRemoteOwnership(d.network, d.addr) |
| 61 |
if err != nil { |
| 62 |
|
| 63 |
|
| 64 |
event.Error(ctx, "unable to check daemon socket owner, failing open", err) |
| 65 |
} else if !ok { |
| 66 |
|
| 67 |
|
| 68 |
return nil, fmt.Errorf("socket %q is owned by a different user", d.addr) |
| 69 |
} |
| 70 |
const dialTimeout = 1 * time.Second |
| 71 |
|
| 72 |
netConn, err := net.DialTimeout(d.network, d.addr, dialTimeout) |
| 73 |
if err == nil { |
| 74 |
return netConn, nil |
| 75 |
} |
| 76 |
if d.isAuto && d.argFunc != nil { |
| 77 |
if d.network == "unix" { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if _, err := os.Stat(d.addr); err == nil { |
| 85 |
if err := os.Remove(d.addr); err != nil { |
| 86 |
return nil, fmt.Errorf("removing remote socket file: %w", err) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
args := d.argFunc(d.network, d.addr) |
| 91 |
cmd := exec.Command(d.executable, args...) |
| 92 |
if err := runRemote(cmd); err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
const retries = 5 |
| 98 |
|
| 99 |
|
| 100 |
for retry := range retries { |
| 101 |
startDial := time.Now() |
| 102 |
netConn, err = net.DialTimeout(d.network, d.addr, dialTimeout) |
| 103 |
if err == nil { |
| 104 |
return netConn, nil |
| 105 |
} |
| 106 |
event.Log(ctx, fmt.Sprintf("failed attempt #%d to connect to remote: %v\n", retry+2, err)) |
| 107 |
|
| 108 |
|
| 109 |
if retry != retries-1 { |
| 110 |
time.Sleep(dialTimeout - time.Since(startDial)) |
| 111 |
} |
| 112 |
} |
| 113 |
return nil, fmt.Errorf("dialing remote: %w", err) |
| 114 |
} |