> ## Content Index
> Fetch the complete content index at: https://blog.hacking.pub/llms.txt
> Use this file to discover other available public pages before exploring further.

# 记一次端口转发
- URL: https://blog.hacking.pub/2017/12/29/ji-yi-ci-duan-kou-zhuan-fa/
- Published: 2017-12-29T02:41:01.000Z
- Updated: 2017-12-29T02:41:26.000Z
- Author: chainhelen
- Tags: 端口转发, 端口, lcx, portmap

#### 问题

有个管理后台的php项目，我搭建了一个docker 容器，放在了公网ip 47 阿里云服务器上，作为调试环境比较麻烦

##### 本地xdebug

通常的调试手段

![dbgp-setup](https://blog.hacking.pub/qiniu/%E8%AE%B0%E4%B8%80%E6%AC%A1%E7%AB%AF%E5%8F%A3%E8%BD%AC%E5%8F%91/dbgp-setup.gif)

本机IP 10.10.1.42 监听本地9000  
服务器IP 10.0.1.2 连接是80端口

```
xdebug.remote_host=10.0.1.42
xdebug.remote_port=9000

```

也就是说xdebug是反向代理过来的

#### 本机ip跳动

![dbgp-setup2](https://blog.hacking.pub/qiniu/%E8%AE%B0%E4%B8%80%E6%AC%A1%E7%AB%AF%E5%8F%A3%E8%BD%AC%E5%8F%91/dbgp-setup2.gif)

第二种其实是本机ip会乱动，远程不会乱动

在xdebug服务端有一个配置`xdebug.remote_connect_back`，设定为1则`remote_host`中值就会无效，xdebug服务端可以从http请求的头部`$_SERVER['REMOTE_ADDR']`获取远程客户端的IP地址

#### 本机ip内网

我的场景中远程是有外网的ip地址，而我本机是出于公司、个人网络的内网之中的，ip地址是一个内网地址  
就算我反向告诉远程xdebug我内网ip，它也连接不上更传输不了debug信息

所以接下来，我做了端口转发

```
1.aliyun线上服务器外网ip    47ip  
2.aliyun线上服务器内网ip    172ip  
3.本机ip未知  

47服务器    ./portmap -m 3 -h1 172.xx.xx.xx -p1 9001 -h2 172.xx.xx.xx -p2 9003  
本机服务器   lcx.exe -slave 47.xx.xx.xx 9002 127.0.0.1 9002  

```

对47服务器上的9001端口 与 端口9003 互转（有序，必须是9001先发起）  
对本机服务器上的9002端口 与 端口 9002 互转（有序，必须是9002先发起）

#### 参考

> [PHP调试工具-Xdebug](http://guojianxiang.com/posts/2015-09-06-PHP%5FDebug%5FTool-Xdebug.html?ref=blog.hacking.pub)