github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/v5cfg/root.go (about)

     1  package v5cfg
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  	"google.golang.org/protobuf/types/known/anypb"
     9  
    10  	core "github.com/imannamdari/v2ray-core/v5"
    11  	"github.com/imannamdari/v2ray-core/v5/app/dispatcher"
    12  	"github.com/imannamdari/v2ray-core/v5/app/proxyman"
    13  	"github.com/imannamdari/v2ray-core/v5/common/platform"
    14  	"github.com/imannamdari/v2ray-core/v5/common/serial"
    15  	"github.com/imannamdari/v2ray-core/v5/infra/conf/cfgcommon"
    16  	"github.com/imannamdari/v2ray-core/v5/infra/conf/geodata"
    17  	"github.com/imannamdari/v2ray-core/v5/infra/conf/synthetic/log"
    18  )
    19  
    20  func (c RootConfig) BuildV5(ctx context.Context) (proto.Message, error) {
    21  	config := &core.Config{
    22  		App: []*anypb.Any{
    23  			serial.ToTypedMessage(&dispatcher.Config{}),
    24  			serial.ToTypedMessage(&proxyman.InboundConfig{}),
    25  			serial.ToTypedMessage(&proxyman.OutboundConfig{}),
    26  		},
    27  	}
    28  
    29  	var logConfMsg *anypb.Any
    30  	if c.LogConfig != nil {
    31  		logConfMsgUnpacked, err := loadHeterogeneousConfigFromRawJSON("service", "log", c.LogConfig)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		logConfMsg = serial.ToTypedMessage(logConfMsgUnpacked)
    36  	} else {
    37  		logConfMsg = serial.ToTypedMessage(log.DefaultLogConfig())
    38  	}
    39  	// let logger module be the first App to start,
    40  	// so that other modules could print log during initiating
    41  	config.App = append([]*anypb.Any{logConfMsg}, config.App...)
    42  
    43  	if c.RouterConfig != nil {
    44  		routerConfig, err := loadHeterogeneousConfigFromRawJSON("service", "router", c.RouterConfig)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		config.App = append(config.App, serial.ToTypedMessage(routerConfig))
    49  	}
    50  
    51  	if c.DNSConfig != nil {
    52  		dnsApp, err := loadHeterogeneousConfigFromRawJSON("service", "dns", c.DNSConfig)
    53  		if err != nil {
    54  			return nil, newError("failed to parse DNS config").Base(err)
    55  		}
    56  		config.App = append(config.App, serial.ToTypedMessage(dnsApp))
    57  	}
    58  
    59  	for _, rawInboundConfig := range c.Inbounds {
    60  		ic, err := rawInboundConfig.BuildV5(ctx)
    61  		if err != nil {
    62  			return nil, err
    63  		}
    64  		config.Inbound = append(config.Inbound, ic.(*core.InboundHandlerConfig))
    65  	}
    66  
    67  	for _, rawOutboundConfig := range c.Outbounds {
    68  		ic, err := rawOutboundConfig.BuildV5(ctx)
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  		config.Outbound = append(config.Outbound, ic.(*core.OutboundHandlerConfig))
    73  	}
    74  
    75  	for serviceName, service := range c.Services {
    76  		servicePackedConfig, err := loadHeterogeneousConfigFromRawJSON("service", serviceName, service)
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  		config.App = append(config.App, serial.ToTypedMessage(servicePackedConfig))
    81  	}
    82  	return config, nil
    83  }
    84  
    85  func loadJSONConfig(data []byte) (*core.Config, error) {
    86  	rootConfig := &RootConfig{}
    87  
    88  	err := json.Unmarshal(data, rootConfig)
    89  	if err != nil {
    90  		return nil, newError("unable to load json").Base(err)
    91  	}
    92  
    93  	buildctx := cfgcommon.NewConfigureLoadingContext(context.Background())
    94  
    95  	geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
    96  		return "standard"
    97  	})
    98  
    99  	if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
   100  		cfgcommon.SetGeoDataLoader(buildctx, loader)
   101  	} else {
   102  		return nil, newError("unable to create geo data loader ").Base(err)
   103  	}
   104  
   105  	message, err := rootConfig.BuildV5(buildctx)
   106  	if err != nil {
   107  		return nil, newError("unable to build config").Base(err)
   108  	}
   109  	return message.(*core.Config), nil
   110  }