2021-09-04 13:45:02 -04:00
|
|
|
package v5cfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-04 14:54:07 -04:00
|
|
|
"encoding/json"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2021-09-04 13:45:02 -04:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-10-28 06:34:19 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2021-09-04 13:45:02 -04:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/dispatcher"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/proxyman"
|
2021-09-04 16:46:10 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
2021-09-04 13:45:02 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2021-09-04 16:46:10 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
|
2021-09-04 13:45:02 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c RootConfig) BuildV5(ctx context.Context) (proto.Message, error) {
|
|
|
|
config := &core.Config{
|
|
|
|
App: []*anypb.Any{
|
|
|
|
serial.ToTypedMessage(&dispatcher.Config{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.InboundConfig{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var logConfMsg *anypb.Any
|
|
|
|
if c.LogConfig != nil {
|
2021-09-06 13:18:22 -04:00
|
|
|
logConfMsgUnpacked, err := loadHeterogeneousConfigFromRawJson("service", "log", c.LogConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logConfMsg = serial.ToTypedMessage(logConfMsgUnpacked)
|
2021-09-04 13:45:02 -04:00
|
|
|
} else {
|
|
|
|
logConfMsg = serial.ToTypedMessage(log.DefaultLogConfig())
|
|
|
|
}
|
|
|
|
// let logger module be the first App to start,
|
|
|
|
// so that other modules could print log during initiating
|
|
|
|
config.App = append([]*anypb.Any{logConfMsg}, config.App...)
|
|
|
|
|
|
|
|
if c.RouterConfig != nil {
|
2021-09-06 13:18:22 -04:00
|
|
|
routerConfig, err := loadHeterogeneousConfigFromRawJson("service", "router", c.RouterConfig)
|
2021-09-04 13:45:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(routerConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DNSConfig != nil {
|
2021-09-06 13:18:22 -04:00
|
|
|
dnsApp, err := loadHeterogeneousConfigFromRawJson("service", "dns", c.DNSConfig)
|
2021-09-04 13:45:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse DNS config").Base(err)
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(dnsApp))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawInboundConfig := range c.Inbounds {
|
|
|
|
ic, err := rawInboundConfig.BuildV5(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Inbound = append(config.Inbound, ic.(*core.InboundHandlerConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawOutboundConfig := range c.Outbounds {
|
|
|
|
ic, err := rawOutboundConfig.BuildV5(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Outbound = append(config.Outbound, ic.(*core.OutboundHandlerConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
for serviceName, service := range c.Services {
|
2021-09-05 07:03:36 -04:00
|
|
|
servicePackedConfig, err := loadHeterogeneousConfigFromRawJson("service", serviceName, service)
|
2021-09-04 13:45:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(servicePackedConfig))
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
2021-09-04 14:54:07 -04:00
|
|
|
|
|
|
|
func loadJsonConfig(data []byte) (*core.Config, error) {
|
|
|
|
rootConfig := &RootConfig{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, rootConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to load json").Base(err)
|
|
|
|
}
|
|
|
|
|
2021-09-04 16:46:10 -04:00
|
|
|
buildctx := cfgcommon.NewConfigureLoadingContext(context.Background())
|
|
|
|
|
|
|
|
geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
|
|
|
|
return "standard"
|
|
|
|
})
|
|
|
|
|
|
|
|
if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
|
|
|
|
cfgcommon.SetGeoDataLoader(buildctx, loader)
|
|
|
|
} else {
|
|
|
|
return nil, newError("unable to create geo data loader ").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := rootConfig.BuildV5(buildctx)
|
2021-09-04 14:54:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unable to build config").Base(err)
|
|
|
|
}
|
|
|
|
return message.(*core.Config), nil
|
|
|
|
}
|