Ejemplo n.º 1
0
        private void SendToServer(int length, AsyncSession session)
        {
            _totalWrite += length;
            int bytesToSend;

            lock (_encryptionLock)
            {
                try
                {
                    _encryptor.Encrypt(_connetionRecvBuffer, length, _connetionSendBuffer, out bytesToSend);
                }
                catch (CryptoErrorException)
                {
                    Logging.Debug("encryption error");
                    Close();
                    return;
                }
            }

            if (!_has_sent)
            {
                List <byte> header_bytes = new List <byte>();
                int         append_size  = 0;
                if (P2pLib.GetInstance().use_smart_route_)
                {
                    string ex_route_ip   = "";
                    ushort ex_route_port = 0;
                    if (P2pLib.GetInstance().GetExRouteNode(ref ex_route_ip, ref ex_route_port) == 0)
                    {
                        RouteHeader tmp_rth = new RouteHeader();
                        tmp_rth.ip   = P2pLib.IpToInt(ex_route_ip);
                        tmp_rth.port = (ushort)(((ex_route_port & 0xff) << 8) |
                                                ((ex_route_port >> 8) & 0xff));
                        byte[] tmp_bt = RouteHeader.StuctToByte(tmp_rth);
                        header_bytes.AddRange(tmp_bt);
                        append_size += 6;
                    }
                    RouteHeader rth = new RouteHeader();
                    rth.ip   = P2pLib.GetInstance().vpn_ip_;
                    rth.port = (ushort)((
                                            (P2pLib.GetInstance().vpn_port_ & 0xff) << 8) |
                                        ((P2pLib.GetInstance().vpn_port_ >> 8) & 0xff));
                    byte[] bt = RouteHeader.StuctToByte(rth);
                    header_bytes.AddRange(bt);
                    append_size += 6;
                }

                string pubkey     = P2pLib.GetInstance().pubkey_;
                byte[] pubkey_arr = System.Text.Encoding.Default.GetBytes(pubkey);
                header_bytes.AddRange(pubkey_arr);

                string method = _server.method;
                header_bytes.Add((byte)method.Length);
                byte[] method_arr = System.Text.Encoding.Default.GetBytes(method);
                header_bytes.AddRange(method_arr);

                header_bytes.AddRange(_connetionSendBuffer);
                bytesToSend += pubkey.Length + 1 + method.Length + append_size;
                byte[] final_bytes = header_bytes.ToArray();
                Array.Copy(final_bytes, _connetionSendBuffer, bytesToSend);
                _has_sent = true;
            }

            _tcprelay.UpdateOutboundCounter(_server, bytesToSend);
            _startSendingTime = DateTime.Now;
            Logging.Debug($"SendToServer: {bytesToSend}");

            session.Remote.BeginSend(_connetionSendBuffer, 0, bytesToSend, SocketFlags.None,
                                     PipeRemoteSendCallback, new object[] { session, bytesToSend });
            IStrategy strategy = _controller.GetCurrentStrategy();

            strategy?.UpdateLastWrite(_server);
        }
Ejemplo n.º 2
0
 private void button1_Click(object sender, EventArgs e)
 {
     // prikey copy
     Clipboard.SetDataObject(P2pLib.GetInstance().prikey_);
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            // .NET Framework 4.7.2 on Win7 compatibility
            System.Net.ServicePointManager.SecurityProtocol |=
                System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

            // store args for further use
            Args = args;
            // Check OS since we are using dual-mode socket
            if (!Utils.IsWinVistaOrHigher())
            {
                MessageBox.Show(I18N.GetString("Unsupported operating system, use Windows Vista at least."),
                                "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Check .NET Framework version
            if (!Utils.IsSupportedRuntimeVersion())
            {
                MessageBox.Show(I18N.GetString("Unsupported .NET Framework, please update to 4.6.2 or later."),
                                "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Process.Start(
                    "https://www.microsoft.com/download/details.aspx?id=53344");
                return;
            }

            Utils.ReleaseMemory(true);
            using (Mutex mutex = new Mutex(false, $"Global\\Shadowsocks_{Application.StartupPath.GetHashCode()}"))
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                // handle UI exceptions
                Application.ThreadException += Application_ThreadException;
                // handle non-UI exceptions
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Application.ApplicationExit   += Application_ApplicationExit;
                SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                AutoStartup.RegisterForRestart(true);

                if (!mutex.WaitOne(0, false))
                {
                    Process[] oldProcesses = Process.GetProcessesByName("Shadowsocks");
                    if (oldProcesses.Length > 0)
                    {
                        Process oldProcess = oldProcesses[0];
                    }
                    MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.")
                                    + Environment.NewLine
                                    + I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."),
                                    I18N.GetString("Shadowsocks is already running."));
                    return;
                }
                Directory.SetCurrentDirectory(Application.StartupPath);
                Logging.OpenLogFile();

                int init_res = P2pLib.GetInstance().InitNetwork();
                if (init_res != 0)
                {
                    MessageBox.Show(I18N.GetString("init p2p network failed, please check out your network."));
                    return;
                }
#if DEBUG
                // truncate privoxy log file while debugging
                string privoxyLogFilename = Utils.GetTempPath("privoxy.log");
                if (File.Exists(privoxyLogFilename))
                {
                    using (new FileStream(privoxyLogFilename, FileMode.Truncate)) { }
                }
#else
                Logging.OpenLogFile();
#endif
                MainController = new ShadowsocksController();
                MenuController = new MenuViewController(MainController);
                HotKeys.Init(MainController);
                MainController.Start();
                Application.Run();
            }
        }
Ejemplo n.º 4
0
 private void button5_Click(object sender, EventArgs e)
 {
     // buy
     System.Diagnostics.Process.Start(P2pLib.GetInstance().buy_tenon_ip_ + "/chongzhi/" + P2pLib.GetInstance().account_id_);
 }
Ejemplo n.º 5
0
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox tmpBox = sender as CheckBox;

            P2pLib.GetInstance().SetGlobalMode(tmpBox.Checked);
        }
Ejemplo n.º 6
0
 private void button3_Click(object sender, EventArgs e)
 {
     // account copy
     Clipboard.SetDataObject(P2pLib.GetInstance().account_id_);
 }
Ejemplo n.º 7
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (P2pLib.GetInstance().connectSuccess)
            {
                _borderColor        = Color.FromArgb(0, 184, 170);
                _onHoverBorderColor = Color.FromArgb(86, 224, 208);
                _buttonColor        = Color.FromArgb(3, 134, 120);
                _onHoverButtonColor = Color.FromArgb(0, 184, 170);
                _textColor          = System.Drawing.SystemColors.ButtonHighlight;
                _onHoverTextColor   = System.Drawing.SystemColors.ButtonHighlight;
            }
            else
            {
                _borderColor        = System.Drawing.Color.WhiteSmoke;
                _onHoverBorderColor = System.Drawing.Color.Gainsboro;
                _buttonColor        = System.Drawing.Color.LightGray;
                _onHoverButtonColor = System.Drawing.Color.Silver;
                _textColor          = System.Drawing.SystemColors.ButtonHighlight;
                _onHoverTextColor   = System.Drawing.SystemColors.ButtonHighlight;
            }
            if (P2pLib.GetInstance().connectStarted || P2pLib.GetInstance().disConnectStarted)
            {
                _isHovering = false;
            }

            base.OnPaint(e);
            {
                Graphics g = e.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                Brush brush = new SolidBrush(_isHovering ? _onHoverBorderColor : _borderColor);

                //Border
                //g.FillEllipse(brush, 0, 0, Height - 2, Height - 2);
                g.FillEllipse(brush, Width - Height + 1, 1, Height - 2, Height - 2);
                //g.FillRectangle(brush, Height / 2, 0, Width - Height, Height);

                brush.Dispose();
                brush = new SolidBrush(_isHovering ? _onHoverButtonColor : _buttonColor);

                //Inner part. Button itself
                g.FillEllipse(brush, _borderThicknessByTwo, _borderThicknessByTwo, Height - _borderThickness,
                              Height - _borderThickness);
                g.FillEllipse(brush, (Width - Height) + _borderThicknessByTwo, _borderThicknessByTwo,
                              Height - _borderThickness, Height - _borderThickness);
                g.FillRectangle(brush, Height / 2 + _borderThicknessByTwo, _borderThicknessByTwo,
                                Width - Height - _borderThickness, Height - _borderThickness);

                brush.Dispose();
                brush = new SolidBrush(_isHovering ? _onHoverTextColor : _textColor);
                //Button Text
                RectangleF rect = new RectangleF(Width / 2 - 35, Height / 2 - 55, 70, 70);
                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
                if (P2pLib.GetInstance().connectSuccess)
                {
                    Text = "Connected";
                    SizeF stringSize = g.MeasureString(Text, _font1);
                    g.DrawString(Text, _font1, brush, (Width - stringSize.Width) / 2, 48 + (Height - stringSize.Height) / 2);
                    e.Graphics.DrawImage(Resources.connected, rect);
                }
                else
                {
                    Text = "Connect";
                    SizeF stringSize = g.MeasureString(Text, _font1);
                    g.DrawString(Text, _font1, brush, (Width - stringSize.Width) / 2, 48 + (Height - stringSize.Height) / 2);
                    e.Graphics.DrawImage(Resources.connect, rect);
                }
            }

            if (P2pLib.GetInstance().connectStarted || P2pLib.GetInstance().disConnectStarted)
            {
                var pen1 = new Pen(_color1, _line1Thinkness);
                var pen2 = new Pen(Color.FromArgb(0, 114, 108), _line1Thinkness);

                var circleRadius = _ignoreHeight
                    ? (Width - ProgressLineThikness)
                    : (Width > Height ? (Height - ProgressLineThikness) : (Width - ProgressLineThikness));
                var radiusByTwo      = circleRadius / 2;
                var progressEndAngle = (_angle) / 100F;

                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.TranslateTransform(_line2Thinkness / 2F + radiusByTwo, _line2Thinkness / 2F + radiusByTwo);
                e.Graphics.RotateTransform((360 - _angle) / 2 + 90);

                if (P2pLib.GetInstance().disConnectStarted)
                {
                    e.Graphics.DrawArc(pen2, -radiusByTwo, -radiusByTwo, circleRadius, circleRadius, _progress, 30);
                }
                else
                {
                    e.Graphics.DrawArc(pen1, -radiusByTwo, -radiusByTwo, circleRadius, circleRadius, _progress, 30);
                }
                pen1.Dispose();
                pen2.Dispose();
            }
        }
Ejemplo n.º 8
0
 private void label12_Click(object sender, EventArgs e)
 {
     // share
     Clipboard.SetDataObject(P2pLib.GetInstance().share_ip_ + "?id=" + P2pLib.GetInstance().account_id_);
     MessageBox.Show(I18N.GetString("Copy sharing link succeeded."));
 }
Ejemplo n.º 9
0
        private void checkServerInfo(bool show_message)
        {
            string ver = P2pLib.GetInstance().GetLatestVer();

            if (ver.IsNullOrEmpty())
            {
                if (show_message)
                {
                    MessageBox.Show(I18N.GetString("Already the latest version."));
                }

                return;
            }

            bool has_windows = false;

            string[] ver_split = ver.Split(',');
            for (int i = 0; i < ver_split.Length; ++i)
            {
                string[] item = ver_split[i].Split(';');
                if (item.Length < 3)
                {
                    continue;
                }

                if (item[0].Equals("windows"))
                {
                    if (String.Compare(item[1], P2pLib.kCurrentVersion) <= 0)
                    {
                        if (show_message)
                        {
                            MessageBox.Show(I18N.GetString("Already the latest version."));
                            return;
                        }
                    }
                    else
                    {
                        has_windows = true;
                    }
                }

                if (item[0].Equals("share_ip"))
                {
                    if (!item[1].IsNullOrEmpty())
                    {
                        if (item[1].StartsWith("http"))
                        {
                            P2pLib.GetInstance().share_ip_ = item[1];
                        }
                        else
                        {
                            P2pLib.GetInstance().share_ip_ = "http://" + item[1];
                        }
                    }
                }

                if (item[0].Equals("buy_ip"))
                {
                    if (!item[1].IsNullOrEmpty())
                    {
                        if (item[1].StartsWith("http"))
                        {
                            P2pLib.GetInstance().buy_tenon_ip_ = item[1];
                        }
                        else
                        {
                            P2pLib.GetInstance().buy_tenon_ip_ = "http://" + item[1];
                        }
                    }
                }

                if (item[0].Equals("er"))
                {
                    P2pLib.GetInstance().SetExRouting(item[1]);
                }
            }

            if (!has_windows)
            {
                if (show_message)
                {
                    MessageBox.Show(I18N.GetString("Already the latest version."));
                }
                return;
            }

            if (upgrade_is_shown)
            {
                return;
            }

            if (!show_message)
            {
                return;
            }
            upgrade_is_shown = true;
            if (upgradeForm != null)
            {
                upgradeForm.Activate();
            }
            else
            {
                upgradeForm = new VersionControl(ver);
                upgradeForm.Show();
                upgradeForm.Activate();
                upgradeForm.FormClosed += tmp_upgradeForm_FormClosed;
            }
        }
Ejemplo n.º 10
0
        public void ResetBalance(object obj)
        {
            autoShowUpgrade();
            checkServerInfo(false);
            if (!P2pLib.GetInstance().server_status.Equals("ok"))
            {
                if (P2pLib.GetInstance().server_status.Equals("cni"))
                {
                    this.label8.Text = I18N.GetString("Agent service is not supported in your country or region.");
                }

                if (P2pLib.GetInstance().server_status.Equals("cnn"))
                {
                    this.label8.Text = I18N.GetString("Connect p2p vpn server failed.");
                }

                if (P2pLib.GetInstance().server_status.Equals("oul"))
                {
                    this.label8.Text = I18N.GetString("Your account is logged in elsewhere.");
                }
                SynchronizationContext syncContext = SynchronizationContext.Current;
                if (P2pLib.GetInstance().connectSuccess)
                {
                    if (!P2pLib.GetInstance().disConnectStarted)
                    {
                        P2pLib.GetInstance().disConnectStarted = true;

                        Thread tmp_thread = new Thread(() =>
                        {
                            controller.Stop();
                            controller.ToggleEnable(false);
                            P2pLib.GetInstance().connectSuccess = false;
                            P2pLib.GetInstance().connectStarted = false;
                            syncContext.Post(ConnectButton.ThreadRefresh, null);
                            P2pLib.GetInstance().disConnectStarted = false;
                        });
                        tmp_thread.IsBackground = true;
                        tmp_thread.Start();
                    }
                }
            }

            long balance = P2pLib.GetInstance().Balance();

            if (balance >= 0)
            {
                P2pLib.GetInstance().now_balance = balance;
                this.label10.Text = balance + " Tenon";
            }

            if (P2pLib.GetInstance().now_balance < 0)
            {
                P2pLib.createAccount();
            }

            if (check_vip_times < 10)
            {
                long tm = P2pLib.GetInstance().CheckVIP();
                if (P2pLib.GetInstance().payfor_timestamp == 0 || tm != long.MaxValue)
                {
                    if (tm != long.MaxValue && tm != 0)
                    {
                        check_vip_times = 11;
                    }
                    P2pLib.GetInstance().payfor_timestamp = tm;
                }
                ++check_vip_times;
            }
            else
            {
                P2pLib.GetInstance().PayforVpn();
            }

//             if (P2pLib.GetInstance().vip_left_days == -1 &&
//                     P2pLib.GetInstance().now_balance != -1)
//             {
//                 this.label9.Text = "";
//                 pictureBox2.Visible = false;
//                 button5.Visible = true;
//             }

            if (P2pLib.GetInstance().vip_left_days >= 0)
            {
//                 pictureBox2.Visible = true;
//                 button5.Visible = false;
                this.label9.Text = I18N.GetString("Due in ") + P2pLib.GetInstance().vip_left_days + I18N.GetString("days");
            }
        }