net.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2016, 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 common
  20. import (
  21. "container/list"
  22. "context"
  23. "net"
  24. "net/http"
  25. "strconv"
  26. "sync"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  29. "github.com/miekg/dns"
  30. "github.com/wader/filtertransport"
  31. )
  32. // Dialer is a custom network dialer.
  33. type Dialer func(context.Context, string, string) (net.Conn, error)
  34. // NetDialer mimicks the net.Dialer interface.
  35. type NetDialer interface {
  36. Dial(network, address string) (net.Conn, error)
  37. DialContext(ctx context.Context, network, address string) (net.Conn, error)
  38. }
  39. // Closer defines the interface to a type, typically a net.Conn, that can be
  40. // closed.
  41. type Closer interface {
  42. IsClosed() bool
  43. }
  44. // CloseWriter defines the interface to a type, typically a net.TCPConn, that
  45. // implements CloseWrite.
  46. type CloseWriter interface {
  47. CloseWrite() error
  48. }
  49. // IrregularIndicator defines the interface for a type, typically a net.Conn,
  50. // that detects and reports irregular conditions during initial network
  51. // connection establishment.
  52. type IrregularIndicator interface {
  53. IrregularTunnelError() error
  54. }
  55. // UnderlyingTCPAddrSource defines the interface for a type, typically a
  56. // net.Conn, such as a server meek Conn, which has an underlying TCP conn(s),
  57. // providing access to the LocalAddr and RemoteAddr properties of the
  58. // underlying TCP conn.
  59. type UnderlyingTCPAddrSource interface {
  60. // GetUnderlyingTCPAddrs returns the LocalAddr and RemoteAddr properties of
  61. // the underlying TCP conn.
  62. GetUnderlyingTCPAddrs() (*net.TCPAddr, *net.TCPAddr, bool)
  63. }
  64. // FragmentorReplayAccessor defines the interface for accessing replay properties
  65. // of a fragmentor Conn.
  66. type FragmentorReplayAccessor interface {
  67. SetReplay(*prng.PRNG)
  68. GetReplay() (*prng.Seed, bool)
  69. }
  70. // HTTPRoundTripper is an adapter that allows using a function as a
  71. // http.RoundTripper.
  72. type HTTPRoundTripper struct {
  73. roundTrip func(*http.Request) (*http.Response, error)
  74. }
  75. // NewHTTPRoundTripper creates a new HTTPRoundTripper, using the specified
  76. // roundTrip function for HTTP round trips.
  77. func NewHTTPRoundTripper(
  78. roundTrip func(*http.Request) (*http.Response, error)) *HTTPRoundTripper {
  79. return &HTTPRoundTripper{roundTrip: roundTrip}
  80. }
  81. // RoundTrip implements http.RoundTripper RoundTrip.
  82. func (h HTTPRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
  83. return h.roundTrip(request)
  84. }
  85. // TerminateHTTPConnection sends a 404 response to a client and also closes
  86. // the persistent connection.
  87. func TerminateHTTPConnection(
  88. responseWriter http.ResponseWriter, request *http.Request) {
  89. http.NotFound(responseWriter, request)
  90. hijack, ok := responseWriter.(http.Hijacker)
  91. if !ok {
  92. return
  93. }
  94. conn, buffer, err := hijack.Hijack()
  95. if err != nil {
  96. return
  97. }
  98. buffer.Flush()
  99. conn.Close()
  100. }
  101. // IPAddressFromAddr is a helper which extracts an IP address
  102. // from a net.Addr or returns "" if there is no IP address.
  103. func IPAddressFromAddr(addr net.Addr) string {
  104. ipAddress := ""
  105. if addr != nil {
  106. host, _, err := net.SplitHostPort(addr.String())
  107. if err == nil {
  108. ipAddress = host
  109. }
  110. }
  111. return ipAddress
  112. }
  113. // PortFromAddr is a helper which extracts a port number from a net.Addr or
  114. // returns 0 if there is no port number.
  115. func PortFromAddr(addr net.Addr) int {
  116. port := 0
  117. if addr != nil {
  118. _, portStr, err := net.SplitHostPort(addr.String())
  119. if err == nil {
  120. port, _ = strconv.Atoi(portStr)
  121. }
  122. }
  123. return port
  124. }
  125. // Conns is a synchronized list of Conns that is used to coordinate
  126. // interrupting a set of goroutines establishing connections, or
  127. // close a set of open connections, etc.
  128. // Once the list is closed, no more items may be added to the
  129. // list (unless it is reset).
  130. type Conns struct {
  131. mutex sync.Mutex
  132. isClosed bool
  133. conns map[net.Conn]bool
  134. }
  135. // NewConns initializes a new Conns.
  136. func NewConns() *Conns {
  137. return &Conns{}
  138. }
  139. func (conns *Conns) Reset() {
  140. conns.mutex.Lock()
  141. defer conns.mutex.Unlock()
  142. conns.isClosed = false
  143. conns.conns = make(map[net.Conn]bool)
  144. }
  145. func (conns *Conns) Add(conn net.Conn) bool {
  146. conns.mutex.Lock()
  147. defer conns.mutex.Unlock()
  148. if conns.isClosed {
  149. return false
  150. }
  151. if conns.conns == nil {
  152. conns.conns = make(map[net.Conn]bool)
  153. }
  154. conns.conns[conn] = true
  155. return true
  156. }
  157. func (conns *Conns) Remove(conn net.Conn) {
  158. conns.mutex.Lock()
  159. defer conns.mutex.Unlock()
  160. delete(conns.conns, conn)
  161. }
  162. func (conns *Conns) CloseAll() {
  163. conns.mutex.Lock()
  164. defer conns.mutex.Unlock()
  165. conns.isClosed = true
  166. for conn := range conns.conns {
  167. conn.Close()
  168. }
  169. conns.conns = make(map[net.Conn]bool)
  170. }
  171. // LRUConns is a concurrency-safe list of net.Conns ordered
  172. // by recent activity. Its purpose is to facilitate closing
  173. // the oldest connection in a set of connections.
  174. //
  175. // New connections added are referenced by a LRUConnsEntry,
  176. // which is used to Touch() active connections, which
  177. // promotes them to the front of the order and to Remove()
  178. // connections that are no longer LRU candidates.
  179. //
  180. // CloseOldest() will remove the oldest connection from the
  181. // list and call net.Conn.Close() on the connection.
  182. //
  183. // After an entry has been removed, LRUConnsEntry Touch()
  184. // and Remove() will have no effect.
  185. type LRUConns struct {
  186. mutex sync.Mutex
  187. list *list.List
  188. }
  189. // NewLRUConns initializes a new LRUConns.
  190. func NewLRUConns() *LRUConns {
  191. return &LRUConns{list: list.New()}
  192. }
  193. // Add inserts a net.Conn as the freshest connection
  194. // in a LRUConns and returns an LRUConnsEntry to be
  195. // used to freshen the connection or remove the connection
  196. // from the LRU list.
  197. func (conns *LRUConns) Add(conn net.Conn) *LRUConnsEntry {
  198. conns.mutex.Lock()
  199. defer conns.mutex.Unlock()
  200. return &LRUConnsEntry{
  201. lruConns: conns,
  202. element: conns.list.PushFront(conn),
  203. }
  204. }
  205. // CloseOldest closes the oldest connection in a
  206. // LRUConns. It calls net.Conn.Close() on the
  207. // connection.
  208. func (conns *LRUConns) CloseOldest() {
  209. conns.mutex.Lock()
  210. oldest := conns.list.Back()
  211. if oldest != nil {
  212. conns.list.Remove(oldest)
  213. }
  214. // Release mutex before closing conn
  215. conns.mutex.Unlock()
  216. if oldest != nil {
  217. oldest.Value.(net.Conn).Close()
  218. }
  219. }
  220. // LRUConnsEntry is an entry in a LRUConns list.
  221. type LRUConnsEntry struct {
  222. lruConns *LRUConns
  223. element *list.Element
  224. }
  225. // Remove deletes the connection referenced by the
  226. // LRUConnsEntry from the associated LRUConns.
  227. // Has no effect if the entry was not initialized
  228. // or previously removed.
  229. func (entry *LRUConnsEntry) Remove() {
  230. if entry.lruConns == nil || entry.element == nil {
  231. return
  232. }
  233. entry.lruConns.mutex.Lock()
  234. defer entry.lruConns.mutex.Unlock()
  235. entry.lruConns.list.Remove(entry.element)
  236. }
  237. // Touch promotes the connection referenced by the
  238. // LRUConnsEntry to the front of the associated LRUConns.
  239. // Has no effect if the entry was not initialized
  240. // or previously removed.
  241. func (entry *LRUConnsEntry) Touch() {
  242. if entry.lruConns == nil || entry.element == nil {
  243. return
  244. }
  245. entry.lruConns.mutex.Lock()
  246. defer entry.lruConns.mutex.Unlock()
  247. entry.lruConns.list.MoveToFront(entry.element)
  248. }
  249. // IsBogon checks if the specified IP is a bogon (loopback, private addresses,
  250. // link-local addresses, etc.)
  251. func IsBogon(IP net.IP) bool {
  252. return filtertransport.FindIPNet(
  253. filtertransport.DefaultFilteredNetworks, IP)
  254. }
  255. // ParseDNSQuestion parses a DNS message. When the message is a query,
  256. // the first question, a fully-qualified domain name, is returned.
  257. //
  258. // For other valid DNS messages, "" is returned. An error is returned only
  259. // for invalid DNS messages.
  260. //
  261. // Limitations:
  262. // - Only the first Question field is extracted.
  263. // - ParseDNSQuestion only functions for plaintext DNS and cannot
  264. // extract domains from DNS-over-TLS/HTTPS, etc.
  265. func ParseDNSQuestion(request []byte) (string, error) {
  266. m := new(dns.Msg)
  267. err := m.Unpack(request)
  268. if err != nil {
  269. return "", errors.Trace(err)
  270. }
  271. if len(m.Question) > 0 {
  272. return m.Question[0].Name, nil
  273. }
  274. return "", nil
  275. }