api.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "crypto/subtle"
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "net"
  26. "regexp"
  27. "runtime/debug"
  28. "strconv"
  29. "strings"
  30. "unicode"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
  34. )
  35. const (
  36. MAX_API_PARAMS_SIZE = 256 * 1024 // 256KB
  37. CLIENT_PLATFORM_ANDROID = "Android"
  38. CLIENT_PLATFORM_WINDOWS = "Windows"
  39. CLIENT_PLATFORM_IOS = "iOS"
  40. )
  41. // sshAPIRequestHandler routes Psiphon API requests transported as
  42. // JSON objects via the SSH request mechanism.
  43. //
  44. // The API request handlers, handshakeAPIRequestHandler, etc., are
  45. // reused by webServer which offers the Psiphon API via web transport.
  46. //
  47. // The API request parameters and event log values follow the legacy
  48. // psi_web protocol and naming conventions. The API is compatible with
  49. // all tunnel-core clients but are not backwards compatible with all
  50. // legacy clients.
  51. //
  52. func sshAPIRequestHandler(
  53. support *SupportServices,
  54. geoIPData GeoIPData,
  55. authorizedAccessTypes []string,
  56. name string,
  57. requestPayload []byte) ([]byte, error) {
  58. // Notes:
  59. //
  60. // - For SSH requests, MAX_API_PARAMS_SIZE is implicitly enforced
  61. // by max SSH request packet size.
  62. //
  63. // - The param protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS is an
  64. // array of base64-encoded strings; the base64 representation should
  65. // not be decoded to []byte values. The default behavior of
  66. // https://golang.org/pkg/encoding/json/#Unmarshal for a target of
  67. // type map[string]interface{} will unmarshal a base64-encoded string
  68. // to a string, not a decoded []byte, as required.
  69. var params common.APIParameters
  70. err := json.Unmarshal(requestPayload, &params)
  71. if err != nil {
  72. return nil, common.ContextError(
  73. fmt.Errorf("invalid payload for request name: %s: %s", name, err))
  74. }
  75. return dispatchAPIRequestHandler(
  76. support,
  77. protocol.PSIPHON_SSH_API_PROTOCOL,
  78. geoIPData,
  79. authorizedAccessTypes,
  80. name,
  81. params)
  82. }
  83. // dispatchAPIRequestHandler is the common dispatch point for both
  84. // web and SSH API requests.
  85. func dispatchAPIRequestHandler(
  86. support *SupportServices,
  87. apiProtocol string,
  88. geoIPData GeoIPData,
  89. authorizedAccessTypes []string,
  90. name string,
  91. params common.APIParameters) (response []byte, reterr error) {
  92. // Recover from and log any unexpected panics caused by user input
  93. // handling bugs. User inputs should be properly validated; this
  94. // mechanism is only a last resort to prevent the process from
  95. // terminating in the case of a bug.
  96. defer func() {
  97. if e := recover(); e != nil {
  98. if intentionalPanic, ok := e.(IntentionalPanicError); ok {
  99. panic(intentionalPanic)
  100. } else {
  101. log.LogPanicRecover(e, debug.Stack())
  102. reterr = common.ContextError(errors.New("request handler panic"))
  103. }
  104. }
  105. }()
  106. // Before invoking the handlers, enforce some preconditions:
  107. //
  108. // - A handshake request must precede any other requests.
  109. // - When the handshake results in a traffic rules state where
  110. // the client is immediately exhausted, no requests
  111. // may succeed. This case ensures that blocked clients do
  112. // not log "connected", etc.
  113. //
  114. // Only one handshake request may be made. There is no check here
  115. // to enforce that handshakeAPIRequestHandler will be called at
  116. // most once. The SetHandshakeState call in handshakeAPIRequestHandler
  117. // enforces that only a single handshake is made; enforcing that there
  118. // ensures no race condition even if concurrent requests are
  119. // in flight.
  120. if name != protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME {
  121. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  122. // applies here.
  123. sessionID, err := getStringRequestParam(params, "client_session_id")
  124. if err == nil {
  125. // Note: follows/duplicates baseRequestParams validation
  126. if !isHexDigits(support.Config, sessionID) {
  127. err = errors.New("invalid param: client_session_id")
  128. }
  129. }
  130. if err != nil {
  131. return nil, common.ContextError(err)
  132. }
  133. completed, exhausted, err := support.TunnelServer.GetClientHandshaked(sessionID)
  134. if err != nil {
  135. return nil, common.ContextError(err)
  136. }
  137. if !completed {
  138. return nil, common.ContextError(errors.New("handshake not completed"))
  139. }
  140. if exhausted {
  141. return nil, common.ContextError(errors.New("exhausted after handshake"))
  142. }
  143. }
  144. switch name {
  145. case protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME:
  146. return handshakeAPIRequestHandler(support, apiProtocol, geoIPData, params)
  147. case protocol.PSIPHON_API_CONNECTED_REQUEST_NAME:
  148. return connectedAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  149. case protocol.PSIPHON_API_STATUS_REQUEST_NAME:
  150. return statusAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  151. case protocol.PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME:
  152. return clientVerificationAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  153. }
  154. return nil, common.ContextError(fmt.Errorf("invalid request name: %s", name))
  155. }
  156. var handshakeRequestParams = append(
  157. append(
  158. // Note: legacy clients may not send "session_id" in handshake
  159. []requestParamSpec{{"session_id", isHexDigits, requestParamOptional}},
  160. tacticsParams...),
  161. baseRequestParams...)
  162. // handshakeAPIRequestHandler implements the "handshake" API request.
  163. // Clients make the handshake immediately after establishing a tunnel
  164. // connection; the response tells the client what homepage to open, what
  165. // stats to record, etc.
  166. func handshakeAPIRequestHandler(
  167. support *SupportServices,
  168. apiProtocol string,
  169. geoIPData GeoIPData,
  170. params common.APIParameters) ([]byte, error) {
  171. // Note: ignoring "known_servers" params
  172. err := validateRequestParams(support.Config, params, baseRequestParams)
  173. if err != nil {
  174. return nil, common.ContextError(err)
  175. }
  176. sessionID, _ := getStringRequestParam(params, "client_session_id")
  177. sponsorID, _ := getStringRequestParam(params, "sponsor_id")
  178. clientVersion, _ := getStringRequestParam(params, "client_version")
  179. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  180. isMobile := isMobileClientPlatform(clientPlatform)
  181. normalizedPlatform := normalizeClientPlatform(clientPlatform)
  182. var authorizations []string
  183. if params[protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS] != nil {
  184. authorizations, err = getStringArrayRequestParam(params, protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS)
  185. if err != nil {
  186. return nil, common.ContextError(err)
  187. }
  188. }
  189. // Note: no guarantee that PsinetDatabase won't reload between database calls
  190. db := support.PsinetDatabase
  191. httpsRequestRegexes := db.GetHttpsRequestRegexes(sponsorID)
  192. // Flag the SSH client as having completed its handshake. This
  193. // may reselect traffic rules and starts allowing port forwards.
  194. // TODO: in the case of SSH API requests, the actual sshClient could
  195. // be passed in and used here. The session ID lookup is only strictly
  196. // necessary to support web API requests.
  197. activeAuthorizationIDs, authorizedAccessTypes, err := support.TunnelServer.SetClientHandshakeState(
  198. sessionID,
  199. handshakeState{
  200. completed: true,
  201. apiProtocol: apiProtocol,
  202. apiParams: copyBaseRequestParams(params),
  203. expectDomainBytes: len(httpsRequestRegexes) > 0,
  204. },
  205. authorizations)
  206. if err != nil {
  207. return nil, common.ContextError(err)
  208. }
  209. tacticsPayload, err := support.TacticsServer.GetTacticsPayload(
  210. common.GeoIPData(geoIPData), params)
  211. if err != nil {
  212. return nil, common.ContextError(err)
  213. }
  214. var marshaledTacticsPayload []byte
  215. if tacticsPayload != nil {
  216. marshaledTacticsPayload, err = json.Marshal(tacticsPayload)
  217. if err != nil {
  218. return nil, common.ContextError(err)
  219. }
  220. // Log a metric when new tactics are issued. Logging here indicates that
  221. // the handshake tactics mechansim is active; but logging for every
  222. // handshake creates unneccesary log data.
  223. if len(tacticsPayload.Tactics) > 0 {
  224. logFields := getRequestLogFields(
  225. tactics.TACTICS_METRIC_EVENT_NAME,
  226. geoIPData,
  227. authorizedAccessTypes,
  228. params,
  229. handshakeRequestParams)
  230. logFields[tactics.NEW_TACTICS_TAG_LOG_FIELD_NAME] = tacticsPayload.Tag
  231. logFields[tactics.IS_TACTICS_REQUEST_LOG_FIELD_NAME] = false
  232. log.LogRawFieldsWithTimestamp(logFields)
  233. }
  234. }
  235. // The log comes _after_ SetClientHandshakeState, in case that call rejects
  236. // the state change (for example, if a second handshake is performed)
  237. //
  238. // The handshake event is no longer shipped to log consumers, so this is
  239. // simply a diagnostic log.
  240. log.WithContextFields(
  241. getRequestLogFields(
  242. "",
  243. geoIPData,
  244. authorizedAccessTypes,
  245. params,
  246. baseRequestParams)).Info("handshake")
  247. handshakeResponse := protocol.HandshakeResponse{
  248. SSHSessionID: sessionID,
  249. Homepages: db.GetRandomizedHomepages(sponsorID, geoIPData.Country, isMobile),
  250. UpgradeClientVersion: db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
  251. PageViewRegexes: make([]map[string]string, 0),
  252. HttpsRequestRegexes: httpsRequestRegexes,
  253. EncodedServerList: db.DiscoverServers(geoIPData.DiscoveryValue),
  254. ClientRegion: geoIPData.Country,
  255. ServerTimestamp: common.GetCurrentTimestamp(),
  256. ActiveAuthorizationIDs: activeAuthorizationIDs,
  257. TacticsPayload: marshaledTacticsPayload,
  258. }
  259. responsePayload, err := json.Marshal(handshakeResponse)
  260. if err != nil {
  261. return nil, common.ContextError(err)
  262. }
  263. return responsePayload, nil
  264. }
  265. var connectedRequestParams = append(
  266. []requestParamSpec{
  267. {"session_id", isHexDigits, 0},
  268. {"last_connected", isLastConnected, 0},
  269. {"establishment_duration", isIntString, requestParamOptional | requestParamLogStringAsInt}},
  270. baseRequestParams...)
  271. // connectedAPIRequestHandler implements the "connected" API request.
  272. // Clients make the connected request once a tunnel connection has been
  273. // established and at least once per day. The last_connected input value,
  274. // which should be a connected_timestamp output from a previous connected
  275. // response, is used to calculate unique user stats.
  276. // connected_timestamp is truncated as a privacy measure.
  277. func connectedAPIRequestHandler(
  278. support *SupportServices,
  279. geoIPData GeoIPData,
  280. authorizedAccessTypes []string,
  281. params common.APIParameters) ([]byte, error) {
  282. err := validateRequestParams(support.Config, params, connectedRequestParams)
  283. if err != nil {
  284. return nil, common.ContextError(err)
  285. }
  286. // Update upstream fragmentor metrics, as the client may have performed
  287. // more upstream fragmentation since the previous metrics reported by the
  288. // handshake request.
  289. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  290. // applies here.
  291. sessionID, _ := getStringRequestParam(params, "client_session_id")
  292. err = support.TunnelServer.UpdateClientAPIParameters(
  293. sessionID, copyUpstreamFragmentorParams(params))
  294. if err != nil {
  295. return nil, common.ContextError(err)
  296. }
  297. log.LogRawFieldsWithTimestamp(
  298. getRequestLogFields(
  299. "connected",
  300. geoIPData,
  301. authorizedAccessTypes,
  302. params,
  303. connectedRequestParams))
  304. connectedResponse := protocol.ConnectedResponse{
  305. ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
  306. }
  307. responsePayload, err := json.Marshal(connectedResponse)
  308. if err != nil {
  309. return nil, common.ContextError(err)
  310. }
  311. return responsePayload, nil
  312. }
  313. var statusRequestParams = append(
  314. []requestParamSpec{
  315. {"session_id", isHexDigits, 0},
  316. {"connected", isBooleanFlag, 0}},
  317. baseRequestParams...)
  318. // statusAPIRequestHandler implements the "status" API request.
  319. // Clients make periodic status requests which deliver client-side
  320. // recorded data transfer and tunnel duration stats.
  321. // Note from psi_web implementation: no input validation on domains;
  322. // any string is accepted (regex transform may result in arbitrary
  323. // string). Stats processor must handle this input with care.
  324. func statusAPIRequestHandler(
  325. support *SupportServices,
  326. geoIPData GeoIPData,
  327. authorizedAccessTypes []string,
  328. params common.APIParameters) ([]byte, error) {
  329. err := validateRequestParams(support.Config, params, statusRequestParams)
  330. if err != nil {
  331. return nil, common.ContextError(err)
  332. }
  333. sessionID, _ := getStringRequestParam(params, "client_session_id")
  334. statusData, err := getJSONObjectRequestParam(params, "statusData")
  335. if err != nil {
  336. return nil, common.ContextError(err)
  337. }
  338. // Logs are queued until the input is fully validated. Otherwise, stats
  339. // could be double counted if the client has a bug in its request
  340. // formatting: partial stats would be logged (counted), the request would
  341. // fail, and clients would then resend all the same stats again.
  342. logQueue := make([]LogFields, 0)
  343. // Domain bytes transferred stats
  344. // Older clients may not submit this data
  345. // Clients are expected to send host_bytes/domain_bytes stats only when
  346. // configured to do so in the handshake reponse. Legacy clients may still
  347. // report "(OTHER)" host_bytes when no regexes are set. Drop those stats.
  348. domainBytesExpected, err := support.TunnelServer.ExpectClientDomainBytes(sessionID)
  349. if err != nil {
  350. return nil, common.ContextError(err)
  351. }
  352. if domainBytesExpected && statusData["host_bytes"] != nil {
  353. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  354. if err != nil {
  355. return nil, common.ContextError(err)
  356. }
  357. for domain, bytes := range hostBytes {
  358. domainBytesFields := getRequestLogFields(
  359. "domain_bytes",
  360. geoIPData,
  361. authorizedAccessTypes,
  362. params,
  363. statusRequestParams)
  364. domainBytesFields["domain"] = domain
  365. domainBytesFields["bytes"] = bytes
  366. logQueue = append(logQueue, domainBytesFields)
  367. }
  368. }
  369. // Remote server list download stats
  370. // Older clients may not submit this data
  371. if statusData["remote_server_list_stats"] != nil {
  372. remoteServerListStats, err := getJSONObjectArrayRequestParam(statusData, "remote_server_list_stats")
  373. if err != nil {
  374. return nil, common.ContextError(err)
  375. }
  376. for _, remoteServerListStat := range remoteServerListStats {
  377. remoteServerListFields := getRequestLogFields(
  378. "remote_server_list",
  379. geoIPData,
  380. authorizedAccessTypes,
  381. params,
  382. statusRequestParams)
  383. clientDownloadTimestamp, err := getStringRequestParam(remoteServerListStat, "client_download_timestamp")
  384. if err != nil {
  385. return nil, common.ContextError(err)
  386. }
  387. remoteServerListFields["client_download_timestamp"] = clientDownloadTimestamp
  388. url, err := getStringRequestParam(remoteServerListStat, "url")
  389. if err != nil {
  390. return nil, common.ContextError(err)
  391. }
  392. remoteServerListFields["url"] = url
  393. etag, err := getStringRequestParam(remoteServerListStat, "etag")
  394. if err != nil {
  395. return nil, common.ContextError(err)
  396. }
  397. remoteServerListFields["etag"] = etag
  398. logQueue = append(logQueue, remoteServerListFields)
  399. }
  400. }
  401. for _, logItem := range logQueue {
  402. log.LogRawFieldsWithTimestamp(logItem)
  403. }
  404. return make([]byte, 0), nil
  405. }
  406. // clientVerificationAPIRequestHandler is just a compliance stub
  407. // for older Android clients that still send verification requests
  408. func clientVerificationAPIRequestHandler(
  409. support *SupportServices,
  410. geoIPData GeoIPData,
  411. authorizedAccessTypes []string,
  412. params common.APIParameters) ([]byte, error) {
  413. return make([]byte, 0), nil
  414. }
  415. var tacticsParams = []requestParamSpec{
  416. {tactics.STORED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  417. {tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME, nil, requestParamOptional | requestParamJSON},
  418. }
  419. var tacticsRequestParams = append(
  420. append(
  421. []requestParamSpec{{"session_id", isHexDigits, 0}},
  422. tacticsParams...),
  423. baseRequestParams...)
  424. func getTacticsAPIParameterValidator(config *Config) common.APIParameterValidator {
  425. return func(params common.APIParameters) error {
  426. return validateRequestParams(config, params, tacticsRequestParams)
  427. }
  428. }
  429. func getTacticsAPIParameterLogFieldFormatter() common.APIParameterLogFieldFormatter {
  430. return func(geoIPData common.GeoIPData, params common.APIParameters) common.LogFields {
  431. logFields := getRequestLogFields(
  432. tactics.TACTICS_METRIC_EVENT_NAME,
  433. GeoIPData(geoIPData),
  434. nil, // authorizedAccessTypes are not known yet
  435. params,
  436. tacticsRequestParams)
  437. return common.LogFields(logFields)
  438. }
  439. }
  440. type requestParamSpec struct {
  441. name string
  442. validator func(*Config, string) bool
  443. flags uint32
  444. }
  445. const (
  446. requestParamOptional = 1
  447. requestParamNotLogged = 2
  448. requestParamArray = 4
  449. requestParamJSON = 8
  450. requestParamLogStringAsInt = 16
  451. )
  452. var upstreamFragmentorParams = []requestParamSpec{
  453. {"upstream_bytes_fragmented", isIntString, requestParamOptional | requestParamLogStringAsInt},
  454. {"upstream_min_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  455. {"upstream_max_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  456. {"upstream_min_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  457. {"upstream_max_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  458. }
  459. // baseRequestParams is the list of required and optional
  460. // request parameters; derived from COMMON_INPUTS and
  461. // OPTIONAL_COMMON_INPUTS in psi_web.
  462. // Each param is expected to be a string, unless requestParamArray
  463. // is specified, in which case an array of string is expected.
  464. var baseRequestParams = append(
  465. []requestParamSpec{
  466. {"server_secret", isServerSecret, requestParamNotLogged},
  467. {"client_session_id", isHexDigits, requestParamNotLogged},
  468. {"propagation_channel_id", isHexDigits, 0},
  469. {"sponsor_id", isHexDigits, 0},
  470. {"client_version", isIntString, requestParamLogStringAsInt},
  471. {"client_platform", isClientPlatform, 0},
  472. {"client_build_rev", isHexDigits, requestParamOptional},
  473. {"relay_protocol", isRelayProtocol, 0},
  474. {"tunnel_whole_device", isBooleanFlag, requestParamOptional},
  475. {"device_region", isAnyString, requestParamOptional},
  476. {"ssh_client_version", isAnyString, requestParamOptional},
  477. {"upstream_proxy_type", isUpstreamProxyType, requestParamOptional},
  478. {"upstream_proxy_custom_header_names", isAnyString, requestParamOptional | requestParamArray},
  479. {"meek_dial_address", isDialAddress, requestParamOptional},
  480. {"meek_resolved_ip_address", isIPAddress, requestParamOptional},
  481. {"meek_sni_server_name", isDomain, requestParamOptional},
  482. {"meek_host_header", isHostHeader, requestParamOptional},
  483. {"meek_transformed_host_name", isBooleanFlag, requestParamOptional},
  484. {"user_agent", isAnyString, requestParamOptional},
  485. {"tls_profile", isAnyString, requestParamOptional},
  486. {"server_entry_region", isRegionCode, requestParamOptional},
  487. {"server_entry_source", isServerEntrySource, requestParamOptional},
  488. {"server_entry_timestamp", isISO8601Date, requestParamOptional},
  489. {tactics.APPLIED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  490. {"dial_port_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  491. {"quic_version", isAnyString, requestParamOptional},
  492. {"quic_dial_sni_address", isAnyString, requestParamOptional},
  493. {"upstream_ossh_padding", isIntString, requestParamOptional | requestParamLogStringAsInt},
  494. },
  495. upstreamFragmentorParams...)
  496. func validateRequestParams(
  497. config *Config,
  498. params common.APIParameters,
  499. expectedParams []requestParamSpec) error {
  500. for _, expectedParam := range expectedParams {
  501. value := params[expectedParam.name]
  502. if value == nil {
  503. if expectedParam.flags&requestParamOptional != 0 {
  504. continue
  505. }
  506. return common.ContextError(
  507. fmt.Errorf("missing param: %s", expectedParam.name))
  508. }
  509. var err error
  510. switch {
  511. case expectedParam.flags&requestParamArray != 0:
  512. err = validateStringArrayRequestParam(config, expectedParam, value)
  513. case expectedParam.flags&requestParamJSON != 0:
  514. // No validation: the JSON already unmarshalled; the parameter
  515. // user will validate that the JSON contains the expected
  516. // objects/data.
  517. // TODO: without validation, any valid JSON will be logged
  518. // by getRequestLogFields, even if the parameter user validates
  519. // and rejects the parameter.
  520. default:
  521. err = validateStringRequestParam(config, expectedParam, value)
  522. }
  523. if err != nil {
  524. return common.ContextError(err)
  525. }
  526. }
  527. return nil
  528. }
  529. // copyBaseRequestParams makes a copy of the params which
  530. // includes only the baseRequestParams.
  531. func copyBaseRequestParams(params common.APIParameters) common.APIParameters {
  532. // Note: not a deep copy; assumes baseRequestParams values
  533. // are all scalar types (int, string, etc.)
  534. paramsCopy := make(common.APIParameters)
  535. for _, baseParam := range baseRequestParams {
  536. value := params[baseParam.name]
  537. if value == nil {
  538. continue
  539. }
  540. paramsCopy[baseParam.name] = value
  541. }
  542. return paramsCopy
  543. }
  544. func copyUpstreamFragmentorParams(params common.APIParameters) common.APIParameters {
  545. // Note: not a deep copy
  546. paramsCopy := make(common.APIParameters)
  547. for _, baseParam := range upstreamFragmentorParams {
  548. value := params[baseParam.name]
  549. if value == nil {
  550. continue
  551. }
  552. paramsCopy[baseParam.name] = value
  553. }
  554. return paramsCopy
  555. }
  556. func validateStringRequestParam(
  557. config *Config,
  558. expectedParam requestParamSpec,
  559. value interface{}) error {
  560. strValue, ok := value.(string)
  561. if !ok {
  562. return common.ContextError(
  563. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  564. }
  565. if !expectedParam.validator(config, strValue) {
  566. return common.ContextError(
  567. fmt.Errorf("invalid param: %s: %s", expectedParam.name, strValue))
  568. }
  569. return nil
  570. }
  571. func validateStringArrayRequestParam(
  572. config *Config,
  573. expectedParam requestParamSpec,
  574. value interface{}) error {
  575. arrayValue, ok := value.([]interface{})
  576. if !ok {
  577. return common.ContextError(
  578. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  579. }
  580. for _, value := range arrayValue {
  581. err := validateStringRequestParam(config, expectedParam, value)
  582. if err != nil {
  583. return common.ContextError(err)
  584. }
  585. }
  586. return nil
  587. }
  588. // getRequestLogFields makes LogFields to log the API event following
  589. // the legacy psi_web and current ELK naming conventions.
  590. func getRequestLogFields(
  591. eventName string,
  592. geoIPData GeoIPData,
  593. authorizedAccessTypes []string,
  594. params common.APIParameters,
  595. expectedParams []requestParamSpec) LogFields {
  596. logFields := make(LogFields)
  597. if eventName != "" {
  598. logFields["event_name"] = eventName
  599. }
  600. // In psi_web, the space replacement was done to accommodate space
  601. // delimited logging, which is no longer required; we retain the
  602. // transformation so that stats aggregation isn't impacted.
  603. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  604. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  605. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  606. if len(authorizedAccessTypes) > 0 {
  607. logFields["authorized_access_types"] = authorizedAccessTypes
  608. }
  609. if params == nil {
  610. return logFields
  611. }
  612. for _, expectedParam := range expectedParams {
  613. if expectedParam.flags&requestParamNotLogged != 0 {
  614. continue
  615. }
  616. value := params[expectedParam.name]
  617. if value == nil {
  618. // Special case: older clients don't send this value,
  619. // so log a default.
  620. if expectedParam.name == "tunnel_whole_device" {
  621. value = "0"
  622. } else {
  623. // Skip omitted, optional params
  624. continue
  625. }
  626. }
  627. switch v := value.(type) {
  628. case string:
  629. strValue := v
  630. // Special cases:
  631. // - Number fields are encoded as integer types.
  632. // - For ELK performance we record certain domain-or-IP
  633. // fields as one of two different values based on type;
  634. // we also omit port from these host:port fields for now.
  635. // - Boolean fields that come into the api as "1"/"0"
  636. // must be logged as actual boolean values
  637. switch expectedParam.name {
  638. case "meek_dial_address":
  639. host, _, _ := net.SplitHostPort(strValue)
  640. if isIPAddress(nil, host) {
  641. logFields["meek_dial_ip_address"] = host
  642. } else {
  643. logFields["meek_dial_domain"] = host
  644. }
  645. case "upstream_proxy_type":
  646. // Submitted value could be e.g., "SOCKS5" or "socks5"; log lowercase
  647. logFields[expectedParam.name] = strings.ToLower(strValue)
  648. case tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME:
  649. // Due to a client bug, clients may deliever an incorrect ""
  650. // value for speed_test_samples via the web API protocol. Omit
  651. // the field in this case.
  652. case "tunnel_whole_device", "meek_transformed_host_name", "connected":
  653. // Submitted value could be "0" or "1"
  654. // "0" and non "0"/"1" values should be transformed to false
  655. // "1" should be transformed to true
  656. if strValue == "1" {
  657. logFields[expectedParam.name] = true
  658. } else {
  659. logFields[expectedParam.name] = false
  660. }
  661. default:
  662. if expectedParam.flags&requestParamLogStringAsInt != 0 {
  663. intValue, _ := strconv.Atoi(strValue)
  664. logFields[expectedParam.name] = intValue
  665. } else {
  666. logFields[expectedParam.name] = strValue
  667. }
  668. }
  669. case []interface{}:
  670. if expectedParam.name == tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME {
  671. logFields[expectedParam.name] = makeSpeedTestSamplesLogField(v)
  672. } else {
  673. logFields[expectedParam.name] = v
  674. }
  675. default:
  676. logFields[expectedParam.name] = v
  677. }
  678. }
  679. return logFields
  680. }
  681. // makeSpeedTestSamplesLogField renames the tactics.SpeedTestSample json tag
  682. // fields to more verbose names for metrics.
  683. func makeSpeedTestSamplesLogField(samples []interface{}) []interface{} {
  684. // TODO: use reflection and add additional tags, e.g.,
  685. // `json:"s" log:"timestamp"` to remove hard-coded
  686. // tag value dependency?
  687. logSamples := make([]interface{}, len(samples))
  688. for i, sample := range samples {
  689. logSample := make(map[string]interface{})
  690. if m, ok := sample.(map[string]interface{}); ok {
  691. for k, v := range m {
  692. logK := k
  693. switch k {
  694. case "s":
  695. logK = "timestamp"
  696. case "r":
  697. logK = "server_region"
  698. case "p":
  699. logK = "relay_protocol"
  700. case "t":
  701. logK = "round_trip_time_ms"
  702. case "u":
  703. logK = "bytes_up"
  704. case "d":
  705. logK = "bytes_down"
  706. }
  707. logSample[logK] = v
  708. }
  709. }
  710. logSamples[i] = logSample
  711. }
  712. return logSamples
  713. }
  714. func getStringRequestParam(params common.APIParameters, name string) (string, error) {
  715. if params[name] == nil {
  716. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  717. }
  718. value, ok := params[name].(string)
  719. if !ok {
  720. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  721. }
  722. return value, nil
  723. }
  724. func getInt64RequestParam(params common.APIParameters, name string) (int64, error) {
  725. if params[name] == nil {
  726. return 0, common.ContextError(fmt.Errorf("missing param: %s", name))
  727. }
  728. value, ok := params[name].(float64)
  729. if !ok {
  730. return 0, common.ContextError(fmt.Errorf("invalid param: %s", name))
  731. }
  732. return int64(value), nil
  733. }
  734. func getJSONObjectRequestParam(params common.APIParameters, name string) (common.APIParameters, error) {
  735. if params[name] == nil {
  736. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  737. }
  738. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  739. value, ok := params[name].(map[string]interface{})
  740. if !ok {
  741. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  742. }
  743. return common.APIParameters(value), nil
  744. }
  745. func getJSONObjectArrayRequestParam(params common.APIParameters, name string) ([]common.APIParameters, error) {
  746. if params[name] == nil {
  747. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  748. }
  749. value, ok := params[name].([]interface{})
  750. if !ok {
  751. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  752. }
  753. result := make([]common.APIParameters, len(value))
  754. for i, item := range value {
  755. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  756. resultItem, ok := item.(map[string]interface{})
  757. if !ok {
  758. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  759. }
  760. result[i] = common.APIParameters(resultItem)
  761. }
  762. return result, nil
  763. }
  764. func getMapStringInt64RequestParam(params common.APIParameters, name string) (map[string]int64, error) {
  765. if params[name] == nil {
  766. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  767. }
  768. // TODO: can't use common.APIParameters type?
  769. value, ok := params[name].(map[string]interface{})
  770. if !ok {
  771. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  772. }
  773. result := make(map[string]int64)
  774. for k, v := range value {
  775. numValue, ok := v.(float64)
  776. if !ok {
  777. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  778. }
  779. result[k] = int64(numValue)
  780. }
  781. return result, nil
  782. }
  783. func getStringArrayRequestParam(params common.APIParameters, name string) ([]string, error) {
  784. if params[name] == nil {
  785. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  786. }
  787. value, ok := params[name].([]interface{})
  788. if !ok {
  789. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  790. }
  791. result := make([]string, len(value))
  792. for i, v := range value {
  793. strValue, ok := v.(string)
  794. if !ok {
  795. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  796. }
  797. result[i] = strValue
  798. }
  799. return result, nil
  800. }
  801. // Normalize reported client platform. Android clients, for example, report
  802. // OS version, rooted status, and Google Play build status in the clientPlatform
  803. // string along with "Android".
  804. func normalizeClientPlatform(clientPlatform string) string {
  805. if strings.Contains(strings.ToLower(clientPlatform), strings.ToLower(CLIENT_PLATFORM_ANDROID)) {
  806. return CLIENT_PLATFORM_ANDROID
  807. } else if strings.HasPrefix(clientPlatform, CLIENT_PLATFORM_IOS) {
  808. return CLIENT_PLATFORM_IOS
  809. }
  810. return CLIENT_PLATFORM_WINDOWS
  811. }
  812. func isAnyString(config *Config, value string) bool {
  813. return true
  814. }
  815. func isMobileClientPlatform(clientPlatform string) bool {
  816. normalizedClientPlatform := normalizeClientPlatform(clientPlatform)
  817. return normalizedClientPlatform == CLIENT_PLATFORM_ANDROID ||
  818. normalizedClientPlatform == CLIENT_PLATFORM_IOS
  819. }
  820. // Input validators follow the legacy validations rules in psi_web.
  821. func isServerSecret(config *Config, value string) bool {
  822. return subtle.ConstantTimeCompare(
  823. []byte(value),
  824. []byte(config.WebServerSecret)) == 1
  825. }
  826. func isHexDigits(_ *Config, value string) bool {
  827. // Allows both uppercase in addition to lowercase, for legacy support.
  828. return -1 == strings.IndexFunc(value, func(c rune) bool {
  829. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  830. })
  831. }
  832. func isDigits(_ *Config, value string) bool {
  833. return -1 == strings.IndexFunc(value, func(c rune) bool {
  834. return c < '0' || c > '9'
  835. })
  836. }
  837. func isIntString(_ *Config, value string) bool {
  838. _, err := strconv.Atoi(value)
  839. return err == nil
  840. }
  841. func isClientPlatform(_ *Config, value string) bool {
  842. return -1 == strings.IndexFunc(value, func(c rune) bool {
  843. // Note: stricter than psi_web's Python string.whitespace
  844. return unicode.Is(unicode.White_Space, c)
  845. })
  846. }
  847. func isRelayProtocol(_ *Config, value string) bool {
  848. return common.Contains(protocol.SupportedTunnelProtocols, value)
  849. }
  850. func isBooleanFlag(_ *Config, value string) bool {
  851. return value == "0" || value == "1"
  852. }
  853. func isUpstreamProxyType(_ *Config, value string) bool {
  854. value = strings.ToLower(value)
  855. return value == "http" || value == "socks5" || value == "socks4a"
  856. }
  857. func isRegionCode(_ *Config, value string) bool {
  858. if len(value) != 2 {
  859. return false
  860. }
  861. return -1 == strings.IndexFunc(value, func(c rune) bool {
  862. return c < 'A' || c > 'Z'
  863. })
  864. }
  865. func isDialAddress(_ *Config, value string) bool {
  866. // "<host>:<port>", where <host> is a domain or IP address
  867. parts := strings.Split(value, ":")
  868. if len(parts) != 2 {
  869. return false
  870. }
  871. if !isIPAddress(nil, parts[0]) && !isDomain(nil, parts[0]) {
  872. return false
  873. }
  874. if !isDigits(nil, parts[1]) {
  875. return false
  876. }
  877. port, err := strconv.Atoi(parts[1])
  878. if err != nil {
  879. return false
  880. }
  881. return port > 0 && port < 65536
  882. }
  883. func isIPAddress(_ *Config, value string) bool {
  884. return net.ParseIP(value) != nil
  885. }
  886. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  887. func isDomain(_ *Config, value string) bool {
  888. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  889. //
  890. // "ensures that each segment
  891. // * contains at least one character and a maximum of 63 characters
  892. // * consists only of allowed characters
  893. // * doesn't begin or end with a hyphen"
  894. //
  895. if len(value) > 255 {
  896. return false
  897. }
  898. value = strings.TrimSuffix(value, ".")
  899. for _, part := range strings.Split(value, ".") {
  900. // Note: regexp doesn't support the following Perl expression which
  901. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  902. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  903. return false
  904. }
  905. if !isDomainRegex.Match([]byte(part)) {
  906. return false
  907. }
  908. }
  909. return true
  910. }
  911. func isHostHeader(_ *Config, value string) bool {
  912. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  913. if strings.Contains(value, ":") {
  914. return isDialAddress(nil, value)
  915. }
  916. return isIPAddress(nil, value) || isDomain(nil, value)
  917. }
  918. func isServerEntrySource(_ *Config, value string) bool {
  919. return common.Contains(protocol.SupportedServerEntrySources, value)
  920. }
  921. var isISO8601DateRegex = regexp.MustCompile(
  922. "(?P<year>[0-9]{4})-(?P<month>[0-9]{1,2})-(?P<day>[0-9]{1,2})T(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2}):(?P<second>[0-9]{2})(\\.(?P<fraction>[0-9]+))?(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))")
  923. func isISO8601Date(_ *Config, value string) bool {
  924. return isISO8601DateRegex.Match([]byte(value))
  925. }
  926. func isLastConnected(_ *Config, value string) bool {
  927. return value == "None" || value == "Unknown" || isISO8601Date(nil, value)
  928. }