Skip to content

Commit

Permalink
7-5
Browse files Browse the repository at this point in the history
  • Loading branch information
aiastia committed Jul 5, 2023
1 parent 93ee0d4 commit d70ea29
Show file tree
Hide file tree
Showing 17 changed files with 536 additions and 323 deletions.
9 changes: 8 additions & 1 deletion api/apimodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"github.com/xtls/xray-core/infra/conf"
)

const (
UserNotModified = "users not modified"
NodeNotModified = "node not modified"
RuleNotModified = "rules not modified"
)

// Config API config
type Config struct {
APIHost string `mapstructure:"ApiHost"`
NodeID int `mapstructure:"NodeID"`
Key string `mapstructure:"ApiKey"`
NodeType string `mapstructure:"NodeType"`
EnableVless bool `mapstructure:"EnableVless"`
EnableXTLS bool `mapstructure:"EnableXTLS"`
VlessFlow string `mapstructure:"VlessFlow"`
Timeout int `mapstructure:"Timeout"`
SpeedLimit float64 `mapstructure:"SpeedLimit"`
DeviceLimit int `mapstructure:"DeviceLimit"`
Expand Down Expand Up @@ -42,6 +48,7 @@ type NodeInfo struct {
Path string
EnableTLS bool
EnableVless bool
VlessFlow string
CypherMethod string
ServerKey string
ServiceName string
Expand Down
30 changes: 22 additions & 8 deletions api/newV2board/v2board.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type APIClient struct {
Key string
NodeType string
EnableVless bool
EnableXTLS bool
VlessFlow string
SpeedLimit float64
DeviceLimit int
LocalRuleList []api.DetectRule
resp atomic.Value
eTag string
eTags map[string]string
}

// New create an api instance
Expand Down Expand Up @@ -69,10 +69,11 @@ func New(apiConfig *api.Config) *APIClient {
APIHost: apiConfig.APIHost,
NodeType: apiConfig.NodeType,
EnableVless: apiConfig.EnableVless,
EnableXTLS: apiConfig.EnableXTLS,
VlessFlow: apiConfig.VlessFlow,
SpeedLimit: apiConfig.SpeedLimit,
DeviceLimit: apiConfig.DeviceLimit,
LocalRuleList: localRuleList,
eTags: make(map[string]string),
}
return apiClient
}
Expand Down Expand Up @@ -147,9 +148,19 @@ func (c *APIClient) GetNodeInfo() (nodeInfo *api.NodeInfo, err error) {
path := "/api/v1/server/UniProxy/config"

res, err := c.client.R().
SetHeader("If-None-Match", c.eTags["node"]).
ForceContentType("application/json").
Get(path)

// Etag identifier for a specific version of a resource. StatusCode = 304 means no changed
if res.StatusCode() == 304 {
return nil, errors.New(api.NodeNotModified)
}
// update etag
if res.Header().Get("Etag") != "" && res.Header().Get("Etag") != c.eTags["node"] {
c.eTags["node"] = res.Header().Get("Etag")
}

nodeInfoResp, err := c.parseResponse(res, path, err)
if err != nil {
return nil, err
Expand Down Expand Up @@ -194,17 +205,17 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
}

res, err := c.client.R().
SetHeader("If-None-Match", c.eTag).
SetHeader("If-None-Match", c.eTags["users"]).
ForceContentType("application/json").
Get(path)

// Etag identifier for a specific version of a resource. StatusCode = 304 means no changed
if res.StatusCode() == 304 {
return nil, errors.New("users no change")
return nil, errors.New(api.UserNotModified)
}
// update etag
if res.Header().Get("Etag") != "" && res.Header().Get("Etag") != c.eTag {
c.eTag = res.Header().Get("Etag")
if res.Header().Get("Etag") != "" && res.Header().Get("Etag") != c.eTags["users"] {
c.eTags["users"] = res.Header().Get("Etag")
}

usersResp, err := c.parseResponse(res, path, err)
Expand All @@ -213,6 +224,9 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
}
b, _ := usersResp.Get("users").Encode()
json.Unmarshal(b, &users)
if len(users) == 0 {
return nil, errors.New("users is null")
}

userList := make([]api.UserInfo, len(users))
for i := 0; i < len(users); i++ {
Expand Down Expand Up @@ -293,7 +307,6 @@ func (c *APIClient) ReportIllegal(detectResultList *[]api.DetectResult) error {

// parseTrojanNodeResponse parse the response for the given nodeInfo format
func (c *APIClient) parseTrojanNodeResponse(s *serverConfig) (*api.NodeInfo, error) {

// Create GeneralNodeInfo
nodeInfo := &api.NodeInfo{
NodeType: c.NodeType,
Expand Down Expand Up @@ -382,6 +395,7 @@ func (c *APIClient) parseV2rayNodeResponse(s *serverConfig) (*api.NodeInfo, erro
Path: s.NetworkSettings.Path,
Host: host,
EnableVless: c.EnableVless,
VlessFlow: c.VlessFlow,
ServiceName: s.NetworkSettings.ServiceName,
Header: header,
NameServerConfig: s.parseDNSConfig(),
Expand Down
47 changes: 27 additions & 20 deletions api/pmpanel/pmpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type APIClient struct {
Key string
NodeType string
EnableVless bool
EnableXTLS bool
VlessFlow string
SpeedLimit float64
DeviceLimit int
LocalRuleList []api.DetectRule
Expand Down Expand Up @@ -61,7 +61,7 @@ func New(apiConfig *api.Config) *APIClient {
APIHost: apiConfig.APIHost,
NodeType: apiConfig.NodeType,
EnableVless: apiConfig.EnableVless,
EnableXTLS: apiConfig.EnableXTLS,
VlessFlow: apiConfig.VlessFlow,
SpeedLimit: apiConfig.SpeedLimit,
DeviceLimit: apiConfig.DeviceLimit,
LocalRuleList: localRuleList,
Expand Down Expand Up @@ -360,7 +360,7 @@ func (c *APIClient) ReportIllegal(detectResultList *[]api.DetectResult) error {
func (c *APIClient) ParseV2rayNodeResponse(nodeInfoResponse *NodeInfoResponse) (*api.NodeInfo, error) {
var enableTLS bool
var path, host, transportProtocol, serviceName string
var speedlimit uint64 = 0
var speedLimit uint64 = 0

port := nodeInfoResponse.Port
alterID := nodeInfoResponse.AlterId
Expand All @@ -374,24 +374,31 @@ func (c *APIClient) ParseV2rayNodeResponse(nodeInfoResponse *NodeInfoResponse) (
case "tcp":
// TODO
}

// Compatible with more node types config
switch nodeInfoResponse.Security {
case "tls":
enableTLS = true
default:
enableTLS = false
}
if c.SpeedLimit > 0 {
speedlimit = uint64((c.SpeedLimit * 1000000) / 8)
speedLimit = uint64((c.SpeedLimit * 1000000) / 8)
} else {
speedlimit = uint64((nodeInfoResponse.SpeedLimit * 1000000) / 8)
speedLimit = uint64((nodeInfoResponse.SpeedLimit * 1000000) / 8)
}
// Create GeneralNodeInfo
nodeinfo := &api.NodeInfo{
NodeType: c.NodeType,
NodeID: c.NodeID,
Port: port,
SpeedLimit: speedlimit,
SpeedLimit: speedLimit,
AlterID: alterID,
TransportProtocol: transportProtocol,
EnableTLS: enableTLS,
Path: path,
Host: host,
EnableVless: c.EnableVless,
VlessFlow: c.VlessFlow,
ServiceName: serviceName,
}

Expand All @@ -400,24 +407,24 @@ func (c *APIClient) ParseV2rayNodeResponse(nodeInfoResponse *NodeInfoResponse) (

// ParseSSNodeResponse parse the response for the given nodeinfor format
func (c *APIClient) ParseSSNodeResponse(nodeInfoResponse *NodeInfoResponse) (*api.NodeInfo, error) {
var speedlimit uint64 = 0
var speedLimit uint64 = 0

if c.SpeedLimit > 0 {
speedlimit = uint64((c.SpeedLimit * 1000000) / 8)
speedLimit = uint64((c.SpeedLimit * 1000000) / 8)
} else {
speedlimit = uint64((nodeInfoResponse.SpeedLimit * 1000000) / 8)
speedLimit = uint64((nodeInfoResponse.SpeedLimit * 1000000) / 8)
}
// Create GeneralNodeInfo
nodeinfo := &api.NodeInfo{
nodeInfo := &api.NodeInfo{
NodeType: c.NodeType,
NodeID: c.NodeID,
Port: nodeInfoResponse.Port,
SpeedLimit: speedlimit,
SpeedLimit: speedLimit,
TransportProtocol: "tcp",
CypherMethod: nodeInfoResponse.Method,
}

return nodeinfo, nil
return nodeInfo, nil
}

// ParseTrojanNodeResponse parse the response for the given nodeinfor format
Expand All @@ -439,7 +446,7 @@ func (c *APIClient) ParseTrojanNodeResponse(nodeInfoResponse *NodeInfoResponse)
transportProtocol = "grpc"
}
// Create GeneralNodeInfo
nodeinfo := &api.NodeInfo{
nodeInfo := &api.NodeInfo{
NodeType: c.NodeType,
NodeID: c.NodeID,
Port: port,
Expand All @@ -450,13 +457,13 @@ func (c *APIClient) ParseTrojanNodeResponse(nodeInfoResponse *NodeInfoResponse)
ServiceName: nodeInfoResponse.Sni,
}

return nodeinfo, nil
return nodeInfo, nil
}

// ParseUserListResponse parse the response for the given nodeinfo format
func (c *APIClient) ParseUserListResponse(userInfoResponse *[]UserResponse) (*[]api.UserInfo, error) {
var deviceLimit int = 0
var speedlimit uint64 = 0
var deviceLimit = 0
var speedLimit uint64 = 0
userList := make([]api.UserInfo, len(*userInfoResponse))
for i, user := range *userInfoResponse {
if c.DeviceLimit > 0 {
Expand All @@ -465,15 +472,15 @@ func (c *APIClient) ParseUserListResponse(userInfoResponse *[]UserResponse) (*[]
deviceLimit = user.DeviceLimit
}
if c.SpeedLimit > 0 {
speedlimit = uint64((c.SpeedLimit * 1000000) / 8)
speedLimit = uint64((c.SpeedLimit * 1000000) / 8)
} else {
speedlimit = uint64((user.SpeedLimit * 1000000) / 8)
speedLimit = uint64((user.SpeedLimit * 1000000) / 8)
}
userList[i] = api.UserInfo{
UID: user.ID,
Passwd: user.Passwd,
UUID: user.Passwd,
SpeedLimit: speedlimit,
SpeedLimit: speedLimit,
DeviceLimit: deviceLimit,
}
}
Expand Down
Loading

0 comments on commit d70ea29

Please sign in to comment.