Skip to content

Commit

Permalink
support server to client control.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoonlight committed Apr 21, 2020
1 parent b8a1f95 commit 9a9e40e
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 91 deletions.
44 changes: 38 additions & 6 deletions src/NSmartProxy.ClientRouter/Dispatchers/NSPDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NSmartProxy.Data;
Expand All @@ -12,25 +13,45 @@ namespace NSmartProxy.ClientRouter.Dispatchers
public class NSPDispatcher
{
private string BaseUrl;
//TODO httpclient的一种解决方案:定时对象
private static HttpClient _client;
private static Timer _timer = new Timer(obj=> {
_client?.Dispose();
_client = null;
});

//_client.Dispose();_client = null

public NSPDispatcher(string baseUrl)
{
BaseUrl = baseUrl;
}

public static HttpClient Client
{
get
{
if (_client == null)
{
//_timer = new
_client = new HttpClient();
}
return _client;
}
}

public async Task<HttpResult<LoginFormClientResult>> LoginFromClient(string username, string userpwd)
{
string url = $"http://{BaseUrl}/LoginFromClient";
HttpClient client = new HttpClient();
var httpmsg = await client.GetAsync($"{url}?username={username}&userpwd={userpwd}").ConfigureAwait(false);
var httpmsg = await Client.GetAsync($"{url}?username={username}&userpwd={userpwd}").ConfigureAwait(false);
var httpstr = await httpmsg.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<HttpResult<LoginFormClientResult>>(httpstr);
}

public async Task<HttpResult<LoginFormClientResult>> Login(string userid, string userpwd)
{
string url = $"http://{BaseUrl}/LoginFromClientById";
HttpClient client = new HttpClient();
var httpmsg = await client.GetAsync($"{url}?username={userid}&userpwd={userpwd}").ConfigureAwait(false);
var httpmsg = await Client.GetAsync($"{url}?username={userid}&userpwd={userpwd}").ConfigureAwait(false);
var httpstr = await httpmsg.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<HttpResult<LoginFormClientResult>>(httpstr);
}
Expand All @@ -41,10 +62,21 @@ public async Task<HttpResult<LoginFormClientResult>> Login(string userid, string
public async Task<HttpResult<ServerPortsDTO>> GetServerPorts()
{
string url = $"http://{BaseUrl}/GetServerPorts";
HttpClient client = new HttpClient();
var httpmsg = await client.GetAsync(url).ConfigureAwait(false);
var httpmsg = await Client.GetAsync(url).ConfigureAwait(false);
var httpstr = await httpmsg.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<HttpResult<ServerPortsDTO>>(httpstr);
}

/// <summary>
/// 当允许服务端端修改客户端时,从服务端获取配置
/// </summary>
/// <returns></returns>
public async Task<HttpResult<NSPClientConfig>> GetServerClientConfig()
{
string url = $"http://{BaseUrl}/GetServerClientConfig";
var httpmsg = await Client.GetAsync(url).ConfigureAwait(false);
var httpstr = await httpmsg.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<HttpResult<NSPClientConfig>>(httpstr);
}
}
}
4 changes: 3 additions & 1 deletion src/NSmartProxy.Data/Config/NSPClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using Newtonsoft.Json;

namespace NSmartProxy.Data
{
{
public class NSPClientConfig
{
[JsonIgnore]
public int ReversePort; //代理转发服务端口
[JsonIgnore]
public int ConfigPort; //配置服务端口

public bool UseServerControl = true; //启用服务端配置
public string ProviderAddress; //代理服务器地址
public int ProviderWebPort; //web管理端的端口,默认12309 //TODO 暂时写死,以后再改
public List<ClientApp> Clients = new List<ClientApp>();//客户端app
Expand Down
6 changes: 4 additions & 2 deletions src/NSmartProxy.Data/ServerProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public enum ServerProtocol : byte
/// </summary>
public enum ControlMethod : byte
{
TCPTransfer = 0x01,
TCPTransfer = 0x01,
KeepAlive = 0x03,
UDPTransfer = 0x04,

// Control = 0x05, //控制协议,用来让服务端控制客户端的配置
Reconnect = 0x05, //重置协议,服务端发送此信号让客户端重新连接

}

}
1 change: 1 addition & 0 deletions src/NSmartProxy.Infrastructure/Shared/IDbOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IDbOperator : IDisposable
void Update(long key, string value);
void UpdateByName(string userName,string newUserName, string value);
List<string> Select(int startIndex, int length);
string GetConfig(string userId);
string Get(long key);
string Get(string key);
void Delete(int index);
Expand Down
6 changes: 6 additions & 0 deletions src/NSmartProxy/Database/LiteDbOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public List<string> Select(int startIndex, int length)
return liteCollection.FindAll().Select(kv => kv.Value).ToList();
}

public string GetConfig(string userId)
{
throw new NotImplementedException();
}

public string Get(long key)
{
return Get(key.ToString());
Expand Down Expand Up @@ -207,5 +212,6 @@ public KV(string k, string val)
[BsonId]
public string Key { get; set; }
public string Value { get; set; }
public string Config { get; set; }//客户端配置后期指定
}
}
5 changes: 5 additions & 0 deletions src/NSmartProxy/Database/NSmartDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public List<string> Select(int startIndex, int length)
//输出
}

public string GetConfig(string userId)
{
throw new NotImplementedException();
}

/// <summary>
/// 通过序列删除数据,并且返回id
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions src/NSmartProxy/Extension/HttpServer_APIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,22 @@ public bool ValidateUserName(string isEdit, string oldUsername, string newUserNa

}

[API]
[Secure]
public NSPClientConfig GetServerClientConfig(string userToken)
{

return new NSPClientConfig();
}

[API]
[Secure]
public void SetServerClientConfig(string userId, NSPClientConfig config)
{

//return new NSPClientConfig();
}

#endregion

#region connections
Expand Down
Loading

0 comments on commit 9a9e40e

Please sign in to comment.