1 Star 0 Fork 216

bear256 / chuantou

forked from 骁龙 / chuantou 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
client.go 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
骁龙 提交于 2017-03-17 15:42 . 多协程版本
package main
import (
"flag"
"fmt"
"net"
"os"
"runtime"
)
var host *string = flag.String("host", "127.0.0.1", "target host or address")
func pass_through(server net.Conn, browser chan net.Conn, ask chan bool) {
retur := true
b := make([]byte, 10240)
var brow net.Conn
for {
n, err := server.Read(b)
if retur {
brow, err = net.Dial("tcp", "127.0.0.1:80")
if err != nil {
fmt.Printf("Unable to start dial, %v\n", err)
os.Exit(1)
}
browser <- brow
ask <- true
}
retur = false
if err != nil {
break
}
if n > 0 {
brow.Write(b[:n])
}
}
fmt.Println("远程服务器其中一条tcp以关闭,关闭此条tcp链接")
server.Close()
brow.Close()
}
func pass_through1(server net.Conn, browser chan net.Conn) {
b := make([]byte, 10240)
brow := <-browser
for {
n, err := brow.Read(b)
if err != nil {
break
}
if n > 0 {
server.Write(b[:n])
}
}
fmt.Println("本地网站其中一条tcp以关闭,关闭此条tcp链接")
server.Close()
brow.Close()
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
browser := make(chan net.Conn)
ask := make(chan bool)
flag.Parse()
if flag.NFlag() != 1 {
fmt.Printf("usage: -host 远程服务器ip\n")
flag.PrintDefaults()
os.Exit(1)
}
target := net.JoinHostPort(*host, "2000")
for {
server, err := net.Dial("tcp", target)
if err != nil {
fmt.Printf("Unable to dial server, %v\n", err)
os.Exit(1)
}
go pass_through(server, browser, ask)
go pass_through1(server, browser)
<-ask
}
}
Go
1
https://gitee.com/bear256/chuantou.git
git@gitee.com:bear256/chuantou.git
bear256
chuantou
chuantou
master

搜索帮助