utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  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 3 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
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package tun
  20. import (
  21. "errors"
  22. "fmt"
  23. "net"
  24. "os/exec"
  25. "strconv"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. )
  28. var unsupportedError = errors.New("operation unsupported on this platform")
  29. // runNetworkConfigCommand execs a network config command, such as "ifconfig"
  30. // or "iptables". On platforms that support capabilities, the network config
  31. // capabilities of the current process is made available to the command
  32. // subprocess. Alternatively, "sudo" will be used when useSudo is true.
  33. func runNetworkConfigCommand(
  34. logger common.Logger,
  35. useSudo bool,
  36. commandName string, commandArgs ...string) error {
  37. // configureSubprocessCapabilities will set inheritable
  38. // capabilities on platforms which support that (Linux).
  39. // Specifically, CAP_NET_ADMIN will be transferred from
  40. // this process to the child command.
  41. err := configureNetworkConfigSubprocessCapabilities()
  42. if err != nil {
  43. return common.ContextError(err)
  44. }
  45. // TODO: use CommandContext to interrupt on server shutdown?
  46. // (the commands currently being issued shouldn't block...)
  47. if useSudo {
  48. commandArgs = append([]string{commandName}, commandArgs...)
  49. commandName = "sudo"
  50. }
  51. cmd := exec.Command(commandName, commandArgs...)
  52. output, err := cmd.CombinedOutput()
  53. logger.WithContextFields(common.LogFields{
  54. "command": commandName,
  55. "args": commandArgs,
  56. "output": string(output),
  57. "error": err,
  58. }).Debug("exec")
  59. if err != nil {
  60. err := fmt.Errorf(
  61. "command %s %+v failed with %s", commandName, commandArgs, string(output))
  62. return common.ContextError(err)
  63. }
  64. return nil
  65. }
  66. func splitIPMask(IPAddressCIDR string) (string, string, error) {
  67. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  68. if err != nil {
  69. return "", "", common.ContextError(err)
  70. }
  71. var netmask string
  72. IPv4Mask := net.IP(IPNet.Mask).To4()
  73. if IPv4Mask != nil {
  74. netmask = fmt.Sprintf(
  75. "%d.%d.%d.%d", IPv4Mask[0], IPv4Mask[1], IPv4Mask[2], IPv4Mask[3])
  76. } else {
  77. netmask = IPNet.Mask.String()
  78. }
  79. return IP.String(), netmask, nil
  80. }
  81. func splitIPPrefixLen(IPAddressCIDR string) (string, string, error) {
  82. IP, IPNet, err := net.ParseCIDR(IPAddressCIDR)
  83. if err != nil {
  84. return "", "", common.ContextError(err)
  85. }
  86. prefixLen, _ := IPNet.Mask.Size()
  87. return IP.String(), strconv.Itoa(prefixLen), nil
  88. }
  89. func getMTU(configMTU int) int {
  90. if configMTU <= 0 {
  91. return DEFAULT_MTU
  92. } else if configMTU > 65536 {
  93. return 65536
  94. }
  95. return configMTU
  96. }