net.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. "net"
  23. "net/http"
  24. "sync"
  25. "sync/atomic"
  26. "time"
  27. "github.com/Psiphon-Labs/goarista/monotime"
  28. )
  29. // Conns is a synchronized list of Conns that is used to coordinate
  30. // interrupting a set of goroutines establishing connections, or
  31. // close a set of open connections, etc.
  32. // Once the list is closed, no more items may be added to the
  33. // list (unless it is reset).
  34. type Conns struct {
  35. mutex sync.Mutex
  36. isClosed bool
  37. conns map[net.Conn]bool
  38. }
  39. func (conns *Conns) Reset() {
  40. conns.mutex.Lock()
  41. defer conns.mutex.Unlock()
  42. conns.isClosed = false
  43. conns.conns = make(map[net.Conn]bool)
  44. }
  45. func (conns *Conns) Add(conn net.Conn) bool {
  46. conns.mutex.Lock()
  47. defer conns.mutex.Unlock()
  48. if conns.isClosed {
  49. return false
  50. }
  51. if conns.conns == nil {
  52. conns.conns = make(map[net.Conn]bool)
  53. }
  54. conns.conns[conn] = true
  55. return true
  56. }
  57. func (conns *Conns) Remove(conn net.Conn) {
  58. conns.mutex.Lock()
  59. defer conns.mutex.Unlock()
  60. delete(conns.conns, conn)
  61. }
  62. func (conns *Conns) CloseAll() {
  63. conns.mutex.Lock()
  64. defer conns.mutex.Unlock()
  65. conns.isClosed = true
  66. for conn := range conns.conns {
  67. conn.Close()
  68. }
  69. conns.conns = make(map[net.Conn]bool)
  70. }
  71. // TerminateHTTPConnection sends a 404 response to a client and also closes
  72. // the persistent connection.
  73. func TerminateHTTPConnection(
  74. responseWriter http.ResponseWriter, request *http.Request) {
  75. http.NotFound(responseWriter, request)
  76. hijack, ok := responseWriter.(http.Hijacker)
  77. if !ok {
  78. return
  79. }
  80. conn, buffer, err := hijack.Hijack()
  81. if err != nil {
  82. return
  83. }
  84. buffer.Flush()
  85. conn.Close()
  86. }
  87. // IPAddressFromAddr is a helper which extracts an IP address
  88. // from a net.Addr or returns "" if there is no IP address.
  89. func IPAddressFromAddr(addr net.Addr) string {
  90. ipAddress := ""
  91. if addr != nil {
  92. host, _, err := net.SplitHostPort(addr.String())
  93. if err == nil {
  94. ipAddress = host
  95. }
  96. }
  97. return ipAddress
  98. }
  99. // LRUConns is a concurrency-safe list of net.Conns ordered
  100. // by recent activity. Its purpose is to facilitate closing
  101. // the oldest connection in a set of connections.
  102. //
  103. // New connections added are referenced by a LRUConnsEntry,
  104. // which is used to Touch() active connections, which
  105. // promotes them to the front of the order and to Remove()
  106. // connections that are no longer LRU candidates.
  107. //
  108. // CloseOldest() will remove the oldest connection from the
  109. // list and call net.Conn.Close() on the connection.
  110. //
  111. // After an entry has been removed, LRUConnsEntry Touch()
  112. // and Remove() will have no effect.
  113. type LRUConns struct {
  114. mutex sync.Mutex
  115. list *list.List
  116. }
  117. // NewLRUConns initializes a new LRUConns.
  118. func NewLRUConns() *LRUConns {
  119. return &LRUConns{list: list.New()}
  120. }
  121. // Add inserts a net.Conn as the freshest connection
  122. // in a LRUConns and returns an LRUConnsEntry to be
  123. // used to freshen the connection or remove the connection
  124. // from the LRU list.
  125. func (conns *LRUConns) Add(conn net.Conn) *LRUConnsEntry {
  126. conns.mutex.Lock()
  127. defer conns.mutex.Unlock()
  128. return &LRUConnsEntry{
  129. lruConns: conns,
  130. element: conns.list.PushFront(conn),
  131. }
  132. }
  133. // CloseOldest closes the oldest connection in a
  134. // LRUConns. It calls net.Conn.Close() on the
  135. // connection.
  136. func (conns *LRUConns) CloseOldest() {
  137. conns.mutex.Lock()
  138. oldest := conns.list.Back()
  139. if oldest != nil {
  140. conns.list.Remove(oldest)
  141. }
  142. // Release mutex before closing conn
  143. conns.mutex.Unlock()
  144. if oldest != nil {
  145. oldest.Value.(net.Conn).Close()
  146. }
  147. }
  148. // LRUConnsEntry is an entry in a LRUConns list.
  149. type LRUConnsEntry struct {
  150. lruConns *LRUConns
  151. element *list.Element
  152. }
  153. // Remove deletes the connection referenced by the
  154. // LRUConnsEntry from the associated LRUConns.
  155. // Has no effect if the entry was not initialized
  156. // or previously removed.
  157. func (entry *LRUConnsEntry) Remove() {
  158. if entry.lruConns == nil || entry.element == nil {
  159. return
  160. }
  161. entry.lruConns.mutex.Lock()
  162. defer entry.lruConns.mutex.Unlock()
  163. entry.lruConns.list.Remove(entry.element)
  164. }
  165. // Touch promotes the connection referenced by the
  166. // LRUConnsEntry to the front of the associated LRUConns.
  167. // Has no effect if the entry was not initialized
  168. // or previously removed.
  169. func (entry *LRUConnsEntry) Touch() {
  170. if entry.lruConns == nil || entry.element == nil {
  171. return
  172. }
  173. entry.lruConns.mutex.Lock()
  174. defer entry.lruConns.mutex.Unlock()
  175. entry.lruConns.list.MoveToFront(entry.element)
  176. }
  177. // Closer defines the interface to a type, typically
  178. // a net.Conn, that can be closed.
  179. type Closer interface {
  180. IsClosed() bool
  181. }
  182. // ActivityMonitoredConn wraps a net.Conn, adding logic to deal with
  183. // events triggered by I/O activity.
  184. //
  185. // When an inactivity timeout is specified, the network I/O will
  186. // timeout after the specified period of read inactivity. Optionally,
  187. // for the purpose of inactivity only, ActivityMonitoredConn will also
  188. // consider the connection active when data is written to it.
  189. //
  190. // When a LRUConnsEntry is specified, then the LRU entry is promoted on
  191. // either a successful read or write.
  192. //
  193. // When an ActivityUpdater is set, then its UpdateActivity method is
  194. // called on each read and write with the number of bytes transferred.
  195. // The durationNanoseconds, which is the time since the last read, is
  196. // reported only on reads.
  197. //
  198. type ActivityMonitoredConn struct {
  199. // Note: 64-bit ints used with atomic operations are placed
  200. // at the start of struct to ensure 64-bit alignment.
  201. // (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
  202. monotonicStartTime int64
  203. lastReadActivityTime int64
  204. realStartTime time.Time
  205. net.Conn
  206. inactivityTimeout time.Duration
  207. activeOnWrite bool
  208. activityUpdater ActivityUpdater
  209. lruEntry *LRUConnsEntry
  210. }
  211. // ActivityUpdater defines an interface for receiving updates for
  212. // ActivityMonitoredConn activity. Values passed to UpdateProgress are
  213. // bytes transferred and conn duration since the previous UpdateProgress.
  214. type ActivityUpdater interface {
  215. UpdateProgress(bytesRead, bytesWritten int64, durationNanoseconds int64)
  216. }
  217. // NewActivityMonitoredConn creates a new ActivityMonitoredConn.
  218. func NewActivityMonitoredConn(
  219. conn net.Conn,
  220. inactivityTimeout time.Duration,
  221. activeOnWrite bool,
  222. activityUpdater ActivityUpdater,
  223. lruEntry *LRUConnsEntry) (*ActivityMonitoredConn, error) {
  224. if inactivityTimeout > 0 {
  225. err := conn.SetDeadline(time.Now().Add(inactivityTimeout))
  226. if err != nil {
  227. return nil, ContextError(err)
  228. }
  229. }
  230. now := int64(monotime.Now())
  231. return &ActivityMonitoredConn{
  232. Conn: conn,
  233. inactivityTimeout: inactivityTimeout,
  234. activeOnWrite: activeOnWrite,
  235. realStartTime: time.Now(),
  236. monotonicStartTime: now,
  237. lastReadActivityTime: now,
  238. activityUpdater: activityUpdater,
  239. lruEntry: lruEntry,
  240. }, nil
  241. }
  242. // GetStartTime gets the time when the ActivityMonitoredConn was
  243. // initialized. Reported time is UTC.
  244. func (conn *ActivityMonitoredConn) GetStartTime() time.Time {
  245. return conn.realStartTime.UTC()
  246. }
  247. // GetActiveDuration returns the time elapsed between the initialization
  248. // of the ActivityMonitoredConn and the last Read. Only reads are used
  249. // for this calculation since writes may succeed locally due to buffering.
  250. func (conn *ActivityMonitoredConn) GetActiveDuration() time.Duration {
  251. return time.Duration(atomic.LoadInt64(&conn.lastReadActivityTime) - conn.monotonicStartTime)
  252. }
  253. // GetLastActivityMonotime returns the arbitrary monotonic time of the last Read.
  254. func (conn *ActivityMonitoredConn) GetLastActivityMonotime() monotime.Time {
  255. return monotime.Time(atomic.LoadInt64(&conn.lastReadActivityTime))
  256. }
  257. func (conn *ActivityMonitoredConn) Read(buffer []byte) (int, error) {
  258. n, err := conn.Conn.Read(buffer)
  259. if err == nil {
  260. if conn.inactivityTimeout > 0 {
  261. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  262. if err != nil {
  263. return n, ContextError(err)
  264. }
  265. }
  266. readActivityTime := int64(monotime.Now())
  267. if conn.activityUpdater != nil {
  268. conn.activityUpdater.UpdateProgress(
  269. int64(n), 0, readActivityTime-atomic.LoadInt64(&conn.lastReadActivityTime))
  270. }
  271. if conn.lruEntry != nil {
  272. conn.lruEntry.Touch()
  273. }
  274. atomic.StoreInt64(&conn.lastReadActivityTime, readActivityTime)
  275. }
  276. // Note: no context error to preserve error type
  277. return n, err
  278. }
  279. func (conn *ActivityMonitoredConn) Write(buffer []byte) (int, error) {
  280. n, err := conn.Conn.Write(buffer)
  281. if err == nil && conn.activeOnWrite {
  282. if conn.inactivityTimeout > 0 {
  283. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  284. if err != nil {
  285. return n, ContextError(err)
  286. }
  287. }
  288. if conn.activityUpdater != nil {
  289. conn.activityUpdater.UpdateProgress(0, int64(n), 0)
  290. }
  291. if conn.lruEntry != nil {
  292. conn.lruEntry.Touch()
  293. }
  294. }
  295. // Note: no context error to preserve error type
  296. return n, err
  297. }
  298. // IsClosed implements the Closer iterface. The return value
  299. // indicates whether the underlying conn has been closed.
  300. func (conn *ActivityMonitoredConn) IsClosed() bool {
  301. closer, ok := conn.Conn.(Closer)
  302. if !ok {
  303. return false
  304. }
  305. return closer.IsClosed()
  306. }