示例#1
0
        private VMess GenerateVMessConfigFromEditor()
        {
            // Base Server Config
            if (String.IsNullOrWhiteSpace(textBoxAddress.Text))
            {
                MsgBox.Error(I18n.MsgPleaseInputAddress);
                textBoxAddress.Focus();
                return(null);
            }

            if (String.IsNullOrWhiteSpace(textBoxID.Text) || !Guid.TryParse(textBoxID.Text, out Guid result))
            {
                MsgBox.Error(I18n.MsgPleaseInputId);
                textBoxID.Focus();
                return(null);
            }

            if (!v2rayP.Setting.VmessSecurity.Contains(comboBoxSecurity.Text))
            {
                MsgBox.Error(string.Format(I18n.MsgPleaseSelectGivenValues, "Security"));
                comboBoxSecurity.Focus();
                return(null);
            }

            string remark = String.IsNullOrWhiteSpace(textBoxRemark.Text) ? String.Empty : textBoxRemark.Text;

            var config = new VMess
            {
                Address   = textBoxAddress.Text.Trim(),
                Port      = Convert.ToInt32(numericUpDownPort.Value),
                AlterId   = Convert.ToInt32(numericUpDownAlterID.Value),
                UserId    = textBoxID.Text.Trim(),
                Security  = comboBoxSecurity.Text.Trim(),
                Remark    = remark,
                EnableMux = checkBoxEnableMux.Checked,
            };
            var stream        = new StreamSetting();
            var streamChanged = false;

            // TLS Settings
            var enableTLS = checkBoxEnableTLS.Checked;

            if (enableTLS)
            {
                streamChanged      = true;
                stream.Security    = "tls";
                stream.TlsSettings = new TLSSetting
                {
                    AllowInsecure = checkBoxAllowInsecure.Checked,
                    ServerName    = textBoxTLSServerName.Text.Trim(),
                };
            }

            // TCP Settings
            if (comboBoxNetwork.SelectedIndex == 0)
            {
                stream.Network = "tcp";
                if (comboBoxTCPType.Text == "http")
                {
                    streamChanged = true;

                    object path = null;
                    if (!String.IsNullOrWhiteSpace(textBoxHTTPRequestPaths.Text))
                    {
                        var paths = textBoxHTTPRequestPaths.Text.Split(',').Select(p => p.Trim()).ToArray();
                        if (paths.Length == 1)
                        {
                            path = paths[0];
                        }
                        else
                        {
                            path = paths;
                        }
                    }

                    stream.TcpSettings = new TCPSetting
                    {
                        Header =
                        {
                            Type    = "http",
                            Request =
                            {
                                Version = textBoxHTTPRequestVersion.Text.Trim(),
                                Method  = textBoxHTTPRequestMethod.Text.Trim(),
                                Headers = ParseHeaders(textBoxHTTPRequestHeaders.Text,true),
                                Path    = path,
                            },
                            Response    =
                            {
                                Version = textBoxHTTPResponseVersion.Text.Trim(),
                                Status  = textBoxHTTPResponseStatus.Text.Trim(),
                                Reason  = textBoxHTTPResponseReason.Text.Trim(),
                                Headers = ParseHeaders(textBoxHTTPResponseHeaders.Text,true),
                            }
                        },
                    };
                }
            }

            // KCP Settings
            else if (comboBoxNetwork.SelectedIndex == 1)
            {
                string headerType = comboBoxKCPHeaderType.Text;
                if (!v2rayP.Setting.KCPHeaderTypes.Contains(headerType))
                {
                    MsgBox.Error(string.Format(I18n.MsgPleaseSelectGivenValues, "Header Type"));
                    comboBoxKCPHeaderType.SelectedIndex = 0;
                    return(null);
                }

                stream.Network     = "kcp";
                stream.KcpSettings = new KCPSetting
                {
                    Congestion       = checkBoxCongestion.Checked,
                    Mtu              = Convert.ToInt32(numericUpDownKCPMTU.Value),
                    Tti              = Convert.ToInt32(numericUpDownKCPTTI.Value),
                    UplinkCapacity   = Convert.ToInt32(numericUpDownKCPUplinkCapacity.Value),
                    DownlinkCapacity = Convert.ToInt32(numericUpDownKCPDownlinkCapacity.Value),
                    WriteBufferSize  = Convert.ToInt32(numericUpDownKCPWriteBufferSize.Value),
                    ReadBufferSize   = Convert.ToInt32(numericUpDownKCPReadBufferSize.Value),
                    Header           =
                    {
                        Type = headerType
                    }
                };
                streamChanged = true;
            }

            // WebSocket Settings
            else if (comboBoxNetwork.SelectedIndex == 2)
            {
                stream.Network    = "ws";
                stream.WsSettings = new WebSocketSetting
                {
                    Path    = textBoxWebSocketPath.Text.Trim(),
                    Headers = ParseHeaders(textBoxWebSocketHeaders.Text, false),
                };
                streamChanged = true;
            }

            else
            {
                MsgBox.Error(String.Format(I18n.MsgPleaseSelectGivenValues, "Network Type"));
                comboBoxNetwork.Focus();
                return(null);
            }

            if (streamChanged)
            {
                config.StreamSettings = stream;
            }
            return(config);
        }
