api.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "crypto/subtle"
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "net"
  26. "regexp"
  27. "strconv"
  28. "strings"
  29. "unicode"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  31. )
  32. const MAX_API_PARAMS_SIZE = 256 * 1024 // 256KB
  33. type requestJSONObject map[string]interface{}
  34. // sshAPIRequestHandler routes Psiphon API requests transported as
  35. // JSON objects via the SSH request mechanism.
  36. //
  37. // The API request handlers, handshakeAPIRequestHandler, etc., are
  38. // reused by webServer which offers the Psiphon API via web transport.
  39. //
  40. // The API request parameters and event log values follow the legacy
  41. // psi_web protocol and naming conventions. The API is compatible all
  42. // tunnel-core clients but are not backwards compatible with older
  43. // clients.
  44. //
  45. func sshAPIRequestHandler(
  46. config *Config,
  47. psinetDatabase *PsinetDatabase,
  48. geoIPData GeoIPData,
  49. name string,
  50. requestPayload []byte) ([]byte, error) {
  51. // Note: for SSH requests, MAX_API_PARAMS_SIZE is implicitly enforced
  52. // by max SSH reqest packet size.
  53. var params requestJSONObject
  54. err := json.Unmarshal(requestPayload, &params)
  55. if err != nil {
  56. return nil, psiphon.ContextError(err)
  57. }
  58. switch name {
  59. case psiphon.SERVER_API_HANDSHAKE_REQUEST_NAME:
  60. return handshakeAPIRequestHandler(config, psinetDatabase, geoIPData, params)
  61. case psiphon.SERVER_API_CONNECTED_REQUEST_NAME:
  62. return connectedAPIRequestHandler(config, geoIPData, params)
  63. case psiphon.SERVER_API_STATUS_REQUEST_NAME:
  64. return statusAPIRequestHandler(config, geoIPData, params)
  65. case psiphon.SERVER_API_CLIENT_VERIFICATION_REQUEST_NAME:
  66. return clientVerificationAPIRequestHandler(config, geoIPData, params)
  67. }
  68. return nil, psiphon.ContextError(fmt.Errorf("invalid request name: %s", name))
  69. }
  70. // handshakeAPIRequestHandler implements the "handshake" API request.
  71. // Clients make the handshake immediately after establishing a tunnel
  72. // connection; the response tells the client what homepage to open, what
  73. // stats to record, etc.
  74. func handshakeAPIRequestHandler(
  75. config *Config,
  76. psinetDatabase *PsinetDatabase,
  77. geoIPData GeoIPData,
  78. params requestJSONObject) ([]byte, error) {
  79. // Note: ignoring "known_servers" params
  80. err := validateRequestParams(config, params, baseRequestParams)
  81. if err != nil {
  82. // TODO: fail2ban?
  83. return nil, psiphon.ContextError(errors.New("invalid params"))
  84. }
  85. log.WithContextFields(
  86. getRequestLogFields(
  87. config,
  88. "handshake",
  89. geoIPData,
  90. params,
  91. baseRequestParams)).Info("API event")
  92. // TODO: share struct definition with psiphon/serverApi.go?
  93. var handshakeResponse struct {
  94. Homepages []string `json:"homepages"`
  95. UpgradeClientVersion string `json:"upgrade_client_version"`
  96. PageViewRegexes []map[string]string `json:"page_view_regexes"`
  97. HttpsRequestRegexes []map[string]string `json:"https_request_regexes"`
  98. EncodedServerList []string `json:"encoded_server_list"`
  99. ClientRegion string `json:"client_region"`
  100. ServerTimestamp string `json:"server_timestamp"`
  101. }
  102. // Ignoring errors as params are validated
  103. sponsorID, _ := getStringRequestParam(params, "sponsor_id")
  104. propagationChannelID, _ := getStringRequestParam(params, "propagation_channel_id")
  105. clientVersion, _ := getStringRequestParam(params, "client_version")
  106. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  107. clientRegion := geoIPData.Country
  108. handshakeResponse.Homepages = psinetDatabase.GetHomepages(
  109. sponsorID, clientRegion, clientPlatform)
  110. handshakeResponse.UpgradeClientVersion = psinetDatabase.GetUpgradeClientVersion(
  111. clientVersion, clientPlatform)
  112. handshakeResponse.HttpsRequestRegexes = psinetDatabase.GetHttpsRequestRegexes(
  113. sponsorID)
  114. handshakeResponse.EncodedServerList = psinetDatabase.DiscoverServers(
  115. propagationChannelID, geoIPData.DiscoveryValue)
  116. handshakeResponse.ClientRegion = clientRegion
  117. handshakeResponse.ServerTimestamp = psiphon.GetCurrentTimestamp()
  118. responsePayload, err := json.Marshal(handshakeResponse)
  119. if err != nil {
  120. return nil, psiphon.ContextError(err)
  121. }
  122. return responsePayload, nil
  123. }
  124. var connectedRequestParams = append(
  125. []requestParamSpec{requestParamSpec{"last_connected", isLastConnected, 0}},
  126. baseRequestParams...)
  127. // connectedAPIRequestHandler implements the "connected" API request.
  128. // Clients make the connected request once a tunnel connection has been
  129. // established and at least once per day. The last_connected input value,
  130. // which should be a connected_timestamp output from a previous connected
  131. // response, is used to calculate unique user stats.
  132. func connectedAPIRequestHandler(
  133. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  134. err := validateRequestParams(config, params, connectedRequestParams)
  135. if err != nil {
  136. // TODO: fail2ban?
  137. return nil, psiphon.ContextError(errors.New("invalid params"))
  138. }
  139. log.WithContextFields(
  140. getRequestLogFields(
  141. config,
  142. "connected",
  143. geoIPData,
  144. params,
  145. connectedRequestParams)).Info("API event")
  146. var connectedResponse struct {
  147. ConnectedTimestamp string `json:"connected_timestamp"`
  148. }
  149. connectedResponse.ConnectedTimestamp =
  150. psiphon.TruncateTimestampToHour(psiphon.GetCurrentTimestamp())
  151. responsePayload, err := json.Marshal(connectedResponse)
  152. if err != nil {
  153. return nil, psiphon.ContextError(err)
  154. }
  155. return responsePayload, nil
  156. }
  157. var statusRequestParams = append(
  158. []requestParamSpec{requestParamSpec{"connected", isBooleanFlag, 0}},
  159. baseRequestParams...)
  160. // statusAPIRequestHandler implements the "status" API request.
  161. // Clients make periodic status requests which deliver client-side
  162. // recorded data transfer and tunnel duration stats.
  163. func statusAPIRequestHandler(
  164. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  165. err := validateRequestParams(config, params, statusRequestParams)
  166. if err != nil {
  167. // TODO: fail2ban?
  168. return nil, psiphon.ContextError(errors.New("invalid params"))
  169. }
  170. statusData, err := getJSONObjectRequestParam(params, "statusData")
  171. if err != nil {
  172. return nil, psiphon.ContextError(err)
  173. }
  174. // Overall bytes transferred stats
  175. bytesTransferred, err := getInt64RequestParam(statusData, "bytes_transferred")
  176. if err != nil {
  177. return nil, psiphon.ContextError(err)
  178. }
  179. bytesTransferredFields := getRequestLogFields(
  180. config, "bytes_transferred", geoIPData, params, statusRequestParams)
  181. bytesTransferredFields["bytes"] = bytesTransferred
  182. log.WithContextFields(bytesTransferredFields).Info("API event")
  183. // Domain bytes transferred stats
  184. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  185. if err != nil {
  186. return nil, psiphon.ContextError(err)
  187. }
  188. domainBytesFields := getRequestLogFields(
  189. config, "domain_bytes", geoIPData, params, statusRequestParams)
  190. for domain, bytes := range hostBytes {
  191. domainBytesFields["domain"] = domain
  192. domainBytesFields["bytes"] = bytes
  193. log.WithContextFields(domainBytesFields).Info("API event")
  194. }
  195. // Tunnel duration and bytes transferred stats
  196. tunnelStats, err := getJSONObjectArrayRequestParam(statusData, "tunnel_stats")
  197. if err != nil {
  198. return nil, psiphon.ContextError(err)
  199. }
  200. sessionFields := getRequestLogFields(
  201. config, "session", geoIPData, params, statusRequestParams)
  202. for _, tunnelStat := range tunnelStats {
  203. sessionID, err := getStringRequestParam(tunnelStat, "session_id")
  204. if err != nil {
  205. return nil, psiphon.ContextError(err)
  206. }
  207. sessionFields["session_id"] = sessionID
  208. tunnelNumber, err := getInt64RequestParam(tunnelStat, "tunnel_number")
  209. if err != nil {
  210. return nil, psiphon.ContextError(err)
  211. }
  212. sessionFields["tunnel_number"] = tunnelNumber
  213. tunnelServerIPAddress, err := getStringRequestParam(tunnelStat, "tunnel_server_ip_address")
  214. if err != nil {
  215. return nil, psiphon.ContextError(err)
  216. }
  217. sessionFields["tunnel_server_ip_address"] = tunnelServerIPAddress
  218. serverHandshakeTimestamp, err := getStringRequestParam(tunnelStat, "server_handshake_timestamp")
  219. if err != nil {
  220. return nil, psiphon.ContextError(err)
  221. }
  222. sessionFields["server_handshake_timestamp"] = serverHandshakeTimestamp
  223. duration, err := getInt64RequestParam(tunnelStat, "duration")
  224. if err != nil {
  225. return nil, psiphon.ContextError(err)
  226. }
  227. // Client reports durations in nanoseconds; divide to get to milliseconds
  228. sessionFields["duration"] = duration / 1000000
  229. totalBytesSent, err := getInt64RequestParam(tunnelStat, "total_bytes_sent")
  230. if err != nil {
  231. return nil, psiphon.ContextError(err)
  232. }
  233. sessionFields["total_bytes_sent"] = totalBytesSent
  234. totalBytesReceived, err := getInt64RequestParam(tunnelStat, "total_bytes_received")
  235. if err != nil {
  236. return nil, psiphon.ContextError(err)
  237. }
  238. sessionFields["total_bytes_received"] = totalBytesReceived
  239. log.WithContextFields(sessionFields).Info("API event")
  240. }
  241. return make([]byte, 0), nil
  242. }
  243. // clientVerificationAPIRequestHandler implements the
  244. // "client verification" API request. Clients make the client
  245. // verification request once per tunnel connection. The payload
  246. // attests that client is a legitimate Psiphon client.
  247. func clientVerificationAPIRequestHandler(
  248. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  249. err := validateRequestParams(config, params, baseRequestParams)
  250. if err != nil {
  251. // TODO: fail2ban?
  252. return nil, psiphon.ContextError(errors.New("invalid params"))
  253. }
  254. // TODO: implement
  255. return make([]byte, 0), nil
  256. }
  257. type requestParamSpec struct {
  258. name string
  259. validator func(*Config, string) bool
  260. flags int32
  261. }
  262. const (
  263. requestParamOptional = 1
  264. requestParamNotLogged = 2
  265. )
  266. // baseRequestParams is the list of required and optional
  267. // request parameters; derived from COMMON_INPUTS and
  268. // OPTIONAL_COMMON_INPUTS in psi_web.
  269. var baseRequestParams = []requestParamSpec{
  270. requestParamSpec{"server_secret", isServerSecret, requestParamNotLogged},
  271. requestParamSpec{"client_session_id", isHexDigits, 0},
  272. requestParamSpec{"propagation_channel_id", isHexDigits, 0},
  273. requestParamSpec{"sponsor_id", isHexDigits, 0},
  274. requestParamSpec{"client_version", isDigits, 0},
  275. requestParamSpec{"client_platform", isClientPlatform, 0},
  276. requestParamSpec{"relay_protocol", isRelayProtocol, 0},
  277. requestParamSpec{"tunnel_whole_device", isBooleanFlag, 0},
  278. requestParamSpec{"device_region", isRegionCode, requestParamOptional},
  279. requestParamSpec{"meek_dial_address", isDialAddress, requestParamOptional},
  280. requestParamSpec{"meek_resolved_ip_address", isIPAddress, requestParamOptional},
  281. requestParamSpec{"meek_sni_server_name", isDomain, requestParamOptional},
  282. requestParamSpec{"meek_host_header", isHostHeader, requestParamOptional},
  283. requestParamSpec{"meek_transformed_host_name", isBooleanFlag, requestParamOptional},
  284. requestParamSpec{"server_entry_region", isRegionCode, requestParamOptional},
  285. requestParamSpec{"server_entry_source", isServerEntrySource, requestParamOptional},
  286. requestParamSpec{"server_entry_timestamp", isISO8601Date, requestParamOptional},
  287. }
  288. func validateRequestParams(
  289. config *Config,
  290. params requestJSONObject,
  291. expectedParams []requestParamSpec) error {
  292. for _, expectedParam := range expectedParams {
  293. value := params[expectedParam.name]
  294. if value == nil {
  295. if expectedParam.flags&requestParamOptional != 0 {
  296. continue
  297. }
  298. return psiphon.ContextError(
  299. fmt.Errorf("missing required param: %s", expectedParam.name))
  300. }
  301. strValue, ok := value.(string)
  302. if !ok {
  303. return psiphon.ContextError(
  304. fmt.Errorf("unexpected param type: %s", expectedParam.name))
  305. }
  306. if !expectedParam.validator(config, strValue) {
  307. return psiphon.ContextError(
  308. fmt.Errorf("invalid param: %s", expectedParam.name))
  309. }
  310. }
  311. return nil
  312. }
  313. // getRequestLogFields makes LogFields to log the API event following
  314. // the legacy psi_web and current ELK naming conventions.
  315. func getRequestLogFields(
  316. config *Config,
  317. eventName string,
  318. geoIPData GeoIPData,
  319. params requestJSONObject,
  320. expectedParams []requestParamSpec) LogFields {
  321. logFields := make(LogFields)
  322. logFields["event_name"] = eventName
  323. logFields["host_id"] = config.HostID
  324. // In psi_web, the space replacement was done to accommodate space
  325. // delimited logging, which is no longer required; we retain the
  326. // transformation so that stats aggregation isn't impacted.
  327. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  328. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  329. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  330. for _, expectedParam := range expectedParams {
  331. if expectedParam.flags&requestParamNotLogged != 0 {
  332. continue
  333. }
  334. value := params[expectedParam.name]
  335. if value == nil {
  336. // Skip optional params
  337. continue
  338. }
  339. strValue, ok := value.(string)
  340. if !ok {
  341. // This type assertion should be checked already in
  342. // validateRequestParams, so failure is unexpected.
  343. continue
  344. }
  345. // Special cases:
  346. // - Number fields are encoded as integer types.
  347. // - For ELK performance we record these domain-or-IP
  348. // fields as one of two different values based on type;
  349. // we also omit port from host:port fields for now.
  350. switch expectedParam.name {
  351. case "client_version":
  352. intValue, _ := strconv.Atoi(strValue)
  353. logFields[expectedParam.name] = intValue
  354. case "meek_dial_address":
  355. host, _, _ := net.SplitHostPort(strValue)
  356. if isIPAddress(config, host) {
  357. logFields["meek_dial_ip_address"] = host
  358. } else {
  359. logFields["meek_dial_domain"] = host
  360. }
  361. case "meek_host_header":
  362. host, _, _ := net.SplitHostPort(strValue)
  363. logFields[expectedParam.name] = host
  364. default:
  365. logFields[expectedParam.name] = strValue
  366. }
  367. }
  368. return logFields
  369. }
  370. func getStringRequestParam(params requestJSONObject, name string) (string, error) {
  371. if params[name] == nil {
  372. return "", psiphon.ContextError(errors.New("missing param"))
  373. }
  374. value, ok := params[name].(string)
  375. if !ok {
  376. return "", psiphon.ContextError(errors.New("invalid param"))
  377. }
  378. return value, nil
  379. }
  380. func getInt64RequestParam(params requestJSONObject, name string) (int64, error) {
  381. if params[name] == nil {
  382. return 0, psiphon.ContextError(errors.New("missing param"))
  383. }
  384. value, ok := params[name].(int64)
  385. if !ok {
  386. return 0, psiphon.ContextError(errors.New("invalid param"))
  387. }
  388. return value, nil
  389. }
  390. func getJSONObjectRequestParam(params requestJSONObject, name string) (requestJSONObject, error) {
  391. if params[name] == nil {
  392. return nil, psiphon.ContextError(errors.New("missing param"))
  393. }
  394. value, ok := params[name].(requestJSONObject)
  395. if !ok {
  396. return nil, psiphon.ContextError(errors.New("invalid param"))
  397. }
  398. return value, nil
  399. }
  400. func getJSONObjectArrayRequestParam(params requestJSONObject, name string) ([]requestJSONObject, error) {
  401. if params[name] == nil {
  402. return nil, psiphon.ContextError(errors.New("missing param"))
  403. }
  404. value, ok := params[name].([]requestJSONObject)
  405. if !ok {
  406. return nil, psiphon.ContextError(errors.New("invalid param"))
  407. }
  408. return value, nil
  409. }
  410. func getMapStringInt64RequestParam(params requestJSONObject, name string) (map[string]int64, error) {
  411. if params[name] == nil {
  412. return nil, psiphon.ContextError(errors.New("missing param"))
  413. }
  414. value, ok := params[name].(map[string]int64)
  415. if !ok {
  416. return nil, psiphon.ContextError(errors.New("invalid param"))
  417. }
  418. return value, nil
  419. }
  420. // Input validators follow the legacy validations rules in psi_web.
  421. func isServerSecret(config *Config, value string) bool {
  422. return subtle.ConstantTimeCompare(
  423. []byte(value),
  424. []byte(config.WebServerSecret)) == 1
  425. }
  426. func isHexDigits(_ *Config, value string) bool {
  427. return -1 == strings.IndexFunc(value, func(c rune) bool {
  428. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  429. })
  430. }
  431. func isDigits(_ *Config, value string) bool {
  432. return -1 == strings.IndexFunc(value, func(c rune) bool {
  433. return c < '0' || c > '9'
  434. })
  435. }
  436. func isClientPlatform(_ *Config, value string) bool {
  437. return -1 == strings.IndexFunc(value, func(c rune) bool {
  438. // Note: stricter than psi_web's Python string.whitespace
  439. return unicode.Is(unicode.White_Space, c)
  440. })
  441. }
  442. func isRelayProtocol(_ *Config, value string) bool {
  443. return psiphon.Contains(psiphon.SupportedTunnelProtocols, value)
  444. }
  445. func isBooleanFlag(_ *Config, value string) bool {
  446. return value == "0" || value == "1"
  447. }
  448. func isRegionCode(_ *Config, value string) bool {
  449. if len(value) != 2 {
  450. return false
  451. }
  452. return -1 == strings.IndexFunc(value, func(c rune) bool {
  453. return c < 'A' || c > 'Z'
  454. })
  455. }
  456. func isDialAddress(config *Config, value string) bool {
  457. // "<host>:<port>", where <host> is a domain or IP address
  458. parts := strings.Split(value, ":")
  459. if len(parts) != 2 {
  460. return false
  461. }
  462. if !isIPAddress(config, parts[0]) && !isDomain(config, parts[0]) {
  463. return false
  464. }
  465. if !isDigits(config, parts[1]) {
  466. return false
  467. }
  468. port, err := strconv.Atoi(parts[1])
  469. if err != nil {
  470. return false
  471. }
  472. return port > 0 && port < 65536
  473. }
  474. func isIPAddress(_ *Config, value string) bool {
  475. return net.ParseIP(value) != nil
  476. }
  477. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  478. func isDomain(_ *Config, value string) bool {
  479. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  480. //
  481. // "ensures that each segment
  482. // * contains at least one character and a maximum of 63 characters
  483. // * consists only of allowed characters
  484. // * doesn't begin or end with a hyphen"
  485. //
  486. if len(value) > 255 {
  487. return false
  488. }
  489. value = strings.TrimSuffix(value, ".")
  490. for _, part := range strings.Split(value, ".") {
  491. // Note: regexp doesn't support the following Perl expression which
  492. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  493. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  494. return false
  495. }
  496. if !isDomainRegex.Match([]byte(part)) {
  497. return false
  498. }
  499. }
  500. return true
  501. }
  502. func isHostHeader(config *Config, value string) bool {
  503. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  504. if strings.Contains(value, ":") {
  505. return isDialAddress(config, value)
  506. }
  507. return isIPAddress(config, value) || isDomain(config, value)
  508. }
  509. func isServerEntrySource(_ *Config, value string) bool {
  510. return psiphon.Contains(psiphon.SupportedServerEntrySources, value)
  511. }
  512. var isISO8601DateRegex = regexp.MustCompile(
  513. "(?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})))")
  514. func isISO8601Date(_ *Config, value string) bool {
  515. return isISO8601DateRegex.Match([]byte(value))
  516. }
  517. func isLastConnected(config *Config, value string) bool {
  518. return value == "None" || value == "Unknown" || isISO8601Date(config, value)
  519. }