教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 [Go]网络代理实现

[Go]网络代理实现

发布时间:2022-01-09   编辑:jiaochengji.com
教程集为您提供[Go]网络代理实现等资源,欢迎您收藏本站,我们将为您提供最新的[Go]网络代理实现资源
<h1 class="postTitle" style="margin: 20px 0px 10px; padding: 0px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); font-size: 14px; font-family: Verdana, Arial, Helvetica, sans-serif;">实现一个代理服务</h1>

在天朝做程序员比较让人蛋疼,比如你想用GOOGLE,你就很蛋疼。原因大家都懂。

然后呢,一开始自己在用GOAGENT, VPN, SSH, ShadowSocks等程序,GOAGENT和SHADOWSOCKS都是非常优秀的。而自己在很早刚开始接触计算机的时候就有想法自己写一个代理程序,因为各种各样的原因总是没去做,或者说自己的需求总是能够被满足,所以没什么动力。但是自从学GO语言后,网络程序的开发变的没有之前做C/C 时那么蛋疼了,所以试着自己写一个代理程序,然后也贯彻Eating your own dog food的实践。这里把一些心得总结记录下。GITHUB地址:https://github.com/eahydra/socks

 

<h2 style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 21px;">SOCKS5协议</h2>

刚开始的时候,是想着做HTTP代理,但是HTTP协议比较复杂,牵扯到的概念比较多,自己又不熟悉,所以就没选中做HTTP通道方式的代理程序。而代理协议里用的最多的其实就是SOCKS协议了。SOCKS4,SOCKS5等。因为我一直在用CHROME, CHROME上的插件SwitchySharp也比较好用,也支持SOCKS5协议,更重要的是SOCKS5协议非常简单。

SOCKS5协议主要分三步,第一步就是握手协商,协商双方的验证方式。我的做法简单粗暴:因为SOCKS5规定的握手协议中,规定了最大的数据长度就是258个直接,所以我在实现的时候,直接申请258字节的BUFFER,然后读取。读取后也不对验证方式进行判断是否合法,支持之类的,直接返回0X05 0X00,也就是不需要验证。

第二步就是获取客户端发来的CMD协议。SOCKS5这里也规定了最长长度,263个字节。所以也一次申请出来以备后用。感谢GO语言封装的网络库,不管是啥域名,还是IPV4,IPV6之类的,调用net.Dial的时候传进去就好,net.Dial函数内部会自己搞定到IP的转换。然后就根据CMD里指定的目标地址链接,成功后返回对应的协议数据。目前我只支持了CONNECT,像UDP之类的就没做支持,遇到了UDP指令直接告诉客户端不支持,然后断掉链接。

第三步就是开始接受客户端发送来的数据,然后发送到远端服务器,然后从远端服务器读数据再转发到客户端。这个就简单了,直接两行代码搞定。

<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important; font-size: 12px !important;"><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">go io.Copy(dest, src) io.Copy(src, dest)</span></pre>

 

<h2 style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 21px;">部署</h2>

写好程序后,就得部署起来。感谢亚马逊提供了免费一年的AWS服务。怎么申请之类的自行搜索解决。GO的移植性这里就体现出来了,因为我开发是在WINDOWS下,然后部署到AWS上的UBUNTU系统上,一个go build搞定。然后程序跑起来后,直接设置SwitchySharp到AWS上,效果显著。不过中间测试的时候,发现链接到http://go-talks.appspot.com/github.com/extemporalgenome/gotalks/error-handling.slide#1 就不行了,抓包发现,从AWS上发来了很多RST,但是我的程序没有打印出对应的CLOSE信息。这个时候我就猜测应该是那个啥(你懂的)做了内容检测,解决的方法之一就是加密。所以我后面就做了加密的功能。

<h2 style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 21px;">加密</h2>

增加加密功能就蛋疼了,因为我可能不只是要一种加密算法,可能这会想用RC4,后面就想用AES或者DES之类的。还有就是怎么把加密算法封装起来,使其对现有的代码影响较小。本来我是想自己手工封装成io.Reader和io.Writer的接口,这样的话就可以组成一个调用链,很自然的把网络数据进行加解密。然后我就去GO的标准库里找,有加密算法,RC4,DES,AES都有提供,然后有个叫crypto/cipher的包,这个包提供了俩接口,一个叫StreamRead,另外一个叫StreamWrite,这俩接口正好可以满足io.Reader和io.Write。这下子就简单多了。具体的代码如下(代码里没有做什么工厂模式之类的,因为实在是没啥必要,写的简单粗暴点也不会影响其他代码):

