V2bX/api/panel/panel.go

79 lines
1.6 KiB
Go
Raw Normal View History

package panel
2022-06-04 00:05:46 -04:00
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
2023-07-14 00:54: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"
)
// Panel is the interface for different panel's api.
type Client struct {
client *resty.Client
APIHost string
Token string
NodeType string
NodeId int
nodeEtag string
userEtag string
responseBodyHash string
2024-08-29 17:48:41 -04:00
UserList *UserListBody
AliveMap *AliveMap
}
func New(c *conf.ApiConfig) (*Client, error) {
client := resty.New()
client.SetRetryCount(3)
if c.Timeout > 0 {
client.SetTimeout(time.Duration(c.Timeout) * time.Second)
} else {
client.SetTimeout(5 * time.Second)
}
client.OnError(func(req *resty.Request, err error) {
var v *resty.ResponseError
if errors.As(err, &v) {
// v.Response contains the last response from the server
// v.Err contains the original error
logrus.Error(v.Err)
}
})
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",
"hysteria2",
2023-08-19 08:06:42 -04:00
"vless":
2023-06-09 00:36:49 -04:00
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
}
2023-07-14 00:54:09 -04:00
// set params
client.SetQueryParams(map[string]string{
2023-05-18 23:27:08 -04:00
"node_type": c.NodeType,
"node_id": strconv.Itoa(c.NodeID),
"token": c.Key,
})
return &Client{
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{},
}, nil
}