Add dashboard support for API service
This commit is contained in:
@@ -0,0 +1,310 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/service"
|
||||
"github.com/sagernet/sing/service/filemanager"
|
||||
)
|
||||
|
||||
const (
|
||||
dashboardRoutePrefix = "/dashboard/"
|
||||
dashboardEtagFileName = ".etag"
|
||||
defaultDashboardURL = "https://github.com/SagerNet/sing-box-dashboard/archive/refs/heads/gh-pages.zip"
|
||||
)
|
||||
|
||||
type dashboardStatus int
|
||||
|
||||
const (
|
||||
dashboardEmpty dashboardStatus = iota
|
||||
dashboardManaged
|
||||
dashboardUserProvided
|
||||
)
|
||||
|
||||
type dashboard struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
logger log.ContextLogger
|
||||
options option.APIDashboardOptions
|
||||
path string
|
||||
url string
|
||||
updateInterval time.Duration
|
||||
fileServer http.Handler
|
||||
httpClient *http.Client
|
||||
lastEtag string
|
||||
lastUpdated time.Time
|
||||
}
|
||||
|
||||
func newDashboard(ctx context.Context, logger log.ContextLogger, options option.APIDashboardOptions) *dashboard {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
path := options.Path
|
||||
if path == "" {
|
||||
path = "dashboard"
|
||||
}
|
||||
path = filemanager.BasePath(ctx, os.ExpandEnv(path))
|
||||
url := options.DownloadURL
|
||||
if url == "" {
|
||||
url = defaultDashboardURL
|
||||
}
|
||||
updateInterval := 24 * time.Hour
|
||||
if options.UpdateInterval > 0 {
|
||||
updateInterval = time.Duration(options.UpdateInterval)
|
||||
}
|
||||
return &dashboard{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
logger: logger,
|
||||
options: options,
|
||||
path: path,
|
||||
url: url,
|
||||
updateInterval: updateInterval,
|
||||
fileServer: http.StripPrefix(dashboardRoutePrefix, http.FileServer(dashboardDir(path))),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dashboard) start() error {
|
||||
transport, err := d.resolveTransport()
|
||||
if err != nil {
|
||||
return E.Cause(err, "create dashboard http client")
|
||||
}
|
||||
d.httpClient = &http.Client{Transport: transport}
|
||||
go d.loopUpdate()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dashboard) close() error {
|
||||
d.cancel()
|
||||
if d.httpClient != nil {
|
||||
d.httpClient.CloseIdleConnections()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dashboard) resolveTransport() (adapter.HTTPTransport, error) {
|
||||
httpClientManager := service.FromContext[adapter.HTTPClientManager](d.ctx)
|
||||
if httpClientManager == nil {
|
||||
return nil, E.New("missing http client manager in context")
|
||||
}
|
||||
if d.options.HTTPClient != nil && !d.options.HTTPClient.IsEmpty() {
|
||||
return httpClientManager.ResolveTransport(d.ctx, d.logger, *d.options.HTTPClient)
|
||||
}
|
||||
defaultTransport := httpClientManager.DefaultTransport()
|
||||
if defaultTransport == nil {
|
||||
return nil, E.New("default http client transport is not initialized")
|
||||
}
|
||||
return defaultTransport, nil
|
||||
}
|
||||
|
||||
func (d *dashboard) serveHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
if strings.HasPrefix(request.URL.Path, dashboardRoutePrefix) {
|
||||
d.fileServer.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
http.Redirect(writer, request, dashboardRoutePrefix, http.StatusFound)
|
||||
}
|
||||
|
||||
func (d *dashboard) loopUpdate() {
|
||||
status := d.loadState()
|
||||
if status == dashboardUserProvided {
|
||||
d.logger.Info("dashboard: serving user-provided files at ", d.path, ", auto-update disabled")
|
||||
return
|
||||
}
|
||||
var nextUpdate time.Time
|
||||
if status == dashboardManaged {
|
||||
nextUpdate = d.lastUpdated.Add(d.updateInterval)
|
||||
}
|
||||
timer := time.NewTimer(0)
|
||||
defer timer.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-d.ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
}
|
||||
now := time.Now()
|
||||
if !now.Before(nextUpdate) {
|
||||
err := d.fetch(d.ctx)
|
||||
if err != nil {
|
||||
d.logger.Error(E.Cause(err, "update dashboard"))
|
||||
nextUpdate = now.Add(d.updateInterval)
|
||||
} else {
|
||||
nextUpdate = d.lastUpdated.Add(d.updateInterval)
|
||||
}
|
||||
}
|
||||
timer.Reset(max(time.Until(nextUpdate), 0))
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dashboard) loadState() dashboardStatus {
|
||||
entries, err := os.ReadDir(d.path)
|
||||
if err != nil {
|
||||
return dashboardEmpty
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
return dashboardEmpty
|
||||
}
|
||||
etagPath := filepath.Join(d.path, dashboardEtagFileName)
|
||||
etagBytes, err := os.ReadFile(etagPath)
|
||||
if err != nil {
|
||||
return dashboardUserProvided
|
||||
}
|
||||
d.lastEtag = strings.TrimSpace(string(etagBytes))
|
||||
info, err := os.Stat(etagPath)
|
||||
if err == nil {
|
||||
d.lastUpdated = info.ModTime()
|
||||
}
|
||||
return dashboardManaged
|
||||
}
|
||||
|
||||
func (d *dashboard) fetch(ctx context.Context) error {
|
||||
d.logger.Info("updating dashboard from URL: ", d.url)
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, d.url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.lastEtag != "" {
|
||||
request.Header.Set("If-None-Match", d.lastEtag)
|
||||
}
|
||||
defer d.httpClient.CloseIdleConnections()
|
||||
response, err := d.httpClient.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
switch response.StatusCode {
|
||||
case http.StatusOK:
|
||||
case http.StatusNotModified:
|
||||
d.lastUpdated = time.Now()
|
||||
err = filemanager.WriteFile(d.ctx, filepath.Join(d.path, dashboardEtagFileName), []byte(d.lastEtag), 0o644)
|
||||
if err != nil {
|
||||
d.logger.Warn(E.Cause(err, "save dashboard update time"))
|
||||
}
|
||||
d.logger.Info("dashboard: not modified")
|
||||
return nil
|
||||
default:
|
||||
return E.New("unexpected status: ", response.Status)
|
||||
}
|
||||
etag := response.Header.Get("Etag")
|
||||
err = d.extract(response.Body, etag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.lastEtag = etag
|
||||
d.lastUpdated = time.Now()
|
||||
d.logger.Info("dashboard: updated")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dashboard) extract(body io.Reader, etag string) error {
|
||||
tempFile, err := filemanager.CreateTemp(d.ctx, "sing-box-dashboard-*.zip")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tempZipPath := tempFile.Name()
|
||||
defer os.Remove(tempZipPath)
|
||||
_, err = io.Copy(tempFile, body)
|
||||
tempFile.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader, err := zip.OpenReader(tempZipPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
tempDir := d.path + ".tmp"
|
||||
err = filemanager.RemoveAll(d.ctx, tempDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = filemanager.MkdirAll(d.ctx, tempDir, 0o755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
trimDir := zipIsInSingleDirectory(reader.File)
|
||||
for _, file := range reader.File {
|
||||
if file.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
pathElements := strings.Split(file.Name, "/")
|
||||
if trimDir {
|
||||
pathElements = pathElements[1:]
|
||||
}
|
||||
if len(pathElements) == 0 {
|
||||
continue
|
||||
}
|
||||
relativePath := filepath.Join(pathElements...)
|
||||
if !filepath.IsLocal(relativePath) {
|
||||
filemanager.RemoveAll(d.ctx, tempDir)
|
||||
return E.New("invalid dashboard archive entry: ", file.Name)
|
||||
}
|
||||
savePath := filepath.Join(tempDir, relativePath)
|
||||
err = filemanager.MkdirAll(d.ctx, filepath.Dir(savePath), 0o755)
|
||||
if err != nil {
|
||||
filemanager.RemoveAll(d.ctx, tempDir)
|
||||
return err
|
||||
}
|
||||
err = extractZipEntry(d.ctx, file, savePath)
|
||||
if err != nil {
|
||||
filemanager.RemoveAll(d.ctx, tempDir)
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = filemanager.WriteFile(d.ctx, filepath.Join(tempDir, dashboardEtagFileName), []byte(etag), 0o644)
|
||||
if err != nil {
|
||||
filemanager.RemoveAll(d.ctx, tempDir)
|
||||
return err
|
||||
}
|
||||
err = filemanager.RemoveAll(d.ctx, d.path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tempDir, d.path)
|
||||
}
|
||||
|
||||
func extractZipEntry(ctx context.Context, file *zip.File, savePath string) error {
|
||||
reader, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
writer, err := filemanager.Create(ctx, savePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer writer.Close()
|
||||
_, err = io.Copy(writer, reader)
|
||||
return err
|
||||
}
|
||||
|
||||
// GitHub archives wrap every file under a single "<repo>-<branch>/" top-level directory.
|
||||
func zipIsInSingleDirectory(files []*zip.File) bool {
|
||||
var dirName string
|
||||
for _, file := range files {
|
||||
if file.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
pathElements := strings.Split(file.Name, "/")
|
||||
if len(pathElements) < 2 {
|
||||
return false
|
||||
}
|
||||
if dirName == "" {
|
||||
dirName = pathElements[0]
|
||||
} else if dirName != pathElements[0] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return dirName != ""
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
type dashboardDir http.Dir
|
||||
|
||||
func (d dashboardDir) Open(name string) (http.File, error) {
|
||||
file, err := http.Dir(d).Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fileWrapper{file}, nil
|
||||
}
|
||||
|
||||
// workaround for #2345 #2596
|
||||
type fileWrapper struct {
|
||||
http.File
|
||||
}
|
||||
+14
-1
@@ -38,6 +38,7 @@ type Service struct {
|
||||
startedService *daemon.StartedService
|
||||
grpcServer *grpc.Server
|
||||
httpServer *http.Server
|
||||
dashboard *dashboard
|
||||
}
|
||||
|
||||
func NewService(ctx context.Context, logger log.ContextLogger, tag string, options option.APIServiceOptions) (adapter.Service, error) {
|
||||
@@ -63,6 +64,9 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio
|
||||
}
|
||||
s.tlsConfig = tlsConfig
|
||||
}
|
||||
if options.Dashboard != nil && options.Dashboard.Enabled {
|
||||
s.dashboard = newDashboard(ctx, logger, *options.Dashboard)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -72,8 +76,14 @@ func (s *Service) Start(stage adapter.StartStage) error {
|
||||
}
|
||||
s.startedService = daemon.NewAttachedService(s.ctx)
|
||||
s.grpcServer = daemon.NewServer(s.startedService, s.options.Secret)
|
||||
if s.dashboard != nil {
|
||||
err := s.dashboard.start()
|
||||
if err != nil {
|
||||
return E.Cause(err, "start dashboard")
|
||||
}
|
||||
}
|
||||
s.httpServer = &http.Server{
|
||||
Handler: h2c.NewHandler(newHTTPHandler(s.logger, s.grpcServer, s.options), new(http2.Server)),
|
||||
Handler: h2c.NewHandler(newHTTPHandler(s.logger, s.grpcServer, s.options, s.dashboard), new(http2.Server)),
|
||||
BaseContext: func(net.Listener) context.Context {
|
||||
return s.ctx
|
||||
},
|
||||
@@ -108,6 +118,9 @@ func (s *Service) Start(stage adapter.StartStage) error {
|
||||
|
||||
func (s *Service) Close() error {
|
||||
s.cancel()
|
||||
if s.dashboard != nil {
|
||||
s.dashboard.close()
|
||||
}
|
||||
if s.httpServer != nil {
|
||||
s.httpServer.Close()
|
||||
}
|
||||
|
||||
@@ -26,14 +26,14 @@ const (
|
||||
// (https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) and gRPC-Web
|
||||
// streams over WebSocket, wire compatible with the improbable-eng/grpc-web
|
||||
// client transports.
|
||||
func newHTTPHandler(logger log.ContextLogger, grpcServer *grpc.Server, options option.APIServiceOptions) http.Handler {
|
||||
func newHTTPHandler(logger log.ContextLogger, grpcServer *grpc.Server, options option.APIServiceOptions, dashboard *dashboard) http.Handler {
|
||||
allowedOrigins := options.AccessControlAllowOrigin
|
||||
if len(allowedOrigins) == 0 {
|
||||
allowedOrigins = []string{"*"}
|
||||
}
|
||||
corsHandler := cors.New(cors.Options{
|
||||
AllowedOrigins: allowedOrigins,
|
||||
AllowedMethods: []string{http.MethodPost, http.MethodOptions},
|
||||
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodOptions},
|
||||
AllowedHeaders: []string{"Content-Type", "Authorization", "X-Grpc-Web", "X-User-Agent", "Grpc-Timeout"},
|
||||
ExposedHeaders: []string{"Grpc-Status", "Grpc-Message", "Grpc-Status-Details-Bin"},
|
||||
AllowPrivateNetwork: options.AccessControlAllowPrivateNetwork,
|
||||
@@ -42,12 +42,14 @@ func newHTTPHandler(logger log.ContextLogger, grpcServer *grpc.Server, options o
|
||||
return corsHandler.Handler(&webBridge{
|
||||
logger: logger,
|
||||
grpcServer: grpcServer,
|
||||
dashboard: dashboard,
|
||||
})
|
||||
}
|
||||
|
||||
type webBridge struct {
|
||||
logger log.ContextLogger
|
||||
grpcServer *grpc.Server
|
||||
dashboard *dashboard
|
||||
}
|
||||
|
||||
func (b *webBridge) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
@@ -59,6 +61,8 @@ func (b *webBridge) ServeHTTP(writer http.ResponseWriter, request *http.Request)
|
||||
b.serveWeb(writer, request)
|
||||
case request.ProtoMajor == 2 && strings.HasPrefix(contentType, contentTypeGRPC):
|
||||
b.grpcServer.ServeHTTP(writer, request)
|
||||
case b.dashboard != nil:
|
||||
b.dashboard.serveHTTP(writer, request)
|
||||
default:
|
||||
http.NotFound(writer, request)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user