コード例 #1
0
        private void Connect()
        {
            int     local_port = ((IPEndPoint)_connection.LocalEndPoint).Port;
            Handler handler    = new Handler();

            handler.getCurrentServer         = delegate(string targetURI, bool usingRandom, bool forceRandom) { return(_config.GetCurrentServer(targetURI, usingRandom, forceRandom)); };
            handler.keepCurrentServer        = delegate(string targetURI, string id) { _config.KeepCurrentServer(targetURI, id); };
            handler.connection               = _connection;
            handler.connectionUDP            = _connectionUDP;
            handler.cfg.reconnectTimesRemain = _config.reconnectTimes;
            handler.cfg.forceRandom          = _config.random;
            handler.setServerTransferTotal(_transfer);
            if (_config.proxyEnable)
            {
                handler.cfg.proxyType            = _config.proxyType;
                handler.cfg.socks5RemoteHost     = _config.proxyHost;
                handler.cfg.socks5RemotePort     = _config.proxyPort;
                handler.cfg.socks5RemoteUsername = _config.proxyAuthUser;
                handler.cfg.socks5RemotePassword = _config.proxyAuthPass;
                handler.cfg.proxyUserAgent       = _config.proxyUserAgent;
            }
            handler.cfg.TTL             = _config.TTL;
            handler.cfg.connect_timeout = _config.connect_timeout;
            handler.cfg.autoSwitchOff   = _config.autoBan;
            if (_config.dns_server != null && _config.dns_server.Length > 0)
            {
                handler.cfg.dns_servers = _config.dns_server;
            }
            if (_config.GetPortMapCache().ContainsKey(local_port))
            {
                PortMapConfigCache cfg = _config.GetPortMapCache()[local_port];
                if (cfg.id == cfg.server.id)
                {
                    handler.select_server = cfg.server;
                    byte[] addr           = System.Text.Encoding.UTF8.GetBytes(cfg.server_addr);
                    byte[] newFirstPacket = new byte[_firstPacketLength + addr.Length + 4];
                    newFirstPacket[0] = 3;
                    newFirstPacket[1] = (byte)addr.Length;
                    Array.Copy(addr, 0, newFirstPacket, 2, addr.Length);
                    newFirstPacket[addr.Length + 2] = (byte)(cfg.server_port / 256);
                    newFirstPacket[addr.Length + 3] = (byte)(cfg.server_port % 256);
                    Array.Copy(_firstPacket, 0, newFirstPacket, addr.Length + 4, _firstPacketLength);
                    _remoteHeaderSendBuffer = newFirstPacket;
                }
            }
            handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length);
        }
コード例 #2
0
        private void Connect()
        {
            Handler.GetCurrentServer  getCurrentServer  = delegate(int localPort, ServerSelectStrategy.FilterFunc filter, string targetURI, bool cfgRandom, bool usingRandom, bool forceRandom) { return(_config.GetCurrentServer(localPort, filter, targetURI, cfgRandom, usingRandom, forceRandom)); };
            Handler.KeepCurrentServer keepCurrentServer = delegate(int localPort, string targetURI, string id) { _config.KeepCurrentServer(localPort, targetURI, id); };

            int     local_port = ((IPEndPoint)_connection.LocalEndPoint).Port;
            Handler handler    = new Handler();

            handler.getCurrentServer         = getCurrentServer;
            handler.keepCurrentServer        = keepCurrentServer;
            handler.connection               = new ProxySocketTunLocal(_connection);
            handler.connectionUDP            = _connectionUDP;
            handler.cfg.ReconnectTimesRemain = _config.reconnectTimes;
            handler.cfg.Random               = _config.random;
            handler.cfg.ForceRandom          = _config.random;
            handler.setServerTransferTotal(_transfer);
            if (_config.proxyEnable)
            {
                handler.cfg.ProxyType            = _config.proxyType;
                handler.cfg.Socks5RemoteHost     = _config.proxyHost;
                handler.cfg.Socks5RemotePort     = _config.proxyPort;
                handler.cfg.Socks5RemoteUsername = _config.proxyAuthUser;
                handler.cfg.Socks5RemotePassword = _config.proxyAuthPass;
                handler.cfg.ProxyUserAgent       = _config.proxyUserAgent;
            }
            handler.cfg.Ttl            = _config.TTL;
            handler.cfg.ConnectTimeout = _config.connectTimeout;
            handler.cfg.AutoSwitchOff  = _config.autoBan;
            if (!string.IsNullOrEmpty(_config.localDnsServer))
            {
                handler.cfg.LocalDnsServers = _config.localDnsServer;
            }
            if (!string.IsNullOrEmpty(_config.dnsServer))
            {
                handler.cfg.DnsServers = _config.dnsServer;
            }
            if (_config.GetPortMapCache().ContainsKey(local_port))
            {
                PortMapConfigCache cfg = _config.GetPortMapCache()[local_port];
                if (cfg.server == null || cfg.id == cfg.server.id)
                {
                    if (cfg.server != null)
                    {
                        handler.select_server = delegate(Server server, Server selServer) { return(server.id == cfg.server.id); };
                    }
                    else if (!string.IsNullOrEmpty(cfg.id))
                    {
                        handler.select_server = delegate(Server server, Server selServer) { return(server.group == cfg.id); };
                    }
                    if (cfg.type == PortMapType.Forward) // tunnel
                    {
                        byte[] addr           = Encoding.UTF8.GetBytes(cfg.server_addr);
                        byte[] newFirstPacket = new byte[_firstPacketLength + addr.Length + 4];
                        newFirstPacket[0] = 3;
                        newFirstPacket[1] = (byte)addr.Length;
                        Array.Copy(addr, 0, newFirstPacket, 2, addr.Length);
                        newFirstPacket[addr.Length + 2] = (byte)(cfg.server_port / 256);
                        newFirstPacket[addr.Length + 3] = (byte)(cfg.server_port % 256);
                        Array.Copy(_firstPacket, 0, newFirstPacket, addr.Length + 4, _firstPacketLength);
                        _remoteHeaderSendBuffer = newFirstPacket;
                        handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, null);
                    }
                    else if (_connectionUDP == null && cfg.type == PortMapType.RuleProxy &&
                             (new Socks5Forwarder(_config, _IPRange)).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol))
                    {
                    }
                    else
                    {
                        handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, "socks5");
                    }
                    Dispose();
                    return;
                }
            }
            else
            {
                if (_connectionUDP == null && new Socks5Forwarder(_config, _IPRange).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol))
                {
                }
                else
                {
                    handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, local_sendback_protocol);
                }
                Dispose();
                return;
            }
            Dispose();
            Close();
        }
