Skip to content

Commit

Permalink
fix binding address calculation for all commands (issue #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Jan 22, 2020
1 parent 93ef724 commit aef0d00
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
13 changes: 2 additions & 11 deletions cmd/tinc-boot/forget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"bytes"
"encoding/json"
"errors"
cmd2 "github.com/reddec/tinc-boot/cmd"
"github.com/reddec/tinc-boot/types"
"net"
"net/http"
"strconv"
)

type Cmd struct {
Expand Down Expand Up @@ -43,13 +42,5 @@ func (cmd *Cmd) Execute(args []string) error {
}

func (cmd *Cmd) binding() (string, error) {
ief, err := net.InterfaceByName(cmd.Iface)
if err != nil {
return "", err
}
addrs, err := ief.Addrs()
if err != nil {
return "", err
}
return addrs[0].(*net.IPNet).IP.String() + ":" + strconv.Itoa(cmd.Port), nil
return cmd2.BindingByName(cmd.Iface, cmd.Port)
}
13 changes: 2 additions & 11 deletions cmd/tinc-boot/kill/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package kill

import (
"errors"
cmd2 "github.com/reddec/tinc-boot/cmd"
"io"
"net"
"net/http"
"strconv"
)

type Cmd struct {
Expand Down Expand Up @@ -34,13 +33,5 @@ func (cmd *Cmd) Execute(args []string) error {
}

func (cmd *Cmd) binding() (string, error) {
ief, err := net.InterfaceByName(cmd.Iface)
if err != nil {
return "", err
}
addrs, err := ief.Addrs()
if err != nil {
return "", err
}
return addrs[0].(*net.IPNet).IP.String() + ":" + strconv.Itoa(cmd.Port), nil
return cmd2.BindingByName(cmd.Iface, cmd.Port)
}
13 changes: 2 additions & 11 deletions cmd/tinc-boot/watch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"bytes"
"encoding/json"
"errors"
cmd2 "github.com/reddec/tinc-boot/cmd"
"github.com/reddec/tinc-boot/types"
"net"
"net/http"
"strconv"
)

type Cmd struct {
Expand Down Expand Up @@ -43,13 +42,5 @@ func (cmd *Cmd) Execute(args []string) error {
}

func (cmd *Cmd) binding() (string, error) {
ief, err := net.InterfaceByName(cmd.Iface)
if err != nil {
return "", err
}
addrs, err := ief.Addrs()
if err != nil {
return "", err
}
return addrs[0].(*net.IPNet).IP.String() + ":" + strconv.Itoa(cmd.Port), nil
return cmd2.BindingByName(cmd.Iface, cmd.Port)
}
20 changes: 20 additions & 0 deletions cmd/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cmd

import (
"context"
"errors"
"net"
"os"
"os/signal"
"strconv"
)

func SignalContext(parent context.Context) context.Context {
Expand All @@ -22,3 +25,20 @@ func SignalContext(parent context.Context) context.Context {
}()
return ctx
}

func BindingByName(iface string, port int) (string, error) {
ief, err := net.InterfaceByName(iface)
if err != nil {
return "", err
}
addrs, err := ief.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if v, ok := addr.(*net.IPNet); ok && v.IP.IsGlobalUnicast() {
return v.IP.String() + ":" + strconv.Itoa(port), nil
}
}
return "127.0.0.1:0", errors.New("unable to detect binding address")
}
30 changes: 6 additions & 24 deletions domain/monitor/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package monitor

import (
"errors"
"net"
cmd2 "github.com/reddec/tinc-boot/cmd"
"path/filepath"
"strconv"
"time"
)

Expand All @@ -27,24 +25,8 @@ func (cfg *Config) Root() string {
}
return r
}
func (cfg *Config) Hosts() string { return filepath.Join(cfg.Root(), "hosts") }
func (cfg *Config) HostFile() string { return filepath.Join(cfg.Hosts(), cfg.Name) }
func (cfg *Config) TincConf() string { return filepath.Join(cfg.Root(), "tinc.conf") }
func (cfg *Config) Network() string { return filepath.Base(cfg.Root()) }

func (cfg *Config) Binding() (string, error) {
ief, err := net.InterfaceByName(cfg.Iface)
if err != nil {
return "", err
}
addrs, err := ief.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if v, ok := addr.(*net.IPNet); ok && v.IP.IsGlobalUnicast() {
return v.IP.String() + ":" + strconv.Itoa(cfg.Port), nil
}
}
return "127.0.0.1:0", errors.New("unable to detect binding address")
}
func (cfg *Config) Hosts() string { return filepath.Join(cfg.Root(), "hosts") }
func (cfg *Config) HostFile() string { return filepath.Join(cfg.Hosts(), cfg.Name) }
func (cfg *Config) TincConf() string { return filepath.Join(cfg.Root(), "tinc.conf") }
func (cfg *Config) Network() string { return filepath.Base(cfg.Root()) }
func (cfg *Config) Binding() (string, error) { return cmd2.BindingByName(cfg.Iface, cfg.Port) }

0 comments on commit aef0d00

Please sign in to comment.