net.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 server
  20. import (
  21. "container/list"
  22. "io"
  23. "net"
  24. "sync"
  25. "sync/atomic"
  26. "time"
  27. "github.com/Psiphon-Inc/ratelimit"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  29. )
  30. // LRUConns is a concurrency-safe list of net.Conns ordered
  31. // by recent activity. Its purpose is to facilitate closing
  32. // the oldest connection in a set of connections.
  33. //
  34. // New connections added are referenced by a LRUConnsEntry,
  35. // which is used to Touch() active connections, which
  36. // promotes them to the front of the order and to Remove()
  37. // connections that are no longer LRU candidates.
  38. //
  39. // CloseOldest() will remove the oldest connection from the
  40. // list and call net.Conn.Close() on the connection.
  41. //
  42. // After an entry has been removed, LRUConnsEntry Touch()
  43. // and Remove() will have no effect.
  44. type LRUConns struct {
  45. mutex sync.Mutex
  46. list *list.List
  47. }
  48. // NewLRUConns initializes a new LRUConns.
  49. func NewLRUConns() *LRUConns {
  50. return &LRUConns{list: list.New()}
  51. }
  52. // Add inserts a net.Conn as the freshest connection
  53. // in a LRUConns and returns an LRUConnsEntry to be
  54. // used to freshen the connection or remove the connection
  55. // from the LRU list.
  56. func (conns *LRUConns) Add(conn net.Conn) *LRUConnsEntry {
  57. conns.mutex.Lock()
  58. defer conns.mutex.Unlock()
  59. return &LRUConnsEntry{
  60. lruConns: conns,
  61. element: conns.list.PushFront(conn),
  62. }
  63. }
  64. // CloseOldest closes the oldest connection in a
  65. // LRUConns. It calls net.Conn.Close() on the
  66. // connection.
  67. func (conns *LRUConns) CloseOldest() {
  68. conns.mutex.Lock()
  69. oldest := conns.list.Back()
  70. conn, ok := oldest.Value.(net.Conn)
  71. if oldest != nil {
  72. conns.list.Remove(oldest)
  73. }
  74. // Release mutex before closing conn
  75. conns.mutex.Unlock()
  76. if ok {
  77. conn.Close()
  78. }
  79. }
  80. // LRUConnsEntry is an entry in a LRUConns list.
  81. type LRUConnsEntry struct {
  82. lruConns *LRUConns
  83. element *list.Element
  84. }
  85. // Remove deletes the connection referenced by the
  86. // LRUConnsEntry from the associated LRUConns.
  87. // Has no effect if the entry was not initialized
  88. // or previously removed.
  89. func (entry *LRUConnsEntry) Remove() {
  90. if entry.lruConns == nil || entry.element == nil {
  91. return
  92. }
  93. entry.lruConns.mutex.Lock()
  94. defer entry.lruConns.mutex.Unlock()
  95. entry.lruConns.list.Remove(entry.element)
  96. }
  97. // Touch promotes the connection referenced by the
  98. // LRUConnsEntry to the front of the associated LRUConns.
  99. // Has no effect if the entry was not initialized
  100. // or previously removed.
  101. func (entry *LRUConnsEntry) Touch() {
  102. if entry.lruConns == nil || entry.element == nil {
  103. return
  104. }
  105. entry.lruConns.mutex.Lock()
  106. defer entry.lruConns.mutex.Unlock()
  107. entry.lruConns.list.MoveToFront(entry.element)
  108. }
  109. // ActivityMonitoredConn wraps a net.Conn, adding logic to deal with
  110. // events triggered by I/O activity.
  111. //
  112. // When an inactivity timeout is specified, the network I/O will
  113. // timeout after the specified period of read inactivity. Optionally,
  114. // ActivityMonitoredConn will also consider the connection active when
  115. // data is written to it.
  116. //
  117. // When a LRUConnsEntry is specified, then the LRU entry is promoted on
  118. // either a successful read or write.
  119. //
  120. type ActivityMonitoredConn struct {
  121. net.Conn
  122. inactivityTimeout time.Duration
  123. activeOnWrite bool
  124. startTime int64
  125. lastReadActivityTime int64
  126. lruEntry *LRUConnsEntry
  127. }
  128. func NewActivityMonitoredConn(
  129. conn net.Conn,
  130. inactivityTimeout time.Duration,
  131. activeOnWrite bool,
  132. lruEntry *LRUConnsEntry) (*ActivityMonitoredConn, error) {
  133. if inactivityTimeout > 0 {
  134. err := conn.SetDeadline(time.Now().Add(inactivityTimeout))
  135. if err != nil {
  136. return nil, psiphon.ContextError(err)
  137. }
  138. }
  139. now := time.Now().UnixNano()
  140. return &ActivityMonitoredConn{
  141. Conn: conn,
  142. inactivityTimeout: inactivityTimeout,
  143. activeOnWrite: activeOnWrite,
  144. startTime: now,
  145. lastReadActivityTime: now,
  146. lruEntry: lruEntry,
  147. }, nil
  148. }
  149. // GetStartTime gets the time when the ActivityMonitoredConn was
  150. // initialized.
  151. func (conn *ActivityMonitoredConn) GetStartTime() time.Time {
  152. return time.Unix(0, conn.startTime)
  153. }
  154. // GetActiveDuration returns the time elapsed between the initialization
  155. // of the ActivityMonitoredConn and the last Read. Only reads are used
  156. // for this calculation since writes may succeed locally due to buffering.
  157. func (conn *ActivityMonitoredConn) GetActiveDuration() time.Duration {
  158. return time.Duration(atomic.LoadInt64(&conn.lastReadActivityTime) - conn.startTime)
  159. }
  160. func (conn *ActivityMonitoredConn) Read(buffer []byte) (int, error) {
  161. n, err := conn.Conn.Read(buffer)
  162. if err == nil {
  163. if conn.inactivityTimeout > 0 {
  164. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  165. if err != nil {
  166. return n, psiphon.ContextError(err)
  167. }
  168. }
  169. if conn.lruEntry != nil {
  170. conn.lruEntry.Touch()
  171. }
  172. atomic.StoreInt64(&conn.lastReadActivityTime, time.Now().UnixNano())
  173. }
  174. // Note: no context error to preserve error type
  175. return n, err
  176. }
  177. func (conn *ActivityMonitoredConn) Write(buffer []byte) (int, error) {
  178. n, err := conn.Conn.Write(buffer)
  179. if err == nil && conn.activeOnWrite {
  180. if conn.inactivityTimeout > 0 {
  181. err = conn.Conn.SetDeadline(time.Now().Add(conn.inactivityTimeout))
  182. if err != nil {
  183. return n, psiphon.ContextError(err)
  184. }
  185. }
  186. if conn.lruEntry != nil {
  187. conn.lruEntry.Touch()
  188. }
  189. }
  190. // Note: no context error to preserve error type
  191. return n, err
  192. }
  193. // ThrottledConn wraps a net.Conn with read and write rate limiters.
  194. // Rates are specified as bytes per second. Optional unlimited byte
  195. // counts allow for a number of bytes to read or write before
  196. // applying rate limiting. Specify limit values of 0 to set no rate
  197. // limit (unlimited counts are ignored in this case).
  198. // The underlying rate limiter uses the token bucket algorithm to
  199. // calculate delay times for read and write operations.
  200. type ThrottledConn struct {
  201. net.Conn
  202. unlimitedReadBytes int64
  203. limitingReads int32
  204. limitedReader io.Reader
  205. unlimitedWriteBytes int64
  206. limitingWrites int32
  207. limitedWriter io.Writer
  208. }
  209. // NewThrottledConn initializes a new ThrottledConn.
  210. func NewThrottledConn(
  211. conn net.Conn,
  212. unlimitedReadBytes, limitReadBytesPerSecond,
  213. unlimitedWriteBytes, limitWriteBytesPerSecond int64) *ThrottledConn {
  214. // When no limit is specified, the rate limited reader/writer
  215. // is simply the base reader/writer.
  216. var reader io.Reader
  217. if limitReadBytesPerSecond == 0 {
  218. reader = conn
  219. } else {
  220. reader = ratelimit.Reader(conn,
  221. ratelimit.NewBucketWithRate(
  222. float64(limitReadBytesPerSecond), limitReadBytesPerSecond))
  223. }
  224. var writer io.Writer
  225. if limitWriteBytesPerSecond == 0 {
  226. writer = conn
  227. } else {
  228. writer = ratelimit.Writer(conn,
  229. ratelimit.NewBucketWithRate(
  230. float64(limitWriteBytesPerSecond), limitWriteBytesPerSecond))
  231. }
  232. return &ThrottledConn{
  233. Conn: conn,
  234. unlimitedReadBytes: unlimitedReadBytes,
  235. limitingReads: 0,
  236. limitedReader: reader,
  237. unlimitedWriteBytes: unlimitedWriteBytes,
  238. limitingWrites: 0,
  239. limitedWriter: writer,
  240. }
  241. }
  242. func (conn *ThrottledConn) Read(buffer []byte) (int, error) {
  243. // Use the base reader until the unlimited count is exhausted.
  244. if atomic.LoadInt32(&conn.limitingReads) == 0 {
  245. if atomic.AddInt64(&conn.unlimitedReadBytes, -int64(len(buffer))) <= 0 {
  246. atomic.StoreInt32(&conn.limitingReads, 1)
  247. } else {
  248. return conn.Read(buffer)
  249. }
  250. }
  251. return conn.limitedReader.Read(buffer)
  252. }
  253. func (conn *ThrottledConn) Write(buffer []byte) (int, error) {
  254. // Use the base writer until the unlimited count is exhausted.
  255. if atomic.LoadInt32(&conn.limitingWrites) == 0 {
  256. if atomic.AddInt64(&conn.unlimitedWriteBytes, -int64(len(buffer))) <= 0 {
  257. atomic.StoreInt32(&conn.limitingWrites, 1)
  258. } else {
  259. return conn.Write(buffer)
  260. }
  261. }
  262. return conn.limitedWriter.Write(buffer)
  263. }