2022-08-16 01:04:33 -04:00
|
|
|
package panel
|
2022-06-04 00:05:46 -04:00
|
|
|
|
2022-09-07 11:02:02 -04:00
|
|
|
import (
|
2023-08-30 02:24:09 -04:00
|
|
|
"errors"
|
2022-12-18 10:31:06 -05:00
|
|
|
"fmt"
|
2022-09-07 11:02:02 -04:00
|
|
|
"strconv"
|
2022-12-18 10:31:06 -05:00
|
|
|
"strings"
|
2022-09-07 11:02:02 -04:00
|
|
|
"time"
|
2023-07-14 00:54:09 -04:00
|
|
|
|
2023-08-30 02:24:09 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/conf"
|
2023-07-14 00:54:09 -04:00
|
|
|
"github.com/go-resty/resty/v2"
|
2022-09-07 11:02:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Panel is the interface for different panel's api.
|
|
|
|
|
|
|
|
type Client struct {
|
2023-11-17 17:05:28 -05:00
|
|
|
client *resty.Client
|
|
|
|
APIHost string
|
|
|
|
Token string
|
|
|
|
NodeType string
|
|
|
|
NodeId int
|
|
|
|
nodeEtag string
|
|
|
|
userEtag string
|
2024-02-06 06:26:30 -05:00
|
|
|
responseBodyHash string
|
2024-08-29 17:48:41 -04:00
|
|
|
UserList *UserListBody
|
|
|
|
AliveMap *AliveMap
|
2022-09-07 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2023-05-17 21:11:28 -04:00
|
|
|
func New(c *conf.ApiConfig) (*Client, error) {
|
2022-09-07 11:02:02 -04:00
|
|
|
client := resty.New()
|
|
|
|
client.SetRetryCount(3)
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.Timeout > 0 {
|
|
|
|
client.SetTimeout(time.Duration(c.Timeout) * time.Second)
|
2022-09-07 11:02:02 -04:00
|
|
|
} else {
|
|
|
|
client.SetTimeout(5 * time.Second)
|
|
|
|
}
|
|
|
|
client.OnError(func(req *resty.Request, err error) {
|
2023-08-30 02:24:09 -04:00
|
|
|
var v *resty.ResponseError
|
|
|
|
if errors.As(err, &v) {
|
2022-09-07 11:02:02 -04:00
|
|
|
// v.Response contains the last response from the server
|
|
|
|
// v.Err contains the original error
|
2023-08-30 02:24:09 -04:00
|
|
|
logrus.Error(v.Err)
|
2022-09-07 11:02:02 -04:00
|
|
|
}
|
|
|
|
})
|
2022-12-18 10:31:06 -05:00
|
|
|
client.SetBaseURL(c.APIHost)
|
|
|
|
// Check node type
|
2023-05-18 23:27:08 -04:00
|
|
|
c.NodeType = strings.ToLower(c.NodeType)
|
2023-06-09 00:36:49 -04:00
|
|
|
switch c.NodeType {
|
2023-08-19 08:06:42 -04:00
|
|
|
case "v2ray":
|
|
|
|
c.NodeType = "vmess"
|
|
|
|
case
|
|
|
|
"vmess",
|
|
|
|
"trojan",
|
|
|
|
"shadowsocks",
|
|
|
|
"hysteria",
|
2023-11-17 17:05:28 -05:00
|
|
|
"hysteria2",
|
2023-08-19 08:06:42 -04:00
|
|
|
"vless":
|
2023-06-09 00:36:49 -04:00
|
|
|
default:
|
2022-12-18 10:31:06 -05:00
|
|
|
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
|
|
|
|
}
|
2023-07-14 00:54:09 -04:00
|
|
|
// set params
|
2022-09-07 11:02:02 -04:00
|
|
|
client.SetQueryParams(map[string]string{
|
2023-05-18 23:27:08 -04:00
|
|
|
"node_type": c.NodeType,
|
2022-12-18 10:31:06 -05:00
|
|
|
"node_id": strconv.Itoa(c.NodeID),
|
|
|
|
"token": c.Key,
|
2022-09-07 11:02:02 -04:00
|
|
|
})
|
|
|
|
return &Client{
|
2023-08-30 02:24:09 -04:00
|
|
|
client: client,
|
|
|
|
Token: c.Key,
|
|
|
|
APIHost: c.APIHost,
|
|
|
|
NodeType: c.NodeType,
|
|
|
|
NodeId: c.NodeID,
|
2024-08-29 17:48:41 -04:00
|
|
|
UserList: &UserListBody{},
|
|
|
|
AliveMap: &AliveMap{},
|
2022-12-18 10:31:06 -05:00
|
|
|
}, nil
|
|
|
|
}
|