tactics.go 56 KB

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