utils.go 3.0 KB

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