网络编程_1

1.前言
公司要为别的公司提供服务器防护,采用本地环回,节点转发的方法实现对服务器真实ip进行隐藏。但是有一个问题:我节点端要创建一个端口接收映射数据和保活数据,采用固定端口的话,攻击者很容易通过扫描端口来获取我较多的节点,进行攻击
2.解决
这里采用的是通过节点ip来生成一个随机端口,客户端和节点通过这个随机端口来进行数据通信
3.代码片段
由ip生成随机端口的一个算法
std::string generateport(const std::string &strvalue)
{
    std::string strport("");
    if (!strvalue.empty())
    {
        std::string strip = strvalue;
        deletechar(strip, '.');
        int nfirst = (strip.at(strip.size() - 1) - '0') % 5 + 5;
        int nsecond = 0, nthird = 1, nfourth = 0;
        for (std::string::iterator it = strip.begin(); it != strip.end(); ++it)
        {
            nsecond += (*it - '0');
            nthird *= (*it - '0');
            if (*it >= '3' && *it <= '9')
                nfourth += (*it - '0');
        }
        nsecond %= 10;
        nthird %= 10;
        nfourth %= 10;

        char szport[64] = {0};
        sprintf(szport, "%d%d%d%d", nfirst, nsecond, nthird, nfourth);
        strport = szport;
    }
    return strport;
}
4.备注
1.但是在实际中发现生成的随机端口可能是木马端口,被360直接误报,这是很纠结的,后续再进行优化
2.由ip生成端口部分算法不能保证所有ip产生的端口都是唯一的,但是可以保证大部分ip产生的端口都不一样,后续还要继续进行优化处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值