コード例 #3
0
ファイル: ProxyAuth.cs プロジェクト: KoakiMiku/ShadowsocksR
        private void Connect()
        {
            Handler.GetCurrentServer getCurrentServer = delegate(int localPort, ServerSelectStrategy.FilterFunc filter, string targetURI) { return(_config.GetCurrentServer(localPort, filter, targetURI)); };

            int     local_port = ((IPEndPoint)_connection.LocalEndPoint).Port;
            Handler handler    = new Handler
            {
                getCurrentServer = getCurrentServer,
                connection       = new ProxySocketTunLocal(_connection),
                connectionUDP    = _connectionUDP
            };

            handler.cfg.reconnectTimesRemain = _config.reconnectTimes;
            handler.setServerTransferTotal(_transfer);
            handler.cfg.TTL             = _config.TTL;
            handler.cfg.connect_timeout = _config.connectTimeout;
            if (!string.IsNullOrEmpty(_config.dnsServer))
            {
                handler.cfg.dns_servers = _config.dnsServer;
            }
            if (_config.GetPortMapCache().ContainsKey(local_port))
            {
                PortMapConfigCache cfg = _config.GetPortMapCache()[local_port];
                if (cfg.server == null || cfg.id == cfg.server.id)
                {
                    if (cfg.server != null)
                    {
                        handler.select_server = delegate(Server server, Server selServer) { return(server.id == cfg.server.id); };
                    }
                    else if (!string.IsNullOrEmpty(cfg.id))
                    {
                        handler.select_server = delegate(Server server, Server selServer) { return(server.group == cfg.id); };
                    }
                    if (cfg.type == PortMapType.Forward) // tunnel
                    {
                        byte[] addr           = Encoding.UTF8.GetBytes(cfg.server_addr);
                        byte[] newFirstPacket = new byte[_firstPacketLength + addr.Length + 4];
                        newFirstPacket[0] = 3;
                        newFirstPacket[1] = (byte)addr.Length;
                        Array.Copy(addr, 0, newFirstPacket, 2, addr.Length);
                        newFirstPacket[addr.Length + 2] = (byte)(cfg.server_port / 256);
                        newFirstPacket[addr.Length + 3] = (byte)(cfg.server_port % 256);
                        Array.Copy(_firstPacket, 0, newFirstPacket, addr.Length + 4, _firstPacketLength);
                        _remoteHeaderSendBuffer = newFirstPacket;
                        handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, null);
                    }
                    else if (_connectionUDP == null && cfg.type == PortMapType.RuleProxy &&
                             (new Socks5Forwarder(_config, _IPRange)).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol))
                    {
                    }
                    else
                    {
                        handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, "socks5");
                    }
                    Dispose();
                    return;
                }
            }
            else
            {
                if (_connectionUDP == null && new Socks5Forwarder(_config, _IPRange).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol))
                {
                }
                else
                {
                    handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, local_sendback_protocol);
                }
                Dispose();
                return;
            }
            Dispose();
            Close();
        }