Skip to content

Commit

Permalink
Allow ability to connect directly to IP
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Mar 18, 2024
1 parent 6ec3a62 commit 5c031d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
8 changes: 7 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CLI struct {

LocalKey []byte
ConnectTo *bonjour.ServiceEntry
DirectIP string

L hclog.Logger
Path string
Expand Down Expand Up @@ -64,7 +65,12 @@ func (c *CLI) Shell(cmd string, stdin io.Reader, stdout io.Writer) error {
err error
)

if c.ConnectTo != nil {
if c.DirectIP != "" {
sconn, err = ssh.Dial("tcp", c.DirectIP, &cfg)
if err != nil {
return err
}
} else if c.ConnectTo != nil {
addr := fmt.Sprintf("[%s]:%d", c.ConnectTo.AddrIPv6.String(), c.ConnectTo.Port)
c.L.Info("connecting to bonjour located instance", "addr", addr)

Expand Down
59 changes: 34 additions & 25 deletions cmd/linux/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
for {
select {
case e := <-results:
fmt.Printf("%s @ %s\n %s\n", e.Instance, e.HostName, strings.Join(e.Text, "\n "))
fmt.Printf("%s @ %s (%s, %s)\n %s\n", e.Instance, e.HostName, e.AddrIPv4, e.AddrIPv6, strings.Join(e.Text, "\n "))
case <-t.C:
return
}
Expand Down Expand Up @@ -156,38 +156,46 @@ func main() {

connectTo := *fConnect

var (
ent *bonjour.ServiceEntry
entIP string
)

if connectTo != "" {
fmt.Printf("Looking up isle in bonjour: %s\n", connectTo)
if strings.ContainsRune(connectTo, ':') {
fmt.Printf("Connecting to IP %s\n", connectTo)
entIP = connectTo
} else {
fmt.Printf("Looking up isle in bonjour: %s\n", connectTo)

r, err := bonjour.NewResolver(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating bonjour resolver: %s\n", err)
os.Exit(1)
}
r, err := bonjour.NewResolver(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating bonjour resolver: %s\n", err)
os.Exit(1)
}

results := make(chan *bonjour.ServiceEntry)
results := make(chan *bonjour.ServiceEntry)

err = r.Browse("_isle._tcp", "local.", results)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to browse:", err.Error())
}
err = r.Browse("_isle._tcp", "local.", results)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to browse:", err.Error())
}

t := time.NewTimer(1 * time.Minute)
t := time.NewTimer(1 * time.Minute)

var ent *bonjour.ServiceEntry
loop:
for {
select {
case e := <-results:
if e.Instance == connectTo || e.HostName == connectTo {
ent = e
break loop
}

loop:
for {
select {
case e := <-results:
if e.Instance == connectTo || e.HostName == connectTo {
ent = e
break loop
case <-t.C:
fmt.Fprintf(os.Stderr, "Unable to locate any instance by the name '%s'\n", connectTo)
return
}

case <-t.C:
fmt.Fprintf(os.Stderr, "Unable to locate any instance by the name '%s'\n", connectTo)
return
}
}

Expand All @@ -199,6 +207,7 @@ func main() {
AsRoot: *fRoot,
IsTerm: isTerm,
ConnectTo: ent,
DirectIP: entIP,
Token: *fToken,
}

Expand Down

0 comments on commit 5c031d6

Please sign in to comment.