Exemple #1
0
            public static Objects.Server.Shadowsocks Shadowsocks(string text)
            {
                var shadowsocks = new Objects.Server.Shadowsocks();

                try
                {
                    Regex finder = new Regex("^(?i)ss://([A-Za-z0-9+-/=_]+)(#(.+))?", RegexOptions.IgnoreCase), parser = new Regex("^((?<method>.+):(?<password>.*)@(?<hostname>.+?):(?<port>\\d+?))$", RegexOptions.IgnoreCase);
                    var   match = finder.Match(text);
                    if (!match.Success)
                    {
                        throw new FormatException();
                    }

                    match = parser.Match(UrlSafeBase64Decode(match.Groups[1].Value));

                    shadowsocks.Password = match.Groups["password"].Value;
                    shadowsocks.Address  = match.Groups["hostname"].Value;
                    shadowsocks.Port     = int.Parse(match.Groups["port"].Value);

                    if (text.Contains("#"))
                    {
                        shadowsocks.Remark = Uri.UnescapeDataString(Regex.Split(text, "#", RegexOptions.IgnoreCase)[1]);
                    }
                    else
                    {
                        shadowsocks.Remark = String.Format("{0}:{1}", shadowsocks.Address, shadowsocks.Port);
                    }

                    switch (match.Groups["method"].Value)
                    {
                    case "aes-256-cfb":
                        shadowsocks.EncryptMethod = 0;
                        break;

                    case "aes-128-cfb":
                        shadowsocks.EncryptMethod = 1;
                        break;

                    case "chacha20":
                        shadowsocks.EncryptMethod = 2;
                        break;

                    case "chacha20-ietf":
                        shadowsocks.EncryptMethod = 3;
                        break;

                    case "aes-256-gcm":
                        shadowsocks.EncryptMethod = 4;
                        break;

                    case "aes-128-gcm":
                        shadowsocks.EncryptMethod = 5;
                        break;

                    case "chacha20-ietf-poly1305":
                        shadowsocks.EncryptMethod = 6;
                        break;

                    default:
                        throw new NotSupportedException(String.Format("不支持的加密方式:{0}", match.Groups["method"].Value));
                    }
                }
                catch (NotSupportedException)
                {
                    throw;
                }
                catch (Exception)
                {
                    var data     = new Uri(text);
                    var userinfo = UrlSafeBase64Decode(data.UserInfo).Split(':');

                    shadowsocks.Remark  = data.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
                    shadowsocks.Address = data.IdnHost;
                    shadowsocks.Port    = data.Port;

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

                    switch (userinfo[0])
                    {
                    case "aes-256-cfb":
                        shadowsocks.EncryptMethod = 0;
                        break;

                    case "aes-128-cfb":
                        shadowsocks.EncryptMethod = 1;
                        break;

                    case "chacha20":
                        shadowsocks.EncryptMethod = 2;
                        break;

                    case "chacha20-ietf":
                        shadowsocks.EncryptMethod = 3;
                        break;

                    case "aes-256-gcm":
                        shadowsocks.EncryptMethod = 4;
                        break;

                    case "aes-128-gcm":
                        shadowsocks.EncryptMethod = 5;
                        break;

                    case "chacha20-ietf-poly1305":
                        shadowsocks.EncryptMethod = 6;
                        break;

                    default:
                        throw new Exception(String.Format("不支持的加密方式:{0}", userinfo[0]));
                    }

                    shadowsocks.Password = userinfo[1];
                }

                return(shadowsocks);
            }
Exemple #2
0
            public static string GetShadowsocks(Objects.Server.Shadowsocks shadowsocks, bool bypassChina = true)
            {
                var data = GetGeneric(bypassChina);

                var server = new Objects.v2rayConfig.Protocol.Outbound.ShadowsocksServer()
                {
                    address  = shadowsocks.Address,
                    port     = shadowsocks.Port,
                    password = shadowsocks.Password
                };

                switch (shadowsocks.EncryptMethod)
                {
                case 0:
                    server.method = "aes-256-cfb";
                    break;

                case 1:
                    server.method = "aes-128-cfb";
                    break;

                case 2:
                    server.method = "chacha20";
                    break;

                case 3:
                    server.method = "chacha20-ietf";
                    break;

                case 4:
                    server.method = "aes-256-gcm";
                    break;

                case 5:
                    server.method = "aes-128-gcm";
                    break;

                case 6:
                    server.method = "chacha20-poly1305";
                    break;

                default:
                    server.method = "aes-256-cfb";
                    break;
                }

                data.outbounds.Insert(0, new Objects.v2rayConfig.Outbound()
                {
                    protocol = "shadowsocks",
                    settings = new Objects.v2rayConfig.Protocol.Outbound.Shadowsocks()
                    {
                        servers = new List <Objects.v2rayConfig.Protocol.Outbound.ShadowsocksServer>()
                        {
                            server
                        }
                    },
                    tag = "defaultOutbound"
                });

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