Exemplo n.º 1
0
            public static Objects.Server.V2Ray V2Ray(string text)
            {
                var data  = SimpleJSON.JSON.Parse(UrlSafeBase64Decode(text.Remove(0, 8)));
                var v2ray = new Objects.Server.V2Ray();

                v2ray.Remark  = data["ps"].Value;
                v2ray.Address = data["add"].Value;
                v2ray.Port    = data["port"].AsInt;
                v2ray.UserID  = data["id"].Value;
                v2ray.AlterID = data["aid"].AsInt;

                if (String.IsNullOrEmpty(v2ray.Remark))
                {
                    v2ray.Remark = String.Format("{0}:{1}", v2ray.Address, v2ray.Port);
                }

                switch (data["net"].Value)
                {
                case "tcp":
                    v2ray.TransferProtocol = 0;
                    break;

                case "kcp":
                    v2ray.TransferProtocol = 1;
                    break;

                case "ws":
                    v2ray.TransferProtocol = 2;
                    break;

                case "h2":
                    v2ray.TransferProtocol = 3;
                    break;

                case "quic":
                    v2ray.TransferProtocol = 4;
                    break;

                default:
                    throw new NotSupportedException(String.Format("不支持的传输协议:{0}", data["net"].Value));
                }

                switch (data["type"].Value)
                {
                case "none":
                    v2ray.FakeType = 0;
                    break;

                case "http":
                    v2ray.FakeType = 1;
                    break;

                case "srtp":
                    v2ray.FakeType = 2;
                    break;

                case "utp":
                    v2ray.FakeType = 3;
                    break;

                case "wechat-video":
                    v2ray.FakeType = 4;
                    break;

                case "dtls":
                    v2ray.FakeType = 5;
                    break;

                case "wireguard":
                    v2ray.FakeType = 6;
                    break;

                default:
                    throw new NotSupportedException(String.Format("不支持的伪装类型:{0}", data["type"].Value));
                }

                v2ray.FakeDomain = data["host"].Value;
                v2ray.Path       = data["path"].Value == "" ? "/" : data["path"].Value;
                v2ray.TLSSecure  = data["tls"].Value == "" ? false : true;

                return(v2ray);
            }
Exemplo n.º 2
0
            public static string GetV2Ray(Objects.Server.V2Ray v2ray, bool bypassChina = true)
            {
                var data = GetGeneric(bypassChina);

                var user = new Objects.v2rayConfig.Protocol.Outbound.VMessUser()
                {
                    id      = v2ray.UserID,
                    alterId = v2ray.AlterID
                };

                switch (v2ray.EncryptMethod)
                {
                case 0:
                    user.security = "chacha20-poly1305";
                    break;

                case 1:
                    user.security = "aes-128-gcm";
                    break;

                case 2:
                    user.security = "auto";
                    break;

                case 3:
                    user.security = "none";
                    break;

                default:
                    user.security = "chacha20-poly1305";
                    break;
                }

                var streamSettings = new Objects.v2rayConfig.StreamSettings();

                switch (v2ray.TransferProtocol)
                {
                case 0:
                    streamSettings.network = "tcp";
                    if (GetFakeType(v2ray.FakeType) == "http")
                    {
                        var tcpSettings = new Objects.v2rayConfig.TCP()
                        {
                            header = new Objects.v2rayConfig.TCPHTTPHeader()
                            {
                                request = new Objects.v2rayConfig.TCPHTTPRequestHeader()
                                {
                                    headers = new Dictionary <string, List <string> >()
                                    {
                                        { "Host", new List <string>()
                                          {
                                              v2ray.FakeDomain
                                          } }
                                    },
                                    path = new List <string>()
                                    {
                                        v2ray.Path == "/" ? "/" : v2ray.Path
                                    }
                                }
                            }
                        };
                        streamSettings.tcpSettings = tcpSettings;
                    }
                    break;

                case 1:
                    streamSettings.network     = "kcp";
                    streamSettings.kcpSettings = new Objects.v2rayConfig.KCP()
                    {
                        header = new Dictionary <string, string>()
                        {
                            { "type", GetFakeType(v2ray.FakeType) }
                        }
                    };
                    break;

                case 2:
                    streamSettings.network = "ws";
                    var wsSettings = new Objects.v2rayConfig.WebSocket();

                    if (v2ray.FakeDomain != "")
                    {
                        wsSettings.headers = new Dictionary <string, string>()
                        {
                            { "Host", v2ray.FakeDomain }
                        };
                    }

                    wsSettings.path           = v2ray.Path;
                    streamSettings.wsSettings = wsSettings;
                    break;

                case 3:
                    streamSettings.network = "http";
                    var httpSettings = new Objects.v2rayConfig.HTTP2();

                    if (v2ray.FakeDomain != "")
                    {
                        httpSettings.host = new List <string>()
                        {
                            v2ray.FakeDomain
                        };
                    }

                    httpSettings.path           = v2ray.Path;
                    streamSettings.httpSettings = httpSettings;
                    break;

                case 4:
                    streamSettings.network      = "quic";
                    streamSettings.quicSettings = new Objects.v2rayConfig.QUIC()
                    {
                        header = new Dictionary <string, string>()
                        {
                            { "type", GetFakeType(v2ray.FakeType) }
                        }
                    };
                    break;

                default:
                    streamSettings.network = "tcp";
                    break;
                }

                if (v2ray.TLSSecure)
                {
                    streamSettings.security = "tls";
                }

                data.outbounds.Insert(0, new Objects.v2rayConfig.Outbound()
                {
                    protocol = "vmess",
                    settings = new Objects.v2rayConfig.Protocol.Outbound.VMess()
                    {
                        vnext = new List <Objects.v2rayConfig.Protocol.Outbound.VMessServer>()
                        {
                            new Objects.v2rayConfig.Protocol.Outbound.VMessServer()
                            {
                                address = v2ray.Address,
                                port    = v2ray.Port,
                                users   = new List <Objects.v2rayConfig.Protocol.Outbound.VMessUser>()
                                {
                                    user
                                }
                            }
                        }
                    },
                    streamSettings = streamSettings,
                    tag            = "defaultOutbound"
                });

                return(Newtonsoft.Json.JsonConvert.SerializeObject(data));
            }