找回密码
 立即注册

QQ登录

只需一步,快速开始

qq8009 活字格认证

银牌会员

162

主题

694

帖子

2202

积分

银牌会员

积分
2202

活字格高级认证微信认证勋章活字格认证

qq8009 活字格认证
银牌会员   /  发表于:2019-3-21 20:03  /   查看:4079  /  回复:1
本帖最后由 qq8009 于 2019-3-21 20:11 编辑

前言      由于本人用内网电脑作为服务器,所以使用了向日葵,不过就在昨天向日葵挂了,如下图所示
blob338650512.png
所以就考虑用ngrok
ps:   ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道。
但是ngrok 是经过第三方服务器代理的,也就说当前的数据有经过第三方服务器,安全性未知!而且网页开启速度又很慢!

这个时候又要想用域名访问,又想提升安全性,又想快速打开网页,我就想到直接用ip访问!
还有一点,由于网络是拨号上网,所以公网IP不是固定的!

这个时候难点就是如何获取服务器的IP

接下来进入正题

                      1.首先要有一定的编程能力
                      2.Visual Studio 2015
                      3.IIS


在VS中创建一个项目,名称随意,我这边取名为跳转,接着新增一个index.aspx页面
blob185020445.png

在cs源码文件里面 blob566361388.png 新增源码


===============
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;


namespace 跳转
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string url = getReaderIpds();
            //System.Windows.Forms.MessageBox.Show(getReaderIpds());
            Label1.Text = url;

            Response.Redirect("http://"+ url+ ":81/erp", false);
        }


        #region 获取外网IP
        //获取外网IP
        public static string getReaderIpds()
        {
            string pubIP = "";
            string serviceUrl = "";
            Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sk.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
            string req = "M-SEARCH * HTTP/1.1\r\n" +
            "HOST: 239.255.255.250:1900\r\n" +
            "ST:upnp:rootdevice\r\n" +
            "MAN:\"ssdp:discover\"\r\n" +
            "MX:3\r\n\r\n";
            byte[] data = System.Text.Encoding.ASCII.GetBytes(req);
            byte[] receive = new byte[0x1000];
            IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, 1900);
            //发送数据,获取服务地址
            sk.SendTo(data, ipe);
            int length = 0;
            length = sk.Receive(receive);
            string resp = Encoding.ASCII.GetString(receive, 0, receive.Length).ToLower();
            if (resp.Contains("upnp:rootdevice"))
            {
                resp = resp.Substring(resp.ToLower().IndexOf("location:") + 9);
                resp = resp.Substring(0, resp.IndexOf("\r")).Trim();
                serviceUrl = GetServiceUrl(resp);//解析获取地址
            }
            //利用ssdp discover服务向serviceIP发送请求,存储返回结果,并在xml中解析出外网ip
            string reqStr = "" +
          "" +
          "" +
          "" + "" +
          "" +
          "";
            byte[] b = Encoding.UTF8.GetBytes(reqStr);
            WebRequest wr = HttpWebRequest.Create(serviceUrl);
            wr.Method = "OST";
            wr.Headers.Add("SOAPACTION", "\"urn:schemas-upnp-org:service:WANIPConnection:1#" + "GetExternalIPAddress" + "\"");
            wr.ContentType = "text/xml; charset=\"utf-8\"";
            wr.ContentLength = b.Length;
            wr.GetRequestStream().Write(b, 0, b.Length);
            //
            XmlDocument xml = new XmlDocument();
            WebResponse ws = wr.GetResponse();
            Stream ress = ws.GetResponseStream();
            xml.Load(ress);
            XmlNamespaceManager xmlMgr = new XmlNamespaceManager(xml.NameTable);
            xmlMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
            pubIP = xml.SelectSingleNode("//NewExternalIPAddress/text()", xmlMgr).Value;
            return pubIP;
        }
        ///
          /// 剖析
          ///
          ///
          ///
        private static string GetServiceUrl(string resp)
        {
            try
            {
                XmlDocument desc = new XmlDocument();
                desc.Load(WebRequest.Create(resp).GetResponse().GetResponseStream());
                XmlNamespaceManager nsMgr = new XmlNamespaceManager(desc.NameTable);
                nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
                XmlNode typen = desc.SelectSingleNode("//tns:device/tns:deviceType/text()", nsMgr);
                if (!typen.Value.Contains("InternetGatewayDevice"))
                    return null;
                XmlNode node = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:controlURL/text()", nsMgr);
                if (node == null)
                    return null;
                XmlNode eventnode = desc.SelectSingleNode("//tns:service[tns:serviceType=\"urn:schemas-upnp-org:service:WANIPConnection:1\"]/tns:eventSubURL/text()", nsMgr);
                int n = resp.IndexOf("://");
                n = resp.IndexOf('/', n + 3);
                string surl = resp.Substring(0, n) + eventnode.Value;
                return surl;
            }
            catch
            {
                return null;
            }
        }
        //获取外网IP
        private static string GetExternalIP()
        {
            return "";
            //以下是通过外网页面读取的
            //try
            //{   
            //    System.Net.WebClient client = new System.Net.WebClient();
            //    client.Encoding = System.Text.Encoding.Default;
            //    string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");
            //    string[] ipStr = reply.Split(new char[] { '[', ']' });
            //    pubIP = ipStr[1];
            //}
            //catch (Exception ex)
            //{
            //    System.Windows.Forms.MessageBox.Show(ex.Message);
            //}
        }
        #endregion


    }



}


===============
忘记说了,
=========index.aspx 源码==============
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="跳转.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>welcome</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />

        ip地址:<aspabel ID="Label1" runat="server" Text="Label"></aspabel>
        <br />

    </div>
    </form>
</body>
</html>

=========index.aspx 源码==============

然后发布,并且在IIS上新增网页,这里就不一一赘述了

最后
     第一种 打开ngrok  下载地址http://ngrok.ciqiuwl.cn/ blob776609496.png
blob606936645.png

这时候就获得了aa.ngrok.xiaomiqiu.cn 类似这样的自定义域名,然后通过域名访问到刚刚跳转的那个网页这样就能实现了

第二种 用向日葵就很简单,这里就不放教程了
总结:其实不管用哪种方法都只是利用了域名转为IP ,类似于解析IP,毕竟内部网络使用,无所谓了
--------------



题外话也在这里感谢我们的梁瑞!!!给了我很大的启发!谢谢!
在昨天本来是想使用PHP,后面发现用ASP.NET也能实现,又不用重新部署PHP环境,就放弃了PHP
---------------
这里有个扩展,可以增加地址传值进行区分多个应用
比如:
aa.ngrok.xiaomiqiu.cn/index.aspx?app=erp1
aa.ngrok.xiaomiqiu.cn/index.aspx?app=erp2
...
可能有多个活字格应用
只需要对应活字格发布的地址就行
第一次发分享,可能有点乱,不清楚的小伙伴可以留言

如果动手能力弱的,可以直接用这个,挂上IIS就可以用了
tiaozhuan.zip (4.95 MB, 下载次数: 40)

评分

参与人数 1金币 +666 收起 理由
Simon.hu + 666 赞一个!

查看全部评分

1 个回复

倒序浏览
Simon.hu讲师达人认证 悬赏达人认证 活字格认证
超级版主   /  发表于:2019-3-22 08:43:16
沙发
感谢您的分享!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部