cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b5cdf3b2c70fcd5224f9f2cb7bd38b59733a7618

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

connection/observer.go

99lines · modecode

1package connection
2
3import (
4 "fmt"
5 "net/url"
6 "strings"
7
8 "github.com/cloudflare/cloudflared/cmd/cloudflared/ui"
9 "github.com/cloudflare/cloudflared/logger"
10 tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
11)
12
13type Observer struct {
14 logger.Service
15 metrics *tunnelMetrics
16 tunnelEventChan chan<- ui.TunnelEvent
17}
18
19func NewObserver(logger logger.Service, tunnelEventChan chan<- ui.TunnelEvent, protocol Protocol) *Observer {
20 return &Observer{
21 logger,
22 newTunnelMetrics(protocol),
23 tunnelEventChan,
24 }
25}
26
27func (o *Observer) logServerInfo(connectionID uint8, location, msg string) {
28 // If launch-ui flag is set, send connect msg
29 if o.tunnelEventChan != nil {
30 o.tunnelEventChan <- ui.TunnelEvent{Index: connectionID, EventType: ui.Connected, Location: location}
31 }
32 o.Infof(msg)
33 o.metrics.registerServerLocation(uint8ToString(connectionID), location)
34}
35
36func (o *Observer) logTrialHostname(registration *tunnelpogs.TunnelRegistration) error {
37 // Print out the user's trial zone URL in a nice box (if they requested and got one and UI flag is not set)
38 if o.tunnelEventChan == nil {
39 if registrationURL, err := url.Parse(registration.Url); err == nil {
40 for _, line := range asciiBox(trialZoneMsg(registrationURL.String()), 2) {
41 o.Info(line)
42 }
43 } else {
44 o.Error("Failed to connect tunnel, please try again.")
45 return fmt.Errorf("empty URL in response from Cloudflare edge")
46 }
47 }
48 return nil
49}
50
51// Print out the given lines in a nice ASCII box.
52func asciiBox(lines []string, padding int) (box []string) {
53 maxLen := maxLen(lines)
54 spacer := strings.Repeat(" ", padding)
55
56 border := "+" + strings.Repeat("-", maxLen+(padding*2)) + "+"
57
58 box = append(box, border)
59 for _, line := range lines {
60 box = append(box, "|"+spacer+line+strings.Repeat(" ", maxLen-len(line))+spacer+"|")
61 }
62 box = append(box, border)
63 return
64}
65
66func maxLen(lines []string) int {
67 max := 0
68 for _, line := range lines {
69 if len(line) > max {
70 max = len(line)
71 }
72 }
73 return max
74}
75
76func trialZoneMsg(url string) []string {
77 return []string{
78 "Your free tunnel has started! Visit it:",
79 " " + url,
80 }
81}
82
83func (o *Observer) sendRegisteringEvent() {
84 if o.tunnelEventChan != nil {
85 o.tunnelEventChan <- ui.TunnelEvent{EventType: ui.RegisteringTunnel}
86 }
87}
88
89func (o *Observer) sendConnectedEvent(connIndex uint8, location string) {
90 if o.tunnelEventChan != nil {
91 o.tunnelEventChan <- ui.TunnelEvent{Index: connIndex, EventType: ui.Connected, Location: location}
92 }
93}
94
95func (o *Observer) sendURL(url string) {
96 if o.tunnelEventChan != nil {
97 o.tunnelEventChan <- ui.TunnelEvent{EventType: ui.SetUrl, Url: url}
98 }
99}