<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nps &#8211; ChaBug安全</title>
	<atom:link href="/tags/nps/feed" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>一个分享知识、结识伙伴、资源共享的博客</description>
	<lastBuildDate>Fri, 23 Aug 2019 01:15:24 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.5.5</generator>
	<item>
		<title>nps：一款go写的轻量高效的内网穿透工具</title>
		<link>/tools/679.html</link>
		
		<dc:creator><![CDATA[Y4er]]></dc:creator>
		<pubDate>Sun, 28 Jul 2019 09:39:56 +0000</pubDate>
				<category><![CDATA[工具分享]]></category>
		<category><![CDATA[nps]]></category>
		<category><![CDATA[代理]]></category>
		<category><![CDATA[内网]]></category>
		<guid isPermaLink="false">/?p=679</guid>

					<description><![CDATA[nps nps是一款轻量级、高性能、功能强大的内网穿透代理服务器。目前支持tcp、udp流量转发，可支持任何tcp、udp上层协议（访问内网网站、本地支付接口调试、ssh访问、远程...]]></description>
										<content:encoded><![CDATA[<h1><span class="wpcom_tag_link"><a href="/tags/nps" title="nps" target="_blank">nps</a></span></h1>
<p>nps是一款轻量级、高性能、功能强大的<strong><span class="wpcom_tag_link"><a href="/tags/%e5%86%85%e7%bd%91" title="内网" target="_blank">内网</a></span>穿透</strong><span class="wpcom_tag_link"><a href="/tags/%e4%bb%a3%e7%90%86" title="代理" target="_blank">代理</a></span>服务器。目前支持<strong>tcp、udp流量转发</strong>，可支持任何<strong>tcp、udp</strong>上层协议（访问内网网站、本地支付接口调试、ssh访问、远程桌面，内网dns解析等等……），此外还<strong>支持内网http代理、内网socks5代理</strong>、<strong>p2p等</strong>，并带有功能强大的web管理端。</p>
<h2>背景</h2>
<p><img src="https://github.com/cnlh/nps/blob/master/image/web.png?raw=true" alt="image" /></p>
<ol>
<li>做微信公众号开发、小程序开发等&#8212;-> 域名代理模式</p>
</li>
<li>
<p>想在外网通过ssh连接内网的机器，做云服务器到内网服务器端口的映射，&#8212;-> tcp代理模式</p>
</li>
<li>
<p>在非内网环境下使用内网dns，或者需要通过udp访问内网机器等&#8212;-> udp代理模式</p>
</li>
<li>
<p>在外网使用HTTP代理访问内网站点&#8212;-> http代理模式</p>
</li>
<li>
<p>搭建一个内网穿透ss，在外网如同使用内网vpn一样访问内网资源或者设备&#8212;-> socks5代理模式</p>
</li>
</ol>
<h2>安装</h2>
<h3>releases安装</h3>
<blockquote>
<p><a href="https://github.com/cnlh/nps/releases">releases</a>
</p></blockquote>
<p>下载对应的系统版本即可，服务端和客户端是单独的</p>
<h3>源码安装</h3>
<ul>
<li>安装源码<br />
> go get -u github.com/cnlh/nps&#8230;</li>
<li>编译<br />
> go build cmd/nps/nps.go</li>
</ul>
<blockquote><p>
  go build cmd/npc/npc.go
</p></blockquote>
<h2>使用示例</h2>
<h3>统一准备工作（必做）</h3>
<ul>
<li>开启服务端，假设公网服务器ip为1.1.1.1，配置文件中<code>bridge_port</code>为8284，配置文件中<code>web_port</code>为8080</li>
<li>访问1.1.1.1:8080</li>
<li>在客户端管理中创建一个客户端，记录下验证密钥</li>
<li>内网客户端运行（windows使用cmd运行加.exe）</li>
</ul>
<pre><code class="language-shell ">./npc -server=1.1.1.1:8284 -vkey=客户端的密钥
</code></pre>
<h3>域名解析</h3>
<p><strong>适用范围：</strong> 小程序开发、微信公众号开发、产品演示</p>
<p><strong>假设场景：</strong><br />
&#8211; 有一个域名proxy.com，有一台公网机器ip为1.1.1.1<br />
&#8211; 两个内网开发站点127.0.0.1:81，127.0.0.1:82<br />
&#8211; 想通过（http|https://）a.proxy.com访问127.0.0.1:81，通过（http|https://）b.proxy.com访问127.0.0.1:82</p>
<p><strong>使用步骤</strong><br />
&#8211; 将*.proxy.com解析到公网服务器1.1.1.1<br />
&#8211; 点击刚才创建的客户端的域名管理，添加两条规则规则：1、域名：<code>a.proxy.com</code>，内网目标：<code>127.0.0.1:81</code>，2、域名：<code>b.proxy.com</code>，内网目标：<code>127.0.0.1:82</code></p>
<p>现在访问（http|https://）/<code>a.proxy.com</code>，<code>b.proxy.com</code>即可成功</p>
<p><strong>https:</strong> 如需使用https请进行相关配置，详见 <a href="#使用https">使用https</a></p>
<h3>tcp隧道</h3>
<p><strong>适用范围：</strong>  ssh、远程桌面等tcp连接场景</p>
<p><strong>假设场景：</strong><br />
 想通过访问公网服务器1.1.1.1的8001端口，连接内网机器10.1.50.101的22端口，实现ssh连接</p>
<p><strong>使用步骤</strong><br />
&#8211; 在刚才创建的客户端隧道管理中添加一条tcp隧道，填写监听的端口（8001）、内网目标ip和目标端口（10.1.50.101:22），保存。<br />
&#8211; 访问公网服务器ip（1.1.1.1）,填写的监听端口(8001)，相当于访问内网ip(10.1.50.101):目标端口(22)，例如：<code>ssh -p 8001 root@1.1.1.1</code></p>
<h3>udp隧道</h3>
<p><strong>适用范围：</strong>  内网dns解析等udp连接场景</p>
<p><strong>假设场景：</strong><br />
内网有一台dns（10.1.50.102:53），在非内网环境下想使用该dns，公网服务器为1.1.1.1</p>
<p><strong>使用步骤</strong><br />
&#8211; 在刚才创建的客户端的隧道管理中添加一条udp隧道，填写监听的端口（53）、内网目标ip和目标端口（10.1.50.102:53），保存。<br />
&#8211; 修改需要使用的内网dns为127.0.0.1，则相当于使用10.1.50.202作为dns服务器</p>
<h3>socks5代理</h3>
<p><strong>适用范围：</strong>  在外网环境下如同使用vpn一样访问内网设备或者资源</p>
<p><strong>假设场景：</strong><br />
想将公网服务器1.1.1.1的8003端口作为socks5代理，达到访问内网任意设备或者资源的效果</p>
<p><strong>使用步骤</strong><br />
&#8211; 在刚才创建的客户端隧道管理中添加一条socks5代理，填写监听的端口（8003），保存。<br />
&#8211; 在外网环境的本机配置socks5代理(例如使用proxifier进行全局代理)，ip为公网服务器ip（1.1.1.1），端口为填写的监听端口(8003)，即可畅享内网了</p>
<h3>http正向代理</h3>
<p><strong>适用范围：</strong>  在外网环境下使用http正向代理访问内网站点</p>
<p><strong>假设场景：</strong><br />
想将公网服务器1.1.1.1的8004端口作为http代理，访问内网网站</p>
<p><strong>使用步骤</strong></p>
<ul>
<li>在刚才创建的客户端隧道管理中添加一条http代理，填写监听的端口（8004），保存。</li>
<li>在外网环境的本机配置http代理，ip为公网服务器ip（1.1.1.1），端口为填写的监听端口(8004)，即可访问了</li>
</ul>
<h3>私密代理</h3>
<p><strong>适用范围：</strong>  无需占用多余的端口、安全性要求较高可以防止其他人连接的tcp服务，例如ssh。</p>
<p><strong>假设场景：</strong><br />
无需新增多的端口实现访问内网服务器10.1.50.2的22端口</p>
<p><strong>使用步骤</strong><br />
&#8211; 在刚才创建的客户端中添加一条私密代理，并设置唯一密钥secrettest和内网目标10.1.50.2:22<br />
&#8211; 在需要连接ssh的机器上以执行命令</p>
<pre><code class="">./npc -server=1.1.1.1:8284 -vkey=vkey -type=tcp -password=secrettest -local_type=secret
</code></pre>
<p>如需指定本地端口可加参数<code>-local_port=xx</code>，默认为2000</p>
<p><strong>注意：</strong> password为web管理上添加的唯一密钥，具体命令可查看web管理上的命令提示</p>
<p>假设10.1.50.2用户名为root，现在执行<code>ssh -p 2000 root@1.1.1.1</code>即可访问ssh</p>
<h3>p2p服务</h3>
<p><strong>适用范围：</strong>  大流量传输场景，流量不经过公网服务器，但是由于p2p穿透和nat类型关系较大，不保证100%成功，支持大部分nat类型。<a href="#nat类型检测">nat类型检测</a></p>
<p><strong>假设场景：</strong><br />
内网1机器ip为10.1.50.2    内网2机器2 ip为10.2.50.2</p>
<p>想通过访问内网1机器1的2000端口&#8212;->访问到内网2机器3 10.2.50.3的22端口</p>
<p><strong>使用步骤</strong><br />
&#8211; 在<code>nps.conf</code>中设置<code>p2p_ip</code>（nps服务器ip）和<code>p2p_port</code>（nps服务器udp端口）<br />
&#8211; 在刚才刚才创建的客户端中添加一条p2p代理，并设置唯一密钥p2pssh<br />
&#8211; 在机器1执行命令</p>
<pre><code class="">./npc -server=1.1.1.1:8284 -vkey=123 -password=p2pssh -target=10.2.50.3:22
</code></pre>
<p>如需指定本地端口可加参数<code>-local_port=xx</code>，默认为2000</p>
<p><strong>注意：</strong> password为web管理上添加的唯一密钥，具体命令可查看web管理上的命令提示</p>
<p>假设机器3用户名为root，现在在机器1上执行<code>ssh -p 2000 root@127.0.0.1</code>即可访问机器2的ssh</p>
<h2>web管理</h2>
<p><img src="https://github.com/cnlh/nps/blob/master/image/web2.png?raw=true" alt="image" /></p>
<h3>介绍</h3>
<p>可在网页上配置和管理各个tcp、udp隧道、内网站点代理，http、https解析等，功能强大，操作方便。</p>
<p><strong>提示：使用web模式时，服务端执行文件必须在项目根目录，否则无法正确加载配置文件</strong></p>
<h3>启动</h3>
<h4>服务端测试</h4>
<pre><code class="language-shell "> ./nps test
</code></pre>
<p>如有错误请及时修改配置文件，无错误可继续进行下去</p>
<h4>服务端启动</h4>
<pre><code class="language-shell "> ./nps start
</code></pre>
<p><strong>如果无需daemon运行或者打开后无法正常访问web管理，去掉start查看日志运行即可</strong></p>
<h4>web管理</h4>
<p>进入web界面，公网ip:web界面端口（默认8080），密码默认为123</p>
<p>进入web管理界面，有详细的说明</p>
<h4>服务端配置文件重载</h4>
<p>如果是daemon启动</p>
<pre><code class="language-shell "> ./nps reload
</code></pre>
<p><strong>说明：</strong> 仅支持部分配置重载，例如<code>allow_user_login</code> <code>auth_crypt_key</code> <code>auth_key</code> <code>web_username</code> <code>web_password</code> 等，未来将支持更多</p>
<h4>服务端停止或重启</h4>
<p>如果是daemon启动</p>
<pre><code class="language-shell "> ./nps stop|restart
</code></pre>
<h3>服务端配置文件</h3>
<ul>
<li>/conf/nps.conf</li>
</ul>
<table>
<thead>
<tr>
<th>名称</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>web_port</td>
<td>web管理端口</td>
</tr>
<tr>
<td>web_password</td>
<td>web界面管理密码</td>
</tr>
<tr>
<td>web_username</td>
<td>web界面管理账号</td>
</tr>
<tr>
<td>bridge_port</td>
<td>服务端客户端通信端口</td>
</tr>
<tr>
<td>https_proxy_port</td>
<td>域名代理https代理监听端口</td>
</tr>
<tr>
<td>http_proxy_port</td>
<td>域名代理http代理监听端口</td>
</tr>
<tr>
<td>auth_key</td>
<td>web api密钥</td>
</tr>
<tr>
<td>bridge_type</td>
<td>客户端与服务端连接方式kcp或tcp</td>
</tr>
<tr>
<td>public_vkey</td>
<td>客户端以配置文件模式启动时的密钥，设置为空表示关闭客户端配置文件连接模式</td>
</tr>
<tr>
<td>ip_limit</td>
<td>是否限制ip访问，true或false或忽略</td>
</tr>
<tr>
<td>flow_store_interval</td>
<td>服务端流量数据持久化间隔，单位分钟，忽略表示不持久化</td>
</tr>
<tr>
<td>log_level</td>
<td>日志输出级别</td>
</tr>
<tr>
<td>auth_crypt_key</td>
<td>获取服务端authKey时的aes加密密钥，16位</td>
</tr>
<tr>
<td>p2p_ip</td>
<td>服务端Ip，使用p2p模式必填</td>
</tr>
<tr>
<td>p2p_port</td>
<td>p2p模式开启的udp端口</td>
</tr>
</tbody>
</table>
<h3>使用https</h3>
<p><strong>方式一：</strong> 类似于nginx实现https的处理</p>
<p>在配置文件中将https_proxy_port设置为443或者其他你想配置的端口，和在web中对应域名编辑中设置对应的证书路径，将<code>https_just_proxy</code>设置为false，然后就和http代理一样了</p>
<p><strong>此外：</strong> 可以在<code>nps.conf</code>中设置一个默认的https配置，当遇到未在web中设置https证书的域名解析时，将自动使用默认证书，另还有一种情况就是对于某些请求的clienthello不携带sni扩展信息，nps也将自动使用默认证书</p>
<p><strong>方式二：</strong> 在内网对应服务器上设置https</p>
<p>在<code>nps.conf</code>中将<code>https_just_proxy</code>设置为true，并且打开<code>https_proxy_port</code>端口，然后nps将直接转发https请求到内网服务器上，由内网服务器进行https处理</p>
<h3>与nginx配合</h3>
<p>有时候我们还需要在云服务器上运行nginx来保证静态文件缓存等，本代理可和nginx配合使用，在配置文件中将httpProxyPort设置为非80端口，并在nginx中配置代理，例如httpProxyPort为8024时</p>
<pre><code class="">server {
    listen 80;
    server_name *.proxy.com;
    location / {
        proxy_set_header Host  $http_host;
        proxy_pass http://127.0.0.1:8024;/
    }
}
</code></pre>
<p>如需使用https也可在nginx监听443端口并配置ssl，并将本代理的httpsProxyPort设置为空关闭https即可，例如httpProxyPort为8024时</p>
<pre><code class="">server {
    listen 443;
    server_name *.proxy.com;
    ssl on;
    ssl_certificate  certificate.crt;
    ssl_certificate_key private.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_set_header Host  $http_host;
        proxy_pass http://127.0.0.1:8024;/
    }
}
</code></pre>
<h3>关闭代理</h3>
<p>如需关闭http代理可在配置文件中将http_proxy_port设置为空，如需关闭https代理可在配置文件中将https_proxy_port设置为空。</p>
<h3>将nps安装到系统</h3>
<p>如果需要长期并且方便的运行nps服务端，可将nps安装到操作系统中，可执行命令</p>
<pre><code class="">(./nps|nps.exe) install
</code></pre>
<p>安装成功后，对于linux，darwin，将会把配置文件和静态文件放置于/etc/nps/，并将可执行文件nps复制到/usr/bin/nps或者/usr/local/bin/nps，安装成功后可在任何位置执行</p>
<pre><code class="">nps test|start|stop|restart|status
</code></pre>
<p>对于windows系统，将会把配置文件和静态文件放置于C:\Program Files\nps，安装成功后可将可执行文件nps.exe复制到任何位置执行</p>
<pre><code class="">nps.exe test|start|stop|restart|status
</code></pre>
<h3>流量数据持久化</h3>
<p>服务端支持将流量数据持久化，默认情况下是关闭的，如果有需求可以设置<code>nps.conf</code>中的<code>flow_store_interval</code>参数，单位为分钟</p>
<p><strong>注意：</strong> nps不会持久化通过公钥连接的客户端</p>
<h3>系统信息显示</h3>
<p>nps服务端支持在web上显示和统计服务器的相关信息，但默认一些统计图表是关闭的，如需开启请在<code>nps.conf</code>中设置<code>system_info_display=true</code></p>
<h3>自定义客户端连接密钥</h3>
<p>web上可以自定义客户端连接的密钥，但是必须具有唯一性</p>
<h3>关闭公钥访问</h3>
<p>可以将<code>nps.conf</code>中的<code>public_vkey</code>设置为空或者删除</p>
<h3>关闭web管理</h3>
<p>可以将<code>nps.conf</code>中的<code>web_port</code>设置为空或者删除</p>
<h3>服务端多用户登陆</h3>
<p>如果将<code>nps.conf</code>中的<code>allow_user_login</code>设置为true,服务端web将支持多用户登陆，登陆用户名为user，默认密码为每个客户端的验证密钥，登陆后可以进入客户端编辑修改web登陆的用户名和密码，默认该功能是关闭的。</p>
<h3>用户注册功能</h3>
<p>nps服务端支持用户注册功能，可将<code>nps.conf</code>中的<code>allow_user_register</code>设置为true，开启后登陆页将会有有注册功能，</p>
<h3>监听指定ip</h3>
<p>nps支持每个隧道监听不同的服务端端口,在<code>nps.conf</code>中设置<code>allow_multi_ip=true</code>后，可在web中控制，或者npc配置文件中(可忽略，默认为0.0.0.0)</p>
<pre><code class="language-ini ">server_ip=xxx
</code></pre>
<h3>代理到服务端本地</h3>
<p>在使用nps监听80或者443端口时，默认是将所有的请求都会转发到内网上，但有时候我们的nps服务器的上一些服务也需要使用这两个端口，nps提供类似于<code>nginx</code> <code>proxy_pass</code> 的功能，支持将代理到服务器本地，该功能支持域名解析，tcp、udp隧道，默认关闭。</p>
<p><strong>即：</strong> 假设在nps的vps服务器上有一个服务使用5000端口，这时候nps占用了80端口和443，我们想能使用一个域名通过http(s)访问到5000的服务。</p>
<p><strong>使用方式：</strong> 在<code>nps.conf</code>中设置<code>allow_local_proxy=true</code>，然后在web上设置想转发的隧道或者域名然后选择转发到本地选项即可成功。</p>
<h2>客户端</h2>
<h3>客户端启动</h3>
<h4>无配置文件模式</h4>
<p>此模式的各种配置在服务端web管理中完成,客户端除运行一条命令外无需任何其他设置</p>
<pre><code class=""> ./npc -server=ip:port -vkey=web界面中显示的密钥
</code></pre>
<h4>配置文件模式</h4>
<p>此模式使用nps的公钥或者客户端私钥验证，各种配置在客户端完成，同时服务端web也可以进行管理</p>
<pre><code class=""> ./npc -config=npc配置文件路径
</code></pre>
<h4>配置文件说明</h4>
<p><a href="https://github.com/cnlh/nps/tree/master/conf/npc.conf">示例配置文件</a></p>
<h5>全局配置</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
conn_type=tcp
vkey=123
username=111
password=222
compress=true
crypt=true
rate_limit=10000
flow_limit=100
remark=test
max_conn=10
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>server_addr</td>
<td>服务端ip:port</td>
</tr>
<tr>
<td>conn_type</td>
<td>与服务端通信模式(tcp或kcp)</td>
</tr>
<tr>
<td>vkey</td>
<td>服务端配置文件中的密钥(非web)</td>
</tr>
<tr>
<td>username</td>
<td>socks5或http(s)密码保护用户名(可忽略)</td>
</tr>
<tr>
<td>password</td>
<td>socks5或http(s)密码保护密码(可忽略)</td>
</tr>
<tr>
<td>compress</td>
<td>是否压缩传输(true或false或忽略)</td>
</tr>
<tr>
<td>crypt</td>
<td>是否加密传输(true或false或忽略)</td>
</tr>
<tr>
<td>rate_limit</td>
<td>速度限制，可忽略</td>
</tr>
<tr>
<td>flow_limit</td>
<td>流量限制，可忽略</td>
</tr>
<tr>
<td>remark</td>
<td>客户端备注，可忽略</td>
</tr>
<tr>
<td>max_conn</td>
<td>最大连接数，可忽略</td>
</tr>
</tbody>
</table>
<h5>域名代理</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[web1]
host=a.proxy.com
target_addr=127.0.0.1:8080,127.0.0.1:8082
host_change=www.proxy.com
header_set_proxy=nps
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>web1</td>
<td>备注</td>
</tr>
<tr>
<td>host</td>
<td>域名(http|https都可解析)</td>
</tr>
<tr>
<td>target_addr</td>
<td>内网目标，负载均衡时多个目标，逗号隔开</td>
</tr>
<tr>
<td>host_change</td>
<td>请求host修改</td>
</tr>
<tr>
<td>header_xxx</td>
<td>请求header修改或添加，header_proxy表示添加header proxy:nps</td>
</tr>
</tbody>
</table>
<h5>tcp隧道模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[tcp]
mode=tcp
target_addr=127.0.0.1:8080
server_port=9001
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>tcp</td>
</tr>
<tr>
<td>server_port</td>
<td>在服务端的代理端口</td>
</tr>
<tr>
<td>tartget_addr</td>
<td>内网目标</td>
</tr>
</tbody>
</table>
<h5>udp隧道模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[udp]
mode=udp
target_addr=127.0.0.1:8080
server_port=9002
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>udp</td>
</tr>
<tr>
<td>server_port</td>
<td>在服务端的代理端口</td>
</tr>
<tr>
<td>target_addr</td>
<td>内网目标</td>
</tr>
</tbody>
</table>
<h5>http代理模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[http]
mode=httpProxy
server_port=9003
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>httpProxy</td>
</tr>
<tr>
<td>server_port</td>
<td>在服务端的代理端口</td>
</tr>
</tbody>
</table>
<h5>socks5代理模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[socks5]
mode=socks5
server_port=9004
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>socks5</td>
</tr>
<tr>
<td>server_port</td>
<td>在服务端的代理端口</td>
</tr>
</tbody>
</table>
<h5>私密代理模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[secret_ssh]
mode=secret
password=ssh2
target_addr=10.1.50.2:22
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>secret</td>
</tr>
<tr>
<td>password</td>
<td>唯一密钥</td>
</tr>
<tr>
<td>target_addr</td>
<td>内网目标</td>
</tr>
</tbody>
</table>
<h5>p2p代理模式</h5>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[p2p_ssh]
mode=p2p
password=ssh2
target_addr=10.1.50.2:22
</code></pre>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>mode</td>
<td>p2p</td>
</tr>
<tr>
<td>password</td>
<td>唯一密钥</td>
</tr>
<tr>
<td>target_addr</td>
<td>内网目标</td>
</tr>
</tbody>
</table>
<h5>文件访问模式</h5>
<p>利用nps提供一个公网可访问的本地文件服务，此模式仅客户端使用配置文件模式方可启动</p>
<pre><code class="language-ini ">[common]
server_addr=1.1.1.1:8284
vkey=123
[file]
mode=file
server_port=9100
local_path=/tmp/
strip_pre=/web/
````

项 | 含义
---|---
mode | file
server_port | 服务端开启的端口
local_path|本地文件目录
strip_pre|前缀

对于`strip_pre`，访问公网`ip:9100/web/`相当于访问`/tmp/`目录

#### 断线重连
```ini
[common]
auto_reconnection=true
</code></pre>
<h4>nat类型检测</h4>
<pre><code class=""> ./npc nat
</code></pre>
<p>如果p2p双方都是Symmetic Nat，肯定不能成功，其他组合都有较大成功率。</p>
<h4>状态检查</h4>
<pre><code class=""> ./npc status -config=npc配置文件路径
</code></pre>
<h4>重载配置文件</h4>
<pre><code class=""> ./npc restart -config=npc配置文件路径
</code></pre>
<h4>通过代理连接nps</h4>
<p>有时候运行npc的内网机器无法直接访问外网，此时可以可以通过socks5代理连接nps</p>
<p>对于配置文件方式启动,设置</p>
<pre><code class="language-ini ">[common]
proxy_url=socks5://111:222@127.0.0.1:8024
</code></pre>
<p>对于无配置文件模式,加上参数</p>
<pre><code class="">-proxy=socks5://111:222@127.0.0.1:8024
</code></pre>
<p>支持socks5和http两种模式</p>
<p>即socks5://username:password@ip:port</p>
<p>或</p>
<h4>群晖支持</h4>
<p>可在releases中下载spk群晖套件，例如<code>npc_x64-6.1_0.19.0-1.spk</code></p>
<h2>相关功能</h2>
<h3>缓存支持</h3>
<p>对于web站点来说，一些静态文件往往消耗更大的流量，且在内网穿透中，静态文件还需到客户端获取一次，这将导致更大的流量消耗。nps在域名解析代理中支持对静态文件进行缓存。</p>
<p>即假设一个站点有a.css，nps将只需从npc客户端读取一次该文件，然后把该文件的内容放在内存中，下一次将不再对npc客户端进行请求而直接返回内存中的对应内容。该功能默认是关闭的，如需开启请在<code>nps.conf</code>中设置<code>http_cache=true</code>，并设置<code>http_cache_length</code>（缓存文件的个数，消耗内存，不宜过大，0表示不限制个数）</p>
<h3>数据压缩支持</h3>
<p>由于是内网穿透，内网客户端与服务端之间的隧道存在大量的数据交换，为节省流量，加快传输速度，由此本程序支持SNNAPY形式的压缩。</p>
<ul>
<li>所有模式均支持数据压缩</li>
<li>在web管理或客户端配置文件中设置</li>
</ul>
<h3>加密传输</h3>
<p>如果公司内网防火墙对外网访问进行了流量识别与屏蔽，例如禁止了ssh协议等，通过设置 配置文件，将服务端与客户端之间的通信内容加密传输，将会有效防止流量被拦截。<br />
&#8211; nps使用tls加密，所以一定要保留conf目录下的密钥文件，同时也可以自行生成<br />
&#8211; 在web管理或客户端配置文件中设置</p>
<h3>站点保护</h3>
<p>域名代理模式所有客户端共用一个http服务端口，在知道域名后任何人都可访问，一些开发或者测试环境需要保密，所以可以设置用户名和密码，nps将通过 Http Basic Auth 来保护，访问时需要输入正确的用户名和密码。</p>
<ul>
<li>在web管理或客户端配置文件中设置</li>
</ul>
<h3>host修改</h3>
<p>由于内网站点需要的host可能与公网域名不一致，域名代理支持host修改功能，即修改request的header中的host字段。</p>
<p><strong>使用方法：在web管理中设置</strong></p>
<h3>自定义header</h3>
<p>支持对header进行新增或者修改，以配合服务的需要</p>
<h3>404页面配置</h3>
<p>支持域名解析模式的自定义404页面，修改/web/static/page/error.html中内容即可，暂不支持静态文件等内容</p>
<h3>流量限制</h3>
<p>支持客户端级流量限制，当该客户端入口流量与出口流量达到设定的总量后会拒绝服务<br />
，域名代理会返回404页面，其他代理会拒绝连接,使用该功能需要在<code>nps.conf</code>中设置<code>allow_flow_limit</code>，默认是关闭的。</p>
<h3>带宽限制</h3>
<p>支持客户端级带宽限制，带宽计算方式为入口和出口总和，权重均衡,使用该功能需要在<code>nps.conf</code>中设置<code>allow_rate_limit</code>，默认是关闭的。</p>
<h3>负载均衡</h3>
<p>本代理支持域名解析模式和tcp代理的负载均衡，在web域名添加或者编辑中内网目标分行填写多个目标即可实现轮训级别的负载均衡</p>
<h3>端口白名单</h3>
<p>为了防止服务端上的端口被滥用，可在nps.conf中配置allow_ports限制可开启的端口，忽略或者不填表示端口不受限制，格式：</p>
<pre><code class="language-ini ">allow_ports=9001-9009,10001,11000-12000
</code></pre>
<h3>端口范围映射</h3>
<p>当客户端以配置文件的方式启动时，可以将本地的端口进行范围映射，仅支持tcp和udp模式，例如：</p>
<pre><code class="language-ini ">[tcp]
mode=tcp
server_port=9001-9009,10001,11000-12000
target_port=8001-8009,10002,13000-14000
</code></pre>
<p>逗号分隔，可单个或者范围，注意上下端口的对应关系，无法一一对应将不能成功</p>
<h3>端口范围映射到其他机器</h3>
<pre><code class="language-ini ">[tcp]
mode=tcp
server_port=9001-9009,10001,11000-12000
target_port=8001-8009,10002,13000-14000
target_ip=10.1.50.2
</code></pre>
<p>填写target_ip后则表示映射的该地址机器的端口，忽略则便是映射本地127.0.0.1,仅范围映射时有效</p>
<h3>守护进程</h3>
<p>本代理支持守护进程，使用示例如下，服务端客户端所有模式通用,支持linux，darwin，windows。</p>
<pre><code class="">./(nps|npc) start|stop|restart|status 若有其他参数可加其他参数
</code></pre>
<pre><code class="">(nps|npc).exe start|stop|restart|status 若有其他参数可加其他参数
</code></pre>
<h3>KCP协议支持</h3>
<p>KCP 是一个快速可靠协议，能以比 TCP浪费10%-20%的带宽的代价，换取平均延迟降低 30%-40%，在弱网环境下对性能能有一定的提升。可在nps.conf中修改<code>bridge_type</code>为kcp<br />
，设置后本代理将开启udp端口（<code>bridge_port</code>）</p>
<p>注意：当服务端为kcp时，客户端连接时也需要使用相同配置，无配置文件模式加上参数type=kcp,配置文件模式在配置文件中设置tp=kcp</p>
<h3>域名泛解析</h3>
<p>支持域名泛解析，例如将host设置为*.proxy.com，a.proxy.com、b.proxy.com等都将解析到同一目标，在web管理中或客户端配置文件中将host设置为此格式即可。</p>
<h3>URL路由</h3>
<p>本代理支持根据URL将同一域名转发到不同的内网服务器，可在web中或客户端配置文件中设置，此参数也可忽略，例如在客户端配置文件中</p>
<pre><code class="language-ini ">[web1]
host=a.proxy.com
target_addr=127.0.0.1:7001
location=/test
[web2]
host=a.proxy.com
target_addr=127.0.0.1:7002
location=/static
</code></pre>
<p>对于<code>a.proxy.com/test</code>将转发到<code>web1</code>，对于<code>a.proxy.com/static</code>将转发到<code>web2</code></p>
<h3>限制ip访问</h3>
<p>如果将一些危险性高的端口例如ssh端口暴露在公网上，可能会带来一些风险，本代理支持限制ip访问。</p>
<p><strong>使用方法:</strong> 在配置文件nps.conf中设置<code>ip_limit</code>=true，设置后仅通过注册的ip方可访问。</p>
<p><strong>ip注册</strong>：</p>
<p><strong>方式一：</strong><br />
在需要访问的机器上，运行客户端</p>
<pre><code class="">./npc register -server=ip:port -vkey=公钥或客户端密钥 time=2
</code></pre>
<p>time为有效小时数，例如time=2，在当前时间后的两小时内，本机公网ip都可以访问nps代理.</p>
<p><strong>方式二：</strong><br />
此外nps的web登陆也可提供验证的功能，成功登陆nps web admin后将自动为登陆的ip注册两小时的允许访问权限。</p>
<p><strong>注意：</strong> 本机公网ip并不是一成不变的，请自行注意有效期的设置，同时同一网络下，多人也可能是在公用同一个公网ip。</p>
<h3>客户端最大连接数</h3>
<p>为防止恶意大量长连接，影响服务端程序的稳定性，可以在web或客户端配置文件中为每个客户端设置最大连接数。该功能针对<code>socks5</code>、<code>http正向代理</code>、<code>域名代理</code>、<code>tcp代理</code>、<code>udp代理</code>、<code>私密代理</code>生效,使用该功能需要在<code>nps.conf</code>中设置<code>allow_connection_num_limit=true</code>，默认是关闭的。</p>
<h3>客户端最大隧道数限制</h3>
<p>nps支持对客户端的隧道数量进行限制，该功能默认是关闭的，如需开启，请在<code>nps.conf</code>中设置<code>allow_tunnel_num_limit=true</code>。</p>
<h3>端口复用</h3>
<p>在一些严格的网络环境中，对端口的个数等限制较大，nps支持强大端口复用功能。将<code>bridge_port</code>、 <code>http_proxy_port</code>、 <code>https_proxy_port</code> 、<code>web_port</code>都设置为同一端口，也能正常使用。</p>
<ul>
<li>使用时将需要复用的端口设置为与<code>bridge_port</code>一致即可，将自动识别。</li>
<li>如需将web管理的端口也复用，需要配置<code>web_host</code>也就是一个二级域名以便区分</li>
</ul>
<h3>多路复用</h3>
<p>nps主要通信默认基于多路复用，无需开启。</p>
<h3>环境变量渲染</h3>
<p>npc支持环境变量渲染以适应在某些特殊场景下的要求。</p>
<p><strong>在无配置文件启动模式下：</strong><br />
设置环境变量</p>
<pre><code class="">export NPC_SERVER_ADDR=1.1.1.1:8284
export NPC_SERVER_VKEY=xxxxx
</code></pre>
<p>直接执行./npc即可运行</p>
<p><strong>在配置文件启动模式下：</strong></p>
<pre><code class="language-ini ">[common]
server_addr={{.NPC_SERVER_ADDR}}
conn_type=tcp
vkey={{.NPC_SERVER_VKEY}}
auto_reconnection=true
[web]
host={{.NPC_WEB_HOST}}
target_addr={{.NPC_WEB_TARGET}}
</code></pre>
<p>在配置文件中填入相应的环境变量名称，npc将自动进行渲染配置文件替换环境变量</p>
<h3>健康检查</h3>
<p>当客户端以配置文件模式启动时，支持多节点的健康检查。配置示例如下</p>
<pre><code class="language-ini ">[health_check_test1]
health_check_timeout=1
health_check_max_failed=3
health_check_interval=1
health_http_url=/
health_check_type=http
health_check_target=127.0.0.1:8083,127.0.0.1:8082

[health_check_test2]
health_check_timeout=1
health_check_max_failed=3
health_check_interval=1
health_check_type=tcp
health_check_target=127.0.0.1:8083,127.0.0.1:8082
</code></pre>
<p><strong>health关键词必须在开头存在</strong></p>
<p>第一种是http模式，也就是以get的方式请求目标+url，返回状态码为200表示成功</p>
<p>第一种是tcp模式，也就是以tcp的方式与目标建立连接，能成功建立连接表示成功</p>
<p>如果失败次数超过<code>health_check_max_failed</code>，nps则会移除该npc下的所有该目标，如果失败后目标重新上线，nps将自动将目标重新加入。</p>
<table>
<thead>
<tr>
<th>项</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>health_check_timeout</td>
<td>健康检查超时时间</td>
</tr>
<tr>
<td>health_check_max_failed</td>
<td>健康检查允许失败次数</td>
</tr>
<tr>
<td>health_check_interval</td>
<td>健康检查间隔</td>
</tr>
<tr>
<td>health_check_type</td>
<td>健康检查类型</td>
</tr>
<tr>
<td>health_check_target</td>
<td>健康检查目标，多个以逗号（,）分隔</td>
</tr>
<tr>
<td>health_check_type</td>
<td>健康检查类型</td>
</tr>
<tr>
<td>health_http_url</td>
<td>健康检查url，仅http模式适用</td>
</tr>
</tbody>
</table>
<h3>日志输出</h3>
<h4>日志输出级别</h4>
<p><strong>对于npc：</strong></p>
<pre><code class="">-log_level=0~7 -log_path=npc.log
</code></pre>
<pre><code class="">LevelEmergency-&gt;0  LevelAlert-&gt;1

LevelCritical-&gt;2 LevelError-&gt;3

LevelWarning-&gt;4 LevelNotice-&gt;5

LevelInformational-&gt;6 LevelDebug-&gt;7
</code></pre>
<p>默认为全输出,级别为0到7</p>
<p><strong>对于nps：</strong></p>
<p>在<code>nps.conf</code>中设置相关配置即可</p>
<h2>相关说明</h2>
<h3>获取用户真实ip</h3>
<p>在域名代理模式中，可以通过request请求 header 中的 X-Forwarded-For 和 X-Real-IP 来获取用户真实 IP。</p>
<p><strong>本代理前会在每一个http(s)请求中添加了这两个 header。</strong></p>
<h3>热更新支持</h3>
<p>对于绝大多数配置，在web管理中的修改将实时使用，无需重启客户端或者服务端</p>
<h3>客户端地址显示</h3>
<p>在web管理中将显示客户端的连接地址</p>
<h3>流量统计</h3>
<p>可统计显示每个代理使用的流量，由于压缩和加密等原因，会和实际环境中的略有差异</p>
<h3>当前客户端带宽</h3>
<p>可统计每个客户端当前的带宽，可能和实际有一定差异，仅供参考。</p>
<h3>客户端与服务端版本对比</h3>
<p>为了程序正常运行，客户端与服务端的核心版本必须一致，否则将导致客户端无法成功连接致服务端。</p>
<h2>webAPI</h2>
<h3>webAPI验证说明</h3>
<ul>
<li>采用auth_key的验证方式</li>
<li>在提交的每个请求后面附带两个参数，<code>auth_key</code> 和<code>timestamp</code></li>
</ul>
<pre><code class="">auth_key的生成方式为：md5(配置文件中的auth_key+当前时间戳)
</code></pre>
<pre><code class="">timestamp为当前时间戳
</code></pre>
<pre><code class="">curl --request POST \
  --url http://127.0.0.1:8080/client/list \
  --data 'auth_key=2a0000d9229e7dbcf79dd0f5e04bb084&amp;timestamp=1553045344&amp;start=0&amp;limit=10'
</code></pre>
<p><strong>注意：</strong> 为保证安全，时间戳的有效范围为20秒内，所以每次提交请求必须重新生成。</p>
<h3>获取服务端时间</h3>
<p>由于服务端与api请求的客户端时间差异不能太大，所以提供了一个可以获取服务端时间的接口</p>
<pre><code class="">POST /auth/gettime
</code></pre>
<h3>获取服务端authKey</h3>
<p>如果想获取authKey，服务端提供获取authKey的接口</p>
<pre><code class="">POST /auth/getauthkey
</code></pre>
<p>将返回加密后的authKey，采用aes cbc加密，请使用与服务端配置文件中cryptKey相同的密钥进行解密</p>
<p><strong>注意：</strong> nps配置文件中<code>auth_crypt_key</code>需为16位<br />
&#8211; 解密密钥长度128<br />
&#8211; 偏移量与密钥相同<br />
&#8211; 补码方式pkcs5padding<br />
&#8211; 解密串编码方式 十六进制</p>
<h3>详细文档</h3>
<ul>
<li><strong>此文档近期可能更新较慢，建议自行抓包</strong></li>
</ul>
<p>为方便第三方扩展，在web模式下可利用webAPI进行相关操作，详情见<br />
<a href="https://github.com/cnlh/nps/wiki/webAPI%E6%96%87%E6%A1%A3">webAPI文档</a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
