api.go 26 KB

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