对高德地图的请求的转发代理

847 阅读2分钟

近期有个需求,项目不知道内网环境,需要对网关拦截处理。前端请求发送到内网服务器后,通过nginx反向代理,监听请求端口,将请求转发至前置机(需要打通内网服务器与外网服务器(前置机)之间的通信)并且对搞得地图实现请求拦截ip转发

先看原始的接口请求,可以看到亲环球的域名分别为 vdata.amap.com,还有其他分别为http://vdata02.am…

222.png 333.png

在启用高德地图服务的过程中,高德会自调用一些高德服务,非前端主动请求。此时,我们需要把这些自调用的请求拦截下来,转发到我们的内网服务器上。然后按照前面的流程再走一遍。
1.这里我们会用到拦截器:ajaxhook.js 需要在public下的index.html中配置

<script type="text/javascript" src="/static/ajaxhook.min.js"></script>
<script>
    ah.proxy({
        onRequest: (config, handler) => {
            // console.log(config.url)
            if (config.url.toString().search('https://vdata.amap.com') != -1) {
                config.url = 'http://192..168.x.x:200/web2' + config.url.split('vdata.amap.com/')[1];
                console.log(config.url)
            } else if (config.url.toString().search('http://restapi.amap.com') != -1) {
                config.url = 'http://192..168.x.x:200/web3' + config.url.split('restapi.amap.com/')[1];
                console.log(config.url)
            } else if (config.url.toString().search('http://vector.amap.com') != -1) {
                config.url = 'http://192..168.x.x:200/web4' + config.url.split('vector.amap.com/')[1];
                console.log(config.url)
            } else if (config.url.toString().search('http://lbs.amap.com') != -1) {
                config.url = 'http://192..168.x.x:200/web5' + config.url.split('lbs.amap.com/')[1];
                console.log(config.url)
            }
            handler.next(config);
        },
        onError: (err, handler) => {
            console.log(err.type)
            handler.next(err)
        },
        onResponse: (response, handler) => {
            // console.log(response.response)
            handler.next(response)
        }
    })
</script>

2.以上的方法仅仅针对ajax的请求。于是发现,高德地图的请求,不仅基于ajax请求,还有fetch请求,所以还要加一个fatch请求拦截过滤。于是乎我找了一篇关于拦截fatch请求文章 (www.cnblogs.com/wenruo/p/17…) 经过一番测试,根据文章的内容可以拦截到fatch请求,但是不能拦截高德地图的fatch请求,怀疑是高德对fatch请求实现了重写,导致拦击无效。于是我根据本地的amap.js的源码上去修改,在最终实现了! 在里面找到makeFetchRequest方法function makeFetchRequest(i, t, e) {} t就是请求前的url地址,根据这个,可以进行请求前的转换

function makeFetchRequest(i, t, e) {
          const newFetchUrl = [
            "http://192..168.x.x:200/",
            "http://192..168.x.x:201/",
            "http://192..168.x.x:202/",
            "http://192..168.x.x:203/",
            "http://192..168.x.x:204/",
            "http://192..168.x.x:205/",
            "http://192..168.x.x:206/",
            "http://192..168.x.x:207/",
            "http://192..168.x.x:208/",
          ];
          if (t.toString().search("http://vdata.amap.com") != -1) {
            t = newFetchUrl[0] + t.split("vdata.amap.com/")[1];
          } else if (t.toString().search("http://vdata01.amap.com") != -1) {
            t = newFetchUrl[1] + t.split("vdata01.amap.com/")[1];
          } else if (t.toString().search("http://vdata02.amap.com") != -1) {
            t = newFetchUrl[2] + t.split("vdata02.amap.com/")[1];
          } else if (t.toString().search("http://vdata03.amap.com") != -1) {
            t = newFetchUrl[3] + t.split("vdata03.amap.com/")[1];
          } else if (t.toString().search("http://vdata04.amap.com") != -1) {
            t = newFetchUrl[4] + t.split("vdata04.amap.com/")[1];
          } else if (t.toString().search("http://192.168.1.183:7776") != -1) {
            t = newFetchUrl[5] + t.split("192.168.1.183:7776/")[1];
          } else if (t.toString().search("http://vector.amap.com") != -1) {
            t = newFetchUrl[6] + t.split("vector.amap.com/")[1];
          } else if (t.toString().search("http://lbs.amap.com") != -1) {
            t = newFetchUrl[7] + t.split("lbs.amap.com/")[1];
          }
   }

最终实现了效果

444.png

555.png