interface.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //go:build !windows
  2. // +build !windows
  3. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along
  16. // with this program; if not, see <http://www.gnu.org/licenses/>.
  17. package vpn
  18. import (
  19. "github.com/fumiama/water"
  20. "github.com/vishvananda/netlink"
  21. )
  22. func createInterface(c *Config) (*water.Interface, error) {
  23. config := water.Config{
  24. DeviceType: c.DeviceType,
  25. }
  26. config.Name = c.InterfaceName
  27. return water.New(config)
  28. }
  29. func prepareInterface(c *Config) error {
  30. link, err := netlink.LinkByName(c.InterfaceName)
  31. if err != nil {
  32. return err
  33. }
  34. addr, err := netlink.ParseAddr(c.InterfaceAddress)
  35. if err != nil {
  36. return err
  37. }
  38. err = netlink.LinkSetMTU(link, c.InterfaceMTU)
  39. if err != nil {
  40. return err
  41. }
  42. err = netlink.AddrAdd(link, addr)
  43. if err != nil {
  44. return err
  45. }
  46. err = netlink.LinkSetUp(link)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }