185 lines
7.0 KiB
Go
185 lines
7.0 KiB
Go
package isolation
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
http *http.Client
|
|
longHTTP *http.Client
|
|
internalToken string
|
|
}
|
|
|
|
func NewTransportClient(transport http.RoundTripper) *Client {
|
|
return &Client{http: &http.Client{Transport: transport, Timeout: 30 * time.Second}, longHTTP: &http.Client{Transport: transport, Timeout: 2 * time.Hour}}
|
|
}
|
|
|
|
func NewClient(socket string, internalToken ...string) *Client {
|
|
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, "unix", socket)
|
|
}}
|
|
token := ""
|
|
if len(internalToken) > 0 {
|
|
token = strings.TrimSpace(internalToken[0])
|
|
}
|
|
return &Client{http: &http.Client{Transport: transport, Timeout: 30 * time.Second}, longHTTP: &http.Client{Transport: transport, Timeout: 2 * time.Hour}, internalToken: token}
|
|
}
|
|
func (c *Client) Ping(ctx context.Context) error {
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://unix/healthz", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c.internalToken != "" {
|
|
request.Header.Set("Authorization", "Bearer "+c.internalToken)
|
|
}
|
|
response, err := c.http.Do(request)
|
|
if err != nil {
|
|
return fmt.Errorf("isolation agent: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 4<<10))
|
|
if response.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("isolation agent health check failed (%d)", response.StatusCode)
|
|
}
|
|
return nil
|
|
}
|
|
func (c *Client) request(ctx context.Context, path string, input any, out any) error {
|
|
return c.requestWithClient(ctx, c.http, path, input, out)
|
|
}
|
|
|
|
func (c *Client) requestLong(ctx context.Context, path string, input any, out any) error {
|
|
return c.requestWithClient(ctx, c.longHTTP, path, input, out)
|
|
}
|
|
|
|
func (c *Client) requestWithClient(ctx context.Context, client *http.Client, path string, input any, out any) error {
|
|
body, err := json.Marshal(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://unix"+path, bytes.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
request.Header.Set("Content-Type", "application/json")
|
|
if c.internalToken != "" {
|
|
request.Header.Set("Authorization", "Bearer "+c.internalToken)
|
|
}
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return fmt.Errorf("isolation agent: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusOK {
|
|
message, _ := io.ReadAll(io.LimitReader(response.Body, 512))
|
|
detail := strings.TrimSpace(string(message))
|
|
if detail == "" {
|
|
detail = "unspecified error"
|
|
}
|
|
return fmt.Errorf("isolation agent rejected request (%d): %s", response.StatusCode, detail)
|
|
}
|
|
if out != nil {
|
|
return json.NewDecoder(response.Body).Decode(out)
|
|
}
|
|
return nil
|
|
}
|
|
func (c *Client) Prepare(ctx context.Context, spec ProjectSpec) (Prepared, error) {
|
|
var result Prepared
|
|
err := c.request(ctx, "/v1/prepare", spec, &result)
|
|
return result, err
|
|
}
|
|
func (c *Client) Quarantine(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/quarantine", spec, nil)
|
|
}
|
|
func (c *Client) ApplyWebProtection(ctx context.Context, spec WebProtectionSpec) error {
|
|
return c.request(ctx, "/v1/web-protection/apply", spec, nil)
|
|
}
|
|
func (c *Client) RemoveWebProtection(ctx context.Context, spec WebProtectionSpec) error {
|
|
return c.request(ctx, "/v1/web-protection/remove", spec, nil)
|
|
}
|
|
func (c *Client) PrepareExecutable(ctx context.Context, spec ExecutableSpec) error {
|
|
return c.request(ctx, "/v1/runtime/executable", spec, nil)
|
|
}
|
|
func (c *Client) PreparePanelRuntime(ctx context.Context, spec PanelRuntimeSpec) error {
|
|
return c.request(ctx, "/v1/runtime/panel-state", spec, nil)
|
|
}
|
|
func (c *Client) InstallPythonRuntime(ctx context.Context, version string) error {
|
|
return c.requestLong(ctx, "/v1/runtime/python/install", PythonRuntimeInstallSpec{Version: version}, nil)
|
|
}
|
|
func (c *Client) TerminateRuntime(ctx context.Context, spec RuntimeProcessSpec) error {
|
|
return c.request(ctx, "/v1/resources/runtime/terminate", spec, nil)
|
|
}
|
|
func (c *Client) ReleaseRunPath(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/run-path/release", spec, nil)
|
|
}
|
|
func (c *Client) PrepareTrash(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/trash/prepare", spec, nil)
|
|
}
|
|
func (c *Client) ConfigureFTPIdentity(ctx context.Context, spec FTPIdentitySpec) error {
|
|
return c.request(ctx, "/v1/ftp/identity", spec, nil)
|
|
}
|
|
func (c *Client) ConfigurePHPIdentity(ctx context.Context, spec PHPIdentitySpec) error {
|
|
return c.request(ctx, "/v1/php/identity", spec, nil)
|
|
}
|
|
func (c *Client) SetPHPIdentityState(ctx context.Context, spec PHPIdentitySpec, enabled bool) error {
|
|
path := "/v1/php/identity/stop"
|
|
if enabled {
|
|
path = "/v1/php/identity/start"
|
|
}
|
|
return c.request(ctx, path, spec, nil)
|
|
}
|
|
func (c *Client) RemovePHPIdentity(ctx context.Context, spec PHPIdentitySpec) error {
|
|
return c.request(ctx, "/v1/php/identity/remove", spec, nil)
|
|
}
|
|
func (c *Client) ResourceUsage(ctx context.Context, spec ResourceUsageSpec) (ResourceUsage, error) {
|
|
var result ResourceUsage
|
|
err := c.request(ctx, "/v1/resources/usage", spec, &result)
|
|
return result, err
|
|
}
|
|
func (c *Client) TerminateProcesses(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/resources/processes/terminate", spec, nil)
|
|
}
|
|
func (c *Client) FreezeWrites(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/project/freeze-writes", spec, nil)
|
|
}
|
|
func (c *Client) ConfigureDatabaseAccess(ctx context.Context, spec DatabaseAccessSpec) error {
|
|
return c.request(ctx, "/v1/database/access", spec, nil)
|
|
}
|
|
func (c *Client) ConfigureTrafficLimits(ctx context.Context, spec TrafficLimitSpec) error {
|
|
return c.request(ctx, "/v1/resources/limits", spec, nil)
|
|
}
|
|
func (c *Client) ConfigureFTPTLS(ctx context.Context, domain string) (string, error) {
|
|
var result struct {
|
|
Policy string `json:"policy"`
|
|
}
|
|
err := c.request(ctx, "/v1/ftp/tls", map[string]string{"domain": domain}, &result)
|
|
return result.Policy, err
|
|
}
|
|
func (c *Client) ConfigureNginxNetwork(ctx context.Context, spec NginxNetworkSpec) error {
|
|
return c.request(ctx, "/v1/nginx/network", spec, nil)
|
|
}
|
|
func (c *Client) CreateProjectBackup(ctx context.Context, spec ProjectSpec) (ProjectBackup, error) {
|
|
var result ProjectBackup
|
|
err := c.requestLong(ctx, "/v1/lifecycle/backup", spec, &result)
|
|
return result, err
|
|
}
|
|
func (c *Client) DeleteProjectBackup(ctx context.Context, backup ProjectBackup) error {
|
|
return c.request(ctx, "/v1/lifecycle/backup/delete", backup, nil)
|
|
}
|
|
func (c *Client) VerifyProjectBackup(ctx context.Context, backup ProjectBackup) error {
|
|
return c.requestLong(ctx, "/v1/lifecycle/backup/verify", backup, nil)
|
|
}
|
|
func (c *Client) PurgeProject(ctx context.Context, spec ProjectSpec) error {
|
|
return c.request(ctx, "/v1/lifecycle/project/purge", spec, nil)
|
|
}
|
|
func (c *Client) ImportLegacyProject(ctx context.Context, spec LegacyImportSpec) error {
|
|
return c.requestLong(ctx, "/v1/import/project", spec, nil)
|
|
}
|