Skip to content

Instantly share code, notes, and snippets.

@mzpqnxow
Created June 2, 2022 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzpqnxow/f7e4b4cd403ba2ced69284c9e1f0a6a4 to your computer and use it in GitHub Desktop.
Save mzpqnxow/f7e4b4cd403ba2ced69284c9e1f0a6a4 to your computer and use it in GitHub Desktop.
diff --git i/conn.go w/conn.go
index ef701c8..f8cfb29 100644
--- i/conn.go
+++ w/conn.go
@@ -5,7 +5,9 @@ import (
"errors"
"io"
"net"
+ "os"
"time"
+ "syscall"
"github.com/sirupsen/logrus"
)
@@ -342,6 +344,27 @@ func (d *Dialer) SetDefaults() *Dialer {
DualStack: true,
}
}
+ if iface := os.Getenv("ZIFACE"); iface != "" {
+
+ ief, err := net.InterfaceByName(iface)
+ if err != nil {
+ panic("InterfaceByName(): interface specified does not exist")
+ }
+
+ addrs, err := ief.Addrs()
+ if err != nil || len(addrs) == 0 {
+ panic("Addrs(): specified interface does not exist or has addresses assigned")
+ }
+ d.Dialer.LocalAddr = addrs[0]
+ d.Dialer.Control = func(network, address string, c syscall.RawConn) error {
+ return c.Control(func (fd uintptr) {
+ if err := syscall.BindToDevice(int(fd), iface); err != nil {
+ panic("BindToDevice(): bind to interface failed")
+ }
+ })
+ }
+
+ }
return d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment