コード例 #1
0
        public static void CopyAddress_Click(object sender, EventArgs e)
        {
            ShadowsocksController controller = new ShadowsocksController();

            Console.WriteLine("Import server!");
            try
            {
                IDataObject iData = Clipboard.GetDataObject();
                if (iData.GetDataPresent(DataFormats.Text))
                {
                    Console.WriteLine((string)iData.GetData(DataFormats.Text));
                    List <string> urls = new List <string>();
                    Utils.URL_Split((string)iData.GetData(DataFormats.Text), ref urls);
                    int count = 0;
                    Console.WriteLine(urls[0]);
                    foreach (string url in urls)
                    {
                        if (controller.AddServerBySSURL(url))
                        {
                            ++count;
                        }
                    }
                    if (count > 0)
                    {
                        Console.WriteLine("Import " + count.ToString() + " servers!");
                    }
                }
            }
            catch
            {
            }
        }
コード例 #2
0
        public static int updateServerFromSSRURL(ShadowsocksController controller, UpdateSubscribeManager updateSubscribeManager, string result)
        {
            //Console.WriteLine(result);
            int count = 0;

            //string result = result;
            //updateFreeNodeChecker.FreeNodeResult = result;
            //MessageBox.Show(result);
            if (!String.IsNullOrEmpty(result))
            {
                List <string> urls         = new List <string>();
                int           max_node_num = 0;
                Configuration config       = controller.GetCurrentConfiguration();
                //Console.WriteLine("config.configs.Count->" + config.configs.Count);
                Server selected_server = null;
                if (config.index >= 0 && config.index < config.configs.Count)
                {
                    selected_server = config.configs[config.index];
                }
                //Console.WriteLine("result:"+result);
                Utils.URL_Split(result, ref urls);

                for (int i = urls.Count - 1; i >= 0; --i)
                {
                    //Console.WriteLine(urls[i]);
                    if (!urls[i].StartsWith("ssr"))
                    {
                        urls.RemoveAt(i);
                    }
                }
                if (urls.Count > 0)
                {
                    bool keep_selected_server = false; // set 'false' if import all nodes
                    if (max_node_num <= 0 || max_node_num >= urls.Count)
                    {
                        urls.Reverse();
                    }
                    else
                    {
                        Random r = new Random();
                        Util.Utils.Shuffle(urls, r);
                        urls.RemoveRange(max_node_num, urls.Count - max_node_num);
                        if (!config.isDefaultConfig())
                        {
                            keep_selected_server = true;
                        }
                    }
                    string lastGroup = null;
                    string curGroup  = null;
                    foreach (string url in urls)
                    {
                        try // try get group name
                        {
                            Server server = new Server(url, null);
                            if (!String.IsNullOrEmpty(server.group))
                            {
                                curGroup = server.group;
                                break;
                            }
                        }
                        catch
                        { }
                    }
                    //MessageBox.Show(curGroup + "||1");
                    string subscribeURL = updateSubscribeManager.URL;
                    //MessageBox.Show(subscribeURL + "||2");
                    if (String.IsNullOrEmpty(curGroup))
                    {
                        curGroup = subscribeURL;
                    }

                    //MessageBox.Show(curGroup + "||3");
                    for (int i = 0; i < config.serverSubscribes.Count; ++i)
                    {
                        if (subscribeURL == config.serverSubscribes[i].URL)
                        {
                            lastGroup = config.serverSubscribes[i].Group;
                            //MessageBox.Show(lastGroup);
                            config.serverSubscribes[i].Group = curGroup;
                            break;
                        }
                    }
                    if (lastGroup == null)
                    {
                        lastGroup = curGroup;
                    }

                    if (keep_selected_server && selected_server.group == curGroup)
                    {
                        bool match = false;
                        for (int i = 0; i < urls.Count; ++i)
                        {
                            try
                            {
                                Server server = new Server(urls[i], null);
                                if (selected_server.isMatchServer(server))
                                {
                                    match = true;
                                    break;
                                }
                            }
                            catch
                            { }
                        }
                        if (!match)
                        {
                            urls.RemoveAt(0);
                            urls.Add(selected_server.GetSSRLinkForServer());
                        }
                    }

                    // import all, find difference
                    {
                        Dictionary <string, Server> old_servers = new Dictionary <string, Server>();
                        if (!String.IsNullOrEmpty(lastGroup))
                        {
                            for (int i = config.configs.Count - 1; i >= 0; --i)
                            {
                                if (lastGroup == config.configs[i].group)
                                {
                                    old_servers[config.configs[i].id] = config.configs[i];
                                }
                            }
                        }
                        foreach (string url in urls)
                        {
                            try
                            {
                                Server server = new Server(url, curGroup);
                                bool   match  = false;
                                foreach (KeyValuePair <string, Server> pair in old_servers)
                                {
                                    if (server.isMatchServer(pair.Value))
                                    {
                                        match = true;
                                        old_servers.Remove(pair.Key);
                                        pair.Value.CopyServerInfo(server);
                                        ++count;
                                        break;
                                    }
                                }
                                if (!match)
                                {
                                    config.configs.Add(server);
                                    ++count;
                                }
                            }
                            catch
                            { }
                        }
                        foreach (KeyValuePair <string, Server> pair in old_servers)
                        {
                            for (int i = config.configs.Count - 1; i >= 0; --i)
                            {
                                if (config.configs[i].id == pair.Key)
                                {
                                    config.configs.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                        controller.SaveServersConfig(config);
                    }
                    // 重新获取config
                    config = controller.GetCurrentConfiguration();
                    if (selected_server != null)
                    {
                        bool match = false;
                        for (int i = config.configs.Count - 1; i >= 0; --i)
                        {
                            if (config.configs[i].id == selected_server.id)
                            {
                                config.index = i;
                                match        = true;
                                break;
                            }
                            else if (config.configs[i].group == selected_server.group)
                            {
                                if (config.configs[i].isMatchServer(selected_server))
                                {
                                    config.index = i;
                                    match        = true;
                                    break;
                                }
                            }
                        }
                        if (!match)
                        {
                            config.index = config.configs.Count - 1;
                        }
                    }
                    else
                    {
                        config.index = config.configs.Count - 1;
                    }
                    controller.SaveServersConfig(config);
                }
            }

            // 更新后
            if (count > 0)
            {
                Configuration config = controller.GetCurrentConfiguration();
                // 删除默认无效服务器
                if (config.configs[0].server == Configuration.GetDefaultServer().server)
                {
                    config.configs.RemoveAt(0);
                    controller.SaveServersConfig(config);
                }
            }

            return(count);
        }