1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-07-26 11:44:22 -04:00

add webroot file serving at webcommmander

This commit is contained in:
Shelikhoo 2025-01-12 17:09:50 +00:00
parent 7e4079ba5b
commit e9b12173dc
No known key found for this signature in database
GPG Key ID: 4C9764E9FE80A3DC
3 changed files with 75 additions and 17 deletions

View File

@ -18,6 +18,9 @@ const (
type Config struct {
state protoimpl.MessageState `protogen:"open.v1"`
Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
WebRoot []byte `protobuf:"bytes,2,opt,name=web_root,json=webRoot,proto3" json:"web_root,omitempty"`
WebRootFile string `protobuf:"bytes,96002,opt,name=web_root_file,json=webRootFile,proto3" json:"web_root_file,omitempty"`
ApiMountpoint string `protobuf:"bytes,3,opt,name=api_mountpoint,json=apiMountpoint,proto3" json:"api_mountpoint,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@ -59,6 +62,27 @@ func (x *Config) GetTag() string {
return ""
}
func (x *Config) GetWebRoot() []byte {
if x != nil {
return x.WebRoot
}
return nil
}
func (x *Config) GetWebRootFile() string {
if x != nil {
return x.WebRootFile
}
return ""
}
func (x *Config) GetApiMountpoint() string {
if x != nil {
return x.ApiMountpoint
}
return ""
}
var File_app_commander_webcommander_config_proto protoreflect.FileDescriptor
var file_app_commander_webcommander_config_proto_rawDesc = []byte{
@ -69,20 +93,27 @@ var file_app_commander_webcommander_config_proto_rawDesc = []byte{
0x64, 0x65, 0x72, 0x2e, 0x77, 0x65, 0x62, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72,
0x1a, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x65, 0x78,
0x74, 0x2f, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x22, 0x37, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a, 0x03,
0x74, 0x61, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x3a, 0x1b,
0x82, 0xb5, 0x18, 0x17, 0x0a, 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x0c, 0x77,
0x65, 0x62, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x42, 0x90, 0x01, 0x0a, 0x29,
0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x61,
0x70, 0x70, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x2e, 0x77, 0x65, 0x62,
0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x50, 0x01, 0x5a, 0x39, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32,
0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x35, 0x2f, 0x61, 0x70, 0x70, 0x2f,
0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x2f, 0x77, 0x65, 0x62, 0x63, 0x6f, 0x6d,
0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0xaa, 0x02, 0x25, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43,
0x6f, 0x72, 0x65, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65,
0x72, 0x2e, 0x57, 0x65, 0x62, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x62, 0x06,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x74, 0x6f, 0x22, 0xaf, 0x01, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a,
0x03, 0x74, 0x61, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12,
0x19, 0x0a, 0x08, 0x77, 0x65, 0x62, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
0x0c, 0x52, 0x07, 0x77, 0x65, 0x62, 0x52, 0x6f, 0x6f, 0x74, 0x12, 0x34, 0x0a, 0x0d, 0x77, 0x65,
0x62, 0x5f, 0x72, 0x6f, 0x6f, 0x74, 0x5f, 0x66, 0x69, 0x6c, 0x65, 0x18, 0x82, 0xee, 0x05, 0x20,
0x01, 0x28, 0x09, 0x42, 0x0e, 0x82, 0xb5, 0x18, 0x0a, 0x22, 0x08, 0x77, 0x65, 0x62, 0x5f, 0x72,
0x6f, 0x6f, 0x74, 0x52, 0x0b, 0x77, 0x65, 0x62, 0x52, 0x6f, 0x6f, 0x74, 0x46, 0x69, 0x6c, 0x65,
0x12, 0x25, 0x0a, 0x0e, 0x61, 0x70, 0x69, 0x5f, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x70, 0x6f, 0x69,
0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x61, 0x70, 0x69, 0x4d, 0x6f, 0x75,
0x6e, 0x74, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x3a, 0x1b, 0x82, 0xb5, 0x18, 0x17, 0x0a, 0x07, 0x73,
0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x0c, 0x77, 0x65, 0x62, 0x63, 0x6f, 0x6d, 0x6d, 0x61,
0x6e, 0x64, 0x65, 0x72, 0x42, 0x90, 0x01, 0x0a, 0x29, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72,
0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x63, 0x6f, 0x6d, 0x6d,
0x61, 0x6e, 0x64, 0x65, 0x72, 0x2e, 0x77, 0x65, 0x62, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
0x65, 0x72, 0x50, 0x01, 0x5a, 0x39, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72,
0x65, 0x2f, 0x76, 0x35, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
0x65, 0x72, 0x2f, 0x77, 0x65, 0x62, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0xaa,
0x02, 0x25, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x41, 0x70, 0x70,
0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x2e, 0x57, 0x65, 0x62, 0x43, 0x6f,
0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -13,4 +13,9 @@ message Config {
option (v2ray.core.common.protoext.message_opt).short_name = "webcommander";
string tag = 1;
bytes web_root = 2;
string web_root_file = 96002 [(v2ray.core.common.protoext.field_opt).convert_time_read_file_into = "web_root"];
string api_mountpoint = 3;
}

View File

@ -1,8 +1,12 @@
package webcommander
import (
"archive/zip"
"bytes"
"context"
"io/fs"
"net/http"
"strings"
"sync"
"time"
@ -24,7 +28,16 @@ func newWebCommander(ctx context.Context, config *Config) (*WebCommander, error)
if config.Tag == "" {
return nil, newError("config.Tag is empty")
}
return &WebCommander{ctx: ctx, config: config}, nil
var webRootfs fs.FS
if config.WebRoot != nil {
zipReader, err := zip.NewReader(bytes.NewReader(config.WebRoot), int64(len(config.WebRoot)))
if err != nil {
return nil, newError("failed to create zip reader").Base(err)
}
webRootfs = zipReader
}
return &WebCommander{ctx: ctx, config: config, webRootfs: webRootfs}, nil
}
@ -37,13 +50,22 @@ type WebCommander struct {
server *http.Server
wrappedGrpc *grpcweb.WrappedGrpcServer
webRootfs fs.FS
config *Config
}
func (w *WebCommander) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if w.wrappedGrpc.IsGrpcWebRequest(request) {
w.wrappedGrpc.ServeHTTP(writer, request)
api_path := w.config.ApiMountpoint
if strings.HasPrefix(request.URL.Path, api_path) {
request.URL.Path = strings.TrimPrefix(request.URL.Path, api_path)
if w.wrappedGrpc.IsGrpcWebRequest(request) {
w.wrappedGrpc.ServeHTTP(writer, request)
return
}
}
if w.webRootfs != nil {
http.ServeFileFS(writer, request, w.webRootfs, request.URL.Path)
return
}
writer.WriteHeader(http.StatusNotFound)