api.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. )
  34. const (
  35. MAX_API_PARAMS_SIZE = 256 * 1024 // 256KB
  36. CLIENT_VERIFICATION_TTL_SECONDS = 60 * 60 * 24 * 7 // 7 days
  37. CLIENT_PLATFORM_ANDROID = "Android"
  38. CLIENT_PLATFORM_WINDOWS = "Windows"
  39. CLIENT_PLATFORM_IOS = "iOS"
  40. )
  41. var CLIENT_VERIFICATION_REQUIRED = false
  42. type requestJSONObject map[string]interface{}
  43. // sshAPIRequestHandler routes Psiphon API requests transported as
  44. // JSON objects via the SSH request mechanism.
  45. //
  46. // The API request handlers, handshakeAPIRequestHandler, etc., are
  47. // reused by webServer which offers the Psiphon API via web transport.
  48. //
  49. // The API request parameters and event log values follow the legacy
  50. // psi_web protocol and naming conventions. The API is compatible all
  51. // tunnel-core clients but are not backwards compatible with older
  52. // clients.
  53. //
  54. func sshAPIRequestHandler(
  55. support *SupportServices,
  56. geoIPData GeoIPData,
  57. name string,
  58. requestPayload []byte) ([]byte, error) {
  59. // Note: for SSH requests, MAX_API_PARAMS_SIZE is implicitly enforced
  60. // by max SSH reqest packet size.
  61. var params requestJSONObject
  62. err := json.Unmarshal(requestPayload, &params)
  63. if err != nil {
  64. return nil, common.ContextError(
  65. fmt.Errorf("invalid payload for request name: %s: %s", name, err))
  66. }
  67. return dispatchAPIRequestHandler(
  68. support,
  69. protocol.PSIPHON_SSH_API_PROTOCOL,
  70. geoIPData,
  71. name,
  72. params)
  73. }
  74. // dispatchAPIRequestHandler is the common dispatch point for both
  75. // web and SSH API requests.
  76. func dispatchAPIRequestHandler(
  77. support *SupportServices,
  78. apiProtocol string,
  79. geoIPData GeoIPData,
  80. name string,
  81. params requestJSONObject) (response []byte, reterr error) {
  82. // Recover from and log any unexpected panics caused by user input
  83. // handling bugs. User inputs should be properly validated; this
  84. // mechanism is only a last resort to prevent the process from
  85. // terminating in the case of a bug.
  86. defer func() {
  87. if e := recover(); e != nil {
  88. if intentionalPanic, ok := e.(IntentionalPanicError); ok {
  89. panic(intentionalPanic)
  90. } else {
  91. log.LogPanicRecover(e, debug.Stack())
  92. reterr = common.ContextError(errors.New("request handler panic"))
  93. }
  94. }
  95. }()
  96. switch name {
  97. case protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME:
  98. return handshakeAPIRequestHandler(support, apiProtocol, geoIPData, params)
  99. case protocol.PSIPHON_API_CONNECTED_REQUEST_NAME:
  100. return connectedAPIRequestHandler(support, geoIPData, params)
  101. case protocol.PSIPHON_API_STATUS_REQUEST_NAME:
  102. return statusAPIRequestHandler(support, geoIPData, params)
  103. case protocol.PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME:
  104. return clientVerificationAPIRequestHandler(support, geoIPData, params)
  105. }
  106. return nil, common.ContextError(fmt.Errorf("invalid request name: %s", name))
  107. }
  108. // handshakeAPIRequestHandler implements the "handshake" API request.
  109. // Clients make the handshake immediately after establishing a tunnel
  110. // connection; the response tells the client what homepage to open, what
  111. // stats to record, etc.
  112. func handshakeAPIRequestHandler(
  113. support *SupportServices,
  114. apiProtocol string,
  115. geoIPData GeoIPData,
  116. params requestJSONObject) ([]byte, error) {
  117. // Note: ignoring "known_servers" params
  118. err := validateRequestParams(support, params, baseRequestParams)
  119. if err != nil {
  120. return nil, common.ContextError(err)
  121. }
  122. log.LogRawFieldsWithTimestamp(
  123. getRequestLogFields(
  124. support,
  125. "handshake",
  126. geoIPData,
  127. params,
  128. baseRequestParams))
  129. // Note: ignoring param format errors as params have been validated
  130. sessionID, _ := getStringRequestParam(params, "client_session_id")
  131. sponsorID, _ := getStringRequestParam(params, "sponsor_id")
  132. clientVersion, _ := getStringRequestParam(params, "client_version")
  133. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  134. isMobile := isMobileClientPlatform(clientPlatform)
  135. normalizedPlatform := normalizeClientPlatform(clientPlatform)
  136. // Flag the SSH client as having completed its handshake. This
  137. // may reselect traffic rules and starts allowing port forwards.
  138. // TODO: in the case of SSH API requests, the actual sshClient could
  139. // be passed in and used here. The session ID lookup is only strictly
  140. // necessary to support web API requests.
  141. err = support.TunnelServer.SetClientHandshakeState(
  142. sessionID,
  143. handshakeState{
  144. completed: true,
  145. apiProtocol: apiProtocol,
  146. apiParams: copyBaseRequestParams(params),
  147. })
  148. if err != nil {
  149. return nil, common.ContextError(err)
  150. }
  151. // Note: no guarantee that PsinetDatabase won't reload between database calls
  152. db := support.PsinetDatabase
  153. handshakeResponse := protocol.HandshakeResponse{
  154. SSHSessionID: sessionID,
  155. Homepages: db.GetRandomizedHomepages(sponsorID, geoIPData.Country, isMobile),
  156. UpgradeClientVersion: db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
  157. PageViewRegexes: make([]map[string]string, 0),
  158. HttpsRequestRegexes: db.GetHttpsRequestRegexes(sponsorID),
  159. EncodedServerList: db.DiscoverServers(geoIPData.DiscoveryValue),
  160. ClientRegion: geoIPData.Country,
  161. ServerTimestamp: common.GetCurrentTimestamp(),
  162. }
  163. responsePayload, err := json.Marshal(handshakeResponse)
  164. if err != nil {
  165. return nil, common.ContextError(err)
  166. }
  167. return responsePayload, nil
  168. }
  169. var connectedRequestParams = append(
  170. []requestParamSpec{
  171. requestParamSpec{"session_id", isHexDigits, 0},
  172. requestParamSpec{"last_connected", isLastConnected, 0}},
  173. baseRequestParams...)
  174. // connectedAPIRequestHandler implements the "connected" API request.
  175. // Clients make the connected request once a tunnel connection has been
  176. // established and at least once per day. The last_connected input value,
  177. // which should be a connected_timestamp output from a previous connected
  178. // response, is used to calculate unique user stats.
  179. func connectedAPIRequestHandler(
  180. support *SupportServices,
  181. geoIPData GeoIPData,
  182. params requestJSONObject) ([]byte, error) {
  183. err := validateRequestParams(support, params, connectedRequestParams)
  184. if err != nil {
  185. return nil, common.ContextError(err)
  186. }
  187. log.LogRawFieldsWithTimestamp(
  188. getRequestLogFields(
  189. support,
  190. "connected",
  191. geoIPData,
  192. params,
  193. connectedRequestParams))
  194. connectedResponse := protocol.ConnectedResponse{
  195. ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
  196. }
  197. responsePayload, err := json.Marshal(connectedResponse)
  198. if err != nil {
  199. return nil, common.ContextError(err)
  200. }
  201. return responsePayload, nil
  202. }
  203. var statusRequestParams = append(
  204. []requestParamSpec{
  205. requestParamSpec{"session_id", isHexDigits, 0},
  206. requestParamSpec{"connected", isBooleanFlag, 0}},
  207. baseRequestParams...)
  208. // statusAPIRequestHandler implements the "status" API request.
  209. // Clients make periodic status requests which deliver client-side
  210. // recorded data transfer and tunnel duration stats.
  211. // Note from psi_web implementation: no input validation on domains;
  212. // any string is accepted (regex transform may result in arbitrary
  213. // string). Stats processor must handle this input with care.
  214. func statusAPIRequestHandler(
  215. support *SupportServices,
  216. geoIPData GeoIPData,
  217. params requestJSONObject) ([]byte, error) {
  218. err := validateRequestParams(support, params, statusRequestParams)
  219. if err != nil {
  220. return nil, common.ContextError(err)
  221. }
  222. statusData, err := getJSONObjectRequestParam(params, "statusData")
  223. if err != nil {
  224. return nil, common.ContextError(err)
  225. }
  226. // Logs are queued until the input is fully validated. Otherwise, stats
  227. // could be double counted if the client has a bug in its request
  228. // formatting: partial stats would be logged (counted), the request would
  229. // fail, and clients would then resend all the same stats again.
  230. logQueue := make([]LogFields, 0)
  231. // Overall bytes transferred stats
  232. bytesTransferred, err := getInt64RequestParam(statusData, "bytes_transferred")
  233. if err != nil {
  234. return nil, common.ContextError(err)
  235. }
  236. bytesTransferredFields := getRequestLogFields(
  237. support, "bytes_transferred", geoIPData, params, statusRequestParams)
  238. bytesTransferredFields["bytes"] = bytesTransferred
  239. logQueue = append(logQueue, bytesTransferredFields)
  240. // Domain bytes transferred stats
  241. // Older clients may not submit this data
  242. if statusData["host_bytes"] != nil {
  243. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  244. if err != nil {
  245. return nil, common.ContextError(err)
  246. }
  247. for domain, bytes := range hostBytes {
  248. domainBytesFields := getRequestLogFields(
  249. support, "domain_bytes", geoIPData, params, statusRequestParams)
  250. domainBytesFields["domain"] = domain
  251. domainBytesFields["bytes"] = bytes
  252. logQueue = append(logQueue, domainBytesFields)
  253. }
  254. }
  255. // Tunnel duration and bytes transferred stats
  256. // Older clients may not submit this data
  257. if statusData["tunnel_stats"] != nil {
  258. tunnelStats, err := getJSONObjectArrayRequestParam(statusData, "tunnel_stats")
  259. if err != nil {
  260. return nil, common.ContextError(err)
  261. }
  262. for _, tunnelStat := range tunnelStats {
  263. sessionFields := getRequestLogFields(
  264. support, "session", geoIPData, params, statusRequestParams)
  265. sessionID, err := getStringRequestParam(tunnelStat, "session_id")
  266. if err != nil {
  267. return nil, common.ContextError(err)
  268. }
  269. sessionFields["session_id"] = sessionID
  270. tunnelNumber, err := getInt64RequestParam(tunnelStat, "tunnel_number")
  271. if err != nil {
  272. return nil, common.ContextError(err)
  273. }
  274. sessionFields["tunnel_number"] = tunnelNumber
  275. tunnelServerIPAddress, err := getStringRequestParam(tunnelStat, "tunnel_server_ip_address")
  276. if err != nil {
  277. return nil, common.ContextError(err)
  278. }
  279. sessionFields["tunnel_server_ip_address"] = tunnelServerIPAddress
  280. // Note: older clients won't send establishment_duration
  281. if _, ok := tunnelStat["establishment_duration"]; ok {
  282. strEstablishmentDuration, err := getStringRequestParam(tunnelStat, "establishment_duration")
  283. if err != nil {
  284. return nil, common.ContextError(err)
  285. }
  286. establishmentDuration, err := strconv.ParseInt(strEstablishmentDuration, 10, 64)
  287. if err != nil {
  288. return nil, common.ContextError(err)
  289. }
  290. // Client reports establishment_duration in nanoseconds; divide to get to milliseconds
  291. sessionFields["establishment_duration"] = establishmentDuration / 1000000
  292. }
  293. serverHandshakeTimestamp, err := getStringRequestParam(tunnelStat, "server_handshake_timestamp")
  294. if err != nil {
  295. return nil, common.ContextError(err)
  296. }
  297. sessionFields["server_handshake_timestamp"] = serverHandshakeTimestamp
  298. strDuration, err := getStringRequestParam(tunnelStat, "duration")
  299. if err != nil {
  300. return nil, common.ContextError(err)
  301. }
  302. duration, err := strconv.ParseInt(strDuration, 10, 64)
  303. if err != nil {
  304. return nil, common.ContextError(err)
  305. }
  306. // Client reports duration in nanoseconds; divide to get to milliseconds
  307. sessionFields["duration"] = duration / 1000000
  308. totalBytesSent, err := getInt64RequestParam(tunnelStat, "total_bytes_sent")
  309. if err != nil {
  310. return nil, common.ContextError(err)
  311. }
  312. sessionFields["total_bytes_sent"] = totalBytesSent
  313. totalBytesReceived, err := getInt64RequestParam(tunnelStat, "total_bytes_received")
  314. if err != nil {
  315. return nil, common.ContextError(err)
  316. }
  317. sessionFields["total_bytes_received"] = totalBytesReceived
  318. logQueue = append(logQueue, sessionFields)
  319. }
  320. }
  321. // Remote server list download stats
  322. // Older clients may not submit this data
  323. if statusData["remote_server_list_stats"] != nil {
  324. remoteServerListStats, err := getJSONObjectArrayRequestParam(statusData, "remote_server_list_stats")
  325. if err != nil {
  326. return nil, common.ContextError(err)
  327. }
  328. for _, remoteServerListStat := range remoteServerListStats {
  329. remoteServerListFields := getRequestLogFields(
  330. support, "remote_server_list", geoIPData, params, statusRequestParams)
  331. clientDownloadTimestamp, err := getStringRequestParam(remoteServerListStat, "client_download_timestamp")
  332. if err != nil {
  333. return nil, common.ContextError(err)
  334. }
  335. remoteServerListFields["client_download_timestamp"] = clientDownloadTimestamp
  336. url, err := getStringRequestParam(remoteServerListStat, "url")
  337. if err != nil {
  338. return nil, common.ContextError(err)
  339. }
  340. remoteServerListFields["url"] = url
  341. etag, err := getStringRequestParam(remoteServerListStat, "etag")
  342. if err != nil {
  343. return nil, common.ContextError(err)
  344. }
  345. remoteServerListFields["etag"] = etag
  346. logQueue = append(logQueue, remoteServerListFields)
  347. }
  348. }
  349. for _, logItem := range logQueue {
  350. log.LogRawFieldsWithTimestamp(logItem)
  351. }
  352. return make([]byte, 0), nil
  353. }
  354. // clientVerificationAPIRequestHandler implements the
  355. // "client verification" API request. Clients make the client
  356. // verification request once per tunnel connection. The payload
  357. // attests that client is a legitimate Psiphon client.
  358. func clientVerificationAPIRequestHandler(
  359. support *SupportServices,
  360. geoIPData GeoIPData,
  361. params requestJSONObject) ([]byte, error) {
  362. err := validateRequestParams(support, params, baseRequestParams)
  363. if err != nil {
  364. return nil, common.ContextError(err)
  365. }
  366. // Ignoring error as params are validated
  367. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  368. // Client sends empty payload to receive TTL
  369. // NOTE: these events are not currently logged
  370. if params["verificationData"] == nil {
  371. if CLIENT_VERIFICATION_REQUIRED {
  372. var clientVerificationResponse struct {
  373. ClientVerificationTTLSeconds int `json:"client_verification_ttl_seconds"`
  374. }
  375. clientVerificationResponse.ClientVerificationTTLSeconds = CLIENT_VERIFICATION_TTL_SECONDS
  376. responsePayload, err := json.Marshal(clientVerificationResponse)
  377. if err != nil {
  378. return nil, common.ContextError(err)
  379. }
  380. return responsePayload, nil
  381. } else {
  382. return make([]byte, 0), nil
  383. }
  384. } else {
  385. verificationData, err := getJSONObjectRequestParam(params, "verificationData")
  386. if err != nil {
  387. return nil, common.ContextError(err)
  388. }
  389. logFields := getRequestLogFields(
  390. support,
  391. "client_verification",
  392. geoIPData,
  393. params,
  394. baseRequestParams)
  395. var verified bool
  396. var safetyNetCheckLogs LogFields
  397. switch normalizeClientPlatform(clientPlatform) {
  398. case CLIENT_PLATFORM_ANDROID:
  399. verified, safetyNetCheckLogs = verifySafetyNetPayload(verificationData)
  400. logFields["safetynet_check"] = safetyNetCheckLogs
  401. }
  402. log.LogRawFieldsWithTimestamp(logFields)
  403. if verified {
  404. // TODO: change throttling treatment
  405. }
  406. return make([]byte, 0), nil
  407. }
  408. }
  409. type requestParamSpec struct {
  410. name string
  411. validator func(*SupportServices, string) bool
  412. flags uint32
  413. }
  414. const (
  415. requestParamOptional = 1
  416. requestParamNotLogged = 2
  417. requestParamArray = 4
  418. )
  419. // baseRequestParams is the list of required and optional
  420. // request parameters; derived from COMMON_INPUTS and
  421. // OPTIONAL_COMMON_INPUTS in psi_web.
  422. // Each param is expected to be a string, unless requestParamArray
  423. // is specified, in which case an array of string is expected.
  424. var baseRequestParams = []requestParamSpec{
  425. requestParamSpec{"server_secret", isServerSecret, requestParamNotLogged},
  426. requestParamSpec{"client_session_id", isHexDigits, requestParamOptional | requestParamNotLogged},
  427. requestParamSpec{"propagation_channel_id", isHexDigits, 0},
  428. requestParamSpec{"sponsor_id", isHexDigits, 0},
  429. requestParamSpec{"client_version", isIntString, 0},
  430. requestParamSpec{"client_platform", isClientPlatform, 0},
  431. requestParamSpec{"client_build_rev", isHexDigits, requestParamOptional},
  432. requestParamSpec{"relay_protocol", isRelayProtocol, 0},
  433. requestParamSpec{"tunnel_whole_device", isBooleanFlag, requestParamOptional},
  434. requestParamSpec{"device_region", isRegionCode, requestParamOptional},
  435. requestParamSpec{"ssh_client_version", isAnyString, requestParamOptional},
  436. requestParamSpec{"upstream_proxy_type", isUpstreamProxyType, requestParamOptional},
  437. requestParamSpec{"upstream_proxy_custom_header_names", isAnyString, requestParamOptional | requestParamArray},
  438. requestParamSpec{"meek_dial_address", isDialAddress, requestParamOptional},
  439. requestParamSpec{"meek_resolved_ip_address", isIPAddress, requestParamOptional},
  440. requestParamSpec{"meek_sni_server_name", isDomain, requestParamOptional},
  441. requestParamSpec{"meek_host_header", isHostHeader, requestParamOptional},
  442. requestParamSpec{"meek_transformed_host_name", isBooleanFlag, requestParamOptional},
  443. requestParamSpec{"user_agent", isAnyString, requestParamOptional},
  444. requestParamSpec{"tls_profile", isAnyString, requestParamOptional},
  445. requestParamSpec{"server_entry_region", isRegionCode, requestParamOptional},
  446. requestParamSpec{"server_entry_source", isServerEntrySource, requestParamOptional},
  447. requestParamSpec{"server_entry_timestamp", isISO8601Date, requestParamOptional},
  448. }
  449. func validateRequestParams(
  450. support *SupportServices,
  451. params requestJSONObject,
  452. expectedParams []requestParamSpec) error {
  453. for _, expectedParam := range expectedParams {
  454. value := params[expectedParam.name]
  455. if value == nil {
  456. if expectedParam.flags&requestParamOptional != 0 {
  457. continue
  458. }
  459. return common.ContextError(
  460. fmt.Errorf("missing param: %s", expectedParam.name))
  461. }
  462. var err error
  463. if expectedParam.flags&requestParamArray != 0 {
  464. err = validateStringArrayRequestParam(support, expectedParam, value)
  465. } else {
  466. err = validateStringRequestParam(support, expectedParam, value)
  467. }
  468. if err != nil {
  469. return common.ContextError(err)
  470. }
  471. }
  472. return nil
  473. }
  474. // copyBaseRequestParams makes a copy of the params which
  475. // includes only the baseRequestParams.
  476. func copyBaseRequestParams(params requestJSONObject) requestJSONObject {
  477. // Note: not a deep copy; assumes baseRequestParams values
  478. // are all scalar types (int, string, etc.)
  479. paramsCopy := make(requestJSONObject)
  480. for _, baseParam := range baseRequestParams {
  481. value := params[baseParam.name]
  482. if value == nil {
  483. continue
  484. }
  485. paramsCopy[baseParam.name] = value
  486. }
  487. return paramsCopy
  488. }
  489. func validateStringRequestParam(
  490. support *SupportServices,
  491. expectedParam requestParamSpec,
  492. value interface{}) error {
  493. strValue, ok := value.(string)
  494. if !ok {
  495. return common.ContextError(
  496. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  497. }
  498. if !expectedParam.validator(support, strValue) {
  499. return common.ContextError(
  500. fmt.Errorf("invalid param: %s", expectedParam.name))
  501. }
  502. return nil
  503. }
  504. func validateStringArrayRequestParam(
  505. support *SupportServices,
  506. expectedParam requestParamSpec,
  507. value interface{}) error {
  508. arrayValue, ok := value.([]interface{})
  509. if !ok {
  510. return common.ContextError(
  511. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  512. }
  513. for _, value := range arrayValue {
  514. err := validateStringRequestParam(support, expectedParam, value)
  515. if err != nil {
  516. return common.ContextError(err)
  517. }
  518. }
  519. return nil
  520. }
  521. // getRequestLogFields makes LogFields to log the API event following
  522. // the legacy psi_web and current ELK naming conventions.
  523. func getRequestLogFields(
  524. support *SupportServices,
  525. eventName string,
  526. geoIPData GeoIPData,
  527. params requestJSONObject,
  528. expectedParams []requestParamSpec) LogFields {
  529. logFields := make(LogFields)
  530. logFields["event_name"] = eventName
  531. // In psi_web, the space replacement was done to accommodate space
  532. // delimited logging, which is no longer required; we retain the
  533. // transformation so that stats aggregation isn't impacted.
  534. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  535. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  536. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  537. if params == nil {
  538. return logFields
  539. }
  540. for _, expectedParam := range expectedParams {
  541. if expectedParam.flags&requestParamNotLogged != 0 {
  542. continue
  543. }
  544. value := params[expectedParam.name]
  545. if value == nil {
  546. // Special case: older clients don't send this value,
  547. // so log a default.
  548. if expectedParam.name == "tunnel_whole_device" {
  549. value = "0"
  550. } else {
  551. // Skip omitted, optional params
  552. continue
  553. }
  554. }
  555. switch v := value.(type) {
  556. case string:
  557. strValue := v
  558. // Special cases:
  559. // - Number fields are encoded as integer types.
  560. // - For ELK performance we record these domain-or-IP
  561. // fields as one of two different values based on type;
  562. // we also omit port from host:port fields for now.
  563. switch expectedParam.name {
  564. case "client_version":
  565. intValue, _ := strconv.Atoi(strValue)
  566. logFields[expectedParam.name] = intValue
  567. case "meek_dial_address":
  568. host, _, _ := net.SplitHostPort(strValue)
  569. if isIPAddress(support, host) {
  570. logFields["meek_dial_ip_address"] = host
  571. } else {
  572. logFields["meek_dial_domain"] = host
  573. }
  574. case "meek_host_header":
  575. host, _, _ := net.SplitHostPort(strValue)
  576. logFields[expectedParam.name] = host
  577. case "upstream_proxy_type":
  578. // Submitted value could be e.g., "SOCKS5" or "socks5"; log lowercase
  579. logFields[expectedParam.name] = strings.ToLower(strValue)
  580. default:
  581. logFields[expectedParam.name] = strValue
  582. }
  583. case []interface{}:
  584. // Note: actually validated as an array of strings
  585. logFields[expectedParam.name] = v
  586. default:
  587. // This type assertion should be checked already in
  588. // validateRequestParams, so failure is unexpected.
  589. continue
  590. }
  591. }
  592. return logFields
  593. }
  594. func getStringRequestParam(params requestJSONObject, name string) (string, error) {
  595. if params[name] == nil {
  596. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  597. }
  598. value, ok := params[name].(string)
  599. if !ok {
  600. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  601. }
  602. return value, nil
  603. }
  604. func getInt64RequestParam(params requestJSONObject, name string) (int64, error) {
  605. if params[name] == nil {
  606. return 0, common.ContextError(fmt.Errorf("missing param: %s", name))
  607. }
  608. value, ok := params[name].(float64)
  609. if !ok {
  610. return 0, common.ContextError(fmt.Errorf("invalid param: %s", name))
  611. }
  612. return int64(value), nil
  613. }
  614. func getJSONObjectRequestParam(params requestJSONObject, name string) (requestJSONObject, error) {
  615. if params[name] == nil {
  616. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  617. }
  618. // Note: generic unmarshal of JSON produces map[string]interface{}, not requestJSONObject
  619. value, ok := params[name].(map[string]interface{})
  620. if !ok {
  621. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  622. }
  623. return requestJSONObject(value), nil
  624. }
  625. func getJSONObjectArrayRequestParam(params requestJSONObject, name string) ([]requestJSONObject, error) {
  626. if params[name] == nil {
  627. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  628. }
  629. value, ok := params[name].([]interface{})
  630. if !ok {
  631. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  632. }
  633. result := make([]requestJSONObject, len(value))
  634. for i, item := range value {
  635. // Note: generic unmarshal of JSON produces map[string]interface{}, not requestJSONObject
  636. resultItem, ok := item.(map[string]interface{})
  637. if !ok {
  638. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  639. }
  640. result[i] = requestJSONObject(resultItem)
  641. }
  642. return result, nil
  643. }
  644. func getMapStringInt64RequestParam(params requestJSONObject, name string) (map[string]int64, error) {
  645. if params[name] == nil {
  646. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  647. }
  648. // TODO: can't use requestJSONObject type?
  649. value, ok := params[name].(map[string]interface{})
  650. if !ok {
  651. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  652. }
  653. result := make(map[string]int64)
  654. for k, v := range value {
  655. numValue, ok := v.(float64)
  656. if !ok {
  657. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  658. }
  659. result[k] = int64(numValue)
  660. }
  661. return result, nil
  662. }
  663. // Normalize reported client platform. Android clients, for example, report
  664. // OS version, rooted status, and Google Play build status in the clientPlatform
  665. // string along with "Android".
  666. func normalizeClientPlatform(clientPlatform string) string {
  667. if strings.Contains(strings.ToLower(clientPlatform), strings.ToLower(CLIENT_PLATFORM_ANDROID)) {
  668. return CLIENT_PLATFORM_ANDROID
  669. } else if strings.HasPrefix(clientPlatform, CLIENT_PLATFORM_IOS) {
  670. return CLIENT_PLATFORM_IOS
  671. }
  672. return CLIENT_PLATFORM_WINDOWS
  673. }
  674. func isAnyString(support *SupportServices, value string) bool {
  675. return true
  676. }
  677. func isMobileClientPlatform(clientPlatform string) bool {
  678. normalizedClientPlatform := normalizeClientPlatform(clientPlatform)
  679. return normalizedClientPlatform == CLIENT_PLATFORM_ANDROID ||
  680. normalizedClientPlatform == CLIENT_PLATFORM_IOS
  681. }
  682. // Input validators follow the legacy validations rules in psi_web.
  683. func isServerSecret(support *SupportServices, value string) bool {
  684. return subtle.ConstantTimeCompare(
  685. []byte(value),
  686. []byte(support.Config.WebServerSecret)) == 1
  687. }
  688. func isHexDigits(_ *SupportServices, value string) bool {
  689. return -1 == strings.IndexFunc(value, func(c rune) bool {
  690. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  691. })
  692. }
  693. func isDigits(_ *SupportServices, value string) bool {
  694. return -1 == strings.IndexFunc(value, func(c rune) bool {
  695. return c < '0' || c > '9'
  696. })
  697. }
  698. func isIntString(_ *SupportServices, value string) bool {
  699. _, err := strconv.Atoi(value)
  700. return err == nil
  701. }
  702. func isClientPlatform(_ *SupportServices, value string) bool {
  703. return -1 == strings.IndexFunc(value, func(c rune) bool {
  704. // Note: stricter than psi_web's Python string.whitespace
  705. return unicode.Is(unicode.White_Space, c)
  706. })
  707. }
  708. func isRelayProtocol(_ *SupportServices, value string) bool {
  709. return common.Contains(protocol.SupportedTunnelProtocols, value)
  710. }
  711. func isBooleanFlag(_ *SupportServices, value string) bool {
  712. return value == "0" || value == "1"
  713. }
  714. func isUpstreamProxyType(_ *SupportServices, value string) bool {
  715. value = strings.ToLower(value)
  716. return value == "http" || value == "socks5" || value == "socks4a"
  717. }
  718. func isRegionCode(_ *SupportServices, value string) bool {
  719. if len(value) != 2 {
  720. return false
  721. }
  722. return -1 == strings.IndexFunc(value, func(c rune) bool {
  723. return c < 'A' || c > 'Z'
  724. })
  725. }
  726. func isDialAddress(support *SupportServices, value string) bool {
  727. // "<host>:<port>", where <host> is a domain or IP address
  728. parts := strings.Split(value, ":")
  729. if len(parts) != 2 {
  730. return false
  731. }
  732. if !isIPAddress(support, parts[0]) && !isDomain(support, parts[0]) {
  733. return false
  734. }
  735. if !isDigits(support, parts[1]) {
  736. return false
  737. }
  738. port, err := strconv.Atoi(parts[1])
  739. if err != nil {
  740. return false
  741. }
  742. return port > 0 && port < 65536
  743. }
  744. func isIPAddress(_ *SupportServices, value string) bool {
  745. return net.ParseIP(value) != nil
  746. }
  747. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  748. func isDomain(_ *SupportServices, value string) bool {
  749. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  750. //
  751. // "ensures that each segment
  752. // * contains at least one character and a maximum of 63 characters
  753. // * consists only of allowed characters
  754. // * doesn't begin or end with a hyphen"
  755. //
  756. if len(value) > 255 {
  757. return false
  758. }
  759. value = strings.TrimSuffix(value, ".")
  760. for _, part := range strings.Split(value, ".") {
  761. // Note: regexp doesn't support the following Perl expression which
  762. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  763. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  764. return false
  765. }
  766. if !isDomainRegex.Match([]byte(part)) {
  767. return false
  768. }
  769. }
  770. return true
  771. }
  772. func isHostHeader(support *SupportServices, value string) bool {
  773. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  774. if strings.Contains(value, ":") {
  775. return isDialAddress(support, value)
  776. }
  777. return isIPAddress(support, value) || isDomain(support, value)
  778. }
  779. func isServerEntrySource(_ *SupportServices, value string) bool {
  780. return common.Contains(protocol.SupportedServerEntrySources, value)
  781. }
  782. var isISO8601DateRegex = regexp.MustCompile(
  783. "(?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})))")
  784. func isISO8601Date(_ *SupportServices, value string) bool {
  785. return isISO8601DateRegex.Match([]byte(value))
  786. }
  787. func isLastConnected(support *SupportServices, value string) bool {
  788. return value == "None" || value == "Unknown" || isISO8601Date(support, value)
  789. }