通过代理ssh方式clone gitlab代码

背景:办公网不能直接访问gitlab机器,需要代理访问,并且要走ssh协议,且命令行git clone git域名(不能带端口号)

以下方案基于git ssh配置免登陆配置。

方案一:nginx 代理(nginx机器:172.16.204.129、gitlab机器:172.16.204.128,客户端测试机器:172.16.204.129)

因为git clone既然要走ssh协议,那nginx代理只能用tcp代理,且端口不能http服务的端口重复。又因为是ssh协议,假如不带端口号clone代码,默认是22端口而不是80端口,故nginx要起22端口(ssh 服务需要另起端口)。需要注意,这里的nginx必须是1.9以上,且编译时要有--with-stream --with-stream_ssl_module参数。

下边这段配置是写在nginx.conf里的,需要注意stream是tcp代理,和http代理是平级的,所以要写在http块之外,可以写在最后就没问题了。

这里有个小坑,nginx的超时时间一定要配长一些,因为代码量级无法评估,最好是配成1分钟以上。若clone代码时间大于超时时间,就会报错,无法clone代码。

stream
{
    upstream gitlab
    {
    server 172.16.204.128:22; (gitlab测试服务器)
    }
server
    {
    listen 22; (nginx proxy:172.16.204.129)
    proxy_connect_timeout 60s;
    proxy_timeout 60s;
    proxy_pass gitlab;
    }
}
 

命令行clone代码:

# git clone ssh://git@172.16.204.129/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 0), reused 0 (delta 0)
接收对象中: 100% (22/22), 2.01 KiB | 0 bytes/s, 完成.
检查连接... 完成。
 

方案二:ssh隧道(gitlab机器:172.16.204.128、proxy机器:172.16.204.130,客户端测试机器:172.16.204.129)

gitlab服务配置ssh正向代理:

ssh -fCNR 8889:localhost:22 root@172.16.204.130(proxy)
 

proxy机器配置反向代理,又因为默认git clone ssh://端口号是22,原ssh服务需另起端口如222:

ssh -p 222 -fCNL *:22:localhost:8889 localhost
 

命令行clone代码:

# git clone ssh://git@172.16.204.130/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 0), reused 0 (delta 0)
接收对象中: 100% (22/22), 完成.
检查连接... 完成。
 

方案三:iptables + haproxy(haproxy机器:172.16.204.130、gitlab机器:172.16.204.128、客户端测试机器:172.16.204.129)

iptables: 办公网外有防火墙限制,这里模拟把访问haproxy 22端口的请求转发到8888端口。

172.16.204.130(haproxy 机器,模拟防火墙转发)
iptables -t nat -A PREROUTING --dst 172.16.204.130 -p tcp --dport 22 -j DNAT --to-destination 172.16.204.130:8888
 

haproxy: 下边这段配置直接写在/etc/haproxy/haproxy.cfg 最后(我是yum安装的haproxy)

listen test1
        bind 0.0.0.0:8888
        mode tcp
        #maxconn 4086
        #log 127.0.0.1 local0 debug
        server s1 172.16.204.128:22
172.16.204.128(gitlab机器)
 

客户端测试:

# git clone ssh://git@172.16.204.130/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
接收对象中: 100% (22/22), 完成.
remote: Total 22 (delta 0), reused 0 (delta 0)
检查连接... 完成。
 
原文地址:https://www.cnblogs.com/Caesary/p/7771319.html