1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-29 05:25:21 -05:00

Add subscription manager

This commit is contained in:
Shelikhoo
2023-11-21 23:03:20 +00:00
committed by Xiaokang Wang (Shelikhoo)
parent b91354901c
commit cc77e90254
54 changed files with 1959 additions and 45 deletions

View File

@@ -0,0 +1,30 @@
syntax = "proto3";
package v2ray.core.app.subscription.specs;
option csharp_namespace = "V2Ray.Core.App.Subscription.Specs";
option go_package = "github.com/v2fly/v2ray-core/v5/app/subscription/specs";
option java_package = "com.v2ray.core.app.subscription.specs";
option java_multiple_files = true;
import "google/protobuf/any.proto";
message ServerConfiguration{
string protocol = 1;
google.protobuf.Any protocol_settings = 2;
string transport = 3;
google.protobuf.Any transport_settings = 4;
string security = 5;
google.protobuf.Any security_settings = 6;
}
message SubscriptionServerConfig{
string id = 1;
map<string, string> metadata = 2;
ServerConfiguration configuration = 3;
}
message SubscriptionDocument {
map<string, string> metadata = 2;
repeated SubscriptionServerConfig server = 3;
}

View File

@@ -0,0 +1,90 @@
package specs
import (
"bytes"
"context"
"encoding/json"
"github.com/golang/protobuf/proto"
"github.com/v2fly/v2ray-core/v5/common/registry"
"github.com/v2fly/v2ray-core/v5/common/serial"
)
func NewOutboundParser() *OutboundParser {
return &OutboundParser{}
}
type OutboundParser struct{}
func (p *OutboundParser) ParseOutboundConfig(rawConfig []byte) (*OutboundConfig, error) {
skeleton := &OutboundConfig{}
decoder := json.NewDecoder(bytes.NewReader(rawConfig))
decoder.DisallowUnknownFields()
err := decoder.Decode(skeleton)
if err != nil {
return nil, newError("failed to parse outbound config skeleton").Base(err)
}
return skeleton, nil
}
func (p *OutboundParser) toAbstractServerSpec(config *OutboundConfig) (*ServerConfiguration, error) {
serverConfig := &ServerConfiguration{}
serverConfig.Protocol = config.Protocol
{
protocolSettings, err := loadHeterogeneousConfigFromRawJSONRestricted("outbound", config.Protocol, config.Settings)
if err != nil {
return nil, newError("failed to parse protocol settings").Base(err)
}
serverConfig.ProtocolSettings = serial.ToTypedMessage(protocolSettings)
}
if config.StreamSetting != nil {
if config.StreamSetting.Transport == "" {
config.StreamSetting.Transport = "tcp"
}
if config.StreamSetting.Security == "" {
config.StreamSetting.Security = "none"
}
{
serverConfig.Transport = config.StreamSetting.Transport
transportSettings, err := loadHeterogeneousConfigFromRawJSONRestricted(
"transport", config.StreamSetting.Transport, config.StreamSetting.TransportSettings)
if err != nil {
return nil, newError("failed to parse transport settings").Base(err)
}
serverConfig.TransportSettings = serial.ToTypedMessage(transportSettings)
}
{
securitySettings, err := loadHeterogeneousConfigFromRawJSONRestricted(
"security", config.StreamSetting.Security, config.StreamSetting.SecuritySettings)
if err != nil {
return nil, newError("failed to parse security settings").Base(err)
}
serverConfig.SecuritySettings = serial.ToTypedMessage(securitySettings)
serverConfig.Security = serial.V2Type(serverConfig.SecuritySettings)
}
}
return serverConfig, nil
}
func (p *OutboundParser) ToSubscriptionServerConfig(config *OutboundConfig) (*SubscriptionServerConfig, error) {
serverSpec, err := p.toAbstractServerSpec(config)
if err != nil {
return nil, newError("unable to parse server specification")
}
return &SubscriptionServerConfig{
Configuration: serverSpec,
Metadata: config.Metadata,
}, nil
}
func loadHeterogeneousConfigFromRawJSONRestricted(interfaceType, name string, rawJSON json.RawMessage) (proto.Message, error) {
ctx := context.TODO()
ctx = registry.CreateRestrictedModeContext(ctx)
if len(rawJSON) == 0 {
rawJSON = []byte("{}")
}
return registry.LoadImplementationByAlias(ctx, interfaceType, name, []byte(rawJSON))
}

View File

@@ -0,0 +1,19 @@
package specs
import (
"encoding/json"
)
type OutboundConfig struct {
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
StreamSetting *StreamConfig `json:"streamSettings"`
Metadata map[string]string `json:"metadata"`
}
type StreamConfig struct {
Transport string `json:"transport"`
TransportSettings json.RawMessage `json:"transportSettings"`
Security string `json:"security"`
SecuritySettings json.RawMessage `json:"securitySettings"`
}

View File

@@ -0,0 +1,3 @@
package specs
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen