notice.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright (c) 2015, 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 psiphon
  20. import (
  21. "bytes"
  22. "encoding/json"
  23. "fmt"
  24. "io"
  25. "os"
  26. "sort"
  27. "strings"
  28. "sync"
  29. "sync/atomic"
  30. "time"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/stacktrace"
  37. )
  38. type noticeLogger struct {
  39. emitDiagnostics int32
  40. emitNetworkParameters int32
  41. mutex sync.Mutex
  42. writer io.Writer
  43. homepageFilename string
  44. homepageFile *os.File
  45. rotatingFilename string
  46. rotatingOlderFilename string
  47. rotatingFile *os.File
  48. rotatingFileSize int64
  49. rotatingCurrentFileSize int64
  50. rotatingSyncFrequency int
  51. rotatingCurrentNoticeCount int
  52. }
  53. var singletonNoticeLogger = noticeLogger{
  54. writer: os.Stderr,
  55. }
  56. // SetEmitDiagnosticNotices toggles whether diagnostic notices are emitted;
  57. // and whether to include circumvention network parameters in diagnostics.
  58. //
  59. // Diagnostic notices contain potentially sensitive user information; and
  60. // sensitive circumvention network parameters, when enabled. Only enable this
  61. // in environments where notices are handled securely (for example, don't
  62. // include these notices in log files which users could post to public
  63. // forums).
  64. func SetEmitDiagnosticNotices(
  65. emitDiagnostics bool, emitNetworkParameters bool) {
  66. if emitDiagnostics {
  67. atomic.StoreInt32(&singletonNoticeLogger.emitDiagnostics, 1)
  68. } else {
  69. atomic.StoreInt32(&singletonNoticeLogger.emitDiagnostics, 0)
  70. }
  71. if emitNetworkParameters {
  72. atomic.StoreInt32(&singletonNoticeLogger.emitNetworkParameters, 1)
  73. } else {
  74. atomic.StoreInt32(&singletonNoticeLogger.emitNetworkParameters, 0)
  75. }
  76. }
  77. // GetEmitDiagnosticNotices returns the current state
  78. // of emitting diagnostic notices.
  79. func GetEmitDiagnosticNotices() bool {
  80. return atomic.LoadInt32(&singletonNoticeLogger.emitDiagnostics) == 1
  81. }
  82. // GetEmitNetworkParameters returns the current state
  83. // of emitting network parameters.
  84. func GetEmitNetworkParameters() bool {
  85. return atomic.LoadInt32(&singletonNoticeLogger.emitNetworkParameters) == 1
  86. }
  87. // SetNoticeWriter sets a target writer to receive notices. By default,
  88. // notices are written to stderr. Notices are newline delimited.
  89. //
  90. // writer specifies an alternate io.Writer where notices are to be written.
  91. //
  92. // Notices are encoded in JSON. Here's an example:
  93. //
  94. // {"data":{"message":"shutdown operate tunnel"},"noticeType":"Info","timestamp":"2006-01-02T15:04:05.999999999Z07:00"}
  95. //
  96. // All notices have the following fields:
  97. // - "noticeType": the type of notice, which indicates the meaning of the notice along with what's in the data payload.
  98. // - "data": additional structured data payload. For example, the "ListeningSocksProxyPort" notice type has a "port" integer
  99. // data in its payload.
  100. // - "timestamp": UTC timezone, RFC3339Milli format timestamp for notice event
  101. //
  102. // See the Notice* functions for details on each notice meaning and payload.
  103. //
  104. func SetNoticeWriter(writer io.Writer) {
  105. singletonNoticeLogger.mutex.Lock()
  106. defer singletonNoticeLogger.mutex.Unlock()
  107. singletonNoticeLogger.writer = writer
  108. }
  109. // setNoticeFiles configures files for notice writing.
  110. //
  111. // - When homepageFilename is not "", homepages are written to the specified file
  112. // and omitted from the writer. The file may be read after the Tunnels notice
  113. // with count of 1. The file should be opened read-only for reading.
  114. //
  115. // - When rotatingFilename is not "", all notices are are written to the specified
  116. // file. Diagnostic notices are omitted from the writer. The file is rotated
  117. // when its size exceeds rotatingFileSize. One rotated older file,
  118. // <rotatingFilename>.1, is retained. The files may be read at any time; and
  119. // should be opened read-only for reading. rotatingSyncFrequency specifies how
  120. // many notices are written before syncing the file.
  121. // If either rotatingFileSize or rotatingSyncFrequency are <= 0, default values
  122. // are used.
  123. //
  124. // - If an error occurs when writing to a file, an InternalError notice is emitted to
  125. // the writer.
  126. //
  127. // setNoticeFiles closes open homepage or rotating files before applying the new
  128. // configuration.
  129. //
  130. func setNoticeFiles(
  131. homepageFilename string,
  132. rotatingFilename string,
  133. rotatingFileSize int,
  134. rotatingSyncFrequency int) error {
  135. singletonNoticeLogger.mutex.Lock()
  136. defer singletonNoticeLogger.mutex.Unlock()
  137. if homepageFilename != "" {
  138. var err error
  139. if singletonNoticeLogger.homepageFile != nil {
  140. singletonNoticeLogger.homepageFile.Close()
  141. }
  142. singletonNoticeLogger.homepageFile, err = os.OpenFile(
  143. homepageFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
  144. if err != nil {
  145. return errors.Trace(err)
  146. }
  147. singletonNoticeLogger.homepageFilename = homepageFilename
  148. }
  149. if rotatingFilename != "" {
  150. var err error
  151. if singletonNoticeLogger.rotatingFile != nil {
  152. singletonNoticeLogger.rotatingFile.Close()
  153. }
  154. singletonNoticeLogger.rotatingFile, err = os.OpenFile(
  155. rotatingFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  156. if err != nil {
  157. return errors.Trace(err)
  158. }
  159. fileInfo, err := singletonNoticeLogger.rotatingFile.Stat()
  160. if err != nil {
  161. return errors.Trace(err)
  162. }
  163. if rotatingFileSize <= 0 {
  164. rotatingFileSize = 1 << 20
  165. }
  166. if rotatingSyncFrequency <= 0 {
  167. rotatingSyncFrequency = 100
  168. }
  169. singletonNoticeLogger.rotatingFilename = rotatingFilename
  170. singletonNoticeLogger.rotatingOlderFilename = rotatingFilename + ".1"
  171. singletonNoticeLogger.rotatingFileSize = int64(rotatingFileSize)
  172. singletonNoticeLogger.rotatingCurrentFileSize = fileInfo.Size()
  173. singletonNoticeLogger.rotatingSyncFrequency = rotatingSyncFrequency
  174. singletonNoticeLogger.rotatingCurrentNoticeCount = 0
  175. }
  176. return nil
  177. }
  178. const (
  179. noticeIsDiagnostic = 1
  180. noticeIsHomepage = 2
  181. noticeClearHomepages = 4
  182. noticeSyncHomepages = 8
  183. )
  184. // outputNotice encodes a notice in JSON and writes it to the output writer.
  185. func (nl *noticeLogger) outputNotice(noticeType string, noticeFlags uint32, args ...interface{}) {
  186. if (noticeFlags&noticeIsDiagnostic != 0) && !GetEmitDiagnosticNotices() {
  187. return
  188. }
  189. obj := make(map[string]interface{})
  190. noticeData := make(map[string]interface{})
  191. obj["noticeType"] = noticeType
  192. obj["data"] = noticeData
  193. obj["timestamp"] = time.Now().UTC().Format(common.RFC3339Milli)
  194. for i := 0; i < len(args)-1; i += 2 {
  195. name, ok := args[i].(string)
  196. value := args[i+1]
  197. if ok {
  198. noticeData[name] = value
  199. }
  200. }
  201. encodedJson, err := json.Marshal(obj)
  202. var output []byte
  203. if err == nil {
  204. output = append(encodedJson, byte('\n'))
  205. } else {
  206. // Try to emit a properly formatted notice that the outer client can report.
  207. // One scenario where this is useful is if the preceding Marshal fails due to
  208. // bad data in the args. This has happened for a json.RawMessage field.
  209. output = makeNoticeInternalError(
  210. fmt.Sprintf("marshal notice failed: %s", errors.Trace(err)))
  211. }
  212. // Ensure direct server IPs are not exposed in notices. The "net" package,
  213. // and possibly other 3rd party packages, will include destination addresses
  214. // in I/O error messages.
  215. output = StripIPAddresses(output)
  216. nl.mutex.Lock()
  217. defer nl.mutex.Unlock()
  218. skipWriter := false
  219. if nl.homepageFile != nil &&
  220. (noticeFlags&noticeIsHomepage != 0) {
  221. skipWriter = true
  222. err := nl.outputNoticeToHomepageFile(noticeFlags, output)
  223. if err != nil {
  224. output := makeNoticeInternalError(
  225. fmt.Sprintf("write homepage file failed: %s", err))
  226. nl.writer.Write(output)
  227. }
  228. }
  229. if nl.rotatingFile != nil {
  230. if !skipWriter {
  231. skipWriter = (noticeFlags&noticeIsDiagnostic != 0)
  232. }
  233. err := nl.outputNoticeToRotatingFile(output)
  234. if err != nil {
  235. output := makeNoticeInternalError(
  236. fmt.Sprintf("write rotating file failed: %s", err))
  237. nl.writer.Write(output)
  238. }
  239. }
  240. if !skipWriter {
  241. _, _ = nl.writer.Write(output)
  242. }
  243. }
  244. // NoticeInteralError is an error formatting or writing notices.
  245. // A NoticeInteralError handler must not call a Notice function.
  246. func makeNoticeInternalError(errorMessage string) []byte {
  247. // Format an Alert Notice (_without_ using json.Marshal, since that can fail)
  248. alertNoticeFormat := "{\"noticeType\":\"InternalError\",\"timestamp\":\"%s\",\"data\":{\"message\":\"%s\"}}\n"
  249. return []byte(fmt.Sprintf(alertNoticeFormat, time.Now().UTC().Format(common.RFC3339Milli), errorMessage))
  250. }
  251. func (nl *noticeLogger) outputNoticeToHomepageFile(noticeFlags uint32, output []byte) error {
  252. if (noticeFlags & noticeClearHomepages) != 0 {
  253. err := nl.homepageFile.Truncate(0)
  254. if err != nil {
  255. return errors.Trace(err)
  256. }
  257. _, err = nl.homepageFile.Seek(0, 0)
  258. if err != nil {
  259. return errors.Trace(err)
  260. }
  261. }
  262. _, err := nl.homepageFile.Write(output)
  263. if err != nil {
  264. return errors.Trace(err)
  265. }
  266. if (noticeFlags & noticeSyncHomepages) != 0 {
  267. err = nl.homepageFile.Sync()
  268. if err != nil {
  269. return errors.Trace(err)
  270. }
  271. }
  272. return nil
  273. }
  274. func (nl *noticeLogger) outputNoticeToRotatingFile(output []byte) error {
  275. nl.rotatingCurrentFileSize += int64(len(output) + 1)
  276. if nl.rotatingCurrentFileSize >= nl.rotatingFileSize {
  277. // Note: all errors are fatal in order to preserve the
  278. // rotatingFileSize limit; e.g., no attempt is made to
  279. // continue writing to the file if it can't be rotated.
  280. err := nl.rotatingFile.Sync()
  281. if err != nil {
  282. return errors.Trace(err)
  283. }
  284. err = nl.rotatingFile.Close()
  285. if err != nil {
  286. return errors.Trace(err)
  287. }
  288. err = os.Rename(nl.rotatingFilename, nl.rotatingOlderFilename)
  289. if err != nil {
  290. return errors.Trace(err)
  291. }
  292. nl.rotatingFile, err = os.OpenFile(
  293. nl.rotatingFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
  294. if err != nil {
  295. return errors.Trace(err)
  296. }
  297. nl.rotatingCurrentFileSize = 0
  298. }
  299. _, err := nl.rotatingFile.Write(output)
  300. if err != nil {
  301. return errors.Trace(err)
  302. }
  303. nl.rotatingCurrentNoticeCount += 1
  304. if nl.rotatingCurrentNoticeCount >= nl.rotatingSyncFrequency {
  305. nl.rotatingCurrentNoticeCount = 0
  306. err = nl.rotatingFile.Sync()
  307. if err != nil {
  308. return errors.Trace(err)
  309. }
  310. }
  311. return nil
  312. }
  313. // NoticeInfo is an informational message
  314. func NoticeInfo(format string, args ...interface{}) {
  315. singletonNoticeLogger.outputNotice(
  316. "Info", noticeIsDiagnostic,
  317. "message", fmt.Sprintf(format, args...))
  318. }
  319. // NoticeWarning is a warning message; typically a recoverable error condition
  320. func NoticeWarning(format string, args ...interface{}) {
  321. singletonNoticeLogger.outputNotice(
  322. "Warning", noticeIsDiagnostic,
  323. "message", fmt.Sprintf(format, args...))
  324. }
  325. // NoticeError is an error message; typically an unrecoverable error condition
  326. func NoticeError(format string, args ...interface{}) {
  327. singletonNoticeLogger.outputNotice(
  328. "Error", noticeIsDiagnostic,
  329. "message", fmt.Sprintf(format, args...))
  330. }
  331. // NoticeUserLog is a log message from the outer client user of tunnel-core
  332. func NoticeUserLog(message string) {
  333. singletonNoticeLogger.outputNotice(
  334. "UserLog", noticeIsDiagnostic,
  335. "message", message)
  336. }
  337. // NoticeCandidateServers is how many possible servers are available for the selected region and protocols
  338. func NoticeCandidateServers(
  339. region string,
  340. constraints *protocolSelectionConstraints,
  341. initialCount int,
  342. count int) {
  343. singletonNoticeLogger.outputNotice(
  344. "CandidateServers", noticeIsDiagnostic,
  345. "region", region,
  346. "initialLimitTunnelProtocols", constraints.initialLimitProtocols,
  347. "initialLimitTunnelProtocolsCandidateCount", constraints.initialLimitProtocolsCandidateCount,
  348. "limitTunnelProtocols", constraints.limitProtocols,
  349. "replayCandidateCount", constraints.replayCandidateCount,
  350. "initialCount", initialCount,
  351. "count", count)
  352. }
  353. // NoticeAvailableEgressRegions is what regions are available for egress from.
  354. // Consecutive reports of the same list of regions are suppressed.
  355. func NoticeAvailableEgressRegions(regions []string) {
  356. sortedRegions := append([]string(nil), regions...)
  357. sort.Strings(sortedRegions)
  358. repetitionMessage := strings.Join(sortedRegions, "")
  359. outputRepetitiveNotice(
  360. "AvailableEgressRegions", repetitionMessage, 0,
  361. "AvailableEgressRegions", 0, "regions", sortedRegions)
  362. }
  363. func noticeWithDialParameters(noticeType string, dialParams *DialParameters) {
  364. args := []interface{}{
  365. "diagnosticID", dialParams.ServerEntry.GetDiagnosticID(),
  366. "region", dialParams.ServerEntry.Region,
  367. "protocol", dialParams.TunnelProtocol,
  368. "isReplay", dialParams.IsReplay,
  369. "candidateNumber", dialParams.CandidateNumber,
  370. "establishedTunnelsCount", dialParams.EstablishedTunnelsCount,
  371. "networkType", dialParams.GetNetworkType(),
  372. }
  373. if GetEmitNetworkParameters() {
  374. if dialParams.BPFProgramName != "" {
  375. args = append(args, "client_bpf", dialParams.BPFProgramName)
  376. }
  377. if dialParams.SelectedSSHClientVersion {
  378. args = append(args, "SSHClientVersion", dialParams.SSHClientVersion)
  379. }
  380. if dialParams.UpstreamProxyType != "" {
  381. args = append(args, "upstreamProxyType", dialParams.UpstreamProxyType)
  382. }
  383. if dialParams.UpstreamProxyCustomHeaderNames != nil {
  384. args = append(args, "upstreamProxyCustomHeaderNames", strings.Join(dialParams.UpstreamProxyCustomHeaderNames, ","))
  385. }
  386. if dialParams.FrontingProviderID != "" {
  387. args = append(args, "frontingProviderID", dialParams.FrontingProviderID)
  388. }
  389. if dialParams.MeekDialAddress != "" {
  390. args = append(args, "meekDialAddress", dialParams.MeekDialAddress)
  391. }
  392. meekResolvedIPAddress := dialParams.MeekResolvedIPAddress.Load().(string)
  393. if meekResolvedIPAddress != "" {
  394. args = append(args, "meekResolvedIPAddress", meekResolvedIPAddress)
  395. }
  396. if dialParams.MeekSNIServerName != "" {
  397. args = append(args, "meekSNIServerName", dialParams.MeekSNIServerName)
  398. }
  399. if dialParams.MeekHostHeader != "" {
  400. args = append(args, "meekHostHeader", dialParams.MeekHostHeader)
  401. }
  402. // MeekTransformedHostName is meaningful when meek is used, which is when MeekDialAddress != ""
  403. if dialParams.MeekDialAddress != "" {
  404. args = append(args, "meekTransformedHostName", dialParams.MeekTransformedHostName)
  405. }
  406. if dialParams.SelectedUserAgent {
  407. args = append(args, "userAgent", dialParams.UserAgent)
  408. }
  409. if dialParams.SelectedTLSProfile {
  410. args = append(args, "TLSProfile", dialParams.TLSProfile)
  411. args = append(args, "TLSVersion", dialParams.GetTLSVersionForMetrics())
  412. }
  413. if dialParams.DialPortNumber != "" {
  414. args = append(args, "dialPortNumber", dialParams.DialPortNumber)
  415. }
  416. if dialParams.QUICVersion != "" {
  417. args = append(args, "QUICVersion", dialParams.QUICVersion)
  418. }
  419. if dialParams.QUICDialSNIAddress != "" {
  420. args = append(args, "QUICDialSNIAddress", dialParams.QUICDialSNIAddress)
  421. }
  422. if dialParams.DialDuration > 0 {
  423. args = append(args, "dialDuration", dialParams.DialDuration)
  424. }
  425. if dialParams.NetworkLatencyMultiplier != 0.0 {
  426. args = append(args, "networkLatencyMultiplier", dialParams.NetworkLatencyMultiplier)
  427. }
  428. if dialParams.DialConnMetrics != nil {
  429. metrics := dialParams.DialConnMetrics.GetMetrics()
  430. for name, value := range metrics {
  431. args = append(args, name, value)
  432. }
  433. }
  434. if dialParams.ObfuscatedSSHConnMetrics != nil {
  435. metrics := dialParams.ObfuscatedSSHConnMetrics.GetMetrics()
  436. for name, value := range metrics {
  437. args = append(args, name, value)
  438. }
  439. }
  440. }
  441. singletonNoticeLogger.outputNotice(
  442. noticeType, noticeIsDiagnostic,
  443. args...)
  444. }
  445. // NoticeConnectingServer reports parameters and details for a single connection attempt
  446. func NoticeConnectingServer(dialParams *DialParameters) {
  447. noticeWithDialParameters("ConnectingServer", dialParams)
  448. }
  449. // NoticeConnectedServer reports parameters and details for a single successful connection
  450. func NoticeConnectedServer(dialParams *DialParameters) {
  451. noticeWithDialParameters("ConnectedServer", dialParams)
  452. }
  453. // NoticeRequestingTactics reports parameters and details for a tactics request attempt
  454. func NoticeRequestingTactics(dialParams *DialParameters) {
  455. noticeWithDialParameters("RequestingTactics", dialParams)
  456. }
  457. // NoticeRequestedTactics reports parameters and details for a successful tactics request
  458. func NoticeRequestedTactics(dialParams *DialParameters) {
  459. noticeWithDialParameters("RequestedTactics", dialParams)
  460. }
  461. // NoticeActiveTunnel is a successful connection that is used as an active tunnel for port forwarding
  462. func NoticeActiveTunnel(diagnosticID, protocol string, isTCS bool) {
  463. singletonNoticeLogger.outputNotice(
  464. "ActiveTunnel", noticeIsDiagnostic,
  465. "diagnosticID", diagnosticID,
  466. "protocol", protocol,
  467. "isTCS", isTCS)
  468. }
  469. // NoticeSocksProxyPortInUse is a failure to use the configured LocalSocksProxyPort
  470. func NoticeSocksProxyPortInUse(port int) {
  471. singletonNoticeLogger.outputNotice(
  472. "SocksProxyPortInUse", 0,
  473. "port", port)
  474. }
  475. // NoticeListeningSocksProxyPort is the selected port for the listening local SOCKS proxy
  476. func NoticeListeningSocksProxyPort(port int) {
  477. singletonNoticeLogger.outputNotice(
  478. "ListeningSocksProxyPort", 0,
  479. "port", port)
  480. }
  481. // NoticeHttpProxyPortInUse is a failure to use the configured LocalHttpProxyPort
  482. func NoticeHttpProxyPortInUse(port int) {
  483. singletonNoticeLogger.outputNotice(
  484. "HttpProxyPortInUse", 0,
  485. "port", port)
  486. }
  487. // NoticeListeningHttpProxyPort is the selected port for the listening local HTTP proxy
  488. func NoticeListeningHttpProxyPort(port int) {
  489. singletonNoticeLogger.outputNotice(
  490. "ListeningHttpProxyPort", 0,
  491. "port", port)
  492. }
  493. // NoticeClientUpgradeAvailable is an available client upgrade, as per the handshake. The
  494. // client should download and install an upgrade.
  495. func NoticeClientUpgradeAvailable(version string) {
  496. singletonNoticeLogger.outputNotice(
  497. "ClientUpgradeAvailable", 0,
  498. "version", version)
  499. }
  500. // NoticeClientIsLatestVersion reports that an upgrade check was made and the client
  501. // is already the latest version. availableVersion is the version available for download,
  502. // if known.
  503. func NoticeClientIsLatestVersion(availableVersion string) {
  504. singletonNoticeLogger.outputNotice(
  505. "ClientIsLatestVersion", 0,
  506. "availableVersion", availableVersion)
  507. }
  508. // NoticeHomepages emits a series of NoticeHomepage, the sponsor homepages. The client
  509. // should display the sponsor's homepages.
  510. func NoticeHomepages(urls []string) {
  511. for i, url := range urls {
  512. noticeFlags := uint32(noticeIsHomepage)
  513. if i == 0 {
  514. noticeFlags |= noticeClearHomepages
  515. }
  516. if i == len(urls)-1 {
  517. noticeFlags |= noticeSyncHomepages
  518. }
  519. singletonNoticeLogger.outputNotice(
  520. "Homepage", noticeFlags,
  521. "url", url)
  522. }
  523. }
  524. // NoticeClientRegion is the client's region, as determined by the server and
  525. // reported to the client in the handshake.
  526. func NoticeClientRegion(region string) {
  527. singletonNoticeLogger.outputNotice(
  528. "ClientRegion", 0,
  529. "region", region)
  530. }
  531. // NoticeTunnels is how many active tunnels are available. The client should use this to
  532. // determine connecting/unexpected disconnect state transitions. When count is 0, the core is
  533. // disconnected; when count > 1, the core is connected.
  534. func NoticeTunnels(count int) {
  535. singletonNoticeLogger.outputNotice(
  536. "Tunnels", 0,
  537. "count", count)
  538. }
  539. // NoticeSessionId is the session ID used across all tunnels established by the controller.
  540. func NoticeSessionId(sessionId string) {
  541. singletonNoticeLogger.outputNotice(
  542. "SessionId", noticeIsDiagnostic,
  543. "sessionId", sessionId)
  544. }
  545. // NoticeUntunneled indicates than an address has been classified as untunneled and is being
  546. // accessed directly.
  547. //
  548. // Note: "address" should remain private; this notice should only be used for alerting
  549. // users, not for diagnostics logs.
  550. //
  551. func NoticeUntunneled(address string) {
  552. singletonNoticeLogger.outputNotice(
  553. "Untunneled", 0,
  554. "address", address)
  555. }
  556. // NoticeSplitTunnelRegion reports that split tunnel is on for the given region.
  557. func NoticeSplitTunnelRegion(region string) {
  558. singletonNoticeLogger.outputNotice(
  559. "SplitTunnelRegion", 0,
  560. "region", region)
  561. }
  562. // NoticeUpstreamProxyError reports an error when connecting to an upstream proxy. The
  563. // user may have input, for example, an incorrect address or incorrect credentials.
  564. func NoticeUpstreamProxyError(err error) {
  565. message := err.Error()
  566. outputRepetitiveNotice(
  567. "UpstreamProxyError", message, 0,
  568. "UpstreamProxyError", 0,
  569. "message", message)
  570. }
  571. // NoticeClientUpgradeDownloadedBytes reports client upgrade download progress.
  572. func NoticeClientUpgradeDownloadedBytes(bytes int64) {
  573. singletonNoticeLogger.outputNotice(
  574. "ClientUpgradeDownloadedBytes", noticeIsDiagnostic,
  575. "bytes", bytes)
  576. }
  577. // NoticeClientUpgradeDownloaded indicates that a client upgrade download
  578. // is complete and available at the destination specified.
  579. func NoticeClientUpgradeDownloaded(filename string) {
  580. singletonNoticeLogger.outputNotice(
  581. "ClientUpgradeDownloaded", 0,
  582. "filename", filename)
  583. }
  584. // NoticeBytesTransferred reports how many tunneled bytes have been
  585. // transferred since the last NoticeBytesTransferred. This is not a diagnostic
  586. // notice: the user app has requested this notice with EmitBytesTransferred
  587. // for functionality such as traffic display; and this frequent notice is not
  588. // intended to be included with feedback.
  589. func NoticeBytesTransferred(diagnosticID string, sent, received int64) {
  590. singletonNoticeLogger.outputNotice(
  591. "BytesTransferred", 0,
  592. "diagnosticID", diagnosticID,
  593. "sent", sent,
  594. "received", received)
  595. }
  596. // NoticeTotalBytesTransferred reports how many tunneled bytes have been
  597. // transferred in total up to this point. This is a diagnostic notice.
  598. func NoticeTotalBytesTransferred(diagnosticID string, sent, received int64) {
  599. singletonNoticeLogger.outputNotice(
  600. "TotalBytesTransferred", noticeIsDiagnostic,
  601. "diagnosticID", diagnosticID,
  602. "sent", sent,
  603. "received", received)
  604. }
  605. // NoticeLocalProxyError reports a local proxy error message. Repetitive
  606. // errors for a given proxy type are suppressed.
  607. func NoticeLocalProxyError(proxyType string, err error) {
  608. // For repeats, only consider the base error message, which is
  609. // the root error that repeats (the full error often contains
  610. // different specific values, e.g., local port numbers, but
  611. // the same repeating root).
  612. // Assumes error format of errors.Trace.
  613. repetitionMessage := err.Error()
  614. index := strings.LastIndex(repetitionMessage, ": ")
  615. if index != -1 {
  616. repetitionMessage = repetitionMessage[index+2:]
  617. }
  618. outputRepetitiveNotice(
  619. "LocalProxyError-"+proxyType, repetitionMessage, 1,
  620. "LocalProxyError", noticeIsDiagnostic,
  621. "message", err.Error())
  622. }
  623. // NoticeBuildInfo reports build version info.
  624. func NoticeBuildInfo() {
  625. singletonNoticeLogger.outputNotice(
  626. "BuildInfo", noticeIsDiagnostic,
  627. "buildInfo", buildinfo.GetBuildInfo())
  628. }
  629. // NoticeExiting indicates that tunnel-core is exiting imminently.
  630. func NoticeExiting() {
  631. singletonNoticeLogger.outputNotice(
  632. "Exiting", 0)
  633. }
  634. // NoticeRemoteServerListResourceDownloadedBytes reports remote server list download progress.
  635. func NoticeRemoteServerListResourceDownloadedBytes(url string, bytes int64, duration time.Duration) {
  636. if !GetEmitNetworkParameters() {
  637. url = "[redacted]"
  638. }
  639. singletonNoticeLogger.outputNotice(
  640. "RemoteServerListResourceDownloadedBytes", noticeIsDiagnostic,
  641. "url", url,
  642. "bytes", bytes,
  643. "duration", duration.String())
  644. }
  645. // NoticeRemoteServerListResourceDownloaded indicates that a remote server list download
  646. // completed successfully.
  647. func NoticeRemoteServerListResourceDownloaded(url string) {
  648. if !GetEmitNetworkParameters() {
  649. url = "[redacted]"
  650. }
  651. singletonNoticeLogger.outputNotice(
  652. "RemoteServerListResourceDownloaded", noticeIsDiagnostic,
  653. "url", url)
  654. }
  655. // NoticeSLOKSeeded indicates that the SLOK with the specified ID was received from
  656. // the Psiphon server. The "duplicate" flags indicates whether the SLOK was previously known.
  657. func NoticeSLOKSeeded(slokID string, duplicate bool) {
  658. singletonNoticeLogger.outputNotice(
  659. "SLOKSeeded", noticeIsDiagnostic,
  660. "slokID", slokID,
  661. "duplicate", duplicate)
  662. }
  663. // NoticeServerTimestamp reports server side timestamp as seen in the handshake.
  664. func NoticeServerTimestamp(timestamp string) {
  665. singletonNoticeLogger.outputNotice(
  666. "ServerTimestamp", 0,
  667. "timestamp", timestamp)
  668. }
  669. // NoticeActiveAuthorizationIDs reports the authorizations the server has accepted.
  670. // Each ID is a base64-encoded accesscontrol.Authorization.ID value.
  671. func NoticeActiveAuthorizationIDs(activeAuthorizationIDs []string) {
  672. // Never emit 'null' instead of empty list
  673. if activeAuthorizationIDs == nil {
  674. activeAuthorizationIDs = make([]string, 0)
  675. }
  676. singletonNoticeLogger.outputNotice(
  677. "ActiveAuthorizationIDs", 0,
  678. "IDs", activeAuthorizationIDs)
  679. }
  680. // NoticeTrafficRateLimits reports the tunnel traffic rate limits in place for
  681. // this client, as reported by the server at the start of the tunnel. Values
  682. // of 0 indicate no limit. Values of -1 indicate that the server did not
  683. // report rate limits.
  684. //
  685. // Limitation: any rate limit changes during the lifetime of the tunnel are
  686. // not reported.
  687. func NoticeTrafficRateLimits(upstreamBytesPerSecond, downstreamBytesPerSecond int64) {
  688. singletonNoticeLogger.outputNotice(
  689. "TrafficRateLimits", 0,
  690. "upstreamBytesPerSecond", upstreamBytesPerSecond,
  691. "downstreamBytesPerSecond", downstreamBytesPerSecond)
  692. }
  693. func NoticeBindToDevice(deviceInfo string) {
  694. outputRepetitiveNotice(
  695. "BindToDevice", deviceInfo, 0,
  696. "BindToDevice", 0, "deviceInfo", deviceInfo)
  697. }
  698. func NoticeNetworkID(networkID string) {
  699. outputRepetitiveNotice(
  700. "NetworkID", networkID, 0,
  701. "NetworkID", 0, "ID", networkID)
  702. }
  703. func NoticeLivenessTest(diagnosticID string, metrics *livenessTestMetrics, success bool) {
  704. if GetEmitNetworkParameters() {
  705. singletonNoticeLogger.outputNotice(
  706. "LivenessTest", noticeIsDiagnostic,
  707. "diagnosticID", diagnosticID,
  708. "metrics", metrics,
  709. "success", success)
  710. }
  711. }
  712. func NoticePruneServerEntry(serverEntryTag string) {
  713. singletonNoticeLogger.outputNotice(
  714. "PruneServerEntry", noticeIsDiagnostic,
  715. "serverEntryTag", serverEntryTag)
  716. }
  717. // NoticeEstablishTunnelTimeout reports that the configured EstablishTunnelTimeout
  718. // duration was exceeded.
  719. func NoticeEstablishTunnelTimeout(timeout time.Duration) {
  720. singletonNoticeLogger.outputNotice(
  721. "EstablishTunnelTimeout", 0,
  722. "timeout", timeout.String())
  723. }
  724. func NoticeFragmentor(diagnosticID string, message string) {
  725. if GetEmitNetworkParameters() {
  726. singletonNoticeLogger.outputNotice(
  727. "Fragmentor", noticeIsDiagnostic,
  728. "diagnosticID", diagnosticID,
  729. "message", message)
  730. }
  731. }
  732. func NoticeApplicationParameters(keyValues parameters.KeyValues) {
  733. for key, value := range keyValues {
  734. singletonNoticeLogger.outputNotice(
  735. "ApplicationParameter", 0,
  736. "key", key,
  737. "value", value)
  738. }
  739. }
  740. // NoticeServerAlert reports server alerts. Each distinct server alert is
  741. // reported at most once per session.
  742. func NoticeServerAlert(alert protocol.AlertRequest) {
  743. // This key ensures that each distinct server alert will appear, not repeat,
  744. // and not interfere with other alerts appearing.
  745. repetitionKey := fmt.Sprintf("ServerAlert-%+v", alert)
  746. outputRepetitiveNotice(
  747. repetitionKey, "", 0,
  748. "ServerAlert", 0, "reason", alert.Reason, "subject", alert.Subject)
  749. }
  750. type repetitiveNoticeState struct {
  751. message string
  752. repeats int
  753. }
  754. var repetitiveNoticeMutex sync.Mutex
  755. var repetitiveNoticeStates = make(map[string]*repetitiveNoticeState)
  756. // outputRepetitiveNotice conditionally outputs a notice. Used for noticies which
  757. // often repeat in noisy bursts. For a repeat limit of N, the notice is emitted
  758. // with a "repeats" count on consecutive repeats up to the limit and then suppressed
  759. // until the repetitionMessage differs.
  760. func outputRepetitiveNotice(
  761. repetitionKey, repetitionMessage string, repeatLimit int,
  762. noticeType string, noticeFlags uint32, args ...interface{}) {
  763. repetitiveNoticeMutex.Lock()
  764. defer repetitiveNoticeMutex.Unlock()
  765. state, keyFound := repetitiveNoticeStates[repetitionKey]
  766. if !keyFound {
  767. state = &repetitiveNoticeState{message: repetitionMessage}
  768. repetitiveNoticeStates[repetitionKey] = state
  769. }
  770. emit := true
  771. if keyFound {
  772. if repetitionMessage != state.message {
  773. state.message = repetitionMessage
  774. state.repeats = 0
  775. } else {
  776. state.repeats += 1
  777. if state.repeats > repeatLimit {
  778. emit = false
  779. }
  780. }
  781. }
  782. if emit {
  783. if state.repeats > 0 {
  784. args = append(args, "repeats", state.repeats)
  785. }
  786. singletonNoticeLogger.outputNotice(
  787. noticeType, noticeFlags,
  788. args...)
  789. }
  790. }
  791. // ResetRepetitiveNotices resets the repetitive notice state, so
  792. // the next instance of any notice will not be supressed.
  793. func ResetRepetitiveNotices() {
  794. repetitiveNoticeMutex.Lock()
  795. defer repetitiveNoticeMutex.Unlock()
  796. repetitiveNoticeStates = make(map[string]*repetitiveNoticeState)
  797. }
  798. type noticeObject struct {
  799. NoticeType string `json:"noticeType"`
  800. Data json.RawMessage `json:"data"`
  801. Timestamp string `json:"timestamp"`
  802. }
  803. // GetNotice receives a JSON encoded object and attempts to parse it as a Notice.
  804. // The type is returned as a string and the payload as a generic map.
  805. func GetNotice(notice []byte) (
  806. noticeType string, payload map[string]interface{}, err error) {
  807. var object noticeObject
  808. err = json.Unmarshal(notice, &object)
  809. if err != nil {
  810. return "", nil, err
  811. }
  812. var objectPayload interface{}
  813. err = json.Unmarshal(object.Data, &objectPayload)
  814. if err != nil {
  815. return "", nil, err
  816. }
  817. return object.NoticeType, objectPayload.(map[string]interface{}), nil
  818. }
  819. // NoticeReceiver consumes a notice input stream and invokes a callback function
  820. // for each discrete JSON notice object byte sequence.
  821. type NoticeReceiver struct {
  822. mutex sync.Mutex
  823. buffer []byte
  824. callback func([]byte)
  825. }
  826. // NewNoticeReceiver initializes a new NoticeReceiver
  827. func NewNoticeReceiver(callback func([]byte)) *NoticeReceiver {
  828. return &NoticeReceiver{callback: callback}
  829. }
  830. // Write implements io.Writer.
  831. func (receiver *NoticeReceiver) Write(p []byte) (n int, err error) {
  832. receiver.mutex.Lock()
  833. defer receiver.mutex.Unlock()
  834. receiver.buffer = append(receiver.buffer, p...)
  835. index := bytes.Index(receiver.buffer, []byte("\n"))
  836. if index == -1 {
  837. return len(p), nil
  838. }
  839. notice := receiver.buffer[:index]
  840. receiver.callback(notice)
  841. if index == len(receiver.buffer)-1 {
  842. receiver.buffer = receiver.buffer[0:0]
  843. } else {
  844. receiver.buffer = receiver.buffer[index+1:]
  845. }
  846. return len(p), nil
  847. }
  848. // NewNoticeConsoleRewriter consumes JSON-format notice input and parses each
  849. // notice and rewrites in a more human-readable format more suitable for
  850. // console output. The data payload field is left as JSON.
  851. func NewNoticeConsoleRewriter(writer io.Writer) *NoticeReceiver {
  852. return NewNoticeReceiver(func(notice []byte) {
  853. var object noticeObject
  854. _ = json.Unmarshal(notice, &object)
  855. fmt.Fprintf(
  856. writer,
  857. "%s %s %s\n",
  858. object.Timestamp,
  859. object.NoticeType,
  860. string(object.Data))
  861. })
  862. }
  863. // NoticeWriter implements io.Writer and emits the contents of Write() calls
  864. // as Notices. This is to transform logger messages, if they can be redirected
  865. // to an io.Writer, to notices.
  866. type NoticeWriter struct {
  867. noticeType string
  868. }
  869. // NewNoticeWriter initializes a new NoticeWriter
  870. func NewNoticeWriter(noticeType string) *NoticeWriter {
  871. return &NoticeWriter{noticeType: noticeType}
  872. }
  873. // Write implements io.Writer.
  874. func (writer *NoticeWriter) Write(p []byte) (n int, err error) {
  875. singletonNoticeLogger.outputNotice(
  876. writer.noticeType, noticeIsDiagnostic,
  877. "message", string(p))
  878. return len(p), nil
  879. }
  880. // NoticeCommonLogger maps the common.Logger interface to the notice facility.
  881. // This is used to make the notice facility available to other packages that
  882. // don't import the "psiphon" package.
  883. func NoticeCommonLogger() common.Logger {
  884. return &commonLogger{}
  885. }
  886. type commonLogger struct {
  887. }
  888. func (logger *commonLogger) WithTrace() common.LogTrace {
  889. return &commonLogTrace{
  890. trace: stacktrace.GetParentFunctionName(),
  891. }
  892. }
  893. func (logger *commonLogger) WithTraceFields(fields common.LogFields) common.LogTrace {
  894. return &commonLogTrace{
  895. trace: stacktrace.GetParentFunctionName(),
  896. fields: fields,
  897. }
  898. }
  899. func (logger *commonLogger) LogMetric(metric string, fields common.LogFields) {
  900. singletonNoticeLogger.outputNotice(
  901. metric, noticeIsDiagnostic,
  902. listCommonFields(fields)...)
  903. }
  904. func listCommonFields(fields common.LogFields) []interface{} {
  905. fieldList := make([]interface{}, 0)
  906. for name, value := range fields {
  907. var formattedValue string
  908. if err, ok := value.(error); ok {
  909. formattedValue = err.Error()
  910. } else {
  911. formattedValue = fmt.Sprintf("%#v", value)
  912. }
  913. fieldList = append(fieldList, name, formattedValue)
  914. }
  915. return fieldList
  916. }
  917. type commonLogTrace struct {
  918. trace string
  919. fields common.LogFields
  920. }
  921. func (log *commonLogTrace) outputNotice(
  922. noticeType string, args ...interface{}) {
  923. singletonNoticeLogger.outputNotice(
  924. noticeType, noticeIsDiagnostic,
  925. append(
  926. []interface{}{
  927. "message", fmt.Sprint(args...),
  928. "trace", log.trace},
  929. listCommonFields(log.fields)...)...)
  930. }
  931. func (log *commonLogTrace) Debug(args ...interface{}) {
  932. // Ignored.
  933. }
  934. func (log *commonLogTrace) Info(args ...interface{}) {
  935. log.outputNotice("Info", args...)
  936. }
  937. func (log *commonLogTrace) Warning(args ...interface{}) {
  938. log.outputNotice("Alert", args...)
  939. }
  940. func (log *commonLogTrace) Error(args ...interface{}) {
  941. log.outputNotice("Error", args...)
  942. }