tactics.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /*
  2. * Copyright (c) 2018, 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. /*
  20. Package tactics provides dynamic Psiphon client configuration based on GeoIP
  21. attributes, API parameters, and speed test data. The tactics implementation
  22. works in concert with the "parameters" package, allowing contextual
  23. optimization of Psiphon client parameters; for example, customizing
  24. NetworkLatencyMultiplier to adjust timeouts for clients on slow networks; or
  25. customizing LimitTunnelProtocols and ConnectionWorkerPoolSize to circumvent
  26. specific blocking conditions.
  27. Clients obtain tactics from a Psiphon server. Tactics are configured with a hot-
  28. reloadable, JSON format server config file. The config file specifies default
  29. tactics for all clients as well as a list of filtered tactics. For each filter,
  30. if the client's attributes satisfy the filter then additional tactics are merged
  31. into the tactics set provided to the client.
  32. Tactics configuration is optimized for a modest number of filters -- dozens --
  33. and very many GeoIP matches in each filter.
  34. A Psiphon client "tactics request" is an an untunneled, pre-establishment
  35. request to obtain tactics, which will in turn be applied and used in the normal
  36. tunnel establishment sequence; the tactics request may result in custom
  37. timeouts, protocol selection, and other tunnel establishment behavior.
  38. The client will delay its normal establishment sequence and launch a tactics
  39. request only when it has no stored, valid tactics for its current network
  40. context. The normal establishment sequence will begin, regardless of tactics
  41. request outcome, after TacticsWaitPeriod; this ensures that the client will not
  42. stall its establishment process when the tactics request cannot complete.
  43. Tactics are configured with a TTL, which is converted to an expiry time on the
  44. client when tactics are received and stored. When the client starts its
  45. establishment sequence and finds stored, unexpired tactics, no tactics request
  46. is made. The expiry time serves to prevent execess tactics requests and avoid a
  47. fingerprintable network sequence that would result from always performing the
  48. tactics request.
  49. The client calls UseStoredTactics to check for stored tactics; and if none is
  50. found (there is no record or it is expired) the client proceeds to call
  51. FetchTactics to make the tactics request.
  52. In the Psiphon client and server, the tactics request is transported using the
  53. meek protocol. In this case, meek is configured as a simple HTTP round trip
  54. transport and does not relay arbitrary streams of data and does not allocate
  55. resources required for relay mode. On the Psiphon server, the same meek
  56. component handles both tactics requests and tunnel relays. Anti-probing for
  57. tactics endpoints are thus provided as usual by meek. A meek request is routed
  58. based on an routing field in the obfuscated meek cookie.
  59. As meek may be plaintext and as TLS certificate verification is sometimes
  60. skipped, the tactics request payload is wrapped with NaCl box and further
  61. wrapped in a padded obfuscator. Distinct request and response nonces are used to
  62. mitigate replay attacks. Clients generate ephemeral NaCl key pairs and the
  63. server public key is obtained from the server entry. The server entry also
  64. contains capabilities indicating that a Psiphon server supports tactics requests
  65. and which meek protocol is to be used.
  66. The Psiphon client requests, stores, and applies distinct tactics based on its
  67. current network context. The client uses platform-specific APIs to obtain a fine
  68. grain network ID based on, for example BSSID for WiFi or MCC/MNC for mobile.
  69. These values provides accurate detection of network context changes and can be
  70. obtained from the client device without any network activity. As the network ID
  71. is personally identifying, this ID is only used by the client and is never sent
  72. to the Psiphon server. The client obtains the current network ID from a callback
  73. made from tunnel-core to native client code.
  74. Tactics returned to the Psiphon client are accompanied by a "tag" which is a
  75. hash digest of the merged tactics data. This tag uniquely identifies the
  76. tactics. The client reports the tactics it is employing through the
  77. "applied_tactics" common metrics API parameter. When fetching new tactics, the
  78. client reports the stored (and possibly expired) tactics it has through the
  79. "stored_tactics" API parameter. The stored tactics tag is used to avoid
  80. redownloading redundant tactics data; when the tactics response indicates the
  81. tag is unchanged, no tactics data is returned and the client simply extends the
  82. expiry of the data is already has.
  83. The Psiphon handshake API returns tactics in its response. This enabled regular
  84. tactics expiry extension without requiring any distinct tactics request or
  85. tactics data transfer when the tag is unchanged. Psiphon clients that connect
  86. regularly and successfully with make almost no untunnled tactics requests except
  87. for new network IDs. Returning tactics in the handshake reponse also provides
  88. tactics in the case where a client is unable to complete an untunneled tactics
  89. request but can otherwise establish a tunnel. Clients will abort any outstanding
  90. untunneled tactics requests or scheduled retries once a handshake has completed.
  91. The client handshake request component calls SetTacticsAPIParameters to populate
  92. the handshake request parameters with tactics inputs, and calls
  93. HandleTacticsPayload to process the tactics payload in the handshake response.
  94. The core tactics data is custom values for a subset of the parameters in
  95. parameters.ClientParameters. A client takes the default ClientParameters,
  96. applies any custom values set in its config file, and then applies any stored or
  97. received tactics. Each time the tactics changes, this process is repeated so
  98. that obsolete tactics parameters are not retained in the client's
  99. ClientParameters instance.
  100. Tactics has a probability parameter that is used in a weighted coin flip to
  101. determine if the tactics is to be applied or skipped for the current client
  102. session. This allows for experimenting with provisional tactics; and obtaining
  103. non-tactic sample metrics in situations which would otherwise always use a
  104. tactic.
  105. Speed test data is used in filtered tactics for selection of parameters such as
  106. timeouts.
  107. A speed test sample records the RTT of an application-level round trip to a
  108. Psiphon server -- either a meek HTTP round trip or an SSH request round trip.
  109. The round trip should be preformed after an TCP, TLS, SSH, etc. handshake so
  110. that the RTT includes only the application-level round trip. Each sample also
  111. records the tunnel/meek protocol used, the Psiphon server region, and a
  112. timestamp; these values may be used to filter out outliers or stale samples. The
  113. samples record bytes up/down, although at this time the speed test is focused on
  114. latency and the payload is simply anti-fingerprint padding and should not be
  115. larger than an IP packet.
  116. The Psiphon client records the latest SpeedTestMaxSampleCount speed test samples
  117. for each network context. SpeedTestMaxSampleCount should be a modest size, as
  118. each speed test sample is ~100 bytes when serialzied and all samples (for one
  119. network ID) are loaded into memory and sent as API inputs to tactics and
  120. handshake requests.
  121. When a tactics request is initiated and there are no speed test samples for
  122. current network ID, the tactics request is proceeded by a speed test round trip,
  123. using the same meek round tripper, and that sample is stored and used for the
  124. tactics request. with a speed test The client records additional samples taken
  125. from regular SSH keep alive round trips and calls AddSpeedTestSample to store
  126. these.
  127. The client sends all its speed test samples, for the current network context, to
  128. the server in tactics and handshake requests; this allows the server logic to
  129. handle outliers and aggregation. Currently, filtered tactics support filerting
  130. on speed test RTT maximum, minimum, and median.
  131. */
  132. package tactics
  133. import (
  134. "bytes"
  135. "context"
  136. "crypto/md5"
  137. "crypto/rand"
  138. "encoding/base64"
  139. "encoding/hex"
  140. "encoding/json"
  141. "errors"
  142. "fmt"
  143. "io/ioutil"
  144. "net"
  145. "net/http"
  146. "sort"
  147. "time"
  148. "github.com/Psiphon-Labs/goarista/monotime"
  149. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  150. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/nacl/box"
  151. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/fragmentor"
  152. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/obfuscator"
  153. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  154. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  155. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  156. )
  157. // TACTICS_PADDING_MAX_SIZE is used by the client as well as the server. This
  158. // value is not a dynamic client parameter since a tactics request is made
  159. // only when the client has no valid tactics, so no override of
  160. // TACTICS_PADDING_MAX_SIZE can be applied.
  161. const (
  162. SPEED_TEST_END_POINT = "speedtest"
  163. TACTICS_END_POINT = "tactics"
  164. MAX_REQUEST_BODY_SIZE = 65536
  165. SPEED_TEST_PADDING_MIN_SIZE = 0
  166. SPEED_TEST_PADDING_MAX_SIZE = 256
  167. TACTICS_PADDING_MAX_SIZE = 256
  168. TACTICS_OBFUSCATED_KEY_SIZE = 32
  169. SPEED_TEST_SAMPLES_PARAMETER_NAME = "speed_test_samples"
  170. APPLIED_TACTICS_TAG_PARAMETER_NAME = "applied_tactics_tag"
  171. STORED_TACTICS_TAG_PARAMETER_NAME = "stored_tactics_tag"
  172. TACTICS_METRIC_EVENT_NAME = "tactics"
  173. NEW_TACTICS_TAG_LOG_FIELD_NAME = "new_tactics_tag"
  174. IS_TACTICS_REQUEST_LOG_FIELD_NAME = "is_tactics_request"
  175. AGGREGATION_MINIMUM = "Minimum"
  176. AGGREGATION_MAXIMUM = "Maximum"
  177. AGGREGATION_MEDIAN = "Median"
  178. )
  179. var (
  180. TACTICS_REQUEST_NONCE = []byte{1}
  181. TACTICS_RESPONSE_NONCE = []byte{2}
  182. )
  183. // Server is a tactics server to be integrated with the Psiphon server meek and handshake
  184. // components.
  185. //
  186. // The meek server calls HandleEndPoint to handle untunneled tactics and speed test requests.
  187. // The handshake handler calls GetTacticsPayload to obtain a tactics payload to include with
  188. // the handsake response.
  189. //
  190. // The Server is a reloadable file; its exported fields are read from the tactics configuration
  191. // file.
  192. //
  193. // Each client will receive at least the DefaultTactics. Client GeoIP, API parameter, and speed
  194. // test sample attributes are matched against all filters and the tactics corresponding to any
  195. // matching filter are merged into the client tactics.
  196. //
  197. // The merge operation replaces any existing item in Parameter with a Parameter specified in
  198. // the newest matching tactics. The TTL and Probability of the newest matching tactics is taken,
  199. // although all but the DefaultTactics can omit the TTL and Probability fields.
  200. type Server struct {
  201. common.ReloadableFile
  202. // RequestPublicKey is the Server's tactics request NaCl box public key.
  203. RequestPublicKey []byte
  204. // RequestPublicKey is the Server's tactics request NaCl box private key.
  205. RequestPrivateKey []byte
  206. // RequestObfuscatedKey is the tactics request obfuscation key.
  207. RequestObfuscatedKey []byte
  208. // DefaultTactics is the baseline tactics for all clients. It must include a
  209. // TTL and Probability.
  210. DefaultTactics Tactics
  211. // FilteredTactics is an ordered list of filter/tactics pairs. For a client,
  212. // each fltered tactics is checked in order and merged into the clients
  213. // tactics if the client's attributes satisfy the filter.
  214. FilteredTactics []struct {
  215. Filter Filter
  216. Tactics Tactics
  217. }
  218. // When no tactics configuration file is provided, there will be no
  219. // request key material or default tactics, and the server will not
  220. // support tactics. The loaded flag, set to true only when a configuration
  221. // file has been successfully loaded, provides an explict check for this
  222. // condition (vs., say, checking for a zero-value Server).
  223. loaded bool
  224. logger common.Logger
  225. logFieldFormatter common.APIParameterLogFieldFormatter
  226. apiParameterValidator common.APIParameterValidator
  227. }
  228. // Filter defines a filter to match against client attributes.
  229. // Each field within the filter is optional and may be omitted.
  230. type Filter struct {
  231. // Regions specifies a list of GeoIP regions/countries the client
  232. // must match.
  233. Regions []string
  234. // ISPs specifies a list of GeoIP ISPs the client must match.
  235. ISPs []string
  236. // APIParameters specifies API, e.g. handshake, parameter names and
  237. // a list of values, one of which must be specified to match this
  238. // filter. Only scalar string API parameters may be filtered.
  239. // Values may be patterns containing the '*' wildcard.
  240. APIParameters map[string][]string
  241. // SpeedTestRTTMilliseconds specifies a Range filter field that the
  242. // client speed test samples must satisfy.
  243. SpeedTestRTTMilliseconds *Range
  244. regionLookup map[string]bool
  245. ispLookup map[string]bool
  246. }
  247. // Range is a filter field which specifies that the aggregation of
  248. // the a client attribute is within specified upper and lower bounds.
  249. // At least one bound must be specified.
  250. //
  251. // For example, Range is to aggregate and filter client speed test
  252. // sample RTTs.
  253. type Range struct {
  254. // Aggregation may be "Maximum", "Minimum", or "Median"
  255. Aggregation string
  256. // AtLeast specifies a lower bound for the aggregarted
  257. // client value.
  258. AtLeast *int
  259. // AtMost specifies an upper bound for the aggregarted
  260. // client value.
  261. AtMost *int
  262. }
  263. // Payload is the data to be returned to the client in response to a
  264. // tactics request or in the handshake response.
  265. type Payload struct {
  266. // Tag is the hash tag of the accompanying Tactics. When the Tag
  267. // is the same as the stored tag the client specified in its
  268. // request, the Tactics will be empty as the client already has the
  269. // correct data.
  270. Tag string
  271. // Tactics is a JSON-encoded Tactics struct and may be nil.
  272. Tactics json.RawMessage
  273. }
  274. // Record is the tactics data persisted by the client. There is one
  275. // record for each network ID.
  276. type Record struct {
  277. // The Tag is the hash of the tactics data and is used as the
  278. // stored tag when making requests.
  279. Tag string
  280. // Expiry is the time when this perisisted tactics expires as
  281. // determined by the client applying the TTL against its local
  282. // clock when the tactics was stored.
  283. Expiry time.Time
  284. // Tactics is the core tactics data.
  285. Tactics Tactics
  286. }
  287. // Tactics is the core tactics data. This is both what is set in
  288. // in the server configuration file and what is stored and used
  289. // by the cient.
  290. type Tactics struct {
  291. // TTL is a string duration (e.g., "24h", the syntax supported
  292. // by time.ParseDuration). This specifies how long the client
  293. // should use the accompanying tactics until it expires.
  294. //
  295. // The client stores the TTL to use for extending the tactics
  296. // expiry when a tactics request or handshake response returns
  297. // no tactics data when the tag is unchanged.
  298. TTL string
  299. // Probability specifies the probability [0.0 - 1.0] with which
  300. // the client should apply the tactics in a new session.
  301. Probability float64
  302. // Parameters specify client parameters to override. These must
  303. // be a subset of parameter.ClientParameter values and follow
  304. // the corresponding data type and minimum value constraints.
  305. Parameters map[string]interface{}
  306. }
  307. // Note: the SpeedTestSample json tags are selected to minimize marshaled
  308. // size. In psiphond, for logging metrics, the field names are translated to
  309. // more verbose values. psiphon/server.makeSpeedTestSamplesLogField currently
  310. // hard-codes these same SpeedTestSample json tag values for that translation.
  311. // SpeedTestSample is speed test data for a single RTT event.
  312. type SpeedTestSample struct {
  313. // Timestamp is the speed test event time, and may be used to discard
  314. // stale samples. The server supplies the speed test timestamp. This
  315. // value is truncated to the nearest hour as a privacy measure.
  316. Timestamp time.Time `json:"s"`
  317. // EndPointRegion is the region of the endpoint, the Psiphon server,
  318. // used for the speed test. This may be used to exclude outlier samples
  319. // using remote data centers.
  320. EndPointRegion string `json:"r"`
  321. // EndPointProtocol is the tactics or tunnel protocol use for the
  322. // speed test round trip. The protocol may impact RTT.
  323. EndPointProtocol string `json:"p"`
  324. // All speed test samples should measure RTT as the time to complete
  325. // an application-level round trip on top of a previously established
  326. // tactics or tunnel prococol connection. The RTT should not include
  327. // TCP, TLS, or SSH handshakes.
  328. // This value is truncated to the nearest millisecond as a privacy
  329. // measure.
  330. RTTMilliseconds int `json:"t"`
  331. // BytesUp is the size of the upstream payload in the round trip.
  332. // Currently, the payload is limited to anti-fingerprint padding.
  333. BytesUp int `json:"u"`
  334. // BytesDown is the size of the downstream payload in the round trip.
  335. // Currently, the payload is limited to anti-fingerprint padding.
  336. BytesDown int `json:"d"`
  337. }
  338. // GenerateKeys generates a tactics request key pair and obfuscation key.
  339. func GenerateKeys() (encodedRequestPublicKey, encodedRequestPrivateKey, encodedObfuscatedKey string, err error) {
  340. requestPublicKey, requestPrivateKey, err := box.GenerateKey(rand.Reader)
  341. if err != nil {
  342. return "", "", "", common.ContextError(err)
  343. }
  344. obfuscatedKey, err := common.MakeSecureRandomBytes(TACTICS_OBFUSCATED_KEY_SIZE)
  345. if err != nil {
  346. return "", "", "", common.ContextError(err)
  347. }
  348. return base64.StdEncoding.EncodeToString(requestPublicKey[:]),
  349. base64.StdEncoding.EncodeToString(requestPrivateKey[:]),
  350. base64.StdEncoding.EncodeToString(obfuscatedKey[:]),
  351. nil
  352. }
  353. // NewServer creates Server using the specified tactics configuration file.
  354. //
  355. // The logger and logFieldFormatter callbacks are used to log errors and
  356. // metrics. The apiParameterValidator callback is used to validate client
  357. // API parameters submitted to the tactics request.
  358. func NewServer(
  359. logger common.Logger,
  360. logFieldFormatter common.APIParameterLogFieldFormatter,
  361. apiParameterValidator common.APIParameterValidator,
  362. configFilename string) (*Server, error) {
  363. server := &Server{
  364. logger: logger,
  365. logFieldFormatter: logFieldFormatter,
  366. apiParameterValidator: apiParameterValidator,
  367. }
  368. server.ReloadableFile = common.NewReloadableFile(
  369. configFilename,
  370. true,
  371. func(fileContent []byte, _ time.Time) error {
  372. var newServer Server
  373. err := json.Unmarshal(fileContent, &newServer)
  374. if err != nil {
  375. return common.ContextError(err)
  376. }
  377. err = newServer.Validate()
  378. if err != nil {
  379. return common.ContextError(err)
  380. }
  381. // Modify actual traffic rules only after validation
  382. server.RequestPublicKey = newServer.RequestPublicKey
  383. server.RequestPrivateKey = newServer.RequestPrivateKey
  384. server.RequestObfuscatedKey = newServer.RequestObfuscatedKey
  385. server.DefaultTactics = newServer.DefaultTactics
  386. server.FilteredTactics = newServer.FilteredTactics
  387. server.initLookups()
  388. server.loaded = true
  389. return nil
  390. })
  391. _, err := server.Reload()
  392. if err != nil {
  393. return nil, common.ContextError(err)
  394. }
  395. return server, nil
  396. }
  397. // Validate checks for correct tactics configuration values.
  398. func (server *Server) Validate() error {
  399. // Key material must either be entirely omitted, or fully populated.
  400. if len(server.RequestPublicKey) == 0 {
  401. if len(server.RequestPrivateKey) != 0 ||
  402. len(server.RequestObfuscatedKey) != 0 {
  403. return common.ContextError(errors.New("unexpected request key"))
  404. }
  405. } else {
  406. if len(server.RequestPublicKey) != 32 ||
  407. len(server.RequestPrivateKey) != 32 ||
  408. len(server.RequestObfuscatedKey) != TACTICS_OBFUSCATED_KEY_SIZE {
  409. return common.ContextError(errors.New("invalid request key"))
  410. }
  411. }
  412. validateTactics := func(tactics *Tactics, isDefault bool) error {
  413. // Allow "" for 0, even though ParseDuration does not.
  414. var d time.Duration
  415. if tactics.TTL != "" {
  416. var err error
  417. d, err = time.ParseDuration(tactics.TTL)
  418. if err != nil {
  419. return common.ContextError(err)
  420. }
  421. }
  422. if d <= 0 {
  423. if isDefault {
  424. return common.ContextError(errors.New("invalid duration"))
  425. }
  426. // For merging logic, Normalize any 0 duration to "".
  427. tactics.TTL = ""
  428. }
  429. if (isDefault && tactics.Probability == 0.0) ||
  430. tactics.Probability < 0.0 ||
  431. tactics.Probability > 1.0 {
  432. return common.ContextError(errors.New("invalid probability"))
  433. }
  434. clientParameters, err := parameters.NewClientParameters(nil)
  435. if err != nil {
  436. return common.ContextError(err)
  437. }
  438. _, err = clientParameters.Set("", false, tactics.Parameters)
  439. if err != nil {
  440. return common.ContextError(err)
  441. }
  442. return nil
  443. }
  444. validateRange := func(r *Range) error {
  445. if r == nil {
  446. return nil
  447. }
  448. if (r.AtLeast == nil && r.AtMost == nil) ||
  449. ((r.AtLeast != nil && r.AtMost != nil) && *r.AtLeast > *r.AtMost) {
  450. return common.ContextError(errors.New("invalid range"))
  451. }
  452. switch r.Aggregation {
  453. case AGGREGATION_MINIMUM, AGGREGATION_MAXIMUM, AGGREGATION_MEDIAN:
  454. default:
  455. return common.ContextError(errors.New("invalid aggregation"))
  456. }
  457. return nil
  458. }
  459. err := validateTactics(&server.DefaultTactics, true)
  460. if err != nil {
  461. return common.ContextError(fmt.Errorf("invalid default tactics: %s", err))
  462. }
  463. for i, filteredTactics := range server.FilteredTactics {
  464. err := validateTactics(&filteredTactics.Tactics, false)
  465. if err == nil {
  466. err = validateRange(filteredTactics.Filter.SpeedTestRTTMilliseconds)
  467. }
  468. // TODO: validate Filter.APIParameters names are valid?
  469. if err != nil {
  470. return common.ContextError(fmt.Errorf("invalid filtered tactics %d: %s", i, err))
  471. }
  472. }
  473. return nil
  474. }
  475. const stringLookupThreshold = 5
  476. // initLookups creates map lookups for filters where the number
  477. // of string values to compare against exceeds a threshold where
  478. // benchmarks show maps are faster than looping through a string
  479. // slice.
  480. func (server *Server) initLookups() {
  481. for _, filteredTactics := range server.FilteredTactics {
  482. if len(filteredTactics.Filter.Regions) >= stringLookupThreshold {
  483. filteredTactics.Filter.regionLookup = make(map[string]bool)
  484. for _, region := range filteredTactics.Filter.Regions {
  485. filteredTactics.Filter.regionLookup[region] = true
  486. }
  487. }
  488. if len(filteredTactics.Filter.ISPs) >= stringLookupThreshold {
  489. filteredTactics.Filter.ispLookup = make(map[string]bool)
  490. for _, ISP := range filteredTactics.Filter.ISPs {
  491. filteredTactics.Filter.ispLookup[ISP] = true
  492. }
  493. }
  494. // TODO: add lookups for APIParameters?
  495. // Not expected to be long lists of values.
  496. }
  497. }
  498. // GetTacticsPayload assembles and returns a tactics payload for a client with
  499. // the specified GeoIP, API parameter, and speed test attributes.
  500. //
  501. // The speed test samples are expected to be in apiParams, as is the stored
  502. // tactics tag.
  503. //
  504. // Unless no tactics configuration was loaded, GetTacticsPayload will always
  505. // return a payload for any client. When the client's stored tactics tag is
  506. // identical to the assembled tactics, the Payload.Tactics is nil.
  507. //
  508. // Elements of the returned Payload, e.g., tactics parameters, will point to
  509. // data in DefaultTactics and FilteredTactics and must not be modifed.
  510. func (server *Server) GetTacticsPayload(
  511. geoIPData common.GeoIPData,
  512. apiParams common.APIParameters) (*Payload, error) {
  513. // includeServerSideOnly is false: server-side only parameters are not
  514. // used by the client, so including them wastes space and unnecessarily
  515. // exposes the values.
  516. tactics, err := server.getTactics(false, geoIPData, apiParams)
  517. if err != nil {
  518. return nil, common.ContextError(err)
  519. }
  520. if tactics == nil {
  521. return nil, nil
  522. }
  523. marshaledTactics, err := json.Marshal(tactics)
  524. if err != nil {
  525. return nil, common.ContextError(err)
  526. }
  527. // MD5 hash is used solely as a data checksum and not for any security purpose.
  528. digest := md5.Sum(marshaledTactics)
  529. tag := hex.EncodeToString(digest[:])
  530. payload := &Payload{
  531. Tag: tag,
  532. }
  533. // New clients should always send STORED_TACTICS_TAG_PARAMETER_NAME. When they have no
  534. // stored tactics, the stored tag will be "" and not match payload.Tag and payload.Tactics
  535. // will be sent.
  536. //
  537. // When new clients send a stored tag that matches payload.Tag, the client already has
  538. // the correct data and payload.Tactics is not sent.
  539. //
  540. // Old clients will not send STORED_TACTICS_TAG_PARAMETER_NAME. In this case, do not
  541. // send payload.Tactics as the client will not use it, will not store it, will not send
  542. // back the new tag and so the handshake response will always contain wasteful tactics
  543. // data.
  544. sendPayloadTactics := true
  545. clientStoredTag, err := getStringRequestParam(apiParams, STORED_TACTICS_TAG_PARAMETER_NAME)
  546. // Old client or new client with same tag.
  547. if err != nil || payload.Tag == clientStoredTag {
  548. sendPayloadTactics = false
  549. }
  550. if sendPayloadTactics {
  551. payload.Tactics = marshaledTactics
  552. }
  553. return payload, nil
  554. }
  555. func (server *Server) getTactics(
  556. includeServerSideOnly bool,
  557. geoIPData common.GeoIPData,
  558. apiParams common.APIParameters) (*Tactics, error) {
  559. server.ReloadableFile.RLock()
  560. defer server.ReloadableFile.RUnlock()
  561. if !server.loaded {
  562. // No tactics configuration was loaded.
  563. return nil, nil
  564. }
  565. tactics := server.DefaultTactics.clone(includeServerSideOnly)
  566. var aggregatedValues map[string]int
  567. for _, filteredTactics := range server.FilteredTactics {
  568. if len(filteredTactics.Filter.Regions) > 0 {
  569. if filteredTactics.Filter.regionLookup != nil {
  570. if !filteredTactics.Filter.regionLookup[geoIPData.Country] {
  571. continue
  572. }
  573. } else {
  574. if !common.Contains(filteredTactics.Filter.Regions, geoIPData.Country) {
  575. continue
  576. }
  577. }
  578. }
  579. if len(filteredTactics.Filter.ISPs) > 0 {
  580. if filteredTactics.Filter.ispLookup != nil {
  581. if !filteredTactics.Filter.ispLookup[geoIPData.ISP] {
  582. continue
  583. }
  584. } else {
  585. if !common.Contains(filteredTactics.Filter.ISPs, geoIPData.ISP) {
  586. continue
  587. }
  588. }
  589. }
  590. if filteredTactics.Filter.APIParameters != nil {
  591. mismatch := false
  592. for name, values := range filteredTactics.Filter.APIParameters {
  593. clientValue, err := getStringRequestParam(apiParams, name)
  594. if err != nil || !common.ContainsWildcard(values, clientValue) {
  595. mismatch = true
  596. break
  597. }
  598. }
  599. if mismatch {
  600. continue
  601. }
  602. }
  603. if filteredTactics.Filter.SpeedTestRTTMilliseconds != nil {
  604. var speedTestSamples []SpeedTestSample
  605. err := getJSONRequestParam(apiParams, SPEED_TEST_SAMPLES_PARAMETER_NAME, &speedTestSamples)
  606. if err != nil {
  607. // TODO: log speed test parameter errors?
  608. // This API param is not explicitly validated elsewhere.
  609. continue
  610. }
  611. // As there must be at least one Range bound, there must be data to aggregate.
  612. if len(speedTestSamples) == 0 {
  613. continue
  614. }
  615. if aggregatedValues == nil {
  616. aggregatedValues = make(map[string]int)
  617. }
  618. // Note: here we could filter out outliers such as samples that are unusually old
  619. // or client/endPoint region pair too distant.
  620. // aggregate may mutate (sort) the speedTestSamples slice.
  621. value := aggregate(
  622. filteredTactics.Filter.SpeedTestRTTMilliseconds.Aggregation,
  623. speedTestSamples,
  624. aggregatedValues)
  625. if filteredTactics.Filter.SpeedTestRTTMilliseconds.AtLeast != nil &&
  626. value < *filteredTactics.Filter.SpeedTestRTTMilliseconds.AtLeast {
  627. continue
  628. }
  629. if filteredTactics.Filter.SpeedTestRTTMilliseconds.AtMost != nil &&
  630. value > *filteredTactics.Filter.SpeedTestRTTMilliseconds.AtMost {
  631. continue
  632. }
  633. }
  634. tactics.merge(includeServerSideOnly, &filteredTactics.Tactics)
  635. // Continue to apply more matches. Last matching tactics has priority for any field.
  636. }
  637. return tactics, nil
  638. }
  639. // TODO: refactor this copy of psiphon/server.getStringRequestParam into common?
  640. func getStringRequestParam(params common.APIParameters, name string) (string, error) {
  641. if params[name] == nil {
  642. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  643. }
  644. value, ok := params[name].(string)
  645. if !ok {
  646. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  647. }
  648. return value, nil
  649. }
  650. func getJSONRequestParam(params common.APIParameters, name string, value interface{}) error {
  651. if params[name] == nil {
  652. return common.ContextError(fmt.Errorf("missing param: %s", name))
  653. }
  654. // Remarshal the parameter from common.APIParameters, as the initial API parameter
  655. // unmarshal will not have known the correct target type. I.e., instead of doing
  656. // unmarhsal-into-struct, common.APIParameters will have an unmarshal-into-interface
  657. // value as described here: https://golang.org/pkg/encoding/json/#Unmarshal.
  658. jsonValue, err := json.Marshal(params[name])
  659. if err != nil {
  660. return common.ContextError(err)
  661. }
  662. err = json.Unmarshal(jsonValue, value)
  663. if err != nil {
  664. return common.ContextError(err)
  665. }
  666. return nil
  667. }
  668. // aggregate may mutate (sort) the speedTestSamples slice.
  669. func aggregate(
  670. aggregation string,
  671. speedTestSamples []SpeedTestSample,
  672. aggregatedValues map[string]int) int {
  673. // Aggregated values are memoized to save recalculating for each filter.
  674. if value, ok := aggregatedValues[aggregation]; ok {
  675. return value
  676. }
  677. var value int
  678. switch aggregation {
  679. case AGGREGATION_MINIMUM:
  680. value = minimumSampleRTTMilliseconds(speedTestSamples)
  681. case AGGREGATION_MAXIMUM:
  682. value = maximumSampleRTTMilliseconds(speedTestSamples)
  683. case AGGREGATION_MEDIAN:
  684. value = medianSampleRTTMilliseconds(speedTestSamples)
  685. default:
  686. return 0
  687. }
  688. aggregatedValues[aggregation] = value
  689. return value
  690. }
  691. func minimumSampleRTTMilliseconds(samples []SpeedTestSample) int {
  692. if len(samples) == 0 {
  693. return 0
  694. }
  695. min := 0
  696. for i := 1; i < len(samples); i++ {
  697. if samples[i].RTTMilliseconds < samples[min].RTTMilliseconds {
  698. min = i
  699. }
  700. }
  701. return samples[min].RTTMilliseconds
  702. }
  703. func maximumSampleRTTMilliseconds(samples []SpeedTestSample) int {
  704. if len(samples) == 0 {
  705. return 0
  706. }
  707. max := 0
  708. for i := 1; i < len(samples); i++ {
  709. if samples[i].RTTMilliseconds > samples[max].RTTMilliseconds {
  710. max = i
  711. }
  712. }
  713. return samples[max].RTTMilliseconds
  714. }
  715. func medianSampleRTTMilliseconds(samples []SpeedTestSample) int {
  716. if len(samples) == 0 {
  717. return 0
  718. }
  719. // This in-place sort mutates the input slice.
  720. sort.Slice(
  721. samples,
  722. func(i, j int) bool {
  723. return samples[i].RTTMilliseconds < samples[j].RTTMilliseconds
  724. })
  725. // See: https://en.wikipedia.org/wiki/Median#Easy_explanation_of_the_sample_median
  726. mid := len(samples) / 2
  727. if len(samples)%2 == 1 {
  728. return samples[mid].RTTMilliseconds
  729. }
  730. return (samples[mid-1].RTTMilliseconds + samples[mid].RTTMilliseconds) / 2
  731. }
  732. func (t *Tactics) clone(includeServerSideOnly bool) *Tactics {
  733. u := &Tactics{
  734. TTL: t.TTL,
  735. Probability: t.Probability,
  736. }
  737. // Note: there is no deep copy of parameter values; the the returned
  738. // Tactics shares memory with the original and it individual parameters
  739. // should not be modified.
  740. if t.Parameters != nil {
  741. u.Parameters = make(map[string]interface{})
  742. for k, v := range t.Parameters {
  743. if includeServerSideOnly || !parameters.IsServerSideOnly(k) {
  744. u.Parameters[k] = v
  745. }
  746. }
  747. }
  748. return u
  749. }
  750. func (t *Tactics) merge(includeServerSideOnly bool, u *Tactics) {
  751. if u.TTL != "" {
  752. t.TTL = u.TTL
  753. }
  754. if u.Probability != 0.0 {
  755. t.Probability = u.Probability
  756. }
  757. // Note: there is no deep copy of parameter values; the the returned
  758. // Tactics shares memory with the original and it individual parameters
  759. // should not be modified.
  760. if u.Parameters != nil {
  761. if t.Parameters == nil {
  762. t.Parameters = make(map[string]interface{})
  763. }
  764. for k, v := range u.Parameters {
  765. if includeServerSideOnly || !parameters.IsServerSideOnly(k) {
  766. t.Parameters[k] = v
  767. }
  768. }
  769. }
  770. }
  771. // HandleEndPoint routes the request to either handleSpeedTestRequest
  772. // or handleTacticsRequest; or returns false if not handled.
  773. func (server *Server) HandleEndPoint(
  774. endPoint string,
  775. geoIPData common.GeoIPData,
  776. w http.ResponseWriter,
  777. r *http.Request) bool {
  778. server.ReloadableFile.RLock()
  779. loaded := server.loaded
  780. hasRequestKeys := len(server.RequestPublicKey) > 0
  781. server.ReloadableFile.RUnlock()
  782. if !loaded || !hasRequestKeys {
  783. // No tactics configuration was loaded, or the configuration contained
  784. // no key material for tactics requests.
  785. return false
  786. }
  787. switch endPoint {
  788. case SPEED_TEST_END_POINT:
  789. server.handleSpeedTestRequest(geoIPData, w, r)
  790. return true
  791. case TACTICS_END_POINT:
  792. server.handleTacticsRequest(geoIPData, w, r)
  793. return true
  794. default:
  795. return false
  796. }
  797. }
  798. func (server *Server) handleSpeedTestRequest(
  799. _ common.GeoIPData, w http.ResponseWriter, r *http.Request) {
  800. _, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, MAX_REQUEST_BODY_SIZE))
  801. if err != nil {
  802. server.logger.WithContextFields(
  803. common.LogFields{"error": err}).Warning("failed to read request body")
  804. common.TerminateHTTPConnection(w, r)
  805. return
  806. }
  807. response, err := MakeSpeedTestResponse(
  808. SPEED_TEST_PADDING_MIN_SIZE, SPEED_TEST_PADDING_MAX_SIZE)
  809. if err != nil {
  810. server.logger.WithContextFields(
  811. common.LogFields{"error": err}).Warning("failed to make response")
  812. common.TerminateHTTPConnection(w, r)
  813. return
  814. }
  815. w.WriteHeader(http.StatusOK)
  816. w.Write(response)
  817. }
  818. func (server *Server) handleTacticsRequest(
  819. geoIPData common.GeoIPData, w http.ResponseWriter, r *http.Request) {
  820. server.ReloadableFile.RLock()
  821. requestPrivateKey := server.RequestPrivateKey
  822. requestObfuscatedKey := server.RequestObfuscatedKey
  823. server.ReloadableFile.RUnlock()
  824. // Read, decode, and unbox request payload.
  825. boxedRequest, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, MAX_REQUEST_BODY_SIZE))
  826. if err != nil {
  827. server.logger.WithContextFields(
  828. common.LogFields{"error": err}).Warning("failed to read request body")
  829. common.TerminateHTTPConnection(w, r)
  830. return
  831. }
  832. var apiParams common.APIParameters
  833. bundledPeerPublicKey, err := unboxPayload(
  834. TACTICS_REQUEST_NONCE,
  835. nil,
  836. requestPrivateKey,
  837. requestObfuscatedKey,
  838. boxedRequest,
  839. &apiParams)
  840. if err != nil {
  841. server.logger.WithContextFields(
  842. common.LogFields{"error": err}).Warning("failed to unbox request")
  843. common.TerminateHTTPConnection(w, r)
  844. return
  845. }
  846. err = server.apiParameterValidator(apiParams)
  847. if err != nil {
  848. server.logger.WithContextFields(
  849. common.LogFields{"error": err}).Warning("invalid request parameters")
  850. common.TerminateHTTPConnection(w, r)
  851. return
  852. }
  853. tacticsPayload, err := server.GetTacticsPayload(geoIPData, apiParams)
  854. if err == nil && tacticsPayload == nil {
  855. err = common.ContextError(errors.New("unexpected missing tactics payload"))
  856. }
  857. if err != nil {
  858. server.logger.WithContextFields(
  859. common.LogFields{"error": err}).Warning("failed to get tactics")
  860. common.TerminateHTTPConnection(w, r)
  861. return
  862. }
  863. // Marshal, box, and write response payload.
  864. boxedResponse, err := boxPayload(
  865. TACTICS_RESPONSE_NONCE,
  866. bundledPeerPublicKey,
  867. requestPrivateKey,
  868. requestObfuscatedKey,
  869. nil,
  870. tacticsPayload)
  871. if err != nil {
  872. server.logger.WithContextFields(
  873. common.LogFields{"error": err}).Warning("failed to box response")
  874. common.TerminateHTTPConnection(w, r)
  875. return
  876. }
  877. w.WriteHeader(http.StatusOK)
  878. w.Write(boxedResponse)
  879. // Log a metric.
  880. logFields := server.logFieldFormatter(geoIPData, apiParams)
  881. logFields[NEW_TACTICS_TAG_LOG_FIELD_NAME] = tacticsPayload.Tag
  882. logFields[IS_TACTICS_REQUEST_LOG_FIELD_NAME] = true
  883. server.logger.LogMetric(TACTICS_METRIC_EVENT_NAME, logFields)
  884. }
  885. // Listener wraps a net.Listener and applies server-side implementation of
  886. // certain tactics parameters to accepted connections. Tactics filtering is
  887. // limited to GeoIP attributes as the client has not yet sent API paramaters.
  888. type Listener struct {
  889. net.Listener
  890. server *Server
  891. tunnelProtocol string
  892. geoIPLookup func(IPaddress string) common.GeoIPData
  893. }
  894. // NewListener creates a new Listener.
  895. func NewListener(
  896. listener net.Listener,
  897. server *Server,
  898. tunnelProtocol string,
  899. geoIPLookup func(IPaddress string) common.GeoIPData) *Listener {
  900. return &Listener{
  901. Listener: listener,
  902. server: server,
  903. tunnelProtocol: tunnelProtocol,
  904. geoIPLookup: geoIPLookup,
  905. }
  906. }
  907. // Accept calls the underlying listener's Accept, and then checks if tactics
  908. // for the connection set LimitTunnelProtocols.
  909. //
  910. // If LimitTunnelProtocols is set and does not include the tunnel protocol the
  911. // listener is running, the accepted connection is immediately closed and the
  912. // underlying Accept is called again.
  913. //
  914. // For retained connections, fragmentation is applied when specified by
  915. // tactics.
  916. func (listener *Listener) Accept() (net.Conn, error) {
  917. for {
  918. conn, err := listener.Listener.Accept()
  919. if err != nil {
  920. // Don't modify error from net.Listener
  921. return nil, err
  922. }
  923. geoIPData := listener.geoIPLookup(common.IPAddressFromAddr(conn.RemoteAddr()))
  924. tactics, err := listener.server.getTactics(true, geoIPData, make(common.APIParameters))
  925. if err != nil {
  926. listener.server.logger.WithContextFields(
  927. common.LogFields{"error": err}).Warning("failed to get tactics for connection")
  928. // If tactics is somehow misconfigured, keep handling connections.
  929. // Other error cases that follow below take the same approach.
  930. return conn, nil
  931. }
  932. if tactics == nil {
  933. // This server isn't configured with tactics.
  934. return conn, nil
  935. }
  936. if !prng.FlipWeightedCoin(tactics.Probability) {
  937. // Skip tactics with the configured probability.
  938. return conn, nil
  939. }
  940. clientParameters, err := parameters.NewClientParameters(nil)
  941. if err != nil {
  942. return conn, nil
  943. }
  944. _, err = clientParameters.Set("", false, tactics.Parameters)
  945. if err != nil {
  946. return conn, nil
  947. }
  948. p := clientParameters.Get()
  949. // Wrap the conn in a fragmentor.Conn, subject to tactics parameters.
  950. //
  951. // Limitation: this server-side fragmentation is not synchronized with
  952. // client-side; where client-side will make a single coin flip to fragment
  953. // or not fragment all TCP connections for a one meek session, the server
  954. // will make a coin flip per connection.
  955. //
  956. // Delay seeding the fragmentor PRNG when we can derive a seed from the
  957. // client's initial obfuscation message. This enables server-side replay
  958. // of fragmentation when initiated by the client. Currently this is only
  959. // supported for OSSH: SSH lacks the initial obfuscation message, and
  960. // meek and other protocols transmit downstream data before the initial
  961. // obfuscation message arrives.
  962. var seed *prng.Seed
  963. if listener.tunnelProtocol != protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH {
  964. seed, err = prng.NewSeed()
  965. if err != nil {
  966. listener.server.logger.WithContextFields(
  967. common.LogFields{"error": err}).Warning("failed to seed fragmentor PRNG")
  968. return conn, nil
  969. }
  970. }
  971. fragmentorConfig := fragmentor.NewDownstreamConfig(
  972. p, listener.tunnelProtocol, seed)
  973. if fragmentorConfig.MayFragment() {
  974. conn = fragmentor.NewConn(
  975. fragmentorConfig,
  976. func(message string) {
  977. listener.server.logger.WithContextFields(
  978. common.LogFields{"message": message}).Debug("Fragmentor")
  979. },
  980. conn)
  981. }
  982. return conn, nil
  983. }
  984. }
  985. // RoundTripper performs a round trip to the specified endpoint, sending the
  986. // request body and returning the response body. The context may be used to
  987. // set a timeout or cancel the rount trip.
  988. //
  989. // The Psiphon client provides a RoundTripper using meek. The client will
  990. // handle connection details including server selection, dialing details
  991. // including device binding and upstream proxy, etc.
  992. type RoundTripper func(
  993. ctx context.Context,
  994. endPoint string,
  995. requestBody []byte) ([]byte, error)
  996. // Storer provides a facility to persist tactics and speed test data.
  997. type Storer interface {
  998. SetTacticsRecord(networkID string, record []byte) error
  999. GetTacticsRecord(networkID string) ([]byte, error)
  1000. SetSpeedTestSamplesRecord(networkID string, record []byte) error
  1001. GetSpeedTestSamplesRecord(networkID string) ([]byte, error)
  1002. }
  1003. // SetTacticsAPIParameters populates apiParams with the additional
  1004. // parameters for tactics. This is used by the Psiphon client when
  1005. // preparing its handshake request.
  1006. func SetTacticsAPIParameters(
  1007. clientParameters *parameters.ClientParameters,
  1008. storer Storer,
  1009. networkID string,
  1010. apiParams common.APIParameters) error {
  1011. // TODO: store the tag in its own record to avoid loading the whole tactics record?
  1012. record, err := getStoredTacticsRecord(storer, networkID)
  1013. if err != nil {
  1014. return common.ContextError(err)
  1015. }
  1016. speedTestSamples, err := getSpeedTestSamples(storer, networkID)
  1017. if err != nil {
  1018. return common.ContextError(err)
  1019. }
  1020. apiParams[STORED_TACTICS_TAG_PARAMETER_NAME] = record.Tag
  1021. apiParams[SPEED_TEST_SAMPLES_PARAMETER_NAME] = speedTestSamples
  1022. return nil
  1023. }
  1024. // HandleTacticsPayload updates the stored tactics with the given payload.
  1025. // If the payload has a new tag/tactics, this is stored and a new expiry
  1026. // time is set. If the payload has the same tag, the existing tactics are
  1027. // retained and the exipry is extended using the previous TTL.
  1028. // HandleTacticsPayload is called by the Psiphon client to handle the
  1029. // tactics payload in the handshake response.
  1030. func HandleTacticsPayload(
  1031. storer Storer,
  1032. networkID string,
  1033. payload *Payload) (*Record, error) {
  1034. // Note: since, in the client, a tactics request and a handshake
  1035. // request could be in flight concurrently, there exists a possibility
  1036. // that one clobbers the others result, and the clobbered result may
  1037. // be newer.
  1038. //
  1039. // However:
  1040. // - in the Storer, the tactics record is a single key/value, so its
  1041. // elements are updated atomically;
  1042. // - the client Controller typically stops/aborts any outstanding
  1043. // tactics request before the handshake
  1044. // - this would have to be concurrent with a tactics configuration hot
  1045. // reload on the server
  1046. // - old and new tactics should both be valid
  1047. if payload == nil {
  1048. return nil, common.ContextError(errors.New("unexpected nil payload"))
  1049. }
  1050. record, err := getStoredTacticsRecord(storer, networkID)
  1051. if err != nil {
  1052. return nil, common.ContextError(err)
  1053. }
  1054. err = applyTacticsPayload(storer, networkID, record, payload)
  1055. if err != nil {
  1056. return nil, common.ContextError(err)
  1057. }
  1058. // TODO: if tags match, just set an expiry record, not the whole tactics record?
  1059. err = setStoredTacticsRecord(storer, networkID, record)
  1060. if err != nil {
  1061. return nil, common.ContextError(err)
  1062. }
  1063. return record, nil
  1064. }
  1065. // UseStoredTactics checks for an unexpired stored tactics record for the
  1066. // given network ID that may be used immediately. When there is no error
  1067. // and the record is nil, the caller should proceed with FetchTactics.
  1068. //
  1069. // When used, Record.Tag should be reported as the applied tactics tag.
  1070. func UseStoredTactics(
  1071. storer Storer, networkID string) (*Record, error) {
  1072. record, err := getStoredTacticsRecord(storer, networkID)
  1073. if err != nil {
  1074. return nil, common.ContextError(err)
  1075. }
  1076. if record.Tag != "" && record.Expiry.After(time.Now().UTC()) {
  1077. return record, nil
  1078. }
  1079. return nil, nil
  1080. }
  1081. // FetchTactics performs a tactics request. When there are no stored
  1082. // speed test samples for the network ID, a speed test request is
  1083. // performed immediately before the tactics request, using the same
  1084. // RoundTripper.
  1085. //
  1086. // The RoundTripper transport should be established in advance, so that
  1087. // calls to RoundTripper don't take additional time in TCP, TLS, etc.
  1088. // handshakes.
  1089. //
  1090. // The caller should first call UseStoredTactics and skip FetchTactics
  1091. // when there is an unexpired stored tactics record available. The
  1092. // caller is expected to set any overall timeout in the context input.
  1093. //
  1094. // Limitation: it is assumed that the network ID obtained from getNetworkID
  1095. // is the one that is active when the tactics request is received by the
  1096. // server. However, it is remotely possible to switch networks
  1097. // immediately after invoking the GetNetworkID callback and initiating
  1098. // the request. This is partially mitigated by rechecking the network ID
  1099. // after the request and failing if it differs from the initial network ID.
  1100. //
  1101. // FetchTactics modifies the apiParams input.
  1102. func FetchTactics(
  1103. ctx context.Context,
  1104. clientParameters *parameters.ClientParameters,
  1105. storer Storer,
  1106. getNetworkID func() string,
  1107. apiParams common.APIParameters,
  1108. endPointRegion string,
  1109. endPointProtocol string,
  1110. encodedRequestPublicKey string,
  1111. encodedRequestObfuscatedKey string,
  1112. roundTripper RoundTripper) (*Record, error) {
  1113. networkID := getNetworkID()
  1114. record, err := getStoredTacticsRecord(storer, networkID)
  1115. if err != nil {
  1116. return nil, common.ContextError(err)
  1117. }
  1118. speedTestSamples, err := getSpeedTestSamples(storer, networkID)
  1119. if err != nil {
  1120. return nil, common.ContextError(err)
  1121. }
  1122. // Perform a speed test when there are no samples.
  1123. if len(speedTestSamples) == 0 {
  1124. p := clientParameters.Get()
  1125. request := prng.Padding(
  1126. p.Int(parameters.SpeedTestPaddingMinBytes),
  1127. p.Int(parameters.SpeedTestPaddingMaxBytes))
  1128. startTime := monotime.Now()
  1129. response, err := roundTripper(ctx, SPEED_TEST_END_POINT, request)
  1130. elapsedTime := monotime.Since(startTime)
  1131. if err != nil {
  1132. return nil, common.ContextError(err)
  1133. }
  1134. if networkID != getNetworkID() {
  1135. return nil, common.ContextError(errors.New("network ID changed"))
  1136. }
  1137. err = AddSpeedTestSample(
  1138. clientParameters,
  1139. storer,
  1140. networkID,
  1141. endPointRegion,
  1142. endPointProtocol,
  1143. elapsedTime,
  1144. request,
  1145. response)
  1146. if err != nil {
  1147. return nil, common.ContextError(err)
  1148. }
  1149. speedTestSamples, err = getSpeedTestSamples(storer, networkID)
  1150. if err != nil {
  1151. return nil, common.ContextError(err)
  1152. }
  1153. }
  1154. // Perform the tactics request.
  1155. apiParams[STORED_TACTICS_TAG_PARAMETER_NAME] = record.Tag
  1156. apiParams[SPEED_TEST_SAMPLES_PARAMETER_NAME] = speedTestSamples
  1157. requestPublicKey, err := base64.StdEncoding.DecodeString(encodedRequestPublicKey)
  1158. if err != nil {
  1159. return nil, common.ContextError(err)
  1160. }
  1161. requestObfuscatedKey, err := base64.StdEncoding.DecodeString(encodedRequestObfuscatedKey)
  1162. if err != nil {
  1163. return nil, common.ContextError(err)
  1164. }
  1165. ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
  1166. if err != nil {
  1167. return nil, common.ContextError(err)
  1168. }
  1169. boxedRequest, err := boxPayload(
  1170. TACTICS_REQUEST_NONCE,
  1171. requestPublicKey,
  1172. ephemeralPrivateKey[:],
  1173. requestObfuscatedKey,
  1174. ephemeralPublicKey[:],
  1175. &apiParams)
  1176. if err != nil {
  1177. return nil, common.ContextError(err)
  1178. }
  1179. boxedResponse, err := roundTripper(ctx, TACTICS_END_POINT, boxedRequest)
  1180. if err != nil {
  1181. return nil, common.ContextError(err)
  1182. }
  1183. if networkID != getNetworkID() {
  1184. return nil, common.ContextError(errors.New("network ID changed"))
  1185. }
  1186. // Process and store the response payload.
  1187. var payload *Payload
  1188. _, err = unboxPayload(
  1189. TACTICS_RESPONSE_NONCE,
  1190. requestPublicKey,
  1191. ephemeralPrivateKey[:],
  1192. requestObfuscatedKey,
  1193. boxedResponse,
  1194. &payload)
  1195. if err != nil {
  1196. return nil, common.ContextError(err)
  1197. }
  1198. err = applyTacticsPayload(storer, networkID, record, payload)
  1199. if err != nil {
  1200. return nil, common.ContextError(err)
  1201. }
  1202. err = setStoredTacticsRecord(storer, networkID, record)
  1203. if err != nil {
  1204. return nil, common.ContextError(err)
  1205. }
  1206. return record, nil
  1207. }
  1208. // MakeSpeedTestResponse creates a speed test response prefixed
  1209. // with a timestamp and followed by random padding. The timestamp
  1210. // enables the client performing the speed test to record the
  1211. // sample time with an accurate server clock; the random padding
  1212. // is to frustrate fingerprinting.
  1213. // The speed test timestamp is truncated as a privacy measure.
  1214. func MakeSpeedTestResponse(minPadding, maxPadding int) ([]byte, error) {
  1215. // MarshalBinary encoding (version 1) is 15 bytes:
  1216. // https://github.com/golang/go/blob/release-branch.go1.9/src/time/time.go#L1112
  1217. timestamp, err := time.Now().UTC().Truncate(1 * time.Hour).MarshalBinary()
  1218. if err == nil && len(timestamp) > 255 {
  1219. err = fmt.Errorf("unexpected marshaled time size: %d", len(timestamp))
  1220. }
  1221. if err != nil {
  1222. return nil, common.ContextError(err)
  1223. }
  1224. randomPadding := prng.Padding(minPadding, maxPadding)
  1225. // On error, proceed without random padding.
  1226. // TODO: log error, even if proceeding?
  1227. response := make([]byte, 0, 1+len(timestamp)+len(randomPadding))
  1228. response = append(response, byte(len(timestamp)))
  1229. response = append(response, timestamp...)
  1230. response = append(response, randomPadding...)
  1231. return response, nil
  1232. }
  1233. // AddSpeedTestSample stores a new speed test sample. A maximum of
  1234. // SpeedTestMaxSampleCount samples per network ID are stored, so once
  1235. // that limit is reached, the oldest samples are removed to make room
  1236. // for the new sample.
  1237. func AddSpeedTestSample(
  1238. clientParameters *parameters.ClientParameters,
  1239. storer Storer,
  1240. networkID string,
  1241. endPointRegion string,
  1242. endPointProtocol string,
  1243. elaspedTime time.Duration,
  1244. request []byte,
  1245. response []byte) error {
  1246. if len(response) < 1 {
  1247. return common.ContextError(errors.New("unexpected empty response"))
  1248. }
  1249. timestampLength := int(response[0])
  1250. if len(response) < 1+timestampLength {
  1251. return common.ContextError(fmt.Errorf(
  1252. "unexpected response shorter than timestamp size %d", timestampLength))
  1253. }
  1254. var timestamp time.Time
  1255. err := timestamp.UnmarshalBinary(response[1 : 1+timestampLength])
  1256. if err != nil {
  1257. return common.ContextError(err)
  1258. }
  1259. sample := SpeedTestSample{
  1260. Timestamp: timestamp,
  1261. EndPointRegion: endPointRegion,
  1262. EndPointProtocol: endPointProtocol,
  1263. RTTMilliseconds: int(elaspedTime / time.Millisecond),
  1264. BytesUp: len(request),
  1265. BytesDown: len(response),
  1266. }
  1267. maxCount := clientParameters.Get().Int(parameters.SpeedTestMaxSampleCount)
  1268. if maxCount == 0 {
  1269. return common.ContextError(errors.New("speed test max sample count is 0"))
  1270. }
  1271. speedTestSamples, err := getSpeedTestSamples(storer, networkID)
  1272. if err != nil {
  1273. return common.ContextError(err)
  1274. }
  1275. if speedTestSamples == nil {
  1276. speedTestSamples = make([]SpeedTestSample, 0)
  1277. }
  1278. if len(speedTestSamples)+1 > maxCount {
  1279. speedTestSamples = speedTestSamples[len(speedTestSamples)+1-maxCount:]
  1280. }
  1281. speedTestSamples = append(speedTestSamples, sample)
  1282. record, err := json.Marshal(speedTestSamples)
  1283. if err != nil {
  1284. return common.ContextError(err)
  1285. }
  1286. err = storer.SetSpeedTestSamplesRecord(networkID, record)
  1287. if err != nil {
  1288. return common.ContextError(err)
  1289. }
  1290. return nil
  1291. }
  1292. func getSpeedTestSamples(
  1293. storer Storer, networkID string) ([]SpeedTestSample, error) {
  1294. record, err := storer.GetSpeedTestSamplesRecord(networkID)
  1295. if err != nil {
  1296. return nil, common.ContextError(err)
  1297. }
  1298. if record == nil {
  1299. return nil, nil
  1300. }
  1301. var speedTestSamples []SpeedTestSample
  1302. err = json.Unmarshal(record, &speedTestSamples)
  1303. if err != nil {
  1304. return nil, common.ContextError(err)
  1305. }
  1306. return speedTestSamples, nil
  1307. }
  1308. func getStoredTacticsRecord(
  1309. storer Storer, networkID string) (*Record, error) {
  1310. marshaledRecord, err := storer.GetTacticsRecord(networkID)
  1311. if err != nil {
  1312. return nil, common.ContextError(err)
  1313. }
  1314. if marshaledRecord == nil {
  1315. return &Record{}, nil
  1316. }
  1317. var record *Record
  1318. err = json.Unmarshal(marshaledRecord, &record)
  1319. if err != nil {
  1320. return nil, common.ContextError(err)
  1321. }
  1322. if record == nil {
  1323. record = &Record{}
  1324. }
  1325. return record, nil
  1326. }
  1327. func applyTacticsPayload(
  1328. storer Storer,
  1329. networkID string,
  1330. record *Record,
  1331. payload *Payload) error {
  1332. if payload.Tag == "" {
  1333. return common.ContextError(errors.New("invalid tag"))
  1334. }
  1335. // Replace the tactics data when the tags differ.
  1336. if payload.Tag != record.Tag {
  1337. record.Tag = payload.Tag
  1338. record.Tactics = Tactics{}
  1339. err := json.Unmarshal(payload.Tactics, &record.Tactics)
  1340. if err != nil {
  1341. return common.ContextError(err)
  1342. }
  1343. }
  1344. // Note: record.Tactics.TTL is validated by server
  1345. ttl, err := time.ParseDuration(record.Tactics.TTL)
  1346. if err != nil {
  1347. return common.ContextError(err)
  1348. }
  1349. if ttl <= 0 {
  1350. return common.ContextError(errors.New("invalid TTL"))
  1351. }
  1352. if record.Tactics.Probability <= 0.0 {
  1353. return common.ContextError(errors.New("invalid probability"))
  1354. }
  1355. // Set or extend the expiry.
  1356. record.Expiry = time.Now().UTC().Add(ttl)
  1357. return nil
  1358. }
  1359. func setStoredTacticsRecord(
  1360. storer Storer,
  1361. networkID string,
  1362. record *Record) error {
  1363. marshaledRecord, err := json.Marshal(record)
  1364. if err != nil {
  1365. return common.ContextError(err)
  1366. }
  1367. err = storer.SetTacticsRecord(networkID, marshaledRecord)
  1368. if err != nil {
  1369. return common.ContextError(err)
  1370. }
  1371. return nil
  1372. }
  1373. func boxPayload(
  1374. nonce, peerPublicKey, privateKey, obfuscatedKey, bundlePublicKey []byte,
  1375. payload interface{}) ([]byte, error) {
  1376. if len(nonce) > 24 ||
  1377. len(peerPublicKey) != 32 ||
  1378. len(privateKey) != 32 {
  1379. return nil, common.ContextError(
  1380. errors.New("unexpected box key length"))
  1381. }
  1382. marshaledPayload, err := json.Marshal(payload)
  1383. if err != nil {
  1384. return nil, common.ContextError(err)
  1385. }
  1386. var nonceArray [24]byte
  1387. copy(nonceArray[:], nonce)
  1388. var peerPublicKeyArray, privateKeyArray [32]byte
  1389. copy(peerPublicKeyArray[:], peerPublicKey)
  1390. copy(privateKeyArray[:], privateKey)
  1391. box := box.Seal(nil, marshaledPayload, &nonceArray, &peerPublicKeyArray, &privateKeyArray)
  1392. if bundlePublicKey != nil {
  1393. bundledBox := make([]byte, 32+len(box))
  1394. copy(bundledBox[0:32], bundlePublicKey[0:32])
  1395. copy(bundledBox[32:], box)
  1396. box = bundledBox
  1397. }
  1398. // TODO: replay tactics request padding?
  1399. paddingPRNGSeed, err := prng.NewSeed()
  1400. if err != nil {
  1401. return nil, common.ContextError(err)
  1402. }
  1403. maxPadding := TACTICS_PADDING_MAX_SIZE
  1404. obfuscator, err := obfuscator.NewClientObfuscator(
  1405. &obfuscator.ObfuscatorConfig{
  1406. Keyword: string(obfuscatedKey),
  1407. PaddingPRNGSeed: paddingPRNGSeed,
  1408. MaxPadding: &maxPadding})
  1409. if err != nil {
  1410. return nil, common.ContextError(err)
  1411. }
  1412. obfuscatedBox := obfuscator.SendSeedMessage()
  1413. seedLen := len(obfuscatedBox)
  1414. obfuscatedBox = append(obfuscatedBox, box...)
  1415. obfuscator.ObfuscateClientToServer(obfuscatedBox[seedLen:])
  1416. return obfuscatedBox, nil
  1417. }
  1418. // unboxPayload mutates obfuscatedBoxedPayload by deobfuscating in-place.
  1419. func unboxPayload(
  1420. nonce, peerPublicKey, privateKey, obfuscatedKey, obfuscatedBoxedPayload []byte,
  1421. payload interface{}) ([]byte, error) {
  1422. if len(nonce) > 24 ||
  1423. (peerPublicKey != nil && len(peerPublicKey) != 32) ||
  1424. len(privateKey) != 32 {
  1425. return nil, common.ContextError(
  1426. errors.New("unexpected box key length"))
  1427. }
  1428. obfuscatedReader := bytes.NewReader(obfuscatedBoxedPayload[:])
  1429. obfuscator, err := obfuscator.NewServerObfuscator(
  1430. obfuscatedReader,
  1431. &obfuscator.ObfuscatorConfig{Keyword: string(obfuscatedKey)})
  1432. if err != nil {
  1433. return nil, common.ContextError(err)
  1434. }
  1435. seedLen, err := obfuscatedReader.Seek(0, 1)
  1436. if err != nil {
  1437. return nil, common.ContextError(err)
  1438. }
  1439. boxedPayload := obfuscatedBoxedPayload[seedLen:]
  1440. obfuscator.ObfuscateClientToServer(boxedPayload)
  1441. var nonceArray [24]byte
  1442. copy(nonceArray[:], nonce)
  1443. var peerPublicKeyArray, privateKeyArray [32]byte
  1444. copy(privateKeyArray[:], privateKey)
  1445. var bundledPeerPublicKey []byte
  1446. if peerPublicKey != nil {
  1447. copy(peerPublicKeyArray[:], peerPublicKey)
  1448. } else {
  1449. if len(boxedPayload) < 32 {
  1450. return nil, common.ContextError(errors.New("unexpected box size"))
  1451. }
  1452. bundledPeerPublicKey = boxedPayload[0:32]
  1453. copy(peerPublicKeyArray[:], bundledPeerPublicKey)
  1454. boxedPayload = boxedPayload[32:]
  1455. }
  1456. marshaledPayload, ok := box.Open(nil, boxedPayload, &nonceArray, &peerPublicKeyArray, &privateKeyArray)
  1457. if !ok {
  1458. return nil, common.ContextError(errors.New("invalid box"))
  1459. }
  1460. err = json.Unmarshal(marshaledPayload, payload)
  1461. if err != nil {
  1462. return nil, common.ContextError(err)
  1463. }
  1464. return bundledPeerPublicKey, nil
  1465. }