Giter Site home page Giter Site logo

xmcat2002 / ddns_port_forwarding Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anjianshi/ddns_port_forwarding

0.0 2.0 0.0 14 KB

让内网机器与通过 DDNS 动态指定的远程主机进行 SSH 远程端口转发,以此实现内网穿透

License: MIT License

Python 100.00%

ddns_port_forwarding's Introduction

ssh 远程端口转发是一种比较简单的实现内网穿透的方式,但它有一个缺点:必须把要连接的 IP 提前设置好。
例如要在家里访问公司的电脑,就必须预先知道家里的 IP,每当家里的 IP 发生变化,公司电脑就要重新设置。
虽然可以让公司电脑连到某个固定的服务器,然后在家里也连接那个服务器,但这样速度会比较慢,且服务器也需要成本。

此脚本就是用来解决这一问题。
它会定时检查指定域名的解析结果,并连接到域名指向的那个 IP 上。我们可以在家里配置上 DDNS,这样任何时候 IP 发生变化,公司的电脑都会重新连接到最新的 IP 上。

环境要求

  1. 内网机器能够访问外网,能够运行 Python 2.7(OpenWRT 下要求安装 python-light 包)和 SSH(支持 Droptear),能够运行 dig 命令
  2. 外网机器上要运行 ssh server 以实现端口转发
  3. 一个公网域名(可以使用花生壳等 DDNS 服务商提供的域名)

使用方法

把 config.example.py 复制为 config.py,修改里面的配置
通过 ssh-agent 以及 ssh-add 预先配置好连接外网机器要用到的 ssh private key
执行 python main.py start

main.py 支持 start、stop、restart、run 命令,其中 run 是前台运行模式,便于进行调试。
不指定命令时默认为 start

注意事项

  1. 此脚本只支持通过 ssh key 登录,不支持密码登陆

  2. 如果外网机器是一个 OpenWRT 路由器,要让它允许远程 ssh 登录。
    详见: http://blog.differentpla.net/blog/2015/05/27/openwrt-ssh-wan
    (安全起见,最好不要用 22 端口,另外禁止密码登录,只用 ssh key 登录)

  3. 如果内网机器的 ssh 客户端是 dropbear(例如这是一个 OpenWRT 路由器),ssh key 需为 dropbear 格式(可通过 dropbearkey 命令创建这种格式的 key)

  4. 建议在外网机器的 /etc/ssh/sshd_config 文件中加上这两行:

ClientAliveInterval = 30
ClientAliveCountMax = 2

代表每 30 秒检查一下客户端是否还正常连接着,如果两次检查都失败,则断开连接。
具体数字可以自己设置。这样客户端意外断线后,远端就不会一直保持着连接了。

如果外网机器是 OpenWRT 路由器,则应在 /etc/config/dropbear 中添加这样的两行:

    option ClientAliveInterval '30'
    option ClientAliveCountMax '2'

参考资料与鸣谢

ddns_port_forwarding's People

Contributors

anjianshi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.