Exemplo n.º 1
0
        public LoginHandler(ILogger logger)
        {
            _logger = logger;
            var custome = FastTunnelGlobal.GetCustomHandler <IConfigHandler>();

            _configHandler = custome == null ? new ConfigHandler() : custome;
        }
Exemplo n.º 2
0
 public SuiDaoServer(ILogger <SuiDaoServer> logger, IConfiguration configuration)
 {
     _logger           = logger;
     _configuration    = configuration;
     _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get <Appsettings>().ServerSettings);
     FastTunnelGlobal.AddCustomHandler <IConfigHandler, SuiDaoConfigHandler>(new SuiDaoConfigHandler());
 }
Exemplo n.º 3
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                FastTunnelGlobal.AddFilter(_authenticationFilter);

                try
                {
                    _server.Run();
                }
                catch (Exception ex)
                {
                    // NLog: catch any exception and log it.
                    _logger.LogError(ex, "Server Error");
                    Console.WriteLine(ex);
                }
            }, cancellationToken));
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("===== FastTunnel Server Starting =====");

            _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get <AppSettings>().ServerSettings);
            FastTunnelGlobal.AddFilter(_authenticationFilter);

            try
            {
                _fastTunnelServer.Run();

                _logger.LogDebug("Server Run Success");
            }
            catch (Exception ex)
            {
                // NLog: catch any exception and log it.
                _logger.LogError(ex, "Server Error");
                Console.WriteLine(ex);
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 5
0
        public void HandleLogin(FastTunnelServer server, Socket client, LogInMassage requet)
        {
            bool hasTunnel = false;

            var filters = FastTunnelGlobal.GetFilters(typeof(IFastTunnelAuthenticationFilter));

            if (filters.Count() > 0)
            {
                foreach (IFastTunnelAuthenticationFilter item in filters)
                {
                    var result = item.Authentication(server, requet);
                    if (!result)
                    {
                        client.Send(new Message <LogMassage>
                        {
                            MessageType = MessageType.Log,
                            Content     = new LogMassage(LogMsgType.Error, "认证失败")
                        });

                        return;
                    }
                }
            }

            var sb = new StringBuilder($"{Environment.NewLine}=====隧道已建立成功,可通过以下方式访问内网服务====={Environment.NewLine}");

            if (requet.Webs != null && requet.Webs.Count() > 0)
            {
                hasTunnel = true;
                foreach (var item in requet.Webs)
                {
                    var hostName = $"{item.SubDomain}.{server.ServerSettings.WebDomain}".Trim();
                    var info     = new WebInfo {
                        Socket = client, WebConfig = item
                    };

                    _logger.LogDebug($"new domain '{hostName}'");
                    server.WebList.AddOrUpdate(hostName, info, (key, oldInfo) => { return(info); });
                    sb.Append($"{Environment.NewLine} http://{hostName}{(server.ServerSettings.WebHasNginxProxy ? string.Empty : ":" + server.ServerSettings.WebProxyPort)} => {item.LocalIp}:{item.LocalPort}");

                    if (item.WWW != null)
                    {
                        foreach (var www in item.WWW)
                        {
                            if (!www.EndsWith(server.ServerSettings.WebDomain))
                            {
                                server.WebList.AddOrUpdate(www, info, (key, oldInfo) => { return(info); });
                                sb.Append($"{Environment.NewLine}  http://{www}{(server.ServerSettings.WebHasNginxProxy ? string.Empty : ":" + server.ServerSettings.WebProxyPort)} => {item.LocalIp}:{item.LocalPort}");
                            }
                            else
                            {
                                // can`t use WebDomain
                                _logger.LogError($"Invalid WebDomain IN WWW {www}");
                            }
                        }
                    }
                }
            }


            if (requet.SSH != null && requet.SSH.Count() > 0)
            {
                hasTunnel = true;

                foreach (var item in requet.SSH)
                {
                    try
                    {
                        if (item.RemotePort.Equals(server.ServerSettings.BindPort))
                        {
                            _logger.LogError($"RemotePort can not be same with BindPort: {item.RemotePort}");
                            continue;
                        }

                        if (item.RemotePort.Equals(server.ServerSettings.WebProxyPort))
                        {
                            _logger.LogError($"RemotePort can not be same with ProxyPort_HTTP: {item.RemotePort}");
                            continue;
                        }

                        SSHInfo <SSHHandlerArg> old;
                        if (server.SSHList.TryGetValue(item.RemotePort, out old))
                        {
                            _logger.LogDebug($"Remove Listener {old.Listener.ListenIp}:{old.Listener.ListenPort}");
                            old.Listener.Stop();
                            server.SSHList.TryRemove(item.RemotePort, out SSHInfo <SSHHandlerArg> _);
                        }

                        var ls = new PortProxyListener("0.0.0.0", item.RemotePort, _logger);

                        ls.Start(new SSHDispatcher(server, client, item));

                        // listen success
                        server.SSHList.TryAdd(item.RemotePort, new SSHInfo <SSHHandlerArg> {
                            Listener = ls, Socket = client, SSHConfig = item
                        });
                        _logger.LogDebug($"SSH proxy success: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}");

                        sb.Append($"{Environment.NewLine}  {server.ServerSettings.WebDomain}:{item.RemotePort} => {item.LocalIp}:{item.LocalPort}");
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"SSH proxy error: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}");
                        _logger.LogError(ex.Message);
                        client.Send(new Message <LogMassage> {
                            MessageType = MessageType.Log, Content = new LogMassage(LogMsgType.Info, ex.Message)
                        });
                        continue;
                    }
                }
            }

            if (!hasTunnel)
            {
                client.Send(new Message <LogMassage> {
                    MessageType = MessageType.Log, Content = new LogMassage(LogMsgType.Info, TunnelResource.NoTunnel)
                });
            }
            else
            {
                sb.Append($"{Environment.NewLine}{Environment.NewLine}====================================================");
                client.Send(new Message <LogMassage> {
                    MessageType = MessageType.Log, Content = new LogMassage(LogMsgType.Info, sb.ToString())
                });
            }
        }