interface_freebsd.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //go:build freebsd
  2. // +build freebsd
  3. /*
  4. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package vpn
  16. import (
  17. "fmt"
  18. "github.com/mudler/water"
  19. "os/exec"
  20. )
  21. func createInterface(c *Config) (*water.Interface, error) {
  22. config := water.Config{
  23. DeviceType: c.DeviceType,
  24. }
  25. config.Name = c.InterfaceName
  26. return water.New(config)
  27. }
  28. func prepareInterface(c *Config) error {
  29. err := sh(fmt.Sprintf("ifconfig %s create", c.InterfaceName))
  30. if err != nil {
  31. return err
  32. }
  33. err = sh(fmt.Sprintf("ifconfig %s inet %s %s netmask %s", c.InterfaceName, c.InterfaceAddress, c.InterfaceAddress, "255.255.255.0"))
  34. if err != nil {
  35. return err
  36. }
  37. return sh(fmt.Sprintf("ifconfig %s up", c.InterfaceName))
  38. }
  39. func sh(c string) (err error) {
  40. _, err = exec.Command("/bin/sh", "-c", c).CombinedOutput()
  41. return
  42. }