• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

foomo / posh-providers / 14311477632

07 Apr 2025 02:11PM UTC coverage: 0.0%. Remained the same
14311477632

push

github

franklinkim
feat: update schema

0 of 8878 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cloudflare/cloudflared/cloudflared.go
1
package cloudflared
2

3
import (
4
        "context"
5
        "fmt"
6
        "os"
7
        "os/exec"
8
        "strings"
9
        "syscall"
10

11
        "github.com/foomo/posh/pkg/log"
12
        "github.com/pkg/errors"
13
        "github.com/shirou/gopsutil/v3/process"
14
        "github.com/spf13/viper"
15
)
16

17
type (
18
        Cloudflared struct {
19
                l         log.Logger
20
                cfg       Config
21
                configKey string
22
        }
23
        Option func(*Cloudflared) error
24
)
25

26
// ------------------------------------------------------------------------------------------------
27
// ~ Options
28
// ------------------------------------------------------------------------------------------------
29

30
func WithConfigKey(v string) Option {
×
31
        return func(o *Cloudflared) error {
×
32
                o.configKey = v
×
33
                return nil
×
34
        }
×
35
}
36

37
// ------------------------------------------------------------------------------------------------
38
// ~ Constructor
39
// ------------------------------------------------------------------------------------------------
40

41
// New command
42
func New(l log.Logger, opts ...Option) (*Cloudflared, error) {
×
43
        inst := &Cloudflared{
×
44
                l:         l,
×
45
                configKey: "cloudflared",
×
46
        }
×
47
        for _, opt := range opts {
×
48
                if opt != nil {
×
49
                        if err := opt(inst); err != nil {
×
50
                                return nil, err
×
51
                        }
×
52
                }
53
        }
54
        if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
×
55
                return nil, err
×
56
        }
×
57

58
        return inst, nil
×
59
}
60

61
// ------------------------------------------------------------------------------------------------
62
// ~ Public methods
63
// ------------------------------------------------------------------------------------------------
64

65
func (t *Cloudflared) Config() Config {
×
66
        return t.cfg
×
67
}
×
68

69
func (t *Cloudflared) Disonnect(ctx context.Context, access Access) error {
×
70
        ps, err := process.Processes()
×
71
        if err != nil {
×
72
                return err
×
73
        }
×
74

75
        for _, p := range ps {
×
76
                if value, _ := p.Name(); value == "cloudflared" {
×
77
                        if cmdline, _ := p.Cmdline(); strings.Contains(cmdline, "--hostname "+access.Hostname) {
×
78
                                t.l.Info("closing connection", "hostname", access.Hostname, "pid", p.Pid, "port", access.Port)
×
79
                                return p.Kill()
×
80
                        }
×
81
                }
82
        }
83
        return nil
×
84
}
85

86
func (t *Cloudflared) Connect(ctx context.Context, access Access) error {
×
87
        if t.IsConnected(ctx, access) {
×
88
                return errors.Errorf("connection already exists: %s", access.Hostname)
×
89
        }
×
90

91
        cmd := exec.CommandContext(ctx, "cloudflared",
×
92
                "access", access.Type,
×
93
                "--hostname", access.Hostname,
×
94
                "--url", fmt.Sprintf("127.0.0.1:%d", access.Port),
×
95
        )
×
96
        cmd.Env = append(os.Environ(), "HOME="+t.Config().Path)
×
97
        cmd.SysProcAttr = &syscall.SysProcAttr{
×
98
                Setpgid: true,
×
99
        }
×
100
        if err := cmd.Start(); err != nil {
×
101
                return err
×
102
        }
×
103

104
        if cmd.Process != nil {
×
105
                t.l.Info("started access", "hostname", access.Hostname, "port", access.Port, "pid", cmd.Process.Pid)
×
106
        }
×
107

108
        return nil
×
109
}
110

111
func (t *Cloudflared) IsConnected(ctx context.Context, access Access) bool {
×
112
        list, err := t.List()
×
113
        if err != nil {
×
114
                return false
×
115
        }
×
116
        for _, p := range list {
×
117
                if strings.Contains(p.Cmdline, "--hostname "+access.Hostname) {
×
118
                        return true
×
119
                }
×
120
        }
121
        return false
×
122
}
123

124
func (t *Cloudflared) List() ([]Process, error) {
×
125
        ps, err := process.Processes()
×
126
        if err != nil {
×
127
                return nil, err
×
128
        }
×
129

130
        var ret []Process
×
131
        for _, p := range ps {
×
132
                if value, _ := p.Name(); value == "cloudflared" {
×
133
                        exe, _ := p.Exe()
×
134
                        cmdline, _ := p.Cmdline()
×
135
                        ret = append(ret, Process{
×
136
                                PID:     fmt.Sprintf("%d", p.Pid),
×
137
                                Exe:     exe,
×
138
                                Cmdline: cmdline,
×
139
                        })
×
140
                }
×
141
        }
142

143
        return ret, nil
×
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc