api.go 27 KB

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