示例#2
0
        public bool WriteConfig(VMess server)
        {
            var streamSettings = server?.StreamSettings;

            if (streamSettings?.TlsSettings != null && string.IsNullOrWhiteSpace(streamSettings?.TlsSettings.ServerName))
            {
                streamSettings = new StreamSetting()
                {
                    Network     = server.StreamSettings.Network,
                    Security    = server.StreamSettings.Security,
                    WsSettings  = server.StreamSettings.WsSettings,
                    TcpSettings = server.StreamSettings.TcpSettings,
                    KcpSettings = server.StreamSettings.KcpSettings,
                };

                streamSettings.TlsSettings = new TLSSetting()
                {
                    ServerName    = null,
                    AllowInsecure = server.StreamSettings.TlsSettings.AllowInsecure,
                };
            }

            var dns = new { servers = new string[] { "8.8.8.8", "8.8.4.4", } };

            var log = new
            {
                error    = ErrorLog == null ? null : errorLog,
                access   = AccessLog == null ? null : accessLog,
                loglevel = LogLevel.ToString().ToLower(),
            };

            var inbound = new
            {
                protocol = "socks",
                port     = SocksPort,
                listen   = ListenAddress,
                settings = new
                {
                    auth    = "noauth",
                    udp     = EnableUdp,
                    ip      = ListenAddress,
                    timeout = 300
                }
            };

            var inboundDetour = new object[]
            {
                new {
                    protocol = "http",
                    port     = HttpPort,
                    listen   = ListenAddress,
                    settings = new { timeout = 0 },
                }
            };

            var outbound = new
            {
                tag      = "tag_proxy",
                protocol = "vmess",
                mux      = new
                {
                    enable = server.EnableMux,
                },
                settings = new
                {
                    vnext = new[]
                    {
                        new {
                            address = server?.Address,
                            port    = server?.Port,
                            users   = new []
                            {
                                new {
                                    id       = server?.UserId,
                                    alterId  = server?.AlterId,
                                    security = server?.Security,
                                }
                            }
                        }
                    }
                },
                streamSettings = streamSettings,
            };

            var outboundDetour = new object[]
            {
                new
                {
                    tag      = "tag_block",
                    protocol = "blackhole",
                    settings = new { response = new { type = "http" } },
                },
                new
                {
                    tag      = "tag_direct",
                    protocol = "freedom",
                    settings = new { tiemout = 0 },
                }
            };

            var routing = new
            {
                strategy = "rules",
                settings = new
                {
                    domainStrategy = "IPIfNonMatch",
                    rules          = new[]
                    {
                        new
                        {
                            type        = "field",
                            outboundTag = "tag_direct",
                            ip          = new string[]
                            {
                                "0.0.0.0/8",
                                "10.0.0.0/8",
                                "100.64.0.0/10",
                                "127.0.0.0/8",
                                "169.254.0.0/16",
                                "172.16.0.0/12",
                                "192.0.0.0/24",
                                "192.0.2.0/24",
                                "192.168.0.0/16",
                                "198.18.0.0/15",
                                "198.51.100.0/24",
                                "203.0.113.0/24",
                                "::1/128",
                                "fc00::/7",
                                "fe80::/10",
                            },
                        },
                    },
                },
            };

            var config = new { log, inbound, inboundDetour, outbound, outboundDetour, routing };

            return(JSON.Write(configPath, config));
        }