Add PostgreSQL, user/squad management, remove private domains from docs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package remnawave
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -48,20 +49,38 @@ func (c *Client) countFromEndpoint(ctx context.Context, path, arrayKey string) (
|
||||
}
|
||||
|
||||
func (c *Client) get(ctx context.Context, path string) (*http.Response, []byte, error) {
|
||||
return c.doRequest(ctx, c.panelURL+path, true)
|
||||
return c.doRequest(ctx, http.MethodGet, c.panelURL+path, nil, true)
|
||||
}
|
||||
|
||||
func (c *Client) getPublic(ctx context.Context, path string) (*http.Response, []byte, error) {
|
||||
return c.doRequest(ctx, c.panelURL+path, false)
|
||||
return c.doRequest(ctx, http.MethodGet, c.panelURL+path, nil, false)
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(ctx context.Context, url string, withBearer bool) (*http.Response, []byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
func (c *Client) post(ctx context.Context, path string, body any) (*http.Response, []byte, error) {
|
||||
return c.doRequest(ctx, http.MethodPost, c.panelURL+path, body, true)
|
||||
}
|
||||
|
||||
func (c *Client) patch(ctx context.Context, path string, body any) (*http.Response, []byte, error) {
|
||||
return c.doRequest(ctx, http.MethodPatch, c.panelURL+path, body, true)
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(ctx context.Context, method, url string, body any, withBearer bool) (*http.Response, []byte, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
bodyReader = bytes.NewReader(raw)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, url, bodyReader)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json, text/html, */*")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if withBearer {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
@@ -75,11 +94,19 @@ func (c *Client) doRequest(ctx context.Context, url string, withBearer bool) (*h
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
return resp, body, nil
|
||||
return resp, respBody, nil
|
||||
}
|
||||
|
||||
func apiError(status int, body []byte) error {
|
||||
msg := trimBody(body, 300)
|
||||
if msg == "" {
|
||||
return fmt.Errorf("HTTP %d", status)
|
||||
}
|
||||
return fmt.Errorf("HTTP %d: %s", status, msg)
|
||||
}
|
||||
|
||||
func parseCount(body []byte, arrayKey string) int {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package remnawave
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Squad struct {
|
||||
UUID string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c *Client) ListInternalSquads(ctx context.Context) ([]Squad, error) {
|
||||
return c.listSquads(ctx, "/api/internal-squads")
|
||||
}
|
||||
|
||||
func (c *Client) ListExternalSquads(ctx context.Context) ([]Squad, error) {
|
||||
return c.listSquads(ctx, "/api/external-squads")
|
||||
}
|
||||
|
||||
func (c *Client) listSquads(ctx context.Context, path string) ([]Squad, error) {
|
||||
resp, body, err := c.get(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, apiError(resp.StatusCode, body)
|
||||
}
|
||||
return parseSquads(body), nil
|
||||
}
|
||||
|
||||
func parseSquads(body []byte) []Squad {
|
||||
var wrap map[string]json.RawMessage
|
||||
if json.Unmarshal(body, &wrap) != nil {
|
||||
return nil
|
||||
}
|
||||
if raw, ok := wrap["response"]; ok {
|
||||
if list := squadsFromRaw(raw); len(list) > 0 {
|
||||
return list
|
||||
}
|
||||
}
|
||||
return squadsFromRaw(body)
|
||||
}
|
||||
|
||||
func squadsFromRaw(data []byte) []Squad {
|
||||
var arr []map[string]json.RawMessage
|
||||
if json.Unmarshal(data, &arr) == nil {
|
||||
return squadsFromMaps(arr)
|
||||
}
|
||||
var obj map[string]json.RawMessage
|
||||
if json.Unmarshal(data, &obj) != nil {
|
||||
return nil
|
||||
}
|
||||
for _, key := range []string{"internalSquads", "externalSquads", "squads"} {
|
||||
if raw, ok := obj[key]; ok {
|
||||
if list := squadsFromRaw(raw); len(list) > 0 {
|
||||
return list
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, v := range obj {
|
||||
if list := squadsFromRaw(v); len(list) > 0 {
|
||||
return list
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func squadsFromMaps(items []map[string]json.RawMessage) []Squad {
|
||||
out := make([]Squad, 0, len(items))
|
||||
for _, m := range items {
|
||||
s := Squad{}
|
||||
if raw, ok := m["uuid"]; ok {
|
||||
_ = json.Unmarshal(raw, &s.UUID)
|
||||
}
|
||||
if raw, ok := m["name"]; ok {
|
||||
_ = json.Unmarshal(raw, &s.Name)
|
||||
}
|
||||
if s.UUID != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package remnawave
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreateUserInput struct {
|
||||
Username string
|
||||
ExpireAt time.Time
|
||||
TelegramID *int64
|
||||
ExternalSquadUUID *string
|
||||
ActiveInternalSquads []string
|
||||
TrafficLimitBytes *int64
|
||||
Description string
|
||||
}
|
||||
|
||||
type PanelUser struct {
|
||||
UUID string
|
||||
Username string
|
||||
ShortUUID string
|
||||
Status string
|
||||
ExpireAt time.Time
|
||||
SubscriptionURL string
|
||||
}
|
||||
|
||||
func (c *Client) CreateUser(ctx context.Context, in CreateUserInput) (*PanelUser, error) {
|
||||
payload := map[string]any{
|
||||
"username": in.Username,
|
||||
"expireAt": in.ExpireAt.UTC().Format(time.RFC3339Nano),
|
||||
"status": "ACTIVE",
|
||||
}
|
||||
if in.TelegramID != nil {
|
||||
payload["telegramId"] = *in.TelegramID
|
||||
}
|
||||
if in.ExternalSquadUUID != nil && *in.ExternalSquadUUID != "" {
|
||||
payload["externalSquadUuid"] = *in.ExternalSquadUUID
|
||||
}
|
||||
if len(in.ActiveInternalSquads) > 0 {
|
||||
payload["activeInternalSquads"] = in.ActiveInternalSquads
|
||||
}
|
||||
if in.TrafficLimitBytes != nil {
|
||||
payload["trafficLimitBytes"] = *in.TrafficLimitBytes
|
||||
}
|
||||
if in.Description != "" {
|
||||
payload["description"] = in.Description
|
||||
}
|
||||
|
||||
resp, body, err := c.post(ctx, "/api/users", payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, apiError(resp.StatusCode, body)
|
||||
}
|
||||
return parsePanelUser(body), nil
|
||||
}
|
||||
|
||||
type AssignSquadsInput struct {
|
||||
UUID string
|
||||
Username string
|
||||
ExternalSquadUUID *string
|
||||
ActiveInternalSquads []string
|
||||
}
|
||||
|
||||
func (c *Client) AssignSquads(ctx context.Context, in AssignSquadsInput) (*PanelUser, error) {
|
||||
if in.UUID == "" && in.Username == "" {
|
||||
return nil, fmt.Errorf("нужен uuid или username")
|
||||
}
|
||||
payload := map[string]any{}
|
||||
if in.UUID != "" {
|
||||
payload["uuid"] = in.UUID
|
||||
} else {
|
||||
payload["username"] = in.Username
|
||||
}
|
||||
if in.ExternalSquadUUID != nil {
|
||||
payload["externalSquadUuid"] = in.ExternalSquadUUID
|
||||
}
|
||||
if in.ActiveInternalSquads != nil {
|
||||
payload["activeInternalSquads"] = in.ActiveInternalSquads
|
||||
}
|
||||
|
||||
resp, body, err := c.patch(ctx, "/api/users", payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, apiError(resp.StatusCode, body)
|
||||
}
|
||||
return parsePanelUser(body), nil
|
||||
}
|
||||
|
||||
func parsePanelUser(body []byte) *PanelUser {
|
||||
var wrap struct {
|
||||
Response map[string]json.RawMessage `json:"response"`
|
||||
}
|
||||
if json.Unmarshal(body, &wrap) != nil || wrap.Response == nil {
|
||||
return nil
|
||||
}
|
||||
u := &PanelUser{}
|
||||
if raw, ok := wrap.Response["uuid"]; ok {
|
||||
_ = json.Unmarshal(raw, &u.UUID)
|
||||
}
|
||||
if raw, ok := wrap.Response["username"]; ok {
|
||||
_ = json.Unmarshal(raw, &u.Username)
|
||||
}
|
||||
if raw, ok := wrap.Response["shortUuid"]; ok {
|
||||
_ = json.Unmarshal(raw, &u.ShortUUID)
|
||||
}
|
||||
if raw, ok := wrap.Response["status"]; ok {
|
||||
_ = json.Unmarshal(raw, &u.Status)
|
||||
}
|
||||
if raw, ok := wrap.Response["expireAt"]; ok {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
|
||||
u.ExpireAt = t
|
||||
}
|
||||
}
|
||||
}
|
||||
if raw, ok := wrap.Response["subscriptionUrl"]; ok {
|
||||
_ = json.Unmarshal(raw, &u.SubscriptionURL)
|
||||
}
|
||||
return u
|
||||
}
|
||||
Reference in New Issue
Block a user