wzshiming / bridge

Bridge is a multi-level proxy that supports clients and servers with multiple protocols. SSHProxy, HTTPProxy, Socks4, Socks5, Shadowsocks.
MIT License
172 stars 17 forks source link

`nc -X 5 -x 127.0.0.1:10022 host port` equivalent implementation by bridge. #25

Open hongyi-zhao opened 3 months ago

hongyi-zhao commented 3 months ago

Can I use bridge to construct a git-protocol proxy which is equivalent to the following command?

nc -X 5 -x 127.0.0.1:10022 host port

Regards, Zhao

wzshiming commented 3 months ago

Yep

export GIT_SSH_COMMAND='ssh -o ProxyCommand="bridge -p %h:%p -p socks5://127.0.0.1:10022"'
hongyi-zhao commented 3 months ago

I mean to work with git protocol like the following:

$ GIT_PROXY_COMMAND=gitproxy.sh /usr/bin/git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
# or
$ /usr/bin/git -c 'core.gitProxy=gitproxy.sh' clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

And I have the following command currently in the executable script gitproxy.sh:

werner@x13dai-t:~$ which gitproxy.sh
/home/werner/.local/bin/gitproxy.sh

werner@x13dai-t:~$ grep -E '^[^#]' ` which gitproxy.sh `
exec nc -X 5 -x 127.0.0.1:10022 "$@"

I have tried the following in the above script:

bridge -p $1:$2 -p socks5://127.0.0.1:10022
#or
#exec bridge -p $1:$2 -p socks5://127.0.0.1:10022

But the test failed:

$ /usr/bin/git -c 'core.gitProxy=gitproxy.sh' clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Cloning into 'linux'...
2024/08/12 18:30:35 INFO DIAL "tcp://git.kernel.org:9418" <- "socks5://127.0.0.1:10022" <- LOCAL <- STDIO
 chains="{Bind:[] Proxy:[{Probe: LB:[git.kernel.org:9418]} {Probe: LB:[socks5://127.0.0.1:10022]}] IdleTimeout:0s}"
2024/08/12 18:30:37 ERROR BridgeWithConfig chains="{Bind:[] Proxy:[{Probe: LB:[git.kernel.org:9418]} {Probe: LB:[socks5://127.0.0.1:10022]}] IdleTimeout:0s}" err="writeto tcp 127.0.0.1:44846->127.0.0.1:10022: read tcp 127.0.0.1:44846->127.0.0.1:10022: use of closed network connection"
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

I wonder how to use bridge to achieve the same purpose.

BTW, the above error is exactly the same with the following command set in the above script:

ncat --proxy 127.0.0.1:10022 --proxy-type socks5 $1 $2

As shown below:

$ /usr/bin/git -c 'core.gitProxy=gitproxy.sh' clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Cloning into 'linux'...
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

But the following setting in the script works smoothly:

nc -X 5 -x 127.0.0.1:10022 $1 $2
#or
#exec nc -X 5 -x 127.0.0.1:10022 "$@"

As shown below:

$ /usr/bin/git -c 'core.gitProxy=gitproxy.sh' clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Cloning into 'linux'...
remote: Enumerating objects: 10343588, done.
remote: Counting objects: 100% (181/181), done.
remote: Compressing objects: 100% (89/89), done.
Receiving objects:   0% (62579/10343588), 32.68 MiB | 3.59 MiB/s
wzshiming commented 3 months ago

Back in the day, I researched git's 3 different proxy methods for 3 different protocols. I ended up keeping the http and ssh proxies. I can't remember why

hongyi-zhao commented 3 months ago

The git protocol doesn't seem to be very stable and robust. Here is the detailed debug information; I wonder if you can find any clues from it:

werner@x13dai-t:~$ GIT_TRACE=1 GIT_CURL_VERBOSE=1 GIT_TRACE_PERFORMANCE=1 GIT_TRACE_SETUP=1 /usr/bin/git -c 'core.gitProxy=gitproxy.sh' clone --verbose --progress git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
07:13:55.149579 git.c:455               trace: built-in: git clone --verbose --progress git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Cloning into 'linux'...
07:13:55.157708 run-command.c:668       trace: run_command: gitproxy.sh git.kernel.org 9418
2024/08/13 07:13:55 INFO DIAL "tcp://git.kernel.org:9418" <- "socks5://127.0.0.1:10022" <- LOCAL <- STDIO
 chains="{Bind:[] Proxy:[{Probe: LB:[git.kernel.org:9418]} {Probe: LB:[socks5://127.0.0.1:10022]}] IdleTimeout:0s}"
07:13:57.402731 run-command.c:668       trace: run_command: git index-pack --stdin -v --fix-thin '--keep=fetch-pack 10708 on x13dai-t' --check-self-contained-and-connected
2024/08/13 07:13:57 ERROR BridgeWithConfig chains="{Bind:[] Proxy:[{Probe: LB:[git.kernel.org:9418]} {Probe: LB:[socks5://127.0.0.1:10022]}] IdleTimeout:0s}" err="writeto tcp 127.0.0.1:37742->127.0.0.1:10022: read tcp 127.0.0.1:37742->127.0.0.1:10022: use of closed network connection"
fetch-pack: unexpected disconnect while reading sideband packet
07:13:57.420051 trace.c:387             setup: git_dir: /home/werner/linux/.git
07:13:57.420091 trace.c:388             setup: git_common_dir: /home/werner/linux/.git
07:13:57.420094 trace.c:389             setup: worktree: /home/werner
07:13:57.420096 trace.c:390             setup: cwd: /home/werner
07:13:57.420098 trace.c:391             setup: prefix: (null)
07:13:57.420100 git.c:455               trace: built-in: git index-pack --stdin -v --fix-thin '--keep=fetch-pack 10708 on x13dai-t' --check-self-contained-and-connected
fatal: early EOF
07:13:57.420618 trace.c:487             performance: 0.001764065 s: git command: /usr/lib/git-core/git index-pack --stdin -v --fix-thin '--keep=fetch-pack 10708 on x13dai-t' --check-self-contained-and-connected
fatal: fetch-pack: invalid index-pack output
07:13:57.423123 trace.c:487             performance: 2.273843841 s: git command: /usr/bin/git -c core.gitProxy=gitproxy.sh clone --verbose --progress git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git