controller/pkg/x3-client/http.go

178 lines
6.4 KiB
Go
Raw Permalink Normal View History

2024-10-10 15:05:46 +00:00
package x3_client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func makeGetRequest[R any](httpClient *http.Client, url string) (R, error) {
var res R
resp, err := httpClient.Get(url)
if err != nil {
return res, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return res, err
}
err = json.Unmarshal(respBody, &res)
if err != nil {
return res, err
}
return res, nil
}
2024-10-10 17:25:05 +00:00
func makePostRequest[R any](httpClient *http.Client, url string, req any) (R, error) {
2024-10-10 15:05:46 +00:00
var res R
body, err := json.Marshal(req)
if err != nil {
return res, err
}
resp, err := httpClient.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return res, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return res, err
}
err = json.Unmarshal(respBody, &res)
if err != nil {
return res, err
}
return res, nil
}
const loginURL = "/login"
func (c *X3Client) Login(request LoginRequest) (GeneralResponse[LoginResponse], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[LoginResponse]](c.httpClient, c.basePath+loginURL+fmt.Sprintf("?username=%s&password=%s", request.Username, request.Password), nil)
2024-10-10 15:05:46 +00:00
}
const inboundsURL = "/panel/api/inbounds/list"
func (c *X3Client) GetInbounds() (GeneralResponse[[]Inbound], error) {
2024-10-10 17:25:05 +00:00
return makeGetRequest[GeneralResponse[[]Inbound]](c.httpClient, c.basePath+inboundsURL)
2024-10-10 15:05:46 +00:00
}
const inboundURL = "/panel/api/inbounds/get/%d"
func (c *X3Client) GetInbound(inboundId int) (GeneralResponse[Inbound], error) {
2024-10-10 17:25:05 +00:00
return makeGetRequest[GeneralResponse[Inbound]](c.httpClient, c.basePath+fmt.Sprintf(inboundURL, inboundId))
2024-10-10 15:05:46 +00:00
}
const inboundClientTrafficURL = "/panel/api/inbounds/getClientTraffics/%s"
func (c *X3Client) GetInboundClientTraffic(email string) (GeneralResponse[ClientStats], error) {
2024-10-10 17:25:05 +00:00
return makeGetRequest[GeneralResponse[ClientStats]](c.httpClient, c.basePath+fmt.Sprintf(inboundClientTrafficURL, email))
2024-10-10 15:05:46 +00:00
}
const inboundClientTrafficByIdURL = "/panel/api/inbounds/getClientTrafficsById/%s"
func (c *X3Client) GetInboundClientTrafficById(uuid string) (GeneralResponse[ClientStats], error) {
2024-10-10 17:25:05 +00:00
return makeGetRequest[GeneralResponse[ClientStats]](c.httpClient, c.basePath+fmt.Sprintf(inboundClientTrafficByIdURL, uuid))
2024-10-10 15:05:46 +00:00
}
const inboundCreateBackupURL = "/panel/api/inbounds/createbackup"
func (c *X3Client) CreateInboundBackup() (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makeGetRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+inboundCreateBackupURL)
2024-10-10 15:05:46 +00:00
}
const clientIpsURL = "/panel/api/inbounds/clientIps/%s"
func (c *X3Client) GetClientIps(email string) (GeneralResponse[string], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[string]](c.httpClient, c.basePath+fmt.Sprintf(clientIpsURL, email), nil)
2024-10-10 15:05:46 +00:00
}
const addInboundURL = "/panel/api/inbounds/add"
func (c *X3Client) AddInbound(inbound Inbound) (GeneralResponse[Inbound], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[Inbound]](c.httpClient, c.basePath+addInboundURL, inbound)
2024-10-10 15:05:46 +00:00
}
const addClientToInboundURL = "/panel/api/inbounds/addClient"
2024-10-10 17:25:05 +00:00
type createClient struct {
Id int `json:"id"`
Settings json.RawMessage `json:"settings"`
}
func (c *X3Client) AddClientToInbound(inboundID int, client []Client) (GeneralResponse[interface{}], error) {
req := createClient{
Id: inboundID,
}
var err error
req.Settings, err = json.Marshal(struct{ client []Client }{client: client})
if err != nil {
return GeneralResponse[interface{}]{}, err
}
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+addClientToInboundURL, req)
2024-10-10 15:05:46 +00:00
}
const updateInboundURL = "/panel/api/inbounds/update/%d"
func (c *X3Client) UpdateInbound(inbound Inbound) (GeneralResponse[Inbound], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[Inbound]](c.httpClient, c.basePath+fmt.Sprintf(updateInboundURL, inbound.Id), inbound)
2024-10-10 15:05:46 +00:00
}
// TODO: Check if client id is int not an uuid
const updateClientInboundURL = "/panel/api/inbounds/updateClient/%d"
2024-10-10 17:25:05 +00:00
func (c *X3Client) UpdateClientInbound(client createClient) (GeneralResponse[interface{}], error) {
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(updateClientInboundURL, client.Id), client)
2024-10-10 15:05:46 +00:00
}
const clearClientIpsURL = "/panel/api/inbounds/clearClientIps/%s"
func (c *X3Client) ClearClientIps(email string) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(clearClientIpsURL, email), nil)
2024-10-10 15:05:46 +00:00
}
const resetAllTrafficURL = "/panel/api/inbounds/resetAllTraffics"
func (c *X3Client) ResetAllTraffic() (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+resetAllTrafficURL, nil)
2024-10-10 15:05:46 +00:00
}
const resetAllClientTrafficByInboundURL = "/panel/api/inbounds/resetAllClientTraffics/%d"
func (c *X3Client) ResetAllClientTrafficByInbound(inboundId int) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(resetAllClientTrafficByInboundURL, inboundId), nil)
2024-10-10 15:05:46 +00:00
}
const resetClientTrafficURL = "/panel/api/inbounds/%d/resetClientTraffic/%s"
func (c *X3Client) ResetClientTraffic(inboundId int, email string) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(resetClientTrafficURL, inboundId, email), nil)
2024-10-10 15:05:46 +00:00
}
const deleteClientURL = "/panel/api/inbounds/%d/delClient/%s"
func (c *X3Client) DeleteClient(inboundId int, uuid string) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(deleteClientURL, inboundId, uuid), nil)
2024-10-10 15:05:46 +00:00
}
const deleteInboundURL = "/panel/api/inbounds/del/%d"
func (c *X3Client) DeleteInbound(inboundId int) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(deleteInboundURL, inboundId), nil)
2024-10-10 15:05:46 +00:00
}
const deleteDepletedClientsURL = "/panel/api/inbounds/delDepletedClients/%d"
func (c *X3Client) DeleteDepletedClients(inboundId int) (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+fmt.Sprintf(deleteDepletedClientsURL, inboundId), nil)
2024-10-10 15:05:46 +00:00
}
const onlineClientsURL = "/panel/api/inbounds/onlines"
func (c *X3Client) GetOnlineClients() (GeneralResponse[interface{}], error) {
2024-10-10 17:25:05 +00:00
return makePostRequest[GeneralResponse[interface{}]](c.httpClient, c.basePath+onlineClientsURL, nil)
2024-10-10 15:05:46 +00:00
}