Merge pull request 'hosts' (#9) from hosts into develop
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #9
This commit is contained in:
JakeHillion 2021-01-23 17:47:31 +00:00
commit 21beb0425e
4 changed files with 70 additions and 14 deletions

View File

@ -72,7 +72,7 @@ func (c Configuration) Build() (*proxy.Proxy, error) {
func buildTcp(p *proxy.Proxy, peer Peer, g func() proxy.MacGenerator, v func() proxy.MacVerifier) error {
if peer.RemoteHost != "" {
f, err := tcp.InitiateFlow(
fmt.Sprintf("%s:", peer.LocalHost),
func() string { return fmt.Sprintf("%s:", peer.GetLocalHost()) },
fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort),
)
@ -86,7 +86,7 @@ func buildTcp(p *proxy.Proxy, peer Peer, g func() proxy.MacGenerator, v func() p
return nil
}
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), v, g)
err := tcp.NewListener(p, fmt.Sprintf("%s:%d", peer.GetLocalHost(), peer.LocalPort), v, g)
if err != nil {
return err
}
@ -107,7 +107,7 @@ func buildUdp(p *proxy.Proxy, peer Peer, g func() proxy.MacGenerator, v func() p
if peer.RemoteHost != "" {
f, err := udp.InitiateFlow(
fmt.Sprintf("%s:", peer.LocalHost),
func() string { return fmt.Sprintf("%s:", peer.GetLocalHost()) },
fmt.Sprintf("%s:%d", peer.RemoteHost, peer.RemotePort),
v(),
g(),
@ -125,7 +125,7 @@ func buildUdp(p *proxy.Proxy, peer Peer, g func() proxy.MacGenerator, v func() p
return nil
}
err := udp.NewListener(p, fmt.Sprintf("%s:%d", peer.LocalHost, peer.LocalPort), v, g, c)
err := udp.NewListener(p, fmt.Sprintf("%s:%d", peer.GetLocalHost(), peer.LocalPort), v, g, c)
if err != nil {
return err
}

View File

@ -1,9 +1,37 @@
package config
import "github.com/go-playground/validator/v10"
import (
"github.com/go-playground/validator/v10"
"log"
"net"
"strings"
)
var v = validator.New()
func init() {
if err := v.RegisterValidation("iface", func(fl validator.FieldLevel) bool {
name, ok := fl.Field().Interface().(string)
if ok {
ifaces, err := net.Interfaces()
if err != nil {
log.Printf("error getting interfaces: %v", err)
return false
}
for _, i := range ifaces {
if i.Name == name {
return true
}
}
}
return false
}); err != nil {
panic(err)
}
}
type Configuration struct {
Host Host
Peers []Peer `validate:"dive"`
@ -19,7 +47,7 @@ type Host struct {
type Peer struct {
Method string `validate:"oneof=TCP UDP"`
LocalHost string `validate:"omitempty,ip"`
LocalHost string `validate:"omitempty,ip|iface"`
LocalPort uint `validate:"max=65535"`
RemoteHost string `validate:"required_with=RemotePort,omitempty,fqdn|ip"`
@ -32,6 +60,33 @@ type Peer struct {
RetryWait uint
}
func (p Peer) GetLocalHost() string {
if err := v.Var(p.LocalHost, "ip"); err == nil {
return p.LocalHost
}
iface, err := net.InterfaceByName(p.LocalHost)
if err != nil {
panic(err)
}
if iface != nil {
addrs, err := iface.Addrs()
if err != nil {
panic(err)
}
if len(addrs) > 0 {
addr := addrs[0].String()
addr = strings.Split(addr, "/")[0]
log.Printf("resolved interface `%s` to `%v`", p.LocalHost, addr)
return addr
}
}
return "invalid"
}
func (c Configuration) Validate() error {
return v.Struct(c)
}

View File

@ -22,7 +22,7 @@ type Conn interface {
}
type InitiatedFlow struct {
Local string
Local func() string
Remote string
mu sync.RWMutex
@ -31,7 +31,7 @@ type InitiatedFlow struct {
}
func (f *InitiatedFlow) String() string {
return fmt.Sprintf("TcpOutbound{%v -> %v}", f.Local, f.Remote)
return fmt.Sprintf("TcpOutbound{%v -> %v}", f.Local(), f.Remote)
}
type Flow struct {
@ -43,7 +43,7 @@ func (f Flow) String() string {
return fmt.Sprintf("TcpInbound{%v -> %v}", f.conn.RemoteAddr(), f.conn.LocalAddr())
}
func InitiateFlow(local, remote string) (*InitiatedFlow, error) {
func InitiateFlow(local func() string, remote string) (*InitiatedFlow, error) {
f := InitiatedFlow{
Local: local,
Remote: remote,
@ -60,7 +60,7 @@ func (f *InitiatedFlow) Reconnect() error {
return nil
}
localAddr, err := net.ResolveTCPAddr("tcp", f.Local)
localAddr, err := net.ResolveTCPAddr("tcp", f.Local())
if err != nil {
return err
}

View File

@ -23,7 +23,7 @@ type PacketConn interface {
}
type InitiatedFlow struct {
Local string
Local func() string
Remote string
g proxy.MacGenerator
@ -34,7 +34,7 @@ type InitiatedFlow struct {
}
func (f *InitiatedFlow) String() string {
return fmt.Sprintf("UdpOutbound{%v -> %v}", f.Local, f.Remote)
return fmt.Sprintf("UdpOutbound{%v -> %v}", f.Local(), f.Remote)
}
type Flow struct {
@ -55,7 +55,8 @@ func (f Flow) String() string {
}
func InitiateFlow(
local, remote string,
local func() string,
remote string,
v proxy.MacVerifier,
g proxy.MacGenerator,
c Congestion,
@ -88,7 +89,7 @@ func (f *InitiatedFlow) Reconnect() error {
return nil
}
localAddr, err := net.ResolveUDPAddr("udp", f.Local)
localAddr, err := net.ResolveUDPAddr("udp", f.Local())
if err != nil {
return err
}