pt_extras.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2014, Yawning Angel <yawning at torproject dot org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package main
  28. import (
  29. "errors"
  30. "fmt"
  31. "net"
  32. "net/url"
  33. "os"
  34. "strconv"
  35. "git.torproject.org/pluggable-transports/goptlib.git"
  36. )
  37. // This file contains things that probably should be in goptlib but are not
  38. // yet or are not finalized.
  39. func ptEnvError(msg string) error {
  40. line := []byte(fmt.Sprintf("ENV-ERROR %s\n", msg))
  41. _, _ = pt.Stdout.Write(line)
  42. return errors.New(msg)
  43. }
  44. func ptProxyError(msg string) error {
  45. line := []byte(fmt.Sprintf("PROXY-ERROR %s\n", msg))
  46. _, _ = pt.Stdout.Write(line)
  47. return errors.New(msg)
  48. }
  49. func ptProxyDone() {
  50. line := []byte("PROXY DONE\n")
  51. _, _ = pt.Stdout.Write(line)
  52. }
  53. func ptIsClient() (bool, error) {
  54. clientEnv := os.Getenv("TOR_PT_CLIENT_TRANSPORTS")
  55. serverEnv := os.Getenv("TOR_PT_SERVER_TRANSPORTS")
  56. if clientEnv != "" && serverEnv != "" {
  57. return false, ptEnvError("TOR_PT_[CLIENT,SERVER]_TRANSPORTS both set")
  58. } else if clientEnv != "" {
  59. return true, nil
  60. } else if serverEnv != "" {
  61. return false, nil
  62. }
  63. return false, errors.New("not launched as a managed transport")
  64. }
  65. func ptGetProxy() (*url.URL, error) {
  66. specString := os.Getenv("TOR_PT_PROXY")
  67. if specString == "" {
  68. return nil, nil
  69. }
  70. spec, err := url.Parse(specString)
  71. if err != nil {
  72. return nil, ptProxyError(fmt.Sprintf("failed to parse proxy config: %s", err))
  73. }
  74. // Validate the TOR_PT_PROXY uri.
  75. if !spec.IsAbs() {
  76. return nil, ptProxyError("proxy URI is relative, must be absolute")
  77. }
  78. if spec.Path != "" {
  79. return nil, ptProxyError("proxy URI has a path defined")
  80. }
  81. if spec.RawQuery != "" {
  82. return nil, ptProxyError("proxy URI has a query defined")
  83. }
  84. if spec.Fragment != "" {
  85. return nil, ptProxyError("proxy URI has a fragment defined")
  86. }
  87. switch spec.Scheme {
  88. case "http":
  89. // The most forgiving of proxies.
  90. case "socks4a":
  91. if spec.User != nil {
  92. _, isSet := spec.User.Password()
  93. if isSet {
  94. return nil, ptProxyError("proxy URI specified SOCKS4a and a password")
  95. }
  96. }
  97. case "socks5":
  98. if spec.User != nil {
  99. // UNAME/PASSWD both must be between 1 and 255 bytes long. (RFC1929)
  100. user := spec.User.Username()
  101. passwd, isSet := spec.User.Password()
  102. if len(user) < 1 || len(user) > 255 {
  103. return nil, ptProxyError("proxy URI specified a invalid SOCKS5 username")
  104. }
  105. if !isSet || len(passwd) < 1 || len(passwd) > 255 {
  106. return nil, ptProxyError("proxy URI specified a invalid SOCKS5 password")
  107. }
  108. }
  109. default:
  110. return nil, ptProxyError(fmt.Sprintf("proxy URI has invalid scheme: %s", spec.Scheme))
  111. }
  112. _, err = resolveAddrStr(spec.Host)
  113. if err != nil {
  114. return nil, ptProxyError(fmt.Sprintf("proxy URI has invalid host: %s", err))
  115. }
  116. return spec, nil
  117. }
  118. // Sigh, pt.resolveAddr() isn't exported. Include our own getto version that
  119. // doesn't work around #7011, because we don't work with pre-0.2.5.x tor, and
  120. // all we care about is validation anyway.
  121. func resolveAddrStr(addrStr string) (*net.TCPAddr, error) {
  122. ipStr, portStr, err := net.SplitHostPort(addrStr)
  123. if err != nil {
  124. return nil, err
  125. }
  126. if ipStr == "" {
  127. return nil, net.InvalidAddrError(fmt.Sprintf("address string %q lacks a host part", addrStr))
  128. }
  129. if portStr == "" {
  130. return nil, net.InvalidAddrError(fmt.Sprintf("address string %q lacks a port part", addrStr))
  131. }
  132. ip := net.ParseIP(ipStr)
  133. if ip == nil {
  134. return nil, net.InvalidAddrError(fmt.Sprintf("not an IP string: %q", ipStr))
  135. }
  136. port, err := strconv.ParseUint(portStr, 10, 16)
  137. if err != nil {
  138. return nil, net.InvalidAddrError(fmt.Sprintf("not a Port string: %q", portStr))
  139. }
  140. return &net.TCPAddr{IP: ip, Port: int(port), Zone: ""}, nil
  141. }
  142. // Feature #15435 adds a new env var for determining if Tor keeps stdin
  143. // open for use in termination detection.
  144. func ptShouldExitOnStdinClose() bool {
  145. return os.Getenv("TOR_PT_EXIT_ON_STDIN_CLOSE") == "1"
  146. }