Feature/android dns (#943)

Support DNS feature on mobile systems

---------

Co-authored-by: Givi Khojanashvili <gigovich@gmail.com>
This commit is contained in:
Zoltan Papp
2023-06-12 14:43:55 +02:00
committed by GitHub
parent f8da516128
commit 481465e1ae
27 changed files with 749 additions and 702 deletions

View File

@@ -36,15 +36,6 @@ func (w *WGIface) GetBind() *bind.ICEBind {
return w.tun.iceBind
}
// Create creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one.
func (w *WGIface) Create() error {
w.mu.Lock()
defer w.mu.Unlock()
log.Debugf("create WireGuard interface %s", w.tun.DeviceName())
return w.tun.Create()
}
// Name returns the interface name
func (w *WGIface) Name() string {
return w.tun.DeviceName()

View File

@@ -1,9 +1,11 @@
package iface
import (
"fmt"
"sync"
"github.com/pion/transport/v2"
log "github.com/sirupsen/logrus"
)
// NewWGIFace Creates a new WireGuard interface instance
@@ -27,7 +29,16 @@ func NewWGIFace(ifaceName string, address string, mtu int, tunAdapter TunAdapter
return wgIFace, nil
}
// SetInitialRoutes store the given routes and on the tun creation will be used
func (w *WGIface) SetInitialRoutes(routes []string) {
w.tun.SetRoutes(routes)
// CreateOnMobile creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one.
func (w *WGIface) CreateOnMobile(mIFaceArgs MobileIFaceArguments) error {
w.mu.Lock()
defer w.mu.Unlock()
log.Debugf("create WireGuard interface %s", w.tun.DeviceName())
return w.tun.Create(mIFaceArgs)
}
// Create this function make sense on mobile only
func (w *WGIface) Create() error {
return fmt.Errorf("this function has not implemented on mobile")
}

View File

@@ -3,9 +3,11 @@
package iface
import (
"fmt"
"sync"
"github.com/pion/transport/v2"
log "github.com/sirupsen/logrus"
)
// NewWGIFace Creates a new WireGuard interface instance
@@ -26,7 +28,16 @@ func NewWGIFace(iFaceName string, address string, mtu int, tunAdapter TunAdapter
return wgIFace, nil
}
// SetInitialRoutes unused function on non Android
func (w *WGIface) SetInitialRoutes(routes []string) {
// CreateOnMobile this function make sense on mobile only
func (w *WGIface) CreateOnMobile(mIFaceArgs MobileIFaceArguments) error {
return fmt.Errorf("this function has not implemented on non mobile")
}
// Create creates a new Wireguard interface, sets a given IP and brings it up.
// Will reuse an existing one.
func (w *WGIface) Create() error {
w.mu.Lock()
defer w.mu.Unlock()
log.Debugf("create WireGuard interface %s", w.tun.DeviceName())
return w.tun.Create()
}

View File

@@ -1,5 +1,10 @@
package iface
type MobileIFaceArguments struct {
Routes []string
Dns string
}
// NetInterface represents a generic network tunnel interface
type NetInterface interface {
Close() error

View File

@@ -2,6 +2,6 @@ package iface
// TunAdapter is an interface for create tun device from externel service
type TunAdapter interface {
ConfigureInterface(address string, mtu int, routes string) (int, error)
ConfigureInterface(address string, mtu int, dns string, routes string) (int, error)
UpdateAddr(address string) error
}

View File

@@ -15,13 +15,12 @@ import (
type tunDevice struct {
address WGAddress
mtu int
routes []string
tunAdapter TunAdapter
iceBind *bind.ICEBind
fd int
name string
device *device.Device
iceBind *bind.ICEBind
wrapper *DeviceWrapper
}
@@ -34,14 +33,10 @@ func newTunDevice(address WGAddress, mtu int, tunAdapter TunAdapter, transportNe
}
}
func (t *tunDevice) SetRoutes(routes []string) {
t.routes = routes
}
func (t *tunDevice) Create() error {
func (t *tunDevice) Create(mIFaceArgs MobileIFaceArguments) error {
var err error
routesString := t.routesToString()
t.fd, err = t.tunAdapter.ConfigureInterface(t.address.String(), t.mtu, routesString)
routesString := t.routesToString(mIFaceArgs.Routes)
t.fd, err = t.tunAdapter.ConfigureInterface(t.address.String(), t.mtu, mIFaceArgs.Dns, routesString)
if err != nil {
log.Errorf("failed to create Android interface: %s", err)
return err
@@ -95,6 +90,6 @@ func (t *tunDevice) Close() (err error) {
return
}
func (t *tunDevice) routesToString() string {
return strings.Join(t.routes, ";")
func (t *tunDevice) routesToString(routes []string) string {
return strings.Join(routes, ";")
}