使用cpolar实现内网穿透
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cpolar实现内网穿透相关的知识,希望对你有一定的参考价值。
参考技术A 登录到cpolar后台,获取到自己的token值,然后复制到命令中,替换xxxxxxxx配置文件会保存在/root/.cpolar/cpolar.yml,记录下该路径
编辑成如下内容:
注:这里的配置文件是yaml格式,每一级的参数前面,会缩进两个空格,不能使用TAB键
https://dashboard.cpolar.com/status
2022年10月 .Net Core使用cpolar内网穿透功能实现钉钉回调事件的监听
文章目录
前言
1.cpolar简介
cpolar是一款拥有远程控制和内网穿透功能的软件。而且还可以监控端口的HTTP请求,利用实时的cpolar Web UI开发者工具,让您调试代码更容易。您可以监听所有隧道上的HTTP消息包,分析消息包的结构内容,找出问题点。还可以单击重放(Replay)按钮,重新发送该HTTP信令请求。
2.cpolar功能
1、个人使用场景:
- 远程桌面访问公司电脑
- 远程方面家中电脑
- 搭建私人Web站点
2、中小企业远程办公场景:
- 远程桌面(个人或公司使用)
- 远程访问文件共享服务器(中小企业使用)
- 远程访问公司内网财务系统、进销存系统、ERP系统(小中企业使用)
3、游戏玩家使用场景:
- 搭建个人游戏私服
4、群晖NAS(个人网络存储服务器)用户使用
- 远程访问家中的NAS文件服务器Web管理界面
- 远程文件共享服务
- 远程用手机看视频(通过手机或PC远程访问家中的视频文件库,观看小姐姐)
- 为啥要用远程,因为NAS服务器通常也是BT迅雷下载服务器,小姐姐或电影都存在这上面。
5、开发人员使用场景:
- 搭建Web站点,用于测试,用于给客户演示场景
- 联调公网API服务,例如远程调试支付宝接口
- 联调开发微信公众号,小程序接口
- 远程访问数据库
- 搭建私有git源代码仓库
- 搭建私有CI服务器
- 搭建私有SVN源代码服务器
- 远程SSH服务器家中的服务器,或者公司的服务器
- 远程使用vs code编辑代码
6、批量商业使用场景:
- 开发智能终端的用户,希望在他们开发的每个终端上,都安装cpolar,用于可以随时ssh远程访问的能力。例如,初期100台设备上安装(试生产),成功后,再部署到1-4万台设备上。
- 企业用户,希望在公司的每台电脑上,都配置远程桌面功能,访问疫情期间,远程办公方便。
- 有一个店家,有5个店,他是老板,但他并不希望每次都跑过去查看电脑上的财务数据。可以安装cpolar到这5个店的电脑中,他只要在家中,远程访问即可。
- 私有云服务,企业希望打造一套自己的内网穿透系统,独立搭建一套私有云服务。
一、无公网IP异地远程连接内网群晖NAS【内网穿透】
1.1 注册cpolar账号
官网链接:https://i.cpolar.com/m/4VfC
进入cpolar官网,我们先点击右上角的免费注册
,使用邮箱注册一个账号,我们后面会需要用到。
1.2 下载cpolar客户端进行内网穿透
下载完一路安装就好了,安装完成进入安装的文件夹执行注册,注册的命令如下:
cpolar authtoken <YOUR_AUTH_TOKEN>
cpolar http 5000
执行成功界面如下
访问网址:https://458ed76c.vip.cpolar.cn/swagger/index.html
内网swagger映射成功
1.3 进行钉钉回调事件的监听测试
进入钉钉后台管理注册事件订阅
进行保存后端断点收到钉钉发过来信息,支持内网应用程序和钉钉已经进行互通了
1.4 进行钉钉回调事件的监听的源码解析如下
1.4.1 控制器代码
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NSwag.Annotations;
using System.Collections;
using YG.Monomer.Framework.Controllers.Other_Manage.DingDing;
using YG.Monomer.Framework.Filters.格式化返回结果;
namespace YG.Monomer.Framework.Controllers.Other_Manage
[Route("/Other_Manage/[controller]/[action]")]
[OpenApiTag("第三方回调-钉钉事件监听")]
public class DDCallBackController : BaseApiController
private readonly IConfiguration Configuration;
/// <summary>
/// 日志注入
/// </summary>
/// <param name="loggerFactory"></param>
public DDCallBackController(IConfiguration configuration)
Configuration = configuration;
[HttpGet]
[AllowAnonymous]
[NoFormatResponse]
public string ReadConfig()
return Configuration["DingDing:Token"];
/// <summary>
/// 钉钉注册的回调地址
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="body"></param>
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
[NoFormatResponse]
public dynamic CallBack(string signature, string timestamp, string nonce, PostBody body)
//接收encrypt参数
string encryptStr = body.encrypt.Replace("\\"encrypt\\":\\"", "").Replace("\\"", "");
//注册时填写的token、aes_key、suitekey
string token = Configuration["DingDing:Token"];
string aes_key = Configuration["DingDing:AesKey"];
string suitekey = Configuration["DingDing:Suitekey"];
#region 验证回调的url
DingTalkCrypt dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
string sEchoStr = "";
dingTalk.VerifyURL(signature, timestamp, nonce, encryptStr, ref sEchoStr);
#endregion
#region 解密接受信息,进行事件处理
string plainText = "";
dingTalk.DecryptMsg(signature, timestamp, nonce, encryptStr, ref plainText);
Hashtable tb = (Hashtable)JsonConvert.DeserializeObject(plainText, typeof(Hashtable))!;
string eventType = tb["EventType"].ToString()!;
//string processCode = tb["processCode"] == null ? null : tb["processCode"].ToString();//任务码
string res = "success";
switch (eventType)
case "bpms_instance_change"://审批实例改变,执行代码
#region 审批实例改变,执行代码
//if (processCode == Configuration["PurProcessCode"])
//
// _purdemitem.AddPurDem(_dDHelper.GetProcessInstance(tb["processInstanceId"].ToString()));
//
#endregion
break;
default:
break;
timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds).ToString();
string encrypt = "";
string signature2 = "";
dingTalk = new DingTalkCrypt(token, aes_key, suitekey);
//返回成功信息
dingTalk.EncryptMsg(res, timestamp, nonce, ref encrypt, ref signature2);
Hashtable jsonMap = new Hashtable
"msg_signature", signature2,
"encrypt", encrypt,
"timeStamp", timestamp,
"nonce", nonce
;
return JsonConvert.SerializeObject(jsonMap);
#endregion
/// <summary>
/// 钉钉解密消息体
/// </summary>
public class PostBody
/// <summary>
/// 密文
/// </summary>
public string? encrypt get; set;
1.4.2 相关加解密代码
AESHepler.cs
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace YG.Monomer.Framework.Controllers.Other_Manage.DingDing
/// <summary>
/// 钉钉加解密库
/// </summary>
public class AESHepler
/// <summary>
/// 加密
/// </summary>
/// <param name="input">原文</param>
/// <param name="aesKey">AES密钥</param>
/// <param name="corpid">企业CorpId</param>
/// <returns></returns>
public static string AESEncrypt(string input, string aesKey, string corpid)
byte[] Key;
Key = Convert.FromBase64String(aesKey + "=");
byte[] Iv = new byte[16];
Array.Copy(Key, Iv, 16);
string Randcode = CreateRandCode(16);
byte[] bRand = Encoding.UTF8.GetBytes(Randcode);
byte[] bCorpid = Encoding.UTF8.GetBytes(corpid);
byte[] btmpMsg = Encoding.UTF8.GetBytes(input);
byte[] bMsgLen = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
byte[] bMsg = new byte[bRand.Length + bMsgLen.Length + bCorpid.Length + btmpMsg.Length];
Array.Copy(bRand, bMsg, bRand.Length);
Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
Array.Copy(bCorpid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bCorpid.Length);
return AESEncrypt(bMsg, Key, Iv);
/// <summary>
/// 解密
/// </summary>
/// <param name="input">密文</param>
/// <param name="aesKey">AES密钥</param>
/// <param name="corpId"></param>
/// <returns></returns>
public static string AESDecrypt(string input, string aesKey, ref string corpId)
byte[] Key;
Key = Convert.FromBase64String(aesKey + "=");
byte[] Iv = new byte[16];
Array.Copy(Key, Iv, 16);
byte[] btmpMsg = null;
using (var aesAlg = Aes.Create())
aesAlg.KeySize = 256;
aesAlg.BlockSize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CBC;
using (var decryptor = aesAlg.CreateDecryptor(Key, Iv))
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
byte[] xXml = Convert.FromBase64String(input);
byte[] msg = new byte[xXml.Length + 32 - xXml.Length % 32];
Array.Copy(xXml, msg, xXml.Length);
cs.Write(xXml, 0, xXml.Length);
btmpMsg = decode2(ms.ToArray());
int len = BitConverter.ToInt32(btmpMsg, 16);
len = IPAddress.NetworkToHostOrder(len);
byte[] bMsg = new byte[len];
byte[] bCorpid = new byte[btmpMsg.Length - 20 - len];
Array.Copy(btmpMsg, 20, bMsg, 0, len);
Array.Copy(btmpMsg, 20 + len, bCorpid, 0, btmpMsg.Length - 20 - len);
string oriMsg = Encoding.UTF8.GetString(bMsg);
corpId = Encoding.UTF8.GetString(bCorpid);
return oriMsg;
/// <summary>
/// 加密算法
/// </summary>
/// <param name="input">原文</param>
/// <param name="key">密钥</param>
/// <param name="iv">IV偏移量</param>
/// <returns></returns>
public static string AESEncrypt(byte[] input, byte[] key, byte[] iv)
using (var aesAlg = Aes.Create())
aesAlg.KeySize = 256;
aesAlg.BlockSize = 128;
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CBC;
byte[] msg = new byte[input.Length + 32 - input.Length % 32];
Array.Copy(input, msg, input.Length);
byte[] pad = KCS7Encoder(input.Length);
Array.Copy(pad, 0, msg, input.Length, pad.Length);
byte[] xBuff = null;
using (var encryptor = aesAlg.CreateEncryptor(key, iv))
using (var msEncrypt = new MemoryStream())
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
cs.Write(msg, 0, msg.Length);
xBuff = ms.ToArray();
return Convert.ToBase64String(xBuff);
private static byte[] decode2(byte[] decrypted)
int pad = (int)decrypted[decrypted.Length - 1];
if (pad < 1 || pad > 32)
pad = 0;
byte[] res = new byte[decrypted.Length - pad];
Array.Copy(decrypted, 0, res, 0, decrypted.Length - pad);
return res;
/// <summary>
///
/// </summary>
/// <param name="codeLen"></param>
/// <returns></returns>
public static string CreateRandCode(int codeLen)
string codeSerial = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z";
if (codeLen == 0)
codeLen = 16;
string[] arr = codeSerial.Split(',');
string code = "";
int randValue = -1;
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < codeLen; i++)
randValue = rand.Next(0, arr.Length - 1);
code += arr[randValue];
return code;
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static UInt32 HostToNetworkOrder(UInt32 inval)
UInt32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
/// <summary>
///
/// </summary>
/// <param name="inval"></param>
/// <returns></returns>
public static Int32 HostToNetworkOrder(Int32 inval)
Int32 outval = 0;
for (int i = 0; i < 4; i++)
outval = (outval << 8) + ((inval >> (i * 8)) & 255);
return outval;
private static byte[] KCS7Encoder(int text_length)
int block_size = 32;
// 计算需要填充的位数
int amount_to_pad = block_size - (text_length % block_size);
if (amount_to_pad == 0)
amount_to_pad = block_size;
// 获得补位所用的字符
char pad_chr = chr(amount_to_pad);
string tmp = "";
for (int index = 0; index < amount_to_pad; index++)
tmp += pad_chr;
return Encoding.UTF8.GetBytes(tm以上是关于使用cpolar实现内网穿透的主要内容,如果未能解决你的问题,请参考以下文章