• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang shadowsocks.PipeThenClose函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/shadowsocks/shadowsocks-go/shadowsocks.PipeThenClose函数的典型用法代码示例。如果您正苦于以下问题:Golang PipeThenClose函数的具体用法?Golang PipeThenClose怎么用?Golang PipeThenClose使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了PipeThenClose函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: handleConnection

func handleConnection(conn net.Conn) {
	if debug {
		debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
	}
	closed := false
	defer func() {
		if !closed {
			conn.Close()
		}
	}()

	var err error = nil
	if err = handShake(conn); err != nil {
		log.Println("socks handshake:", err)
		return
	}
	rawaddr, addr, err := getRequest(conn)
	if err != nil {
		log.Println("error getting request:", err)
		return
	}
	// Sending connection established message immediately to client.
	// This some round trip time for creating socks connection with the client.
	// But if connection failed, the client will get connection reset error.
	_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
	if err != nil {
		debug.Println("send connection confirmation:", err)
		return
	}

	remote, err := createServerConn(rawaddr, addr)
	if err != nil {
		if len(servers.srvCipher) > 1 {
			log.Println("Failed connect to all avaiable shadowsocks server")
		}
		return
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()

	debug.Println("go ss.PipeThenClose(conn, remote)")
	go ss.PipeThenClose(conn, remote)

	debug.Println("ss.PipeThenClose(remote, conn)")
	ss.PipeThenClose(remote, conn)

	closed = true
	debug.Println("closed connection to", addr)
}
开发者ID:uin57,项目名称:shadowsocks-go-hack,代码行数:52,代码来源:local.go


示例2: handleConnection

func handleConnection(conn net.Conn) {
	if debug {
		debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
	}
	closed := false
	defer func() {
		if !closed {
			conn.Close()
		}
	}()

	var err error = nil
	// _ := conn.RemoteAddr().(*net.TCPAddr)
	destAddr, err := getSockOriginalDst(conn)
	if err != nil {
		log.Printf("get original addr error. err:%v", err)
		return
	}

	rawaddr := make([]byte, 1+4+4)
	w := bytes.NewBuffer(rawaddr)
	var adType int8 = 0x01
	binary.Write(w, binary.BigEndian, adType)
	w.Write(destAddr.IP.To4())
	var port int32 = int32(destAddr.Port)
	addr := destAddr.String()
	binary.Write(w, binary.BigEndian, port)
	remote, err := createServerConn(rawaddr, addr)
	if err != nil {
		if len(servers.srvCipher) > 1 {
			log.Println("Failed connect to all avaiable shadowsocks server")
		}
		return
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()

	go ss.PipeThenClose(conn, remote)
	ss.PipeThenClose(remote, conn)
	closed = true
	debug.Println("closed connection to", addr)
}
开发者ID:yangzhao28,项目名称:shadowsocks-go,代码行数:45,代码来源:local.go


示例3: handleConnection

func handleConnection(conn *ss.Conn) {
	var host string

	connCnt++ // this maybe not accurate, but should be enough
	if connCnt-nextLogConnCnt >= 0 {
		// XXX There's no xadd in the atomic package, so it's difficult to log
		// the message only once with low cost. Also note nextLogConnCnt maybe
		// added twice for current peak connection number level.
		log.Printf("Number of client connections reaches %d\n", nextLogConnCnt)
		nextLogConnCnt += logCntDelta
	}

	// function arguments are always evaluated, so surround debug statement
	// with if statement
	if debug {
		debug.Printf("new client %s->%s\n", conn.RemoteAddr().String(), conn.LocalAddr())
	}
	closed := false
	defer func() {
		if debug {
			debug.Printf("closed pipe %s<->%s\n", conn.RemoteAddr(), host)
		}
		connCnt--
		if !closed {
			conn.Close()
		}
	}()

	host, extra, err := getRequest(conn)
	if err != nil {
		log.Println("error getting request", conn.RemoteAddr(), conn.LocalAddr(), err)
		return
	}
	debug.Println("connecting", host)
	remote, err := net.Dial("tcp", host)
	if err != nil {
		if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) {
			// log too many open file error
			// EMFILE is process reaches open file limits, ENFILE is system limit
			log.Println("dial error:", err)
		} else {
			log.Println("error connecting to:", host, err)
		}
		return
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()
	// write extra bytes read from
	if extra != nil {
		// debug.Println("getRequest read extra data, writing to remote, len", len(extra))
		if _, err = remote.Write(extra); err != nil {
			debug.Println("write request extra error:", err)
			return
		}
	}
	if debug {
		debug.Printf("piping %s<->%s", conn.RemoteAddr(), host)
	}
	go ss.PipeThenClose(conn, remote, ss.SET_TIMEOUT)
	ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT)
	closed = true
	return
}
开发者ID:WaylandGod,项目名称:shadowsocks-go,代码行数:66,代码来源:server.go


示例4: Start

func (s ShadowsocksTransport) Start() error {
	handler := server.NewHandler(s.Name)

	listener, err := net.Listen("tcp", s.BindAddr)
	if err != nil {
		listener, err = net.Listen("tcp", "127.0.0.1:0")
	}
	if err != nil {
		handler.PrintError("CANNOT BIND TO ANY PORT")
		return errors.New("Cannot bind to any port")
	}

	listener = handler.MonitorListener(listener)
	handler.PrintExpose("socks5", listener.Addr().String())

	config := &ss.Config{
		Server:     s.RemoteHost,
		ServerPort: s.RemotePort,
		Password:   s.Secret,
		LocalPort:  s.BindPort,
		Method:     "aes-256-cfb",
	}

	parseServerConfig(config)

	handler.PrintDone()

	verbose := server.NewOption("verbose")

	if verbose.Has() {
		s.Verbose = true
		ss.SetDebug(true)
		debug = true
	}

	go func() {
		for {
			defer listener.Close()
			conn, err := listener.Accept()
			if err != nil {
				debug.Println("accept:", err)
				continue
			}
			go func(conn net.Conn) {
				conn = handler.MonitorConn(conn)
				if debug {
					debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
				}
				closed := false
				defer func() {
					if !closed {
						conn.Close()
					}
				}()

				var err error = nil
				if err = handShake(conn); err != nil {
					debug.Printf("socks handshake: %s", err)
					return
				}
				rawaddr, addr, err := getRequest(conn)
				if err != nil {
					debug.Println("error getting request:", err)
					return
				}
				// Sending connection established message immediately to client.
				// This some round trip time for creating socks connection with the client.
				// But if connection failed, the client will get connection reset error.
				_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
				if err != nil {
					debug.Println("send connection confirmation:", err)
					return
				}

				remote, err := createServerConn(rawaddr, addr)
				if err != nil {
					if len(servers.srvCipher) > 1 {
						debug.Println("Failed connect to all avaiable shadowsocks server")
					}
					return
				}
				handler.TrackOpenConn(addr)
				defer func() {
					if !closed {
						remote.Close()
					}
				}()

				go ss.PipeThenClose(conn, remote)
				ss.PipeThenClose(remote, conn)
				closed = true
				handler.TrackCloseConn(addr)

			}(conn)
		}
	}()
	handler.Wait()
	return nil
}
开发者ID:thomasf,项目名称:alkasir,代码行数:99,代码来源:service.go


示例5: handleConnection

func handleConnection(conn *ss.Conn, port string, pflag *uint32, openvpn string) {
	var host string

	newConnCnt := atomic.AddUint64(&connCnt, 1) // connCnt++
	if newConnCnt%logCntDelta == 0 {
		log.Printf("Number of client connections reaches %d\n", newConnCnt)
	}

	// function arguments are always evaluated, so surround debug statement
	// with if statement
	ss.Debug.Printf("new client %s->%s\n", conn.RemoteAddr().String(), conn.LocalAddr())
	closed := false
	defer func() {
		ss.Debug.Printf("closed pipe %s<->%s\n", conn.RemoteAddr(), host)
		atomic.AddUint64(&connCnt, ^uint64(0)) // connCnt--
		if !closed {
			conn.Close()
		}
	}()

	h, p, extra, err := getRequest(conn)
	if err != nil {
		log.Println("error getting request", conn.RemoteAddr(), conn.LocalAddr(), err)
		return
	}
	host = h + ":" + p
	ss.Debug.Println("connecting", host)
	addr, err := net.ResolveIPAddr("ip", h)
	if err != nil {
		log.Println(err)
		return
	}
	ip := addr.String()
	if (strings.HasPrefix(ip, "127.") && (p != "1194" || openvpn != "ok")) ||
		strings.HasPrefix(ip, "10.8.") || ip == "::1" {
		log.Printf("illegal connect to local network(%s)\n", ip)
		return
	}
	remote, err := net.Dial("tcp", net.JoinHostPort(ip, p))
	if err != nil {
		if ne, ok := err.(*net.OpError); ok && (ne.Err == syscall.EMFILE || ne.Err == syscall.ENFILE) {
			// log too many open file error
			// EMFILE is process reaches open file limits, ENFILE is system limit
			log.Println("dial error:", err)
		} else {
			log.Println("error connecting to:", host, err)
		}
		return
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()
	// write extra bytes read from
	if extra != nil {
		// Debug.Println("getRequest read extra data, writing to remote, len", len(extra))
		if _, err = remote.Write(extra); err != nil {
			ss.Debug.Println("write request extra error:", err)
			return
		}
	}
	ss.Debug.Printf("ping %s<->%s", conn.RemoteAddr(), host)
	go ss.PipeThenClose(conn, remote, ss.SET_TIMEOUT, pflag, port, "out")
	ss.PipeThenClose(remote, conn, ss.NO_TIMEOUT, pflag, port, "in")
	closed = true
	return
}
开发者ID:better0332,项目名称:my-shadowsocks-go,代码行数:68,代码来源:server.go



注:本文中的github.com/shadowsocks/shadowsocks-go/shadowsocks.PipeThenClose函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang shadowsocks.PrintVersion函数代码示例发布时间:2022-05-28
下一篇:
Golang shadowsocks.ParseConfig函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap