using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Lskj.PubEncryption
{
public interface ServerDirectConnection
{
string ConnectServer(bool isSave);
}
///
/// 公共调用加密功能
///
[ClassInterface(ClassInterfaceType.None)]
[Guid("D98EA347-25B6-4DEB-89E2-610730EF728E")]
[ProgId("Lskj.PubEncryption.ServerDirectConnection")]
public class PubEncryption:ServerDirectConnection
{
///
/// 默认下载地址
///
public string filePath = string.Empty;
///
/// 返回地址值
///
public string outText = string.Empty;
///
/// 账套数据
///
public DataTable sourceTab = new DataTable();
///
/// 验证成功后写入本地ini配置中
///
///
///
///
public void Verification(string txtIP,string txtPort,string txtZTName)
{
if (!string.IsNullOrEmpty(txtZTName))
{
//服务器下载
string newServerName = "http://" + txtIP + txtPort;
newServerName = !newServerName.EndsWith("/") ? newServerName + "/" : newServerName;
string newfilePath = Path.Combine(newServerName, "SystemResources.txt");
//string name = WebRequest.Create(filePath).GetResponse().GetResponseStream().ToString();
string fileName = "SystemResources.txt";//客户端保存的文件名
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
string newoutText = newfilePath != filePath ? webClient.DownloadString(newfilePath) : outText;
outText = newoutText.Trim();
string IPPort = string.Format("{0}:{1}", txtIP, txtPort);
string writeText = string.Format("{0}^{1}", txtZTName, outText);
IniHelper.Write("SystemResources.ini", string.Format("DownloadAESStr_{0}", IPPort), writeText);
IniHelper.Write("SystemResources.ini", "LocalLastIP", txtIP);
IniHelper.Write("SystemResources.ini", "LocalLastPort", txtPort);
IniHelper.Write("SystemResources.ini", "LocalLastPortName", txtZTName);
}
else
{
MessageBox.Show("请设置账套名!");
}
}
///
/// 说明:绑定多账套类型接口
/// 创建人:王一帆
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private Transmission AllConfigLoad()
{
Transmission transmission = new Transmission();
sourceTab.Columns.Add("dm");
sourceTab.Columns.Add("mc");
try
{
Dictionary sourceDic = IniHelper.GetSectionKeys("SystemResources.ini", "SystemResources");
foreach (string key in sourceDic.Keys)
{
if (key.Contains("DownloadAESStr_"))
{
DataRow row = sourceTab.NewRow();
string[] keyArgs = key.Split('_');
if (keyArgs.Length == 2)
{
row["dm"] = keyArgs[1];//ip+port
row["mc"] = sourceDic[key].Split('^')[0];//账套名
sourceTab.Rows.Add(row);
}
else
continue;
}
}
transmission.BandTable = sourceTab;
transmission.txtZT= string.Format("{0}:{1}", IniHelper.Read("SystemResources.ini", "LocalLastIP"), IniHelper.Read("SystemResources.ini", "LocalLastPort"));
try
{
DataRow focusRow = sourceTab.Select(transmission.txtZT)[0];
transmission.txtiP = (focusRow["dm"] + "").Split(':')[0];
transmission.txtport = (focusRow["dm"] + "").Split(':')[1];
transmission.txtZTName = focusRow["mc"] + "";
return transmission;
}
catch (Exception)
{
}
return transmission;
//this.txtIP.Text = IniHelper.Read("SystemResources.ini", "LocalLastIP");
//this.txtPort.Text = IniHelper.Read("SystemResources.ini", "LocalLastPort");
//this.txtZTName.Text = IniHelper.Read("SystemResources.ini", "LocalLastPortName");
}
catch (Exception ex)
{
MessageBox.Show("获取数据失败");
return transmission;
}
}
///
/// 说明:连接服务器
/// 创建人:王一帆
/// 创建日期:2017-08-09
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// if set to true [is save].
public string ConnectServer(bool isSave = false)
{
string ip = DBConfig.Instance.LocalLastIP;
string textport = DBConfig.Instance.LocalLastPort;
string dephistr = string.Empty;//调用时候创建返回集合
if (!string.IsNullOrEmpty(textport))
{
// 测试数据库连接
try
{
string defaultPort = ":" + textport;
//服务器下载
string newServerName = "http://" + ip + defaultPort;
newServerName = !newServerName.EndsWith("/") ? newServerName + "/" : newServerName;
string newfilePath = Path.Combine(newServerName, "SystemResources.txt");
//string name = WebRequest.Create(filePath).GetResponse().GetResponseStream().ToString();
string fileName = "SystemResources.txt";//客户端保存的文件名
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
//这里使用DownloadString方法,如果是不需要对文件的文本内容做处理,直接保存,那么可以直接使用功能DownloadFile(url,savepath)直接进行文件保存。
string newoutText = newfilePath != filePath ? webClient.DownloadString(newfilePath) : outText;
outText = newoutText.Trim();
filePath = newfilePath;
//File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.txt"), outText);
string args = AESUtil.Decrypt(outText);
string[] controlAfgs = args.Split('^');
if (controlAfgs != null && controlAfgs.Length == 5)
{
dephistr = "Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5";
//dephistr = AESUtil.Encrypt(dephistr);
}
Verification(ip, textport, dephistr);
//IniHelper.Write("SystemResources.ini", "FrmConfigFlag", "1");
return dephistr;
}
catch (Exception ex)
{
MessageBox.Show("解析失败!" + ex);
return dephistr;
}
}
else
{
return DBConfig.Instance.dephiConnection;
}
}
}
}