api.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. // TODO: populate response data using psinet database
  94. var handshakeResponse struct {
  95. Homepages []string `json:"homepages"`
  96. UpgradeClientVersion string `json:"upgrade_client_version"`
  97. PageViewRegexes []map[string]string `json:"page_view_regexes"`
  98. HttpsRequestRegexes []map[string]string `json:"https_request_regexes"`
  99. EncodedServerList []string `json:"encoded_server_list"`
  100. ClientRegion string `json:"client_region"`
  101. ServerTimestamp string `json:"server_timestamp"`
  102. }
  103. handshakeResponse.Homepages = psinetDatabase.GetHomepages(
  104. "", "", "") // TODO: sponsorID, clientRegion, clientPlatform)
  105. handshakeResponse.UpgradeClientVersion = psinetDatabase.GetUpgradeClientVersion(
  106. "") // TODO: clientVersion)
  107. handshakeResponse.HttpsRequestRegexes = psinetDatabase.GetHttpsRequestRegexes(
  108. "", "", "") // TODO: sponsorID, clientRegion, clientPlatform)
  109. handshakeResponse.EncodedServerList = psinetDatabase.DiscoverServers(
  110. "", 0) // TODO: propagationChannelID, discoveryValue)
  111. handshakeResponse.ClientRegion = geoIPData.Country
  112. handshakeResponse.ServerTimestamp = psiphon.GetCurrentTimestamp()
  113. responsePayload, err := json.Marshal(handshakeResponse)
  114. if err != nil {
  115. return nil, psiphon.ContextError(err)
  116. }
  117. return responsePayload, nil
  118. }
  119. var connectedRequestParams = append(
  120. []requestParamSpec{requestParamSpec{"last_connected", isLastConnected, 0}},
  121. baseRequestParams...)
  122. // connectedAPIRequestHandler implements the "connected" API request.
  123. // Clients make the connected request once a tunnel connection has been
  124. // established and at least once per day. The last_connected input value,
  125. // which should be a connected_timestamp output from a previous connected
  126. // response, is used to calculate unique user stats.
  127. func connectedAPIRequestHandler(
  128. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  129. err := validateRequestParams(config, params, connectedRequestParams)
  130. if err != nil {
  131. // TODO: fail2ban?
  132. return nil, psiphon.ContextError(errors.New("invalid params"))
  133. }
  134. log.WithContextFields(
  135. getRequestLogFields(
  136. config,
  137. "connected",
  138. geoIPData,
  139. params,
  140. connectedRequestParams)).Info("API event")
  141. var connectedResponse struct {
  142. ConnectedTimestamp string `json:"connected_timestamp"`
  143. }
  144. connectedResponse.ConnectedTimestamp =
  145. psiphon.TruncateTimestampToHour(psiphon.GetCurrentTimestamp())
  146. responsePayload, err := json.Marshal(connectedResponse)
  147. if err != nil {
  148. return nil, psiphon.ContextError(err)
  149. }
  150. return responsePayload, nil
  151. }
  152. var statusRequestParams = append(
  153. []requestParamSpec{requestParamSpec{"connected", isBooleanFlag, 0}},
  154. baseRequestParams...)
  155. // statusAPIRequestHandler implements the "status" API request.
  156. // Clients make periodic status requests which deliver client-side
  157. // recorded data transfer and tunnel duration stats.
  158. func statusAPIRequestHandler(
  159. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  160. err := validateRequestParams(config, params, statusRequestParams)
  161. if err != nil {
  162. // TODO: fail2ban?
  163. return nil, psiphon.ContextError(errors.New("invalid params"))
  164. }
  165. statusData, err := getJSONObjectRequestParam(params, "statusData")
  166. if err != nil {
  167. return nil, psiphon.ContextError(err)
  168. }
  169. // Overall bytes transferred stats
  170. bytesTransferred, err := getInt64RequestParam(statusData, "bytes_transferred")
  171. if err != nil {
  172. return nil, psiphon.ContextError(err)
  173. }
  174. bytesTransferredFields := getRequestLogFields(
  175. config, "bytes_transferred", geoIPData, params, statusRequestParams)
  176. bytesTransferredFields["bytes"] = bytesTransferred
  177. log.WithContextFields(bytesTransferredFields).Info("API event")
  178. // Domain bytes transferred stats
  179. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  180. if err != nil {
  181. return nil, psiphon.ContextError(err)
  182. }
  183. domainBytesFields := getRequestLogFields(
  184. config, "domain_bytes", geoIPData, params, statusRequestParams)
  185. for domain, bytes := range hostBytes {
  186. domainBytesFields["domain"] = domain
  187. domainBytesFields["bytes"] = bytes
  188. log.WithContextFields(domainBytesFields).Info("API event")
  189. }
  190. // Tunnel duration and bytes transferred stats
  191. tunnelStats, err := getJSONObjectArrayRequestParam(statusData, "tunnel_stats")
  192. if err != nil {
  193. return nil, psiphon.ContextError(err)
  194. }
  195. sessionFields := getRequestLogFields(
  196. config, "session", geoIPData, params, statusRequestParams)
  197. for _, tunnelStat := range tunnelStats {
  198. sessionID, err := getStringRequestParam(tunnelStat, "session_id")
  199. if err != nil {
  200. return nil, psiphon.ContextError(err)
  201. }
  202. sessionFields["session_id"] = sessionID
  203. tunnelNumber, err := getInt64RequestParam(tunnelStat, "tunnel_number")
  204. if err != nil {
  205. return nil, psiphon.ContextError(err)
  206. }
  207. sessionFields["tunnel_number"] = tunnelNumber
  208. tunnelServerIPAddress, err := getStringRequestParam(tunnelStat, "tunnel_server_ip_address")
  209. if err != nil {
  210. return nil, psiphon.ContextError(err)
  211. }
  212. sessionFields["tunnel_server_ip_address"] = tunnelServerIPAddress
  213. serverHandshakeTimestamp, err := getStringRequestParam(tunnelStat, "server_handshake_timestamp")
  214. if err != nil {
  215. return nil, psiphon.ContextError(err)
  216. }
  217. sessionFields["server_handshake_timestamp"] = serverHandshakeTimestamp
  218. duration, err := getInt64RequestParam(tunnelStat, "duration")
  219. if err != nil {
  220. return nil, psiphon.ContextError(err)
  221. }
  222. // Client reports durations in nanoseconds; divide to get to milliseconds
  223. sessionFields["duration"] = duration / 1000000
  224. totalBytesSent, err := getInt64RequestParam(tunnelStat, "total_bytes_sent")
  225. if err != nil {
  226. return nil, psiphon.ContextError(err)
  227. }
  228. sessionFields["total_bytes_sent"] = totalBytesSent
  229. totalBytesReceived, err := getInt64RequestParam(tunnelStat, "total_bytes_received")
  230. if err != nil {
  231. return nil, psiphon.ContextError(err)
  232. }
  233. sessionFields["total_bytes_received"] = totalBytesReceived
  234. log.WithContextFields(sessionFields).Info("API event")
  235. }
  236. return make([]byte, 0), nil
  237. }
  238. // clientVerificationAPIRequestHandler implements the
  239. // "client verification" API request. Clients make the client
  240. // verification request once per tunnel connection. The payload
  241. // attests that client is a legitimate Psiphon client.
  242. func clientVerificationAPIRequestHandler(
  243. config *Config, geoIPData GeoIPData, params requestJSONObject) ([]byte, error) {
  244. err := validateRequestParams(config, params, baseRequestParams)
  245. if err != nil {
  246. // TODO: fail2ban?
  247. return nil, psiphon.ContextError(errors.New("invalid params"))
  248. }
  249. // TODO: implement
  250. return make([]byte, 0), nil
  251. }
  252. type requestParamSpec struct {
  253. name string
  254. validator func(*Config, string) bool
  255. flags int32
  256. }
  257. const (
  258. requestParamOptional = 1
  259. requestParamNotLogged = 2
  260. )
  261. // baseRequestParams is the list of required and optional
  262. // request parameters; derived from COMMON_INPUTS and
  263. // OPTIONAL_COMMON_INPUTS in psi_web.
  264. var baseRequestParams = []requestParamSpec{
  265. requestParamSpec{"server_secret", isServerSecret, requestParamNotLogged},
  266. requestParamSpec{"client_session_id", isHexDigits, 0},
  267. requestParamSpec{"propagation_channel_id", isHexDigits, 0},
  268. requestParamSpec{"sponsor_id", isHexDigits, 0},
  269. requestParamSpec{"client_version", isDigits, 0},
  270. requestParamSpec{"client_platform", isClientPlatform, 0},
  271. requestParamSpec{"relay_protocol", isRelayProtocol, 0},
  272. requestParamSpec{"tunnel_whole_device", isBooleanFlag, 0},
  273. requestParamSpec{"device_region", isRegionCode, requestParamOptional},
  274. requestParamSpec{"meek_dial_address", isDialAddress, requestParamOptional},
  275. requestParamSpec{"meek_resolved_ip_address", isIPAddress, requestParamOptional},
  276. requestParamSpec{"meek_sni_server_name", isDomain, requestParamOptional},
  277. requestParamSpec{"meek_host_header", isHostHeader, requestParamOptional},
  278. requestParamSpec{"meek_transformed_host_name", isBooleanFlag, requestParamOptional},
  279. requestParamSpec{"server_entry_region", isRegionCode, requestParamOptional},
  280. requestParamSpec{"server_entry_source", isServerEntrySource, requestParamOptional},
  281. requestParamSpec{"server_entry_timestamp", isISO8601Date, requestParamOptional},
  282. }
  283. func validateRequestParams(
  284. config *Config,
  285. params requestJSONObject,
  286. expectedParams []requestParamSpec) error {
  287. for _, expectedParam := range expectedParams {
  288. value := params[expectedParam.name]
  289. if value == nil {
  290. if expectedParam.flags&requestParamOptional != 0 {
  291. continue
  292. }
  293. return psiphon.ContextError(
  294. fmt.Errorf("missing required param: %s", expectedParam.name))
  295. }
  296. strValue, ok := value.(string)
  297. if !ok {
  298. return psiphon.ContextError(
  299. fmt.Errorf("unexpected param type: %s", expectedParam.name))
  300. }
  301. if !expectedParam.validator(config, strValue) {
  302. return psiphon.ContextError(
  303. fmt.Errorf("invalid param: %s", expectedParam.name))
  304. }
  305. }
  306. return nil
  307. }
  308. // getRequestLogFields makes LogFields to log the API event following
  309. // the legacy psi_web and current ELK naming conventions.
  310. func getRequestLogFields(
  311. config *Config,
  312. eventName string,
  313. geoIPData GeoIPData,
  314. params requestJSONObject,
  315. expectedParams []requestParamSpec) LogFields {
  316. logFields := make(LogFields)
  317. logFields["event_name"] = eventName
  318. logFields["host_id"] = config.HostID
  319. // In psi_web, the space replacement was done to accommodate space
  320. // delimited logging, which is no longer required; we retain the
  321. // transformation so that stats aggregation isn't impacted.
  322. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  323. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  324. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  325. for _, expectedParam := range expectedParams {
  326. value := params[expectedParam.name]
  327. if value == nil {
  328. // Skip optional params
  329. continue
  330. }
  331. strValue, ok := value.(string)
  332. if !ok {
  333. // This type assertion should be checked already in
  334. // validateRequestParams, so failure is unexpected.
  335. continue
  336. }
  337. // Special cases:
  338. // - Number fields are encoded as integer types.
  339. // - For ELK performance we record these domain-or-IP
  340. // fields as one of two different values based on type;
  341. // we also omit port from host:port fields for now.
  342. switch expectedParam.name {
  343. case "client_version":
  344. intValue, _ := strconv.Atoi(strValue)
  345. logFields[expectedParam.name] = intValue
  346. case "meek_dial_address":
  347. host, _, _ := net.SplitHostPort(strValue)
  348. if isIPAddress(config, host) {
  349. logFields["meek_dial_ip_address"] = host
  350. } else {
  351. logFields["meek_dial_domain"] = host
  352. }
  353. case "meek_host_header":
  354. host, _, _ := net.SplitHostPort(strValue)
  355. logFields[expectedParam.name] = host
  356. default:
  357. logFields[expectedParam.name] = strValue
  358. }
  359. }
  360. return logFields
  361. }
  362. func getStringRequestParam(params requestJSONObject, name string) (string, error) {
  363. if params[name] == nil {
  364. return "", psiphon.ContextError(errors.New("missing param"))
  365. }
  366. value, ok := params[name].(string)
  367. if !ok {
  368. return "", psiphon.ContextError(errors.New("invalid param"))
  369. }
  370. return value, nil
  371. }
  372. func getInt64RequestParam(params requestJSONObject, name string) (int64, error) {
  373. if params[name] == nil {
  374. return 0, psiphon.ContextError(errors.New("missing param"))
  375. }
  376. value, ok := params[name].(int64)
  377. if !ok {
  378. return 0, psiphon.ContextError(errors.New("invalid param"))
  379. }
  380. return value, nil
  381. }
  382. func getJSONObjectRequestParam(params requestJSONObject, name string) (requestJSONObject, error) {
  383. if params[name] == nil {
  384. return nil, psiphon.ContextError(errors.New("missing param"))
  385. }
  386. value, ok := params[name].(requestJSONObject)
  387. if !ok {
  388. return nil, psiphon.ContextError(errors.New("invalid param"))
  389. }
  390. return value, nil
  391. }
  392. func getJSONObjectArrayRequestParam(params requestJSONObject, name string) ([]requestJSONObject, error) {
  393. if params[name] == nil {
  394. return nil, psiphon.ContextError(errors.New("missing param"))
  395. }
  396. value, ok := params[name].([]requestJSONObject)
  397. if !ok {
  398. return nil, psiphon.ContextError(errors.New("invalid param"))
  399. }
  400. return value, nil
  401. }
  402. func getMapStringInt64RequestParam(params requestJSONObject, name string) (map[string]int64, error) {
  403. if params[name] == nil {
  404. return nil, psiphon.ContextError(errors.New("missing param"))
  405. }
  406. value, ok := params[name].(map[string]int64)
  407. if !ok {
  408. return nil, psiphon.ContextError(errors.New("invalid param"))
  409. }
  410. return value, nil
  411. }
  412. // Input validators follow the legacy validations rules in psi_web.
  413. func isServerSecret(config *Config, value string) bool {
  414. return subtle.ConstantTimeCompare(
  415. []byte(value),
  416. []byte(config.WebServerSecret)) == 1
  417. }
  418. func isHexDigits(_ *Config, value string) bool {
  419. return -1 == strings.IndexFunc(value, func(c rune) bool {
  420. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  421. })
  422. }
  423. func isDigits(_ *Config, value string) bool {
  424. return -1 == strings.IndexFunc(value, func(c rune) bool {
  425. return c < '0' || c > '9'
  426. })
  427. }
  428. func isClientPlatform(_ *Config, value string) bool {
  429. return -1 == strings.IndexFunc(value, func(c rune) bool {
  430. // Note: stricter than psi_web's Python string.whitespace
  431. return unicode.Is(unicode.White_Space, c)
  432. })
  433. }
  434. func isRelayProtocol(_ *Config, value string) bool {
  435. return psiphon.Contains(psiphon.SupportedTunnelProtocols, value)
  436. }
  437. func isBooleanFlag(_ *Config, value string) bool {
  438. return value == "0" || value == "1"
  439. }
  440. func isRegionCode(_ *Config, value string) bool {
  441. if len(value) != 2 {
  442. return false
  443. }
  444. return -1 == strings.IndexFunc(value, func(c rune) bool {
  445. return c < 'A' || c > 'Z'
  446. })
  447. }
  448. func isDialAddress(config *Config, value string) bool {
  449. // "<host>:<port>", where <host> is a domain or IP address
  450. parts := strings.Split(value, ":")
  451. if len(parts) != 2 {
  452. return false
  453. }
  454. if !isIPAddress(config, parts[0]) && !isDomain(config, parts[0]) {
  455. return false
  456. }
  457. if !isDigits(config, parts[1]) {
  458. return false
  459. }
  460. port, err := strconv.Atoi(parts[1])
  461. if err != nil {
  462. return false
  463. }
  464. return port > 0 && port < 65536
  465. }
  466. func isIPAddress(_ *Config, value string) bool {
  467. return net.ParseIP(value) != nil
  468. }
  469. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  470. func isDomain(_ *Config, value string) bool {
  471. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  472. //
  473. // "ensures that each segment
  474. // * contains at least one character and a maximum of 63 characters
  475. // * consists only of allowed characters
  476. // * doesn't begin or end with a hyphen"
  477. //
  478. if len(value) > 255 {
  479. return false
  480. }
  481. value = strings.TrimSuffix(value, ".")
  482. for _, part := range strings.Split(value, ".") {
  483. // Note: regexp doesn't support the following Perl expression which
  484. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  485. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  486. return false
  487. }
  488. if !isDomainRegex.Match([]byte(part)) {
  489. return false
  490. }
  491. }
  492. return true
  493. }
  494. func isHostHeader(config *Config, value string) bool {
  495. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  496. if strings.Contains(value, ":") {
  497. return isDialAddress(config, value)
  498. }
  499. return isIPAddress(config, value) || isDomain(config, value)
  500. }
  501. func isServerEntrySource(_ *Config, value string) bool {
  502. return psiphon.Contains(psiphon.SupportedServerEntrySources, value)
  503. }
  504. var isISO8601DateRegex = regexp.MustCompile(
  505. "(?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})))")
  506. func isISO8601Date(_ *Config, value string) bool {
  507. return isISO8601DateRegex.Match([]byte(value))
  508. }
  509. func isLastConnected(config *Config, value string) bool {
  510. return value == "None" || value == "Unknown" || isISO8601Date(config, value)
  511. }