<span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; font-size: 12px !important; line-height: 1.5 !important;"></span>
<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important; font-size: 12px !important;">func NewCipherStream(rwc io.ReadWriteCloser, cryptMethod <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">string</span>, password []<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">byte</span>) (*<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream, error) { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">var</span> stream *<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">switch</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> cryptMethod { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">default</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">: stream </span>= &<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream{ reader: rwc, writeCloser: rwc, } </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">case</span> <span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">rc4</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">: { rc4CipherRead, err :</span>=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> rc4.NewCipher(password) </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">if</span> err !=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil, err } rc4CipherWrite, err :</span>=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> rc4.NewCipher(password) </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">if</span> err !=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil, err } stream </span>= &<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream{ reader: </span>&<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">cipher.StreamReader{ S: rc4CipherRead, R: rwc, }, writeCloser: </span>&<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">cipher.StreamWriter{ S: rc4CipherWrite, W: rwc, }, } } </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">case</span> <span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">des</span><span style="margin: 0px; padding: 0px; color: rgb(128, 0, 0); font-size: 12px !important; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">: { block, err :</span>=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> des.NewCipher(password) </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">if</span> err !=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil { </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> nil, err } desRead :</span>=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> cipher.NewCFBDecrypter(block, desIV[:]) desWrite :</span>=<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> cipher.NewCFBEncrypter(block, desIV[:]) </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">return</span> &<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream{ reader: </span>&<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">cipher.StreamReader{ S: desRead, R: rwc, }, writeCloser: </span>&<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">cipher.StreamWriter{ S: desWrite, W: rwc, }, }, nil } } </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">return</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> stream, nil }</span></pre>
<span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; font-size: 12px !important; line-height: 1.5 !important;"></span>

如果没有指定加密方式或者是不支持的加密方式,还是沿用原有的io.Reader和io.Writer接口。这样的话就可以减少对上层的影响。

因为支持了多种加密方式,那么就得用配置文件了。这个就很简单,直接JSON格式搞定。

另外一个问题是,浏览器只认SOCKS协议,你现在加了加密,那你得对浏览器隐藏掉,然后就得支持俩模式,一个模式是本地的SOCKS运行方式,对浏览器提供的,另外一个模式就是到远端SOCKS服务的数据要加密。本来是想再写一个本地SOCKS,但是想了下,实在是毫无必要,因为这个程序的大部分功能在现有的代码基础上都能满足,那么一种实现方式就是代码拷贝出来,但是我要维护两分;另外一种方式就是在原有的代码上改。决定在现有代码上改,无非是在客户端的请求来了后,要先链接到自己的远端SOCKS服务,然后再提供数据中转服务。这里就涉及到怎么和远端服务交互的问题。

<h2 style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 21px;">和远端SOCKS交互的解决方法</h2>

为了解决这个问题,就得需要订协议。是否有必要增加协议呢?我第一个想法是好像没必要,第二个想法是真的没必要?真的没必要!因为SOCKS5的协议足够使用,我也不想因为要链接到远端还要增加一套额外的协议,而这个协议最终也会和SOCKS5差不多,实现出来的代码又会很蛋疼。所以决定使用SOCKS5协议,做成一个调用链的形式。客户端来的握手,本地SOCKS也到远端SOCKS上进行握手。客户端发来的CMD直接转发到远端SOCKS。然后中间走加密。这下子实现就变的非常简单了。定义了一个结构体RemoteSocks,匿名组合了*CipherStream,这样就可以直接走加密了。具体定义代码如下:

<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important; font-size: 12px !important;">type RemoteSocks <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); font-size: 12px !important; line-height: 1.5 !important;">struct</span><span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;"> { conn net.Conn </span>*<span style="margin: 0px; padding: 0px; font-size: 12px !important; line-height: 1.5 !important;">CipherStream }</span></pre>

然后同步代码到AWS上,编译开跑,然后发现很流畅。给基友狗眼坤分享了下,遇到了BUG,在他机器上链接YOUTUBE出现链接错误。然后想大概问题在哪,开始做代码REVIEW,发现自己实现的加密部分,RC4相关的部分读和写复用了同一个加密对象,然后这个对象又不是线程安全的,问题应该是这里了,修正掉,然后测试之,OK了。

<h2 style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); font-size: 21px;">总结</h2>

总算是有了自己的代理程序,用起来很开心,因为是自己做的!Eating your own dog food很重要。自己实现一个程序的时候,可以不用一下子跨很大的步子,那样容易扯到蛋,可以一点点来。


到此这篇关于“[Go]网络代理实现”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
想系统学习GO语言(Golang
深入Go语言网络库的基础实现
正在执行的goruntine发生阻塞,golang调度策略
Go语言微服务开发框架实践-go chassis(中篇)
golang url 收集
golang和python有什么区别?
Golang包管理详解
Go 开发关键技术指南 | 为什么你要选择 Go?(内含超全知识大图)
golang https 代理_goproxy.cn - 为中国 Go 语言开发者量身打造的模块代理
Go Module入门及Golang项目组织

[关闭]
~ ~