httpTransformer.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2023, 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 transforms
  20. import (
  21. "bytes"
  22. "context"
  23. "math"
  24. "net"
  25. "net/textproto"
  26. "strconv"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  30. )
  31. type HTTPTransformerParameters struct {
  32. // ProtocolTransformName specifies the name associated with
  33. // ProtocolTransformSpec and is used for metrics.
  34. ProtocolTransformName string
  35. // ProtocolTransformSpec specifies a transform to apply to the HTTP request.
  36. // See: "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms".
  37. //
  38. // HTTP transforms include strategies discovered by the Geneva team,
  39. // https://geneva.cs.umd.edu.
  40. ProtocolTransformSpec Spec
  41. // ProtocolTransformSeed specifies the seed to use for generating random
  42. // data in the ProtocolTransformSpec transform. To replay a transform,
  43. // specify the same seed.
  44. ProtocolTransformSeed *prng.Seed
  45. }
  46. const (
  47. // httpTransformerReadHeader HTTPTransformer is waiting to finish reading
  48. // the next HTTP request header.
  49. httpTransformerReadHeader = 0
  50. // httpTransformerReadWriteBody HTTPTransformer is waiting to finish reading
  51. // and writing the current HTTP request body.
  52. httpTransformerReadWriteBody = 1
  53. )
  54. // HTTPTransformer wraps a net.Conn, intercepting Write calls and applying the
  55. // specified protocol transform.
  56. //
  57. // The HTTP request to be written (input to the Write) is converted to a
  58. // string, transformed, and converted back to binary and then actually written
  59. // to the underlying net.Conn.
  60. //
  61. // HTTPTransformer is not safe for concurrent use.
  62. type HTTPTransformer struct {
  63. transform Spec
  64. seed *prng.Seed
  65. // state is the HTTPTransformer state. Possible values are
  66. // httpTransformerReadingHeader and httpTransformerReadingBody.
  67. state int64
  68. // b is the accumulated bytes of the current HTTP request.
  69. b []byte
  70. // remain is the number of remaining HTTP request body bytes to read into b.
  71. remain uint64
  72. net.Conn
  73. }
  74. // Warning: Does not handle chunked encoding. Must be called synchronously.
  75. func (t *HTTPTransformer) Write(b []byte) (int, error) {
  76. if t.state == httpTransformerReadHeader {
  77. t.b = append(t.b, b...)
  78. // Wait until the entire HTTP request header has been read. Must check
  79. // all accumulated bytes incase the "\r\n\r\n" separator is written over
  80. // multiple Write() calls; from reading the net/http code the entire
  81. // HTTP request is written in a single Write() call.
  82. sep := []byte("\r\n\r\n")
  83. headerBodyLines := bytes.SplitN(t.b, sep, 2) // split header and body
  84. if len(headerBodyLines) > 1 {
  85. // read Content-Length before applying transform
  86. var headerLines [][]byte
  87. lines := bytes.Split(headerBodyLines[0], []byte("\r\n"))
  88. if len(lines) > 1 {
  89. // skip request line, e.g. "GET /foo HTTP/1.1"
  90. headerLines = lines[1:]
  91. }
  92. var cl []byte
  93. contentLengthHeader := []byte("Content-Length:")
  94. for _, header := range headerLines {
  95. if bytes.HasPrefix(header, contentLengthHeader) {
  96. cl = textproto.TrimBytes(header[len(contentLengthHeader):])
  97. break
  98. }
  99. }
  100. if len(cl) == 0 {
  101. // Irrecoverable error because either Content-Length header
  102. // missing, or Content-Length header value is empty, e.g.
  103. // "Content-Length: ", and request body length cannot be
  104. // determined.
  105. //
  106. // b buffered in t.b, return len(b) in an attempt to get
  107. // through the current Write() sequence instead of getting
  108. // stuck.
  109. return len(b), errors.TraceNew("Content-Length missing")
  110. }
  111. contentLength, err := strconv.ParseUint(string(cl), 10, 63)
  112. if err != nil {
  113. // Irrecoverable error because Content-Length is malformed and
  114. // request body length cannot be determined.
  115. //
  116. // b buffered in t.b, return len(b) in an attempt to get
  117. // through the current Write() sequence instead of getting
  118. // stuck.
  119. return len(b), errors.Trace(err)
  120. }
  121. t.remain = contentLength
  122. // transform and write header
  123. headerLen := len(headerBodyLines[0]) + len(sep)
  124. header := t.b[:headerLen]
  125. if t.transform != nil {
  126. newHeaderS, err := t.transform.Apply(t.seed, string(header))
  127. if err != nil {
  128. // TODO: consider logging an error and skiping transform
  129. // instead of returning an error, if the transform is broken
  130. // then all subsequent applications may fail.
  131. //
  132. // b buffered in t.b, return len(b) in an attempt to get
  133. // through the current Write() sequence instead of getting
  134. // stuck.
  135. return len(b), errors.Trace(err)
  136. }
  137. newHeader := []byte(newHeaderS)
  138. // only allocate new slice if header length changed
  139. if len(newHeader) == len(header) {
  140. copy(t.b[:len(header)], newHeader)
  141. } else {
  142. t.b = append(newHeader, t.b[len(header):]...)
  143. }
  144. header = newHeader
  145. }
  146. if math.MaxUint64-t.remain < uint64(len(header)) {
  147. // Irrecoverable error because request is malformed:
  148. // Content-Length + len(header) > math.MaxUint64.
  149. //
  150. // b buffered in t.b, return len(b) in an attempt to get
  151. // through the current Write() sequence instead of getting
  152. // stuck.
  153. return len(b), errors.TraceNew("t.remain + uint64(len(header)) overflows")
  154. }
  155. t.remain += uint64(len(header))
  156. n, err := t.writeBuffer()
  157. written := len(b) // all bytes of b buffered in t.b
  158. if n < len(header) ||
  159. len(t.b) > 0 && t.remain == 0 {
  160. // All bytes of b were not written, but all bytes of b have been
  161. // buffered in t.b. Drop 1 byte of b from t.b to pretend 1 byte
  162. // of b was not written to trigger another Write() call. This
  163. // handles the scenario where all request bytes have been
  164. // received but writing to the underlying net.Conn fails and
  165. // another Write() call cannot be expected unless a value
  166. // less than len(b) is returned. An alternative solution would
  167. // be to retry writes, or spawn a goroutine which writes t.b,
  168. // but we want to return the error to the caller immediately so
  169. // it can act accordingly.
  170. written = len(b) - 1
  171. t.b = t.b[:len(t.b)-1]
  172. }
  173. if t.remain > 0 {
  174. t.state = httpTransformerReadWriteBody
  175. }
  176. return written, err
  177. }
  178. // b buffered in t.b and the entire HTTP request header has not been
  179. // recieved so another Write() call is expected.
  180. return len(b), nil
  181. }
  182. // HTTP request header has been transformed. Write any remaining bytes of
  183. // HTTP request header and then write HTTP request body.
  184. // Must write buffered bytes first, in-order, to write bytes to underlying
  185. // Conn in the same order they were received in.
  186. _, err := t.writeBuffer()
  187. if err != nil {
  188. // b not written or buffered
  189. return 0, errors.Trace(err)
  190. }
  191. // Only write bytes of current request
  192. writeN := uint64(len(b))
  193. if writeN > t.remain {
  194. writeN = t.remain
  195. }
  196. n, err := t.Conn.Write(b[:writeN])
  197. // Do not need to check for underflow because n <= t.remain
  198. t.remain -= uint64(n)
  199. if t.remain <= 0 {
  200. // Entire request, header and body, has been written. Return to
  201. // waiting for next HTTP request header to arrive.
  202. //
  203. // Return the number of bytes written to the underlying Conn instead of
  204. // calling t.Write() with any remaining bytes of b which were not
  205. // written or buffered, i.e. when n < len(b). The caller must call
  206. // Write() again with the unwritten, and unbuffered, bytes of b.
  207. t.state = httpTransformerReadHeader
  208. t.remain = 0
  209. }
  210. return n, err
  211. }
  212. func (t *HTTPTransformer) writeBuffer() (written int, err error) {
  213. // Continue writing buffered bytes until either all buffered bytes have
  214. // been written or all remaining bytes of the current HTTP request have
  215. // been written.
  216. for len(t.b) > 0 && t.remain > 0 {
  217. // Write all buffered bytes of the current request
  218. writeN := uint64(len(t.b))
  219. if writeN > t.remain {
  220. // t.b contains bytes of the next request(s), only write current
  221. // request bytes.
  222. writeN = t.remain
  223. }
  224. // Check for potential overflow before Write() call
  225. if math.MaxInt-written < int(writeN) {
  226. return written, errors.TraceNew("written + bytesToWrite overflows")
  227. }
  228. var n int
  229. n, err = t.Conn.Write(t.b[:writeN])
  230. written += n
  231. // Do not need to check for underflow because n <= t.remain
  232. t.remain -= uint64(n)
  233. if n == len(t.b) {
  234. t.b = nil
  235. } else {
  236. t.b = t.b[n:]
  237. }
  238. // Stop writing and return if there was an error
  239. if err != nil {
  240. return
  241. }
  242. }
  243. return
  244. }
  245. func WrapDialerWithHTTPTransformer(dialer common.Dialer, params *HTTPTransformerParameters) common.Dialer {
  246. return func(ctx context.Context, network, addr string) (net.Conn, error) {
  247. conn, err := dialer(ctx, network, addr)
  248. if err != nil {
  249. return nil, errors.Trace(err)
  250. }
  251. return &HTTPTransformer{
  252. Conn: conn,
  253. transform: params.ProtocolTransformSpec,
  254. seed: params.ProtocolTransformSeed,
  255. }, nil
  256. }
  257. }