api.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. "runtime/debug"
  28. "strconv"
  29. "strings"
  30. "unicode"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
  34. )
  35. const (
  36. MAX_API_PARAMS_SIZE = 256 * 1024 // 256KB
  37. PADDING_MAX_BYTES = 16 * 1024
  38. CLIENT_PLATFORM_ANDROID = "Android"
  39. CLIENT_PLATFORM_WINDOWS = "Windows"
  40. CLIENT_PLATFORM_IOS = "iOS"
  41. )
  42. // sshAPIRequestHandler routes Psiphon API requests transported as
  43. // JSON objects via the SSH request mechanism.
  44. //
  45. // The API request handlers, handshakeAPIRequestHandler, etc., are
  46. // reused by webServer which offers the Psiphon API via web transport.
  47. //
  48. // The API request parameters and event log values follow the legacy
  49. // psi_web protocol and naming conventions. The API is compatible with
  50. // all tunnel-core clients but are not backwards compatible with all
  51. // legacy clients.
  52. //
  53. func sshAPIRequestHandler(
  54. support *SupportServices,
  55. geoIPData GeoIPData,
  56. authorizedAccessTypes []string,
  57. name string,
  58. requestPayload []byte) ([]byte, error) {
  59. // Notes:
  60. //
  61. // - For SSH requests, MAX_API_PARAMS_SIZE is implicitly enforced
  62. // by max SSH request packet size.
  63. //
  64. // - The param protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS is an
  65. // array of base64-encoded strings; the base64 representation should
  66. // not be decoded to []byte values. The default behavior of
  67. // https://golang.org/pkg/encoding/json/#Unmarshal for a target of
  68. // type map[string]interface{} will unmarshal a base64-encoded string
  69. // to a string, not a decoded []byte, as required.
  70. var params common.APIParameters
  71. err := json.Unmarshal(requestPayload, &params)
  72. if err != nil {
  73. return nil, common.ContextError(
  74. fmt.Errorf("invalid payload for request name: %s: %s", name, err))
  75. }
  76. return dispatchAPIRequestHandler(
  77. support,
  78. protocol.PSIPHON_SSH_API_PROTOCOL,
  79. geoIPData,
  80. authorizedAccessTypes,
  81. name,
  82. params)
  83. }
  84. // dispatchAPIRequestHandler is the common dispatch point for both
  85. // web and SSH API requests.
  86. func dispatchAPIRequestHandler(
  87. support *SupportServices,
  88. apiProtocol string,
  89. geoIPData GeoIPData,
  90. authorizedAccessTypes []string,
  91. name string,
  92. params common.APIParameters) (response []byte, reterr error) {
  93. // Recover from and log any unexpected panics caused by user input
  94. // handling bugs. User inputs should be properly validated; this
  95. // mechanism is only a last resort to prevent the process from
  96. // terminating in the case of a bug.
  97. defer func() {
  98. if e := recover(); e != nil {
  99. if intentionalPanic, ok := e.(IntentionalPanicError); ok {
  100. panic(intentionalPanic)
  101. } else {
  102. log.LogPanicRecover(e, debug.Stack())
  103. reterr = common.ContextError(errors.New("request handler panic"))
  104. }
  105. }
  106. }()
  107. // Before invoking the handlers, enforce some preconditions:
  108. //
  109. // - A handshake request must precede any other requests.
  110. // - When the handshake results in a traffic rules state where
  111. // the client is immediately exhausted, no requests
  112. // may succeed. This case ensures that blocked clients do
  113. // not log "connected", etc.
  114. //
  115. // Only one handshake request may be made. There is no check here
  116. // to enforce that handshakeAPIRequestHandler will be called at
  117. // most once. The SetHandshakeState call in handshakeAPIRequestHandler
  118. // enforces that only a single handshake is made; enforcing that there
  119. // ensures no race condition even if concurrent requests are
  120. // in flight.
  121. if name != protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME {
  122. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  123. // applies here.
  124. sessionID, err := getStringRequestParam(params, "client_session_id")
  125. if err == nil {
  126. // Note: follows/duplicates baseRequestParams validation
  127. if !isHexDigits(support.Config, sessionID) {
  128. err = errors.New("invalid param: client_session_id")
  129. }
  130. }
  131. if err != nil {
  132. return nil, common.ContextError(err)
  133. }
  134. completed, exhausted, err := support.TunnelServer.GetClientHandshaked(sessionID)
  135. if err != nil {
  136. return nil, common.ContextError(err)
  137. }
  138. if !completed {
  139. return nil, common.ContextError(errors.New("handshake not completed"))
  140. }
  141. if exhausted {
  142. return nil, common.ContextError(errors.New("exhausted after handshake"))
  143. }
  144. }
  145. switch name {
  146. case protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME:
  147. return handshakeAPIRequestHandler(support, apiProtocol, geoIPData, params)
  148. case protocol.PSIPHON_API_CONNECTED_REQUEST_NAME:
  149. return connectedAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  150. case protocol.PSIPHON_API_STATUS_REQUEST_NAME:
  151. return statusAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  152. case protocol.PSIPHON_API_CLIENT_VERIFICATION_REQUEST_NAME:
  153. return clientVerificationAPIRequestHandler(support, geoIPData, authorizedAccessTypes, params)
  154. }
  155. return nil, common.ContextError(fmt.Errorf("invalid request name: %s", name))
  156. }
  157. var handshakeRequestParams = append(
  158. append(
  159. // Note: legacy clients may not send "session_id" in handshake
  160. []requestParamSpec{{"session_id", isHexDigits, requestParamOptional}},
  161. tacticsParams...),
  162. baseRequestParams...)
  163. // handshakeAPIRequestHandler implements the "handshake" API request.
  164. // Clients make the handshake immediately after establishing a tunnel
  165. // connection; the response tells the client what homepage to open, what
  166. // stats to record, etc.
  167. func handshakeAPIRequestHandler(
  168. support *SupportServices,
  169. apiProtocol string,
  170. geoIPData GeoIPData,
  171. params common.APIParameters) ([]byte, error) {
  172. // Note: ignoring "known_servers" params
  173. err := validateRequestParams(support.Config, params, baseRequestParams)
  174. if err != nil {
  175. return nil, common.ContextError(err)
  176. }
  177. sessionID, _ := getStringRequestParam(params, "client_session_id")
  178. sponsorID, _ := getStringRequestParam(params, "sponsor_id")
  179. clientVersion, _ := getStringRequestParam(params, "client_version")
  180. clientPlatform, _ := getStringRequestParam(params, "client_platform")
  181. isMobile := isMobileClientPlatform(clientPlatform)
  182. normalizedPlatform := normalizeClientPlatform(clientPlatform)
  183. var authorizations []string
  184. if params[protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS] != nil {
  185. authorizations, err = getStringArrayRequestParam(params, protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS)
  186. if err != nil {
  187. return nil, common.ContextError(err)
  188. }
  189. }
  190. // Note: no guarantee that PsinetDatabase won't reload between database calls
  191. db := support.PsinetDatabase
  192. httpsRequestRegexes := db.GetHttpsRequestRegexes(sponsorID)
  193. // Flag the SSH client as having completed its handshake. This
  194. // may reselect traffic rules and starts allowing port forwards.
  195. // TODO: in the case of SSH API requests, the actual sshClient could
  196. // be passed in and used here. The session ID lookup is only strictly
  197. // necessary to support web API requests.
  198. activeAuthorizationIDs, authorizedAccessTypes, err := support.TunnelServer.SetClientHandshakeState(
  199. sessionID,
  200. handshakeState{
  201. completed: true,
  202. apiProtocol: apiProtocol,
  203. apiParams: copyBaseRequestParams(params),
  204. expectDomainBytes: len(httpsRequestRegexes) > 0,
  205. },
  206. authorizations)
  207. if err != nil {
  208. return nil, common.ContextError(err)
  209. }
  210. tacticsPayload, err := support.TacticsServer.GetTacticsPayload(
  211. common.GeoIPData(geoIPData), params)
  212. if err != nil {
  213. return nil, common.ContextError(err)
  214. }
  215. var marshaledTacticsPayload []byte
  216. if tacticsPayload != nil {
  217. marshaledTacticsPayload, err = json.Marshal(tacticsPayload)
  218. if err != nil {
  219. return nil, common.ContextError(err)
  220. }
  221. // Log a metric when new tactics are issued. Logging here indicates that
  222. // the handshake tactics mechanism is active; but logging for every
  223. // handshake creates unneccesary log data.
  224. if len(tacticsPayload.Tactics) > 0 {
  225. logFields := getRequestLogFields(
  226. tactics.TACTICS_METRIC_EVENT_NAME,
  227. geoIPData,
  228. authorizedAccessTypes,
  229. params,
  230. handshakeRequestParams)
  231. logFields[tactics.NEW_TACTICS_TAG_LOG_FIELD_NAME] = tacticsPayload.Tag
  232. logFields[tactics.IS_TACTICS_REQUEST_LOG_FIELD_NAME] = false
  233. log.LogRawFieldsWithTimestamp(logFields)
  234. }
  235. }
  236. // The log comes _after_ SetClientHandshakeState, in case that call rejects
  237. // the state change (for example, if a second handshake is performed)
  238. //
  239. // The handshake event is no longer shipped to log consumers, so this is
  240. // simply a diagnostic log. Since the "server_tunnel" event includes all
  241. // common API parameters and "handshake_completed" flag, this handshake
  242. // log is mostly redundant and set to debug level.
  243. log.WithContextFields(
  244. getRequestLogFields(
  245. "",
  246. geoIPData,
  247. authorizedAccessTypes,
  248. params,
  249. baseRequestParams)).Debug("handshake")
  250. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  251. handshakeResponse := protocol.HandshakeResponse{
  252. SSHSessionID: sessionID,
  253. Homepages: db.GetRandomizedHomepages(sponsorID, geoIPData.Country, isMobile),
  254. UpgradeClientVersion: db.GetUpgradeClientVersion(clientVersion, normalizedPlatform),
  255. PageViewRegexes: make([]map[string]string, 0),
  256. HttpsRequestRegexes: httpsRequestRegexes,
  257. EncodedServerList: db.DiscoverServers(geoIPData.DiscoveryValue),
  258. ClientRegion: geoIPData.Country,
  259. ServerTimestamp: common.GetCurrentTimestamp(),
  260. ActiveAuthorizationIDs: activeAuthorizationIDs,
  261. TacticsPayload: marshaledTacticsPayload,
  262. Padding: strings.Repeat(" ", pad_response),
  263. }
  264. responsePayload, err := json.Marshal(handshakeResponse)
  265. if err != nil {
  266. return nil, common.ContextError(err)
  267. }
  268. return responsePayload, nil
  269. }
  270. var connectedRequestParams = append(
  271. []requestParamSpec{
  272. {"session_id", isHexDigits, 0},
  273. {"last_connected", isLastConnected, 0},
  274. {"establishment_duration", isIntString, requestParamOptional | requestParamLogStringAsInt}},
  275. baseRequestParams...)
  276. // updateOnConnectedParamNames are connected request parameters which are
  277. // copied to update data logged with server_tunnel: these fields either only
  278. // ship with or ship newer data with connected requests.
  279. var updateOnConnectedParamNames = []string{
  280. "last_connected",
  281. "establishment_duration",
  282. "upstream_bytes_fragmented",
  283. "upstream_min_bytes_written",
  284. "upstream_max_bytes_written",
  285. "upstream_min_delayed",
  286. "upstream_max_delayed",
  287. }
  288. // connectedAPIRequestHandler implements the "connected" API request.
  289. // Clients make the connected request once a tunnel connection has been
  290. // established and at least once per day. The last_connected input value,
  291. // which should be a connected_timestamp output from a previous connected
  292. // response, is used to calculate unique user stats.
  293. // connected_timestamp is truncated as a privacy measure.
  294. func connectedAPIRequestHandler(
  295. support *SupportServices,
  296. geoIPData GeoIPData,
  297. authorizedAccessTypes []string,
  298. params common.APIParameters) ([]byte, error) {
  299. err := validateRequestParams(support.Config, params, connectedRequestParams)
  300. if err != nil {
  301. return nil, common.ContextError(err)
  302. }
  303. // Update, for server_tunnel logging, upstream fragmentor metrics, as the
  304. // client may have performed more upstream fragmentation since the
  305. // previous metrics reported by the handshake request. Also, additional
  306. // fields reported only in the connected request, are added to
  307. // server_tunnel here.
  308. // TODO: same session-ID-lookup TODO in handshakeAPIRequestHandler
  309. // applies here.
  310. sessionID, _ := getStringRequestParam(params, "client_session_id")
  311. err = support.TunnelServer.UpdateClientAPIParameters(
  312. sessionID, copyUpdateOnConnectedParams(params))
  313. if err != nil {
  314. return nil, common.ContextError(err)
  315. }
  316. log.LogRawFieldsWithTimestamp(
  317. getRequestLogFields(
  318. "connected",
  319. geoIPData,
  320. authorizedAccessTypes,
  321. params,
  322. connectedRequestParams))
  323. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  324. connectedResponse := protocol.ConnectedResponse{
  325. ConnectedTimestamp: common.TruncateTimestampToHour(common.GetCurrentTimestamp()),
  326. Padding: strings.Repeat(" ", pad_response),
  327. }
  328. responsePayload, err := json.Marshal(connectedResponse)
  329. if err != nil {
  330. return nil, common.ContextError(err)
  331. }
  332. return responsePayload, nil
  333. }
  334. var statusRequestParams = append(
  335. []requestParamSpec{
  336. {"session_id", isHexDigits, 0},
  337. {"connected", isBooleanFlag, requestParamLogFlagAsBool}},
  338. baseRequestParams...)
  339. var remoteServerListStatParams = []requestParamSpec{
  340. {"session_id", isHexDigits, requestParamOptional},
  341. {"propagation_channel_id", isHexDigits, requestParamOptional},
  342. {"sponsor_id", isHexDigits, requestParamOptional},
  343. {"client_version", isAnyString, requestParamOptional},
  344. {"client_platform", isAnyString, requestParamOptional},
  345. {"client_build_rev", isAnyString, requestParamOptional},
  346. {"client_download_timestamp", isISO8601Date, 0},
  347. {"url", isAnyString, 0},
  348. {"etag", isAnyString, 0},
  349. }
  350. var failedTunnelStatParams = append(
  351. []requestParamSpec{
  352. {"server_entry_tag", isAnyString, requestParamOptional},
  353. {"session_id", isHexDigits, 0},
  354. {"last_connected", isLastConnected, 0},
  355. {"client_failed_timestamp", isISO8601Date, 0},
  356. {"tunnel_error", isAnyString, 0}},
  357. baseRequestParams...)
  358. // statusAPIRequestHandler implements the "status" API request.
  359. // Clients make periodic status requests which deliver client-side
  360. // recorded data transfer and tunnel duration stats.
  361. // Note from psi_web implementation: no input validation on domains;
  362. // any string is accepted (regex transform may result in arbitrary
  363. // string). Stats processor must handle this input with care.
  364. func statusAPIRequestHandler(
  365. support *SupportServices,
  366. geoIPData GeoIPData,
  367. authorizedAccessTypes []string,
  368. params common.APIParameters) ([]byte, error) {
  369. err := validateRequestParams(support.Config, params, statusRequestParams)
  370. if err != nil {
  371. return nil, common.ContextError(err)
  372. }
  373. sessionID, _ := getStringRequestParam(params, "client_session_id")
  374. statusData, err := getJSONObjectRequestParam(params, "statusData")
  375. if err != nil {
  376. return nil, common.ContextError(err)
  377. }
  378. // Logs are queued until the input is fully validated. Otherwise, stats
  379. // could be double counted if the client has a bug in its request
  380. // formatting: partial stats would be logged (counted), the request would
  381. // fail, and clients would then resend all the same stats again.
  382. logQueue := make([]LogFields, 0)
  383. // Domain bytes transferred stats
  384. // Older clients may not submit this data
  385. // Clients are expected to send host_bytes/domain_bytes stats only when
  386. // configured to do so in the handshake reponse. Legacy clients may still
  387. // report "(OTHER)" host_bytes when no regexes are set. Drop those stats.
  388. domainBytesExpected, err := support.TunnelServer.ExpectClientDomainBytes(sessionID)
  389. if err != nil {
  390. return nil, common.ContextError(err)
  391. }
  392. if domainBytesExpected && statusData["host_bytes"] != nil {
  393. hostBytes, err := getMapStringInt64RequestParam(statusData, "host_bytes")
  394. if err != nil {
  395. return nil, common.ContextError(err)
  396. }
  397. for domain, bytes := range hostBytes {
  398. domainBytesFields := getRequestLogFields(
  399. "domain_bytes",
  400. geoIPData,
  401. authorizedAccessTypes,
  402. params,
  403. statusRequestParams)
  404. domainBytesFields["domain"] = domain
  405. domainBytesFields["bytes"] = bytes
  406. logQueue = append(logQueue, domainBytesFields)
  407. }
  408. }
  409. // Limitation: for "persistent" stats, host_id and geolocation is time-of-sending
  410. // not time-of-recording.
  411. // Remote server list download persistent stats.
  412. // Older clients may not submit this data.
  413. if statusData["remote_server_list_stats"] != nil {
  414. remoteServerListStats, err := getJSONObjectArrayRequestParam(statusData, "remote_server_list_stats")
  415. if err != nil {
  416. return nil, common.ContextError(err)
  417. }
  418. for _, remoteServerListStat := range remoteServerListStats {
  419. err := validateRequestParams(support.Config, remoteServerListStat, remoteServerListStatParams)
  420. if err != nil {
  421. return nil, common.ContextError(err)
  422. }
  423. // remote_server_list defaults to using the common params from the
  424. // outer statusRequestParams
  425. remoteServerListFields := getRequestLogFields(
  426. "remote_server_list",
  427. geoIPData,
  428. authorizedAccessTypes,
  429. params,
  430. statusRequestParams)
  431. for name, value := range remoteServerListStat {
  432. remoteServerListFields[name] = value
  433. }
  434. logQueue = append(logQueue, remoteServerListFields)
  435. }
  436. }
  437. // Failed tunnel persistent stats.
  438. // Older clients may not submit this data.
  439. var invalidServerEntryTags map[string]bool
  440. if statusData["failed_tunnel_stats"] != nil {
  441. // Note: no guarantee that PsinetDatabase won't reload between database calls
  442. db := support.PsinetDatabase
  443. invalidServerEntryTags = make(map[string]bool)
  444. failedTunnelStats, err := getJSONObjectArrayRequestParam(statusData, "failed_tunnel_stats")
  445. if err != nil {
  446. return nil, common.ContextError(err)
  447. }
  448. for _, failedTunnelStat := range failedTunnelStats {
  449. // failed_tunnel supplies a full set of common params, but the
  450. // server secret must use the correct value from the outer
  451. // statusRequestParams
  452. failedTunnelStat["server_secret"] = params["server_secret"]
  453. err := validateRequestParams(support.Config, failedTunnelStat, failedTunnelStatParams)
  454. if err != nil {
  455. return nil, common.ContextError(err)
  456. }
  457. failedTunnelFields := getRequestLogFields(
  458. "failed_tunnel",
  459. geoIPData,
  460. authorizedAccessTypes,
  461. failedTunnelStat,
  462. failedTunnelStatParams)
  463. // Return a list of servers, identified by server entry tag, that are
  464. // invalid and presumed to be deleted. This information is used by clients
  465. // to prune deleted servers from their local datastores and stop attempting
  466. // connections to servers that no longer exist.
  467. //
  468. // This mechanism uses tags instead of server IPs: (a) to prevent an
  469. // enumeration attack, where a malicious client can query the entire IPv4
  470. // range and build a map of the Psiphon network; (b) to deal with recyling
  471. // cases where a server deleted and its IP is reused for a new server with
  472. // a distinct server entry.
  473. //
  474. // IsValidServerEntryTag ensures that the local copy of psinet is not stale
  475. // before returning a negative result, to mitigate accidental pruning.
  476. var serverEntryTagStr string
  477. serverEntryTag, ok := failedTunnelStat["server_entry_tag"]
  478. if ok {
  479. serverEntryTagStr, ok = serverEntryTag.(string)
  480. }
  481. if ok {
  482. serverEntryValid := db.IsValidServerEntryTag(serverEntryTagStr)
  483. if !serverEntryValid {
  484. invalidServerEntryTags[serverEntryTagStr] = true
  485. }
  486. // Add a field to the failed_tunnel log indicating if the server entry is
  487. // valid.
  488. failedTunnelFields["server_entry_valid"] = serverEntryValid
  489. }
  490. // Log failed_tunnel.
  491. logQueue = append(logQueue, failedTunnelFields)
  492. }
  493. }
  494. for _, logItem := range logQueue {
  495. log.LogRawFieldsWithTimestamp(logItem)
  496. }
  497. pad_response, _ := getPaddingSizeRequestParam(params, "pad_response")
  498. statusResponse := protocol.StatusResponse{
  499. Padding: strings.Repeat(" ", pad_response),
  500. }
  501. if len(invalidServerEntryTags) > 0 {
  502. statusResponse.InvalidServerEntryTags = make([]string, len(invalidServerEntryTags))
  503. i := 0
  504. for tag, _ := range invalidServerEntryTags {
  505. statusResponse.InvalidServerEntryTags[i] = tag
  506. i++
  507. }
  508. }
  509. responsePayload, err := json.Marshal(statusResponse)
  510. if err != nil {
  511. return nil, common.ContextError(err)
  512. }
  513. return responsePayload, nil
  514. }
  515. // clientVerificationAPIRequestHandler is just a compliance stub
  516. // for older Android clients that still send verification requests
  517. func clientVerificationAPIRequestHandler(
  518. support *SupportServices,
  519. geoIPData GeoIPData,
  520. authorizedAccessTypes []string,
  521. params common.APIParameters) ([]byte, error) {
  522. return make([]byte, 0), nil
  523. }
  524. var tacticsParams = []requestParamSpec{
  525. {tactics.STORED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  526. {tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME, nil, requestParamOptional | requestParamJSON},
  527. }
  528. var tacticsRequestParams = append(
  529. append(
  530. []requestParamSpec{{"session_id", isHexDigits, 0}},
  531. tacticsParams...),
  532. baseRequestParams...)
  533. func getTacticsAPIParameterValidator(config *Config) common.APIParameterValidator {
  534. return func(params common.APIParameters) error {
  535. return validateRequestParams(config, params, tacticsRequestParams)
  536. }
  537. }
  538. func getTacticsAPIParameterLogFieldFormatter() common.APIParameterLogFieldFormatter {
  539. return func(geoIPData common.GeoIPData, params common.APIParameters) common.LogFields {
  540. logFields := getRequestLogFields(
  541. tactics.TACTICS_METRIC_EVENT_NAME,
  542. GeoIPData(geoIPData),
  543. nil, // authorizedAccessTypes are not known yet
  544. params,
  545. tacticsRequestParams)
  546. return common.LogFields(logFields)
  547. }
  548. }
  549. type requestParamSpec struct {
  550. name string
  551. validator func(*Config, string) bool
  552. flags uint32
  553. }
  554. const (
  555. requestParamOptional = 1
  556. requestParamNotLogged = 1 << 1
  557. requestParamArray = 1 << 2
  558. requestParamJSON = 1 << 3
  559. requestParamLogStringAsInt = 1 << 4
  560. requestParamLogStringLengthAsInt = 1 << 5
  561. requestParamLogFlagAsBool = 1 << 6
  562. requestParamLogOnlyForFrontedMeek = 1 << 7
  563. requestParamNotLoggedForUnfrontedMeekNonTransformedHeader = 1 << 8
  564. )
  565. // baseRequestParams is the list of required and optional
  566. // request parameters; derived from COMMON_INPUTS and
  567. // OPTIONAL_COMMON_INPUTS in psi_web.
  568. // Each param is expected to be a string, unless requestParamArray
  569. // is specified, in which case an array of string is expected.
  570. var baseRequestParams = []requestParamSpec{
  571. {"server_secret", isServerSecret, requestParamNotLogged},
  572. {"client_session_id", isHexDigits, requestParamNotLogged},
  573. {"propagation_channel_id", isHexDigits, 0},
  574. {"sponsor_id", isHexDigits, 0},
  575. {"client_version", isIntString, requestParamLogStringAsInt},
  576. {"client_platform", isClientPlatform, 0},
  577. {"client_build_rev", isHexDigits, requestParamOptional},
  578. {"relay_protocol", isRelayProtocol, 0},
  579. {"tunnel_whole_device", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  580. {"device_region", isAnyString, requestParamOptional},
  581. {"ssh_client_version", isAnyString, requestParamOptional},
  582. {"upstream_proxy_type", isUpstreamProxyType, requestParamOptional},
  583. {"upstream_proxy_custom_header_names", isAnyString, requestParamOptional | requestParamArray},
  584. {"meek_dial_address", isDialAddress, requestParamOptional | requestParamLogOnlyForFrontedMeek},
  585. {"meek_resolved_ip_address", isIPAddress, requestParamOptional | requestParamLogOnlyForFrontedMeek},
  586. {"meek_sni_server_name", isDomain, requestParamOptional},
  587. {"meek_host_header", isHostHeader, requestParamOptional | requestParamNotLoggedForUnfrontedMeekNonTransformedHeader},
  588. {"meek_transformed_host_name", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  589. {"user_agent", isAnyString, requestParamOptional},
  590. {"tls_profile", isAnyString, requestParamOptional},
  591. {"tls_version", isAnyString, requestParamOptional},
  592. {"server_entry_region", isRegionCode, requestParamOptional},
  593. {"server_entry_source", isServerEntrySource, requestParamOptional},
  594. {"server_entry_timestamp", isISO8601Date, requestParamOptional},
  595. {tactics.APPLIED_TACTICS_TAG_PARAMETER_NAME, isAnyString, requestParamOptional},
  596. {"dial_port_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  597. {"quic_version", isAnyString, requestParamOptional},
  598. {"quic_dial_sni_address", isAnyString, requestParamOptional},
  599. {"upstream_bytes_fragmented", isIntString, requestParamOptional | requestParamLogStringAsInt},
  600. {"upstream_min_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  601. {"upstream_max_bytes_written", isIntString, requestParamOptional | requestParamLogStringAsInt},
  602. {"upstream_min_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  603. {"upstream_max_delayed", isIntString, requestParamOptional | requestParamLogStringAsInt},
  604. {"padding", isAnyString, requestParamOptional | requestParamLogStringLengthAsInt},
  605. {"pad_response", isIntString, requestParamOptional | requestParamLogStringAsInt},
  606. {"is_replay", isBooleanFlag, requestParamOptional | requestParamLogFlagAsBool},
  607. {"egress_region", isRegionCode, requestParamOptional},
  608. {"dial_duration", isIntString, requestParamOptional | requestParamLogStringAsInt},
  609. {"candidate_number", isIntString, requestParamOptional | requestParamLogStringAsInt},
  610. }
  611. func validateRequestParams(
  612. config *Config,
  613. params common.APIParameters,
  614. expectedParams []requestParamSpec) error {
  615. for _, expectedParam := range expectedParams {
  616. value := params[expectedParam.name]
  617. if value == nil {
  618. if expectedParam.flags&requestParamOptional != 0 {
  619. continue
  620. }
  621. return common.ContextError(
  622. fmt.Errorf("missing param: %s", expectedParam.name))
  623. }
  624. var err error
  625. switch {
  626. case expectedParam.flags&requestParamArray != 0:
  627. err = validateStringArrayRequestParam(config, expectedParam, value)
  628. case expectedParam.flags&requestParamJSON != 0:
  629. // No validation: the JSON already unmarshalled; the parameter
  630. // user will validate that the JSON contains the expected
  631. // objects/data.
  632. // TODO: without validation, any valid JSON will be logged
  633. // by getRequestLogFields, even if the parameter user validates
  634. // and rejects the parameter.
  635. default:
  636. err = validateStringRequestParam(config, expectedParam, value)
  637. }
  638. if err != nil {
  639. return common.ContextError(err)
  640. }
  641. }
  642. return nil
  643. }
  644. // copyBaseRequestParams makes a copy of the params which
  645. // includes only the baseRequestParams.
  646. func copyBaseRequestParams(params common.APIParameters) common.APIParameters {
  647. // Note: not a deep copy; assumes baseRequestParams values
  648. // are all scalar types (int, string, etc.)
  649. paramsCopy := make(common.APIParameters)
  650. for _, baseParam := range baseRequestParams {
  651. value := params[baseParam.name]
  652. if value == nil {
  653. continue
  654. }
  655. paramsCopy[baseParam.name] = value
  656. }
  657. return paramsCopy
  658. }
  659. func copyUpdateOnConnectedParams(params common.APIParameters) common.APIParameters {
  660. // Note: not a deep copy
  661. paramsCopy := make(common.APIParameters)
  662. for _, name := range updateOnConnectedParamNames {
  663. value := params[name]
  664. if value == nil {
  665. continue
  666. }
  667. paramsCopy[name] = value
  668. }
  669. return paramsCopy
  670. }
  671. func validateStringRequestParam(
  672. config *Config,
  673. expectedParam requestParamSpec,
  674. value interface{}) error {
  675. strValue, ok := value.(string)
  676. if !ok {
  677. return common.ContextError(
  678. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  679. }
  680. if !expectedParam.validator(config, strValue) {
  681. return common.ContextError(
  682. fmt.Errorf("invalid param: %s: %s", expectedParam.name, strValue))
  683. }
  684. return nil
  685. }
  686. func validateStringArrayRequestParam(
  687. config *Config,
  688. expectedParam requestParamSpec,
  689. value interface{}) error {
  690. arrayValue, ok := value.([]interface{})
  691. if !ok {
  692. return common.ContextError(
  693. fmt.Errorf("unexpected string param type: %s", expectedParam.name))
  694. }
  695. for _, value := range arrayValue {
  696. err := validateStringRequestParam(config, expectedParam, value)
  697. if err != nil {
  698. return common.ContextError(err)
  699. }
  700. }
  701. return nil
  702. }
  703. // getRequestLogFields makes LogFields to log the API event following
  704. // the legacy psi_web and current ELK naming conventions.
  705. func getRequestLogFields(
  706. eventName string,
  707. geoIPData GeoIPData,
  708. authorizedAccessTypes []string,
  709. params common.APIParameters,
  710. expectedParams []requestParamSpec) LogFields {
  711. logFields := make(LogFields)
  712. if eventName != "" {
  713. logFields["event_name"] = eventName
  714. }
  715. // In psi_web, the space replacement was done to accommodate space
  716. // delimited logging, which is no longer required; we retain the
  717. // transformation so that stats aggregation isn't impacted.
  718. logFields["client_region"] = strings.Replace(geoIPData.Country, " ", "_", -1)
  719. logFields["client_city"] = strings.Replace(geoIPData.City, " ", "_", -1)
  720. logFields["client_isp"] = strings.Replace(geoIPData.ISP, " ", "_", -1)
  721. if len(authorizedAccessTypes) > 0 {
  722. logFields["authorized_access_types"] = authorizedAccessTypes
  723. }
  724. if params == nil {
  725. return logFields
  726. }
  727. for _, expectedParam := range expectedParams {
  728. if expectedParam.flags&requestParamNotLogged != 0 {
  729. continue
  730. }
  731. var tunnelProtocol string
  732. if value, ok := params["relay_protocol"]; ok {
  733. tunnelProtocol, _ = value.(string)
  734. }
  735. if expectedParam.flags&requestParamLogOnlyForFrontedMeek != 0 &&
  736. !protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  737. continue
  738. }
  739. if expectedParam.flags&requestParamNotLoggedForUnfrontedMeekNonTransformedHeader != 0 &&
  740. protocol.TunnelProtocolUsesMeek(tunnelProtocol) &&
  741. !protocol.TunnelProtocolUsesFrontedMeek(tunnelProtocol) {
  742. // Non-HTTP unfronted meek protocols never tranform the host header.
  743. if protocol.TunnelProtocolUsesMeekHTTPS(tunnelProtocol) {
  744. continue
  745. }
  746. var transformedHostName string
  747. if value, ok := params["meek_transformed_host_name"]; ok {
  748. transformedHostName, _ = value.(string)
  749. }
  750. if transformedHostName != "1" {
  751. continue
  752. }
  753. }
  754. value := params[expectedParam.name]
  755. if value == nil {
  756. // Special case: older clients don't send this value,
  757. // so log a default.
  758. if expectedParam.name == "tunnel_whole_device" {
  759. value = "0"
  760. } else {
  761. // Skip omitted, optional params
  762. continue
  763. }
  764. }
  765. switch v := value.(type) {
  766. case string:
  767. strValue := v
  768. // Special cases:
  769. // - Number fields are encoded as integer types.
  770. // - For ELK performance we record certain domain-or-IP
  771. // fields as one of two different values based on type;
  772. // we also omit port from these host:port fields for now.
  773. // - Boolean fields that come into the api as "1"/"0"
  774. // must be logged as actual boolean values
  775. switch expectedParam.name {
  776. case "meek_dial_address":
  777. host, _, _ := net.SplitHostPort(strValue)
  778. if isIPAddress(nil, host) {
  779. logFields["meek_dial_ip_address"] = host
  780. } else {
  781. logFields["meek_dial_domain"] = host
  782. }
  783. case "upstream_proxy_type":
  784. // Submitted value could be e.g., "SOCKS5" or "socks5"; log lowercase
  785. logFields[expectedParam.name] = strings.ToLower(strValue)
  786. case tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME:
  787. // Due to a client bug, clients may deliever an incorrect ""
  788. // value for speed_test_samples via the web API protocol. Omit
  789. // the field in this case.
  790. default:
  791. if expectedParam.flags&requestParamLogStringAsInt != 0 {
  792. intValue, _ := strconv.Atoi(strValue)
  793. logFields[expectedParam.name] = intValue
  794. } else if expectedParam.flags&requestParamLogStringLengthAsInt != 0 {
  795. logFields[expectedParam.name] = len(strValue)
  796. } else if expectedParam.flags&requestParamLogFlagAsBool != 0 {
  797. // Submitted value could be "0" or "1"
  798. // "0" and non "0"/"1" values should be transformed to false
  799. // "1" should be transformed to true
  800. if strValue == "1" {
  801. logFields[expectedParam.name] = true
  802. } else {
  803. logFields[expectedParam.name] = false
  804. }
  805. } else {
  806. logFields[expectedParam.name] = strValue
  807. }
  808. }
  809. case []interface{}:
  810. if expectedParam.name == tactics.SPEED_TEST_SAMPLES_PARAMETER_NAME {
  811. logFields[expectedParam.name] = makeSpeedTestSamplesLogField(v)
  812. } else {
  813. logFields[expectedParam.name] = v
  814. }
  815. default:
  816. logFields[expectedParam.name] = v
  817. }
  818. }
  819. return logFields
  820. }
  821. // makeSpeedTestSamplesLogField renames the tactics.SpeedTestSample json tag
  822. // fields to more verbose names for metrics.
  823. func makeSpeedTestSamplesLogField(samples []interface{}) []interface{} {
  824. // TODO: use reflection and add additional tags, e.g.,
  825. // `json:"s" log:"timestamp"` to remove hard-coded
  826. // tag value dependency?
  827. logSamples := make([]interface{}, len(samples))
  828. for i, sample := range samples {
  829. logSample := make(map[string]interface{})
  830. if m, ok := sample.(map[string]interface{}); ok {
  831. for k, v := range m {
  832. logK := k
  833. switch k {
  834. case "s":
  835. logK = "timestamp"
  836. case "r":
  837. logK = "server_region"
  838. case "p":
  839. logK = "relay_protocol"
  840. case "t":
  841. logK = "round_trip_time_ms"
  842. case "u":
  843. logK = "bytes_up"
  844. case "d":
  845. logK = "bytes_down"
  846. }
  847. logSample[logK] = v
  848. }
  849. }
  850. logSamples[i] = logSample
  851. }
  852. return logSamples
  853. }
  854. func getStringRequestParam(params common.APIParameters, name string) (string, error) {
  855. if params[name] == nil {
  856. return "", common.ContextError(fmt.Errorf("missing param: %s", name))
  857. }
  858. value, ok := params[name].(string)
  859. if !ok {
  860. return "", common.ContextError(fmt.Errorf("invalid param: %s", name))
  861. }
  862. return value, nil
  863. }
  864. func getInt64RequestParam(params common.APIParameters, name string) (int64, error) {
  865. if params[name] == nil {
  866. return 0, common.ContextError(fmt.Errorf("missing param: %s", name))
  867. }
  868. value, ok := params[name].(float64)
  869. if !ok {
  870. return 0, common.ContextError(fmt.Errorf("invalid param: %s", name))
  871. }
  872. return int64(value), nil
  873. }
  874. func getPaddingSizeRequestParam(params common.APIParameters, name string) (int, error) {
  875. value, err := getInt64RequestParam(params, name)
  876. if err != nil {
  877. return 0, common.ContextError(err)
  878. }
  879. if value < 0 {
  880. value = 0
  881. }
  882. if value > PADDING_MAX_BYTES {
  883. value = PADDING_MAX_BYTES
  884. }
  885. return int(value), nil
  886. }
  887. func getJSONObjectRequestParam(params common.APIParameters, name string) (common.APIParameters, error) {
  888. if params[name] == nil {
  889. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  890. }
  891. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  892. value, ok := params[name].(map[string]interface{})
  893. if !ok {
  894. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  895. }
  896. return common.APIParameters(value), nil
  897. }
  898. func getJSONObjectArrayRequestParam(params common.APIParameters, name string) ([]common.APIParameters, error) {
  899. if params[name] == nil {
  900. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  901. }
  902. value, ok := params[name].([]interface{})
  903. if !ok {
  904. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  905. }
  906. result := make([]common.APIParameters, len(value))
  907. for i, item := range value {
  908. // Note: generic unmarshal of JSON produces map[string]interface{}, not common.APIParameters
  909. resultItem, ok := item.(map[string]interface{})
  910. if !ok {
  911. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  912. }
  913. result[i] = common.APIParameters(resultItem)
  914. }
  915. return result, nil
  916. }
  917. func getMapStringInt64RequestParam(params common.APIParameters, name string) (map[string]int64, error) {
  918. if params[name] == nil {
  919. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  920. }
  921. // TODO: can't use common.APIParameters type?
  922. value, ok := params[name].(map[string]interface{})
  923. if !ok {
  924. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  925. }
  926. result := make(map[string]int64)
  927. for k, v := range value {
  928. numValue, ok := v.(float64)
  929. if !ok {
  930. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  931. }
  932. result[k] = int64(numValue)
  933. }
  934. return result, nil
  935. }
  936. func getStringArrayRequestParam(params common.APIParameters, name string) ([]string, error) {
  937. if params[name] == nil {
  938. return nil, common.ContextError(fmt.Errorf("missing param: %s", name))
  939. }
  940. value, ok := params[name].([]interface{})
  941. if !ok {
  942. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  943. }
  944. result := make([]string, len(value))
  945. for i, v := range value {
  946. strValue, ok := v.(string)
  947. if !ok {
  948. return nil, common.ContextError(fmt.Errorf("invalid param: %s", name))
  949. }
  950. result[i] = strValue
  951. }
  952. return result, nil
  953. }
  954. // Normalize reported client platform. Android clients, for example, report
  955. // OS version, rooted status, and Google Play build status in the clientPlatform
  956. // string along with "Android".
  957. func normalizeClientPlatform(clientPlatform string) string {
  958. if strings.Contains(strings.ToLower(clientPlatform), strings.ToLower(CLIENT_PLATFORM_ANDROID)) {
  959. return CLIENT_PLATFORM_ANDROID
  960. } else if strings.HasPrefix(clientPlatform, CLIENT_PLATFORM_IOS) {
  961. return CLIENT_PLATFORM_IOS
  962. }
  963. return CLIENT_PLATFORM_WINDOWS
  964. }
  965. func isAnyString(config *Config, value string) bool {
  966. return true
  967. }
  968. func isMobileClientPlatform(clientPlatform string) bool {
  969. normalizedClientPlatform := normalizeClientPlatform(clientPlatform)
  970. return normalizedClientPlatform == CLIENT_PLATFORM_ANDROID ||
  971. normalizedClientPlatform == CLIENT_PLATFORM_IOS
  972. }
  973. // Input validators follow the legacy validations rules in psi_web.
  974. func isServerSecret(config *Config, value string) bool {
  975. return subtle.ConstantTimeCompare(
  976. []byte(value),
  977. []byte(config.WebServerSecret)) == 1
  978. }
  979. func isHexDigits(_ *Config, value string) bool {
  980. // Allows both uppercase in addition to lowercase, for legacy support.
  981. return -1 == strings.IndexFunc(value, func(c rune) bool {
  982. return !unicode.Is(unicode.ASCII_Hex_Digit, c)
  983. })
  984. }
  985. func isDigits(_ *Config, value string) bool {
  986. return -1 == strings.IndexFunc(value, func(c rune) bool {
  987. return c < '0' || c > '9'
  988. })
  989. }
  990. func isIntString(_ *Config, value string) bool {
  991. _, err := strconv.Atoi(value)
  992. return err == nil
  993. }
  994. func isClientPlatform(_ *Config, value string) bool {
  995. return -1 == strings.IndexFunc(value, func(c rune) bool {
  996. // Note: stricter than psi_web's Python string.whitespace
  997. return unicode.Is(unicode.White_Space, c)
  998. })
  999. }
  1000. func isRelayProtocol(_ *Config, value string) bool {
  1001. return common.Contains(protocol.SupportedTunnelProtocols, value)
  1002. }
  1003. func isBooleanFlag(_ *Config, value string) bool {
  1004. return value == "0" || value == "1"
  1005. }
  1006. func isUpstreamProxyType(_ *Config, value string) bool {
  1007. value = strings.ToLower(value)
  1008. return value == "http" || value == "socks5" || value == "socks4a"
  1009. }
  1010. func isRegionCode(_ *Config, value string) bool {
  1011. if len(value) != 2 {
  1012. return false
  1013. }
  1014. return -1 == strings.IndexFunc(value, func(c rune) bool {
  1015. return c < 'A' || c > 'Z'
  1016. })
  1017. }
  1018. func isDialAddress(_ *Config, value string) bool {
  1019. // "<host>:<port>", where <host> is a domain or IP address
  1020. parts := strings.Split(value, ":")
  1021. if len(parts) != 2 {
  1022. return false
  1023. }
  1024. if !isIPAddress(nil, parts[0]) && !isDomain(nil, parts[0]) {
  1025. return false
  1026. }
  1027. if !isDigits(nil, parts[1]) {
  1028. return false
  1029. }
  1030. port, err := strconv.Atoi(parts[1])
  1031. if err != nil {
  1032. return false
  1033. }
  1034. return port > 0 && port < 65536
  1035. }
  1036. func isIPAddress(_ *Config, value string) bool {
  1037. return net.ParseIP(value) != nil
  1038. }
  1039. var isDomainRegex = regexp.MustCompile("[a-zA-Z\\d-]{1,63}$")
  1040. func isDomain(_ *Config, value string) bool {
  1041. // From: http://stackoverflow.com/questions/2532053/validate-a-hostname-string
  1042. //
  1043. // "ensures that each segment
  1044. // * contains at least one character and a maximum of 63 characters
  1045. // * consists only of allowed characters
  1046. // * doesn't begin or end with a hyphen"
  1047. //
  1048. if len(value) > 255 {
  1049. return false
  1050. }
  1051. value = strings.TrimSuffix(value, ".")
  1052. for _, part := range strings.Split(value, ".") {
  1053. // Note: regexp doesn't support the following Perl expression which
  1054. // would check for '-' prefix/suffix: "(?!-)[a-zA-Z\\d-]{1,63}(?<!-)$"
  1055. if strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
  1056. return false
  1057. }
  1058. if !isDomainRegex.Match([]byte(part)) {
  1059. return false
  1060. }
  1061. }
  1062. return true
  1063. }
  1064. func isHostHeader(_ *Config, value string) bool {
  1065. // "<host>:<port>", where <host> is a domain or IP address and ":<port>" is optional
  1066. if strings.Contains(value, ":") {
  1067. return isDialAddress(nil, value)
  1068. }
  1069. return isIPAddress(nil, value) || isDomain(nil, value)
  1070. }
  1071. func isServerEntrySource(_ *Config, value string) bool {
  1072. return common.Contains(protocol.SupportedServerEntrySources, value)
  1073. }
  1074. var isISO8601DateRegex = regexp.MustCompile(
  1075. "(?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})))")
  1076. func isISO8601Date(_ *Config, value string) bool {
  1077. return isISO8601DateRegex.Match([]byte(value))
  1078. }
  1079. func isLastConnected(_ *Config, value string) bool {
  1080. return value == "None" || value == "Unknown" || isISO8601Date(nil, value)
  1081. }