private void HandleServerRequest(string words) { Message <object> Msg; try { Msg = JsonConvert.DeserializeObject <Message <object> >(words); _logger.Info($"收到服务端指令 {Msg.MessageType}"); switch (Msg.MessageType) { case MessageType.C_Heart: break; case MessageType.S_NewCustomer: var request = (Msg.Content as JObject).ToObject <NewCustomerRequest>(); var connecter = new Connecter(_clientConfig.Common.ServerAddr, _clientConfig.Common.ServerPort); connecter.Connect(); connecter.Send(new Message <string> { MessageType = MessageType.C_SwapMsg, Content = request.MsgId }); var localConnecter = new Connecter(request.WebConfig.LocalIp, request.WebConfig.LocalPort); localConnecter.Connect(); new SocketSwap(connecter.Client, localConnecter.Client).StartSwap(); break; case MessageType.S_NewSSH: var request_ssh = (Msg.Content as JObject).ToObject <NewSSHRequest>(); var connecter_ssh = new Connecter(_clientConfig.Common.ServerAddr, _clientConfig.Common.ServerPort); connecter_ssh.Connect(); connecter_ssh.Send(new Message <string> { MessageType = MessageType.C_SwapMsg, Content = request_ssh.MsgId }); var localConnecter_ssh = new Connecter(request_ssh.SSHConfig.LocalIp, request_ssh.SSHConfig.LocalPort); localConnecter_ssh.Connect(); new SocketSwap(connecter_ssh.Client, localConnecter_ssh.Client).StartSwap(); break; case MessageType.C_SwapMsg: case MessageType.C_LogIn: default: throw new Exception("参数异常"); } } catch (Exception ex) { _logger.Error(ex); _logger.Error(words); } }
private void _ConnectAsync(string ip, int port) { var resultTask = _Connecter.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); var result = resultTask.Result; Command.Unregister("Disconnect"); if (result) { Command.Register("Disconnect", () => _Connecter.Disconnect().Wait()); } }
private static void Run(IServiceProvider servicesProvider, ILogger _logger, string key, bool log) { var res = HttpHelper.PostAsJson("https://api1.suidao.io/api/Client/GetServerByKey", $"{{ \"key\":\"{key}\"}}").Result; var jobj = JObject.Parse(res); if ((bool)jobj["success"] == true) { // 记录登录记录 if (log) { AppendTextToFile(Path.Combine(AppContext.BaseDirectory, KeyLogName), Environment.NewLine + key); } var server = jobj["data"].ToObject <SuiDaoServerConfig>(); var client = servicesProvider.GetRequiredService <FastTunnelClient>(); client.Login(() => { Connecter _client = null; try { _client = new Connecter(server.ip, server.bind_port); _client.Connect(); } catch (Exception ex) { _logger.Error(ex.Message); _client.Socket.Close(); throw; } // 登录 _client.Send(new Message <LogInByKeyMassage> { MessageType = MessageType.C_LogIn, Content = new LogInByKeyMassage { key = key } }); return(_client); }, new SuiDaoServer { ServerAddr = server.ip, ServerPort = server.bind_port }); } else { Console.WriteLine(jobj["errorMsg"].ToString()); NewKey(_logger); } }
public void HandlerMsg(FastTunnelClient cleint, Message <JObject> Msg) { var request = Msg.Content.ToObject <NewCustomerMassage>(); var connecter = new Connecter(cleint._serverConfig.ServerAddr, cleint._serverConfig.ServerPort); connecter.Connect(); connecter.Send(new Message <SwapMassage> { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request.MsgId) }); var localConnecter = new Connecter(request.WebConfig.LocalIp, request.WebConfig.LocalPort); try { localConnecter.Connect(); } catch (SocketException sex) { localConnecter.Close(); if (sex.ErrorCode == 10061) { // 内网的站点不存在或无法访问 string statusLine = "HTTP/1.1 200 OK\r\n"; string responseHeader = "Content-Type: text/html\r\n"; byte[] responseBody; responseBody = Encoding.UTF8.GetBytes(TunnelResource.Page_NoSite); connecter.Send(Encoding.UTF8.GetBytes(statusLine)); connecter.Send(Encoding.UTF8.GetBytes(responseHeader)); connecter.Send(Encoding.UTF8.GetBytes("\r\n")); connecter.Send(responseBody); connecter.Socket.Disconnect(false); connecter.Socket.Close(); return; } else { throw; } } catch (Exception) { localConnecter.Close(); throw; } new SocketSwap(connecter.Socket, localConnecter.Socket).StartSwap(); }
public void Login() { //连接到的目标IP connecter.Connect(); // 登录 connecter.Send(new Message <LogInRequest> { MessageType = MessageType.C_LogIn, Content = new LogInRequest { WebList = _clientConfig.Webs } }); _logger.Debug("登录成功"); ReceiveServer(connecter.Client); _logger.Debug("客户端退出"); }
public void HandlerMsg(FastTunnelClient cleint, Message <JObject> Msg) { var request_ssh = Msg.Content.ToObject <NewSSHRequest>(); var connecter_ssh = new Connecter(cleint._serverConfig.ServerAddr, cleint._serverConfig.ServerPort); connecter_ssh.Connect(); connecter_ssh.Send(new Message <SwapMassage> { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request_ssh.MsgId) }); var localConnecter_ssh = new Connecter(request_ssh.SSHConfig.LocalIp, request_ssh.SSHConfig.LocalPort); localConnecter_ssh.Connect(); new SocketSwap(connecter_ssh.Socket, localConnecter_ssh.Socket).StartSwap(); }
private static void Run(IServiceProvider servicesProvider) { var client = servicesProvider.GetRequiredService <FastTunnelClient>(); var config = servicesProvider.GetRequiredService <ClientConfig>(); client.Login(() => { Connecter _client; try { // 连接到的目标IP _client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort); _client.Connect(); } catch (Exception ex) { Thread.Sleep(5000); throw; } // 登录 _client.Send(new Message <LogInRequest> { MessageType = MessageType.C_LogIn, Content = new LogInRequest { Webs = config.Webs, SSH = config.SSH } }); return(_client); }, config.Common); while (true) { Thread.Sleep(10000 * 60); } }
public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("===== FastTunnel Client Start ====="); config = _configuration.Get <AppSettings>().ClientSettings; _fastTunnelClient.Login(() => { Connecter _client; try { // 连接到的目标IP _client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort); _client.Connect(); } catch (Exception) { Thread.Sleep(5000); throw; } // 登录 _client.Send(new Message <LogInMassage> { MessageType = MessageType.C_LogIn, Content = new LogInMassage { Webs = config.Webs, SSH = config.SSH, AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa" }, }); return(_client); }, config.Common); return(Task.CompletedTask); }
private static void Run(IServiceProvider servicesProvider, ILogger _logger, string key, bool log) { var res_str = HttpHelper.PostAsJson(SuiDaoApi.GetServerListByKey, $"{{ \"key\":\"{key}\"}}").Result; var jobj = JObject.Parse(res_str); if ((bool)jobj["success"] == true) { // 记录登录记录 if (log) { AppendTextToFile(Path.Combine(AppContext.BaseDirectory, KeyLogName), Environment.NewLine + key); } SuiDaoServerInfo server; var res = jobj["data"].ToObject <SuiDaoServerConfig>(); if (res.servers != null && res.servers.Count() > 0) { // 选择其中一个服务器继续 if (res.servers.Count() == 1) { server = res.servers.First(); } else { Console.WriteLine("请选择其中一个服务器进行连接(输入序号,回车键确认):"); for (int i = 0; i < res.servers.Length; i++) { Console.WriteLine($"{i}:{res.servers[i].server_name}"); } while (true) { var input = Console.ReadLine(); int index; if (int.TryParse(input, out index) && index <= res.servers.Length - 1 && index >= 0) { // 输入有效,退出循环 server = res.servers[index]; Console.WriteLine($"您选择的服务器为:{server.server_name}"); break; } else { Console.WriteLine("输入有误,请重新输入"); } } } } else { Console.WriteLine("您无可用的服务器"); NewKey(_logger); return; } var client = servicesProvider.GetRequiredService <FastTunnelClient>(); client.Login(() => { Connecter _client = null; try { _client = new Connecter(server.ip, server.bind_port); _client.Connect(); } catch (Exception ex) { _logger.Error(ex.Message); _client.Socket.Close(); throw; } // 登录 _client.Send(new Message <LogInByKeyMassage> { MessageType = MessageType.C_LogIn, Content = new LogInByKeyMassage { key = key, server_id = server.server_id } }); return(_client); }, new SuiDaoServer { ServerAddr = server.ip, ServerPort = server.bind_port }); } else { Console.WriteLine(jobj["errorMsg"].ToString()); NewKey(_logger); } }
private void HandleServerRequest(string words) { Message <object> Msg; try { Msg = JsonConvert.DeserializeObject <Message <object> >(words); switch (Msg.MessageType) { case MessageType.Heart: lastHeart = DateTime.Now; break; case MessageType.S_NewCustomer: var request = (Msg.Content as JObject).ToObject <NewCustomerRequest>(); var connecter = new Connecter(_serverConfig.ServerAddr, _serverConfig.ServerPort); connecter.Connect(); connecter.Send(new Message <string> { MessageType = MessageType.C_SwapMsg, Content = request.MsgId }); var localConnecter = new Connecter(request.WebConfig.LocalIp, request.WebConfig.LocalPort); localConnecter.Connect(); new SocketSwap(connecter.Socket, localConnecter.Socket).StartSwap(); break; case MessageType.S_NewSSH: var request_ssh = (Msg.Content as JObject).ToObject <NewSSHRequest>(); var connecter_ssh = new Connecter(_serverConfig.ServerAddr, _serverConfig.ServerPort); connecter_ssh.Connect(); connecter_ssh.Send(new Message <string> { MessageType = MessageType.C_SwapMsg, Content = request_ssh.MsgId }); var localConnecter_ssh = new Connecter(request_ssh.SSHConfig.LocalIp, request_ssh.SSHConfig.LocalPort); localConnecter_ssh.Connect(); new SocketSwap(connecter_ssh.Socket, localConnecter_ssh.Socket).StartSwap(); break; case MessageType.Info: var info = Msg.Content.ToJson(); _logger.LogInformation("From Server:" + info); break; case MessageType.LogDebug: var LogDebug = Msg.Content.ToJson(); _logger.LogDebug("From Server:" + LogDebug); break; case MessageType.Error: var err = Msg.Content.ToJson(); _logger.LogError("From Server:" + err); break; case MessageType.C_SwapMsg: case MessageType.C_LogIn: default: throw new Exception("参数异常"); } } catch (Exception ex) { _logger.LogError(ex); _logger.LogError(words); } }