当前位置: 首页>>代码示例>>Golang>>正文


Golang Conn.LocalAddr方法代码示例

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


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

示例1: handleConnection

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

	atomic.AddInt32(&connCnt, 1)
	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())
	}
	defer func() {
		if debug {
			debug.Printf("closing pipe %s<->%s\n", conn.RemoteAddr(), host)
		}
		atomic.AddInt32(&connCnt, -1)
		conn.Close()
	}()

	host, extra, err := getRequest(conn)
	if err != nil {
		log.Println("error getting request:", 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 {
			debug.Println("error connecting to:", host, err)
		}
		return
	}
	defer 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)
	}
	c := make(chan byte, 2)
	go ss.Pipe(conn, remote, c)
	go ss.Pipe(remote, conn, c)
	<-c // close the other connection whenever one connection is closed
	return
}
开发者ID:code2u,项目名称:shadowsocks-go,代码行数:60,代码来源:server.go

示例2: handleConnection

func handleConnection(user User, conn *ss.Conn) {
	var host string
	var size = 0
	var raw_req_header, raw_res_header []byte
	var is_http = false
	var res_size = 0
	var req_chan = make(chan []byte)

	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 is_http {
			tmp_req_header := <-req_chan
			buffer := bytes.NewBuffer(raw_req_header)
			buffer.Write(tmp_req_header)
			raw_req_header = buffer.Bytes()
		}
		showConn(raw_req_header, raw_res_header, host, user, size, is_http)
		close(req_chan)
		if !closed {
			remote.Close()
		}
	}()
	// write extra bytes read from

	is_http, extra, _ = checkHttp(extra, conn)
	if strings.HasSuffix(host, ":80") {
		is_http = true
	}
	raw_req_header = extra
	res_size, err = remote.Write(extra)
	storage.IncrSize("flow:"+user.Name, res_size)
	storage.ZincrbySize("flow:"+user.Name, host, res_size)
	size += res_size
	if err != nil {
		debug.Println("write request extra error:", err)
		return
	}

	if debug {
		debug.Printf("piping %s<->%s", conn.RemoteAddr(), host)
	}

	go func() {
		_, raw_header := PipeThenClose(conn, remote, is_http, false, host, user)
		if is_http {
			req_chan <- raw_header
		}
	}()

	res_size, raw_res_header = PipeThenClose(remote, conn, is_http, true, host, user)
	size += res_size
	closed = true
	return
}
开发者ID:tallriham,项目名称:shadowsocks-auth,代码行数:95,代码来源:server.go

示例3: handleConnection

func handleConnection(user user.User, conn *ss.Conn, auth bool) {
	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.Debug("Number of client connections reaches %d\n", nextLogConnCnt)
		nextLogConnCnt += logCntDelta
	}

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

	host, res_size, ota, err := getRequest(conn, auth)
	if err != nil {
		Log.Error("error getting request", conn.RemoteAddr(), conn.LocalAddr(), err)
		return
	}
	Log.Info(fmt.Sprintf("[port-%d]connecting %s ", user.GetPort(), 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.Error("dial error:", err)
		} else {
			Log.Error("error connecting to:", host, err)
		}
		return
	}
	defer func() {
		if !closed {
			remote.Close()
		}
	}()

	// debug conn info
	Log.Debug(fmt.Sprintf("%d conn debug:  local addr: %s | remote addr: %s network: %s ", user.GetPort(),
		conn.LocalAddr().String(), conn.RemoteAddr().String(), conn.RemoteAddr().Network()))
	err = storage.IncrSize(user, res_size)
	if err != nil {
		Log.Error(err)
		return
	}
	err = storage.MarkUserOnline(user)
	if err != nil {
		Log.Error(err)
		return
	}
	Log.Debug(fmt.Sprintf("[port-%d] store size: %d", user.GetPort(), res_size))

	Log.Info(fmt.Sprintf("piping %s<->%s ota=%v connOta=%v", conn.RemoteAddr(), host, ota, conn.IsOta()))

	if ota {
		go PipeThenCloseOta(conn, remote, false, host, user)
	} else {
		go PipeThenClose(conn, remote, false, host, user)
	}

	PipeThenClose(remote, conn, true, host, user)
	closed = true
	return
}
开发者ID:Xuen90,项目名称:shadowsocks-go,代码行数:76,代码来源:func.go

示例4: handleConnection

func handleConnection(conn *ss.Conn, auth bool) {
	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, ota, err := getRequest(conn, auth)
	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()
		}
	}()
	if debug {
		debug.Printf("piping %s<->%s ota=%v connOta=%v", conn.RemoteAddr(), host, ota, conn.IsOta())
	}
	if ota {
		go ss.PipeThenCloseOta(conn, remote)
	} else {
		go ss.PipeThenClose(conn, remote)
	}
	ss.PipeThenClose(remote, conn)
	closed = true
	return
}
开发者ID:CowLeo,项目名称:shadowsocks-go,代码行数:62,代码来源:server.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.Conn.LocalAddr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。