api.go 27 KB

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