| 9 | "strconv" |
| 10 | "time" |
| 11 |
| 12 | "github.com/fatedier/frp/pkg/config" |
| 13 | flog "github.com/fatedier/frp/pkg/util/log" |
| 14 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 15 | "github.com/fatedier/frp/test/e2e/pkg/process" |
| 16 | ) |
| 17 |
| 18 | // RunProcesses starts one frps and zero or more frpc processes from templates. |
| 19 | func (f *Framework) RunProcesses(serverTemplate string, clientTemplates []string) (*process.Process, []*process.Process) { |
| 20 | return f.RunProcessesWithBinaries(TestContext.FRPServerPath, TestContext.FRPClientPath, serverTemplate, clientTemplates) |
| 21 | } |
| 22 |
| 23 | // RunProcessesWithBinaries starts one frps and zero or more frpc processes with explicit binary paths. |
| 24 | func (f *Framework) RunProcessesWithBinaries( |
| 25 | serverBinaryPath string, |
| 26 | clientBinaryPath string, |
| 27 | serverTemplate string, |
| 28 | clientTemplates []string, |
| 29 | ) (*process.Process, []*process.Process) { |
| 30 | templates := append([]string{serverTemplate}, clientTemplates...) |
| 31 | outs, ports, err := f.RenderTemplates(templates) |
| 32 | ExpectNoError(err) |
| 33 |
| 34 | maps.Copy(f.usedPorts, ports) |
| 35 |
| 36 | // Start frps. |
| 37 | serverPath := filepath.Join(f.TempDirectory, "frp-e2e-server-0") |
| 38 | err = os.WriteFile(serverPath, []byte(outs[0]), 0o600) |
| 39 | ExpectNoError(err) |
| 40 |
| 41 | if TestContext.Debug { |
| 42 | flog.Debugf("[%s] %s", serverPath, outs[0]) |
| 43 | } |
| 44 |
| 45 | serverProcess := process.NewWithEnvs(serverBinaryPath, []string{"-c", serverPath}, f.osEnvs) |
| 46 | f.serverConfPaths = append(f.serverConfPaths, serverPath) |
| 47 | f.serverProcesses = append(f.serverProcesses, serverProcess) |
| 48 | err = serverProcess.Start() |
| 49 | ExpectNoError(err) |
| 50 |
| 51 | if port, ok := ports[consts.PortServerName]; ok { |
| 52 | ExpectNoError(WaitForTCPReady(net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), 5*time.Second)) |
| 53 | } else { |
| 54 | time.Sleep(2 * time.Second) |
| 55 | } |
| 56 |
| 57 | // Start frpc(s). |
| 58 | clientProcesses := make([]*process.Process, 0, len(clientTemplates)) |
| 59 | for i := range clientTemplates { |
| 60 | path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i)) |
| 61 | err = os.WriteFile(path, []byte(outs[1+i]), 0o600) |
| 62 | ExpectNoError(err) |
| 63 |
| 64 | if TestContext.Debug { |
| 65 | flog.Debugf("[%s] %s", path, outs[1+i]) |
| 66 | } |
| 67 |
| 68 | p := process.NewWithEnvs(clientBinaryPath, []string{"-c", path}, f.osEnvs) |
| 69 | f.clientConfPaths = append(f.clientConfPaths, path) |
| 70 | f.clientProcesses = append(f.clientProcesses, p) |
| 71 | clientProcesses = append(clientProcesses, p) |
| 72 | err = p.Start() |
| 73 | ExpectNoError(err) |
| 74 | } |
| 75 | // Wait for each client's proxies to register with frps. |
| 76 | // If any client has no proxies (e.g. visitor-only), fall back to sleep |
| 77 | // for the remaining time since visitors have no deterministic readiness signal. |
| 78 | allConfirmed := len(clientProcesses) > 0 |
| 79 | start := time.Now() |
| 80 | for i, p := range clientProcesses { |
| 81 | configPath := f.clientConfPaths[len(f.clientConfPaths)-len(clientProcesses)+i] |
| 82 | if !waitForClientProxyReady(configPath, p, 5*time.Second) { |
| 83 | allConfirmed = false |
| 84 | } |
| 85 | } |
| 86 | if len(clientProcesses) > 0 && !allConfirmed { |
| 87 | remaining := 1500*time.Millisecond - time.Since(start) |
| 88 | if remaining > 0 { |
| 89 | time.Sleep(remaining) |
| 90 | } |
| 91 | } |
| 92 |
| 93 | return serverProcess, clientProcesses |
| 94 | } |
| 95 |
| 96 | func (f *Framework) RunFrps(args ...string) (*process.Process, string, error) { |
| 97 | p := process.NewWithEnvs(TestContext.FRPServerPath, args, f.osEnvs) |
| 98 | f.serverProcesses = append(f.serverProcesses, p) |
| 99 | err := p.Start() |
| 100 | if err != nil { |
| 101 | return p, p.Output(), err |
| 102 | } |
| 103 | select { |
| 104 | case <-p.Done(): |
| 105 | case <-time.After(2 * time.Second): |
| 106 | } |
| 107 | return p, p.Output(), nil |
| 108 | } |
| 109 |
| 110 | func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) { |
| 111 | p := process.NewWithEnvs(TestContext.FRPClientPath, args, f.osEnvs) |
| 112 | f.clientProcesses = append(f.clientProcesses, p) |
| 113 | err := p.Start() |
| 114 | if err != nil { |
| 115 | return p, p.Output(), err |
| 116 | } |
| 117 | select { |
| 118 | case <-p.Done(): |
| 119 | case <-time.After(1500 * time.Millisecond): |
| 120 | } |
| 121 | return p, p.Output(), nil |
| 122 | } |
| 123 |
| 124 | func (f *Framework) GenerateConfigFile(content string) string { |
| 125 | f.configFileIndex++ |
| 126 | path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex)) |
| 127 | err := os.WriteFile(path, []byte(content), 0o600) |
| 128 | ExpectNoError(err) |
| 129 | return path |
| 130 | } |
| 131 |
| 132 | // waitForClientProxyReady parses the client config to extract proxy names, |
| 133 | // then waits for each proxy's "start proxy success" log in the process output. |
| 134 | // Returns true only if proxies were expected and all registered successfully. |
| 135 | func waitForClientProxyReady(configPath string, p *process.Process, timeout time.Duration) bool { |
| 136 | _, proxyCfgs, _, _, err := config.LoadClientConfig(configPath, false) |
| 137 | if err != nil || len(proxyCfgs) == 0 { |
| 138 | return false |
| 139 | } |
| 140 |
| 141 | // Use a single deadline so the total wait across all proxies does not exceed timeout. |
| 142 | deadline := time.Now().Add(timeout) |
| 143 | for _, cfg := range proxyCfgs { |
| 144 | remaining := time.Until(deadline) |
| 145 | if remaining <= 0 { |
| 146 | return false |
| 147 | } |
| 148 | name := cfg.GetBaseConfig().Name |
| 149 | pattern := fmt.Sprintf("[%s] start proxy success", name) |
| 150 | if err := p.WaitForOutput(pattern, 1, remaining); err != nil { |
| 151 | return false |
| 152 | } |
| 153 | } |
| 154 | return true |
| 155 | } |
| 156 |
| 157 | // WaitForTCPUnreachable polls a TCP address until a connection fails or timeout. |
| 158 | func WaitForTCPUnreachable(addr string, interval, timeout time.Duration) error { |
| 159 | if interval <= 0 { |
| 160 | return fmt.Errorf("invalid interval for TCP unreachable on %s: interval must be positive", addr) |
| 161 | } |
| 162 | if timeout <= 0 { |
| 163 | return fmt.Errorf("invalid timeout for TCP unreachable on %s: timeout must be positive", addr) |
| 164 | } |
| 165 | deadline := time.Now().Add(timeout) |
| 166 | for { |
| 167 | remaining := time.Until(deadline) |
| 168 | if remaining <= 0 { |
| 169 | return fmt.Errorf("timeout waiting for TCP unreachable on %s", addr) |
| 170 | } |
| 171 | dialTimeout := min(interval, remaining) |
| 172 | conn, err := net.DialTimeout("tcp", addr, dialTimeout) |
| 173 | if err != nil { |
| 174 | return nil |
| 175 | } |
| 176 | conn.Close() |
| 177 | time.Sleep(min(interval, time.Until(deadline))) |
| 178 | } |
| 179 | } |
| 180 |
| 181 | // WaitForTCPReady polls a TCP address until a connection succeeds or timeout. |
| 182 | func WaitForTCPReady(addr string, timeout time.Duration) error { |
| 183 | if timeout <= 0 { |
| 184 | return fmt.Errorf("invalid timeout for TCP readiness on %s: timeout must be positive", addr) |
| 185 | } |
| 186 | deadline := time.Now().Add(timeout) |
| 187 | var lastErr error |
| 188 | for time.Now().Before(deadline) { |
| 189 | conn, err := net.DialTimeout("tcp", addr, 100*time.Millisecond) |
| 190 | if err == nil { |
| 191 | conn.Close() |
| 192 | return nil |
| 193 | } |
| 194 | lastErr = err |
| 195 | time.Sleep(50 * time.Millisecond) |
| 196 | } |
| 197 | if lastErr == nil { |
| 198 | return fmt.Errorf("timeout waiting for TCP readiness on %s before any dial attempt", addr) |
| 199 | } |
| 200 | return fmt.Errorf("timeout waiting for TCP readiness on %s: %w", addr, lastErr) |
| 201 | } |