api.go 25 KB

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