mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-05-26 14:11:46 +03:00
Add new admin panel, failover, dns fallback, providers, limiters. Update XHTTP
This commit is contained in:
133
service/manager_api/http/client/client.go
Normal file
133
service/manager_api/http/client/client.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
boxService "github.com/sagernet/sing-box/adapter/service"
|
||||
"github.com/sagernet/sing-box/common/dialer"
|
||||
"github.com/sagernet/sing-box/common/tls"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
CM "github.com/sagernet/sing-box/service/manager/constant"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
boxService.Adapter
|
||||
|
||||
ctx context.Context
|
||||
logger log.ContextLogger
|
||||
options option.ManagerAPIClientOptions
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func NewClient(ctx context.Context, logger log.ContextLogger, tag string, options option.ManagerAPIClientOptions) (*Client, error) {
|
||||
outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return outboundDialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
|
||||
},
|
||||
}
|
||||
scheme := "http"
|
||||
if options.TLS != nil {
|
||||
tlsConfig, err := tls.NewClient(ctx, logger, options.Server, common.PtrValueOrDefault(options.TLS))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !common.Contains(tlsConfig.NextProtos(), "http/1.1") {
|
||||
tlsConfig.SetNextProtos(append([]string{"http/1.1"}, tlsConfig.NextProtos()...))
|
||||
}
|
||||
transport.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
rawConn, err := outboundDialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tls.ClientHandshake(ctx, rawConn, tlsConfig)
|
||||
}
|
||||
scheme = "https"
|
||||
}
|
||||
return &Client{
|
||||
Adapter: boxService.NewAdapter(C.TypeManagerAPI, tag),
|
||||
ctx: ctx,
|
||||
logger: logger,
|
||||
options: options,
|
||||
httpClient: &http.Client{Transport: transport},
|
||||
baseURL: scheme + "://" + options.ServerOptions.Build().String() + "/manager/v1",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Client) Start(stage adapter.StartStage) error { return nil }
|
||||
|
||||
func (s *Client) Close() error {
|
||||
s.httpClient.CloseIdleConnections()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Client) doJSON(method, path string, query url.Values, body any, out any) error {
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reqBody = bytes.NewReader(b)
|
||||
}
|
||||
u := s.baseURL + path
|
||||
if len(query) > 0 {
|
||||
u += "?" + query.Encode()
|
||||
}
|
||||
req, err := http.NewRequestWithContext(s.ctx, method, u, reqBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if s.options.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+s.options.APIKey)
|
||||
}
|
||||
resp, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return CM.ErrNotFound
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := io.ReadAll(resp.Body)
|
||||
return E.New("manager-api http ", resp.StatusCode, ": ", string(msg))
|
||||
}
|
||||
if out != nil {
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func filtersQuery(filters map[string][]string) url.Values {
|
||||
q := url.Values{}
|
||||
for k, vs := range filters {
|
||||
for _, v := range vs {
|
||||
q.Add(k, v)
|
||||
}
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func intPath(id int) string { return "/" + strconv.Itoa(id) }
|
||||
func stringPath(id string) string { return "/" + id }
|
||||
233
service/manager_api/http/client/manager.go
Normal file
233
service/manager_api/http/client/manager.go
Normal file
@@ -0,0 +1,233 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
CM "github.com/sagernet/sing-box/service/manager/constant"
|
||||
)
|
||||
|
||||
var _ CM.Manager = (*Client)(nil)
|
||||
|
||||
type countReply struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func (s *Client) CreateSquad(in CM.SquadCreate) (CM.Squad, error) {
|
||||
return postItem[CM.Squad](s, "/squads", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetSquads(f map[string][]string) ([]CM.Squad, error) {
|
||||
return getList[CM.Squad](s, "/squads", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetSquadsCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/squads", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetSquad(id int) (CM.Squad, error) {
|
||||
return getItem[CM.Squad](s, "/squads"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateSquad(id int, in CM.SquadUpdate) (CM.Squad, error) {
|
||||
return putItem[CM.Squad](s, "/squads"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteSquad(id int) (CM.Squad, error) {
|
||||
return deleteItem[CM.Squad](s, "/squads"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) CreateNode(in CM.NodeCreate) (CM.Node, error) {
|
||||
return postItem[CM.Node](s, "/nodes", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetNodes(f map[string][]string) ([]CM.Node, error) {
|
||||
return getList[CM.Node](s, "/nodes", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetNodesCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/nodes", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetNode(uuid string) (CM.Node, error) {
|
||||
return getItem[CM.Node](s, "/nodes"+stringPath(uuid))
|
||||
}
|
||||
|
||||
func (s *Client) GetNodeStatus(uuid string) (string, error) {
|
||||
var reply struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := s.doJSON(http.MethodGet, "/nodes"+stringPath(uuid)+"/status", nil, nil, &reply); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return reply.Status, nil
|
||||
}
|
||||
|
||||
func (s *Client) UpdateNode(uuid string, in CM.NodeUpdate) (CM.Node, error) {
|
||||
return putItem[CM.Node](s, "/nodes"+stringPath(uuid), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteNode(uuid string) (CM.Node, error) {
|
||||
return deleteItem[CM.Node](s, "/nodes"+stringPath(uuid))
|
||||
}
|
||||
|
||||
func (s *Client) CreateUser(in CM.UserCreate) (CM.User, error) {
|
||||
return postItem[CM.User](s, "/users", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetUsers(f map[string][]string) ([]CM.User, error) {
|
||||
return getList[CM.User](s, "/users", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetUsersCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/users", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetUser(id int) (CM.User, error) {
|
||||
return getItem[CM.User](s, "/users"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateUser(id int, in CM.UserUpdate) (CM.User, error) {
|
||||
return putItem[CM.User](s, "/users"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteUser(id int) (CM.User, error) {
|
||||
return deleteItem[CM.User](s, "/users"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) CreateBandwidthLimiter(in CM.BandwidthLimiterCreate) (CM.BandwidthLimiter, error) {
|
||||
return postItem[CM.BandwidthLimiter](s, "/bandwidth-limiters", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetBandwidthLimiters(f map[string][]string) ([]CM.BandwidthLimiter, error) {
|
||||
return getList[CM.BandwidthLimiter](s, "/bandwidth-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetBandwidthLimitersCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/bandwidth-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetBandwidthLimiter(id int) (CM.BandwidthLimiter, error) {
|
||||
return getItem[CM.BandwidthLimiter](s, "/bandwidth-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateBandwidthLimiter(id int, in CM.BandwidthLimiterUpdate) (CM.BandwidthLimiter, error) {
|
||||
return putItem[CM.BandwidthLimiter](s, "/bandwidth-limiters"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteBandwidthLimiter(id int) (CM.BandwidthLimiter, error) {
|
||||
return deleteItem[CM.BandwidthLimiter](s, "/bandwidth-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) CreateTrafficLimiter(in CM.TrafficLimiterCreate) (CM.TrafficLimiter, error) {
|
||||
return postItem[CM.TrafficLimiter](s, "/traffic-limiters", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetTrafficLimiters(f map[string][]string) ([]CM.TrafficLimiter, error) {
|
||||
return getList[CM.TrafficLimiter](s, "/traffic-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetTrafficLimitersCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/traffic-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetTrafficLimiter(id int) (CM.TrafficLimiter, error) {
|
||||
return getItem[CM.TrafficLimiter](s, "/traffic-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateTrafficLimiter(id int, in CM.TrafficLimiterUpdate) (CM.TrafficLimiter, error) {
|
||||
return putItem[CM.TrafficLimiter](s, "/traffic-limiters"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) UpdateTrafficLimiterUsed(id int, used uint64) (CM.TrafficLimiter, error) {
|
||||
return putItem[CM.TrafficLimiter](s, "/traffic-limiters"+intPath(id)+"/used", struct {
|
||||
Used uint64 `json:"used"`
|
||||
}{Used: used})
|
||||
}
|
||||
|
||||
func (s *Client) DeleteTrafficLimiter(id int) (CM.TrafficLimiter, error) {
|
||||
return deleteItem[CM.TrafficLimiter](s, "/traffic-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) CreateConnectionLimiter(in CM.ConnectionLimiterCreate) (CM.ConnectionLimiter, error) {
|
||||
return postItem[CM.ConnectionLimiter](s, "/connection-limiters", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetConnectionLimiters(f map[string][]string) ([]CM.ConnectionLimiter, error) {
|
||||
return getList[CM.ConnectionLimiter](s, "/connection-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetConnectionLimitersCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/connection-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetConnectionLimiter(id int) (CM.ConnectionLimiter, error) {
|
||||
return getItem[CM.ConnectionLimiter](s, "/connection-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateConnectionLimiter(id int, in CM.ConnectionLimiterUpdate) (CM.ConnectionLimiter, error) {
|
||||
return putItem[CM.ConnectionLimiter](s, "/connection-limiters"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteConnectionLimiter(id int) (CM.ConnectionLimiter, error) {
|
||||
return deleteItem[CM.ConnectionLimiter](s, "/connection-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) CreateRateLimiter(in CM.RateLimiterCreate) (CM.RateLimiter, error) {
|
||||
return postItem[CM.RateLimiter](s, "/rate-limiters", in)
|
||||
}
|
||||
|
||||
func (s *Client) GetRateLimiters(f map[string][]string) ([]CM.RateLimiter, error) {
|
||||
return getList[CM.RateLimiter](s, "/rate-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetRateLimitersCount(f map[string][]string) (int, error) {
|
||||
return getCount(s, "/rate-limiters", f)
|
||||
}
|
||||
|
||||
func (s *Client) GetRateLimiter(id int) (CM.RateLimiter, error) {
|
||||
return getItem[CM.RateLimiter](s, "/rate-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func (s *Client) UpdateRateLimiter(id int, in CM.RateLimiterUpdate) (CM.RateLimiter, error) {
|
||||
return putItem[CM.RateLimiter](s, "/rate-limiters"+intPath(id), in)
|
||||
}
|
||||
|
||||
func (s *Client) DeleteRateLimiter(id int) (CM.RateLimiter, error) {
|
||||
return deleteItem[CM.RateLimiter](s, "/rate-limiters"+intPath(id))
|
||||
}
|
||||
|
||||
func getList[T any](c *Client, path string, filters map[string][]string) ([]T, error) {
|
||||
var out []T
|
||||
err := c.doJSON(http.MethodGet, path, filtersQuery(filters), nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func getCount(c *Client, path string, filters map[string][]string) (int, error) {
|
||||
var out countReply
|
||||
err := c.doJSON(http.MethodGet, path+"/count", filtersQuery(filters), nil, &out)
|
||||
return out.Count, err
|
||||
}
|
||||
|
||||
func getItem[T any](c *Client, path string) (T, error) {
|
||||
var out T
|
||||
err := c.doJSON(http.MethodGet, path, nil, nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func postItem[T any, In any](c *Client, path string, in In) (T, error) {
|
||||
var out T
|
||||
err := c.doJSON(http.MethodPost, path, nil, in, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func putItem[T any, In any](c *Client, path string, in In) (T, error) {
|
||||
var out T
|
||||
err := c.doJSON(http.MethodPut, path, nil, in, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func deleteItem[T any](c *Client, path string) (T, error) {
|
||||
var out T
|
||||
err := c.doJSON(http.MethodDelete, path, nil, nil, &out)
|
||||
return out, err
|
||||
}
|
||||
Reference in New Issue
Block a user