2022-10-27 17:50:29 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-31 12:43:04 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-10-27 17:50:29 +08:00
|
|
|
"net"
|
|
|
|
|
2022-10-27 19:50:06 +08:00
|
|
|
"github.com/gravitl/netmaker/nm-proxy/wg"
|
2022-10-27 17:50:29 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultBodySize = 10000
|
|
|
|
defaultPort = 51722
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Port int
|
|
|
|
BodySize int
|
|
|
|
Addr string
|
|
|
|
RemoteKey string
|
2022-10-31 12:43:04 +08:00
|
|
|
LocalKey string
|
2022-10-27 17:50:29 +08:00
|
|
|
WgInterface *wg.WGIface
|
|
|
|
AllowedIps []net.IPNet
|
|
|
|
PreSharedKey *wgtypes.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy - WireguardProxy proxies
|
|
|
|
type Proxy struct {
|
|
|
|
Ctx context.Context
|
|
|
|
Cancel context.CancelFunc
|
|
|
|
|
|
|
|
Config Config
|
|
|
|
RemoteConn net.Conn
|
|
|
|
LocalConn net.Conn
|
|
|
|
}
|
2022-10-31 12:43:04 +08:00
|
|
|
|
|
|
|
func GetInterfaceIpv4Addr(interfaceName string) (addr string, err error) {
|
|
|
|
var (
|
|
|
|
ief *net.Interface
|
|
|
|
addrs []net.Addr
|
|
|
|
ipv4Addr net.IP
|
|
|
|
)
|
|
|
|
if ief, err = net.InterfaceByName(interfaceName); err != nil { // get interface
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if addrs, err = ief.Addrs(); err != nil { // get addresses
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, addr := range addrs { // get ipv4 address
|
|
|
|
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ipv4Addr == nil {
|
|
|
|
return "", errors.New(fmt.Sprintf("interface %s don't have an ipv4 address\n", interfaceName))
|
|
|
|
}
|
|
|
|
return ipv4Addr.String(), nil
|
|
|
|
}
|