50 lines
1015 B
Go
50 lines
1015 B
Go
//go:build linux && !android
|
|
// +build linux,!android
|
|
|
|
package Atom_Device
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/milosgajdos/tenus"
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
"net"
|
|
)
|
|
|
|
func NewTun(ifName string, mtu int, localIP net.IP, ipMask net.IPMask) (tun.Device, error) {
|
|
ipNet := &net.IPNet{
|
|
IP: localIP.Mask(ipMask),
|
|
Mask: ipMask,
|
|
}
|
|
|
|
tunDevice, err := tun.CreateTUN(ifName, mtu)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create tun: %v", err)
|
|
}
|
|
|
|
link, err := tenus.NewLinkFrom(ifName)
|
|
if nil != err {
|
|
return nil, fmt.Errorf("unable to get interface info: %v", err)
|
|
}
|
|
|
|
err = link.SetLinkIp(localIP, ipNet)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to set IP (%s) to (%v on interface): %v", localIP, ipNet, err)
|
|
}
|
|
|
|
err = link.SetLinkUp()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to UP interface: %v", err)
|
|
}
|
|
|
|
return tunDevice, nil
|
|
}
|
|
|
|
func (d *Device) InterfaceName() (string, error) {
|
|
interfaceName, err := d.tun.Name()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return interfaceName, nil
|
|
}
|