V2bX/api/api.go

79 lines
1.8 KiB
Go
Raw Normal View History

2022-06-01 13:35:41 -04:00
// Package api contains all the api used by XrayR
// To implement an api , one needs to implement the interface below.
package api
2022-06-04 00:05:46 -04:00
import (
"github.com/Yuzuki616/V2bX/conf"
2022-06-04 00:05:46 -04:00
"github.com/go-resty/resty/v2"
"log"
"strconv"
"sync"
"time"
)
2022-06-01 13:35:41 -04:00
// API is the interface for different panel's api.
2022-06-04 00:05:46 -04:00
type ClientInfo struct {
APIHost string
NodeID int
Key string
NodeType string
}
type Client struct {
2022-06-04 04:59:10 -04:00
client *resty.Client
APIHost string
NodeID int
Key string
NodeType string
//EnableSS2022 bool
2022-06-09 09:19:57 -04:00
EnableVless bool
EnableXTLS bool
SpeedLimit float64
DeviceLimit int
LocalRuleList []DetectRule
2022-06-12 09:10:20 -04:00
RemoteRuleCache *[]Rule
2022-06-09 09:19:57 -04:00
access sync.Mutex
NodeInfoRspMd5 [16]byte
NodeRuleRspMd5 [16]byte
2022-06-04 00:05:46 -04:00
}
func New(apiConfig *conf.ApiConfig) API {
2022-06-04 00:05:46 -04:00
client := resty.New()
client.SetRetryCount(3)
if apiConfig.Timeout > 0 {
client.SetTimeout(time.Duration(apiConfig.Timeout) * time.Second)
} else {
client.SetTimeout(5 * time.Second)
}
client.OnError(func(req *resty.Request, err error) {
if v, ok := err.(*resty.ResponseError); ok {
// v.Response contains the last response from the server
// v.Err contains the original error
log.Print(v.Err)
}
})
client.SetBaseURL(apiConfig.APIHost)
// Create Key for each requests
client.SetQueryParams(map[string]string{
"node_id": strconv.Itoa(apiConfig.NodeID),
"token": apiConfig.Key,
})
// Read local rule list
localRuleList := readLocalRuleList(apiConfig.RuleListPath)
2022-06-06 03:23:40 -04:00
return &Client{
2022-06-04 04:59:10 -04:00
client: client,
NodeID: apiConfig.NodeID,
Key: apiConfig.Key,
APIHost: apiConfig.APIHost,
NodeType: apiConfig.NodeType,
//EnableSS2022: apiConfig.EnableSS2022,
2022-06-04 00:05:46 -04:00
EnableVless: apiConfig.EnableVless,
EnableXTLS: apiConfig.EnableXTLS,
SpeedLimit: apiConfig.SpeedLimit,
DeviceLimit: apiConfig.DeviceLimit,
LocalRuleList: localRuleList,
}
2022-06-01 13:35:41 -04:00
}