|  |      1Siril      2016-06-29 22:44:26 +08:00 感觉你描述不清。。。 是不是这种场景: 例如 vps 的 ip 1.2.3.4 , 公司 出口路由器 公网 ip 2.3.4.5 , 公司出口路由器上做好了映射。 然而出于跨 ISP 速度蛋疼之类的原因,由 VPS 转发: 例如 vps 的 2222 转发到公司的 22: iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 2222 -j DNAT --to-destination 2.3.4.5:22 iptables -t nat -A POSTROUTING -d 2.3.4.5 -p tcp --dport 22 -j SNAT --to-source 1.2.3.4 如此这般,就等价于微林了。 | 
|  |      2Siril      2016-06-29 22:47:22 +08:00 我弄错了,  貌似你是用的 ssh 反向连接。 先在 vps 上看下监听的地址。 | 
|  |      3lilifenghao44 OP @Siril vps 可以看到所有的 ssh 监听端口, curl 能拿到网页, ssh 能连。 就是无法在其它机器用 vps 的 IP 和端口 进行连接。用 Debian 时是可以的。 | 
|      4robin4700      2016-06-29 23:07:41 +08:00 /etc/ssh/sshd_config 里加一句 GatewayPorts yes | 
|  |      5fatedier      2016-06-29 23:20:32 +08:00  1 我之前用的 ssh tunnel ,不过用着不是很稳定的样子,可以尝试下我的一个用于内网穿透的开源项目 https://github.com/fatedier/frp 。 | 
|  |      6lilifenghao44 OP @robin4700 添加这句后,重启客户机的 autossh 重启 vps 的 ssh ,还是不行。 root@lovit:~# cat /etc/ssh/ssh ssh_config ssh_host_ecdsa_key ssh_host_rsa_key sshd_config ssh_host_ecdsa_key.pub ssh_host_rsa_key.pub ssh_host_dsa_key ssh_host_ed25519_key ssh_host_dsa_key.pub ssh_host_ed25519_key.pub root@lovit:~# cat /etc/ssh/sshd_config # Package generated configuration file # See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for Port 29688 # Use these options to restrict which interfaces/protocols sshd will bind to #ListenAddress :: #ListenAddress 0.0.0.0 Protocol 2 # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key #Privilege Separation is turned on for security UsePrivilegeSeparation yes # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 ServerKeyBits 1024 # Logging SyslogFacility AUTH LogLevel INFO # Authentication: LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile %h/.ssh/authorized_keys # Don't read the user's ~/.rhosts and ~/.shosts files IgnoreRhosts yes # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes X11Forwarding yes X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes #UseLogin no #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variables AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. GatewayPorts yes UsePAM yes root@lovit:~# | 
|  |      7lilifenghao44 OP 感谢,明天上班运行看看。 这个还是要用 vps 跳转所有流量啊。要是能用 Python 写个 socket ,借助 vps 通信,然后可以两个不同 nat 内的机子尝试穿透直接路由就好了。 | 
|  |      8lilifenghao44 OP @fatedier 回复忘记 @ 了 | 
|  |      9Siril      2016-06-30 08:25:48 +08:00 在内网机和 vps 上弄个 n2n / tinc / openvpn  呗。 | 
|  |      10lilifenghao44 OP @Siril 主要是之前可以用,现在不能用,想找到原因. 如果用 vpn 的话,其它设备要访问资源也要连进来. | 
|  |      11Siril      2016-06-30 09:15:20 +08:00 @lilifenghao44  vps 上一个 vpn ,内网 linux 上一个 vpn , 其他设备就不需要了。 分别作 DNAT + SNAT , 可以转发任意内网 linux 可访问的 tcp/udp 端口 从 vps 访问, 很灵活。 只是。。。 你明显没有公司出口路由器(或防火墙)的控制权限, 如此这般在防火墙上开洞, 也不太合适吧。 给例子: vps IP 1.2.3.4 , 隧道接口的 ip 192.168.1.1 内网 linux 的 隧道接口 192.168.1.2 , 内网接口 192.168.2.2. 目的为从外网 ssh 到 192.168.2.3:22 , 可以这样: vps 上打开 ip forwarding ,然后 iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 2222 -j DNAT --to-destination 192.168.1.2:1001 iptables -t nat -A POSTROUTING -d 192.168.1.2 -p tcp --dport 1001 -j SNAT --to-source 192.168.1.1 内网 linux 上也打开 ip forwarding , 然后 iptables -t nat -A PREROUTING -d 192.168.1.2 -p tcp --dport 1001 -j DNAT --to-destination 192.168.2.3:22 iptables -t nat -A POSTROUTING -d 192.168.2.3 -p tcp --dport 22 -j SNAT --to-source 192.168.2.2 然后 ssh 到 1.2.3.4:2222 等同于 192.168.2.3:22 个人认为比较简单易配置的有 openvpn 、 n2n 、 tinc , 当然 pptp 、 ipsec 之类也可。 | 
|  |      12Siril      2016-06-30 09:19:38 +08:00 | 
|  |      13Siril      2016-06-30 10:22:51 +08:00  1 @lilifenghao44  避免 vps 中转所有流量, 还要穿透 nat , 则 vpn 貌似只能用 udp , tcp 的不行; udp 在许多地方的 ISP 有明显的限速, 你试过就知道了; 可以试试 n2n 或 tinc 。 | 
|  |      14lilifenghao44 OP @Siril 上面用 shadowVPN 加 iptables 是可以的. 关于这个我有点疑惑需要请问,可以留个联系方式么. 现在边看看推荐的 n2n 用起来怎样. | 
|  |      15Siril      2016-06-30 11:30:17 +08:00 | 
|  |      16fatedier      2016-06-30 11:40:13 +08:00 @lilifenghao44 你说的那种打洞方式的话确实效率比较高,不过不一定能穿透所有的 NAT 设备吧, udp 感觉还好, tcp 的话感觉难度会比较大,而且用户也需要安装客户端支持。相对来说通过 VPS 中转流量的话更加稳定,对用户来说是透明的。 我对这方面也比较感兴趣,如果有什么比较好的方案的话希望能够学习借鉴。 | 
|      17gocoolshj4      2016-06-30 14:15:02 +08:00 via Android 自己编译个 ngrok,超简单 |