本文整理汇总了Golang中net.TCPConn.LocalAddr方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.LocalAddr方法的具体用法?Golang TCPConn.LocalAddr怎么用?Golang TCPConn.LocalAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.LocalAddr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewTcpTask
func (netservice *NetService) NewTcpTask(conn *net.TCPConn) {
fmt.Println(conn.RemoteAddr())
fmt.Println(conn.LocalAddr())
/*
go func (conn *net.TCPConn) {
readdata := make([]byte,1000)
for true {
_,err := conn.Read(readdata)
if err == nil {
fmt.Println("========",readdata)
msg := &MsgDefine.BaseMsg{}
proto.Unmarshal(readdata[4:],msg)
basemsg := msg.String()
fmt.Println("basemsg",basemsg)
fmt.Printf("%s===\n",basemsg)
} else {
fmt.Println("close================ error")
conn.Close()
break
}
}
}(conn)
*/
}
示例2: newRCConn
func newRCConn(c *net.TCPConn, iface *Interface) (*RCConn, error) {
// Leave enough room in the completion queue for any operation,
// including inline sends, to return an error. CQ overruns
// sometimes cause internal errors in the HCA, which can make the
// kernel very unhappy.
qp, err := iface.NewQueuePair(10)
if err != nil {
return nil, err
}
if err := c.SetDeadline(ioDeadline()); err != nil {
checkClose(qp)
return nil, err
}
destLid, destQpn, destPsn, err := writeReadQPParams(c, iface.Lid(), qp.Qpn(), qp.Psn())
if err != nil {
checkClose(qp)
return nil, err
}
messages, meta := CreateBuffers()
if err := qp.Setup(destLid, destQpn, destPsn, messages); err != nil {
checkClose(qp)
return nil, err
}
laddr, raddr := c.LocalAddr(), c.RemoteAddr()
rcc := &RCConn{iface, laddr, raddr, qp, math.MaxInt64, true, messages, meta, false}
return rcc, nil
}
示例3: copyBytes
func copyBytes(in, out *net.TCPConn) {
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
if _, err := io.Copy(in, out); err != nil {
glog.Errorf("I/O error: %v", err)
}
in.CloseRead()
out.CloseWrite()
}
示例4: proxyTCP
// proxyTCP proxies data bi-directionally between in and out.
func proxyTCP(in, out *net.TCPConn) {
var wg sync.WaitGroup
wg.Add(2)
glog.V(4).Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
go copyBytes("from backend", in, out, &wg)
go copyBytes("to backend", out, in, &wg)
wg.Wait()
}
示例5: GetOriginalDST
// GetOriginalDST retrieves the original destination address from
// NATed connection. Currently, only Linux iptables using DNAT/REDIRECT
// is supported. For other operating systems, this will just return
// conn.LocalAddr().
//
// Note that this function only works when nf_conntrack_ipv4 and/or
// nf_conntrack_ipv6 is loaded in the kernel.
func GetOriginalDST(conn *net.TCPConn) (*net.TCPAddr, error) {
f, err := conn.File()
if err != nil {
return nil, err
}
defer f.Close()
fd := int(f.Fd())
// revert to non-blocking mode.
// see http://stackoverflow.com/a/28968431/1493661
if err = syscall.SetNonblock(fd, true); err != nil {
return nil, os.NewSyscallError("setnonblock", err)
}
v6 := conn.LocalAddr().(*net.TCPAddr).IP.To4() == nil
if v6 {
var addr syscall.RawSockaddrInet6
var len uint32
len = uint32(unsafe.Sizeof(addr))
err = getsockopt(fd, syscall.IPPROTO_IPV6, IP6T_SO_ORIGINAL_DST,
unsafe.Pointer(&addr), &len)
if err != nil {
return nil, os.NewSyscallError("getsockopt", err)
}
ip := make([]byte, 16)
for i, b := range addr.Addr {
ip[i] = b
}
pb := *(*[2]byte)(unsafe.Pointer(&addr.Port))
return &net.TCPAddr{
IP: ip,
Port: int(pb[0])*256 + int(pb[1]),
}, nil
}
// IPv4
var addr syscall.RawSockaddrInet4
var len uint32
len = uint32(unsafe.Sizeof(addr))
err = getsockopt(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST,
unsafe.Pointer(&addr), &len)
if err != nil {
return nil, os.NewSyscallError("getsockopt", err)
}
ip := make([]byte, 4)
for i, b := range addr.Addr {
ip[i] = b
}
pb := *(*[2]byte)(unsafe.Pointer(&addr.Port))
return &net.TCPAddr{
IP: ip,
Port: int(pb[0])*256 + int(pb[1]),
}, nil
}
示例6: proxyTCP
// proxyTCP proxies data bi-directionally between in and out.
func proxyTCP(in, out *net.TCPConn) {
var wg sync.WaitGroup
wg.Add(2)
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
go copyBytes(in, out, &wg)
go copyBytes(out, in, &wg)
wg.Wait()
in.Close()
out.Close()
}
示例7: CopyBytes
func CopyBytes(in, out *net.TCPConn) {
log.Printf("Copying from %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
_, err := io.Copy(in, out)
if err != nil && err != io.EOF {
log.Printf("I/O error: %v", err)
}
in.CloseRead()
out.CloseWrite()
}
示例8: NewClient
func NewClient(conn *net.TCPConn) *Client {
client := new(Client)
client.conn = conn
client.wt = make(chan *Message, 10)
addr := conn.LocalAddr()
if taddr, ok := addr.(*net.TCPAddr); ok {
ip4 := taddr.IP.To4()
client.public_ip = int32(ip4[0])<<24 | int32(ip4[1])<<16 | int32(ip4[2])<<8 | int32(ip4[3])
}
return client
}
示例9: handleConnection
func (s *Server) handleConnection(c *net.TCPConn) {
defer c.Close()
var addr string
switch s.config.Mode {
case ModeNAT:
orig_addr, err := GetOriginalDST(c)
if err != nil {
log.Error(err.Error(), nil)
return
}
addr = orig_addr.String()
default:
addr = c.LocalAddr().String()
}
if log.Enabled(log.LvDebug) {
log.Debug("making proxy connection", map[string]interface{}{
"_dst": addr,
})
}
pconn, err := s.dialer.Dial("tcp", addr)
if err != nil {
log.Error(err.Error(), map[string]interface{}{
"_dst": addr,
})
return
}
defer pconn.Close()
ch := make(chan error, 2)
go copyData(c, pconn, ch)
go copyData(pconn, c, ch)
for i := 0; i < 2; i++ {
e := <-ch
if e != nil {
log.Error(e.Error(), map[string]interface{}{
"_dst": addr,
})
break
}
}
if log.Enabled(log.LvDebug) {
log.Debug("closing proxy connection", map[string]interface{}{
"_dst": addr,
})
}
}
示例10: StartGateway
func StartGateway(conn *net.TCPConn) {
logger.Logf("new client: %s->%s\n",
conn.RemoteAddr(),
conn.LocalAddr())
ircConn := NewIRCConn(conn, SERVERNAME)
lilyConn := NewLilyConn(LILYADDRESS)
dis := NewDispatcher(ircConn, lilyConn)
dis.Dispatch()
logger.Log("ending session")
}
示例11: proxyTCP
// proxyTCP proxies data bi-directionally between in and out.
func proxyTCP(logLevel int, in, out *net.TCPConn) {
var wg sync.WaitGroup
wg.Add(2)
if logLevel > 0 {
log.Printf("Creating proxy between %v <-> %v <-> %v <-> %v",
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
}
go copyBytes(logLevel, "from backend", in, out, &wg)
go copyBytes(logLevel, "to backend", out, in, &wg)
wg.Wait()
in.Close()
out.Close()
}
示例12: newTunnel
func newTunnel(conn *net.TCPConn, rc4key []byte) *Tunnel {
desc := fmt.Sprintf("tunnel[%s <-> %s]", conn.LocalAddr(), conn.RemoteAddr())
bufsize := int(PacketSize) * 2
tunnel := &Tunnel{
writer: bufio.NewWriterSize(NewRC4Writer(conn, rc4key), bufsize),
reader: bufio.NewReaderSize(NewRC4Reader(conn, rc4key), bufsize),
wch: make(chan Payload),
closed: make(chan struct{}),
conn: conn,
desc: desc,
}
go tunnel.pump()
return tunnel
}
示例13: serveTCP
func serveTCP(server *Server, conn *net.TCPConn, r int) {
var (
// timer
tr = server.round.Timer(r)
rp = server.round.Reader(r)
wp = server.round.Writer(r)
// ip addr
lAddr = conn.LocalAddr().String()
rAddr = conn.RemoteAddr().String()
)
if Debug {
log.Debug("start tcp serve \"%s\" with \"%s\"", lAddr, rAddr)
}
server.serveTCP(conn, rp, wp, tr)
}
示例14: newTunnel
func newTunnel(conn *net.TCPConn) *Tunnel {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 60)
conn.SetLinger(-1)
// conn.SetWriteBuffer(64 * 1024)
// conn.SetReadBuffer(64 * 1024)
desc := fmt.Sprintf("tunnel[%s <-> %s]", conn.LocalAddr(), conn.RemoteAddr())
return &Tunnel{
wlock: new(sync.Mutex),
writer: NewRC4Writer(conn, options.RC4Key),
rlock: new(sync.Mutex),
reader: NewRC4Reader(bufio.NewReaderSize(conn, 8192), options.RC4Key),
conn: conn,
desc: desc,
}
}
示例15: proxyTCPStream
func (p *Proxy) proxyTCPStream(ctx context.Context, src *net.TCPConn) {
srcRemoteAddr := src.RemoteAddr().(*net.TCPAddr)
srcLocalAddr := src.LocalAddr().(*net.TCPAddr)
route := p.routes.GetTable().Lookup(protocols.TCP,
srcRemoteAddr.IP, srcLocalAddr.IP,
uint16(srcRemoteAddr.Port), uint16(srcLocalAddr.Port))
if route == nil {
src.Close()
return
}
go func() {
dstAddr := net.TCPAddr{
IP: route.Outbound.DstIP,
Port: int(route.Outbound.DstPort),
}
dst, err := net.DialTCP("tcp", nil, &dstAddr)
if err != nil {
src.Close()
return
}
dst.SetKeepAlivePeriod(10 * time.Second)
src.SetKeepAlivePeriod(10 * time.Second)
go func() {
<-ctx.Done()
src.Close()
dst.Close()
}()
go func() {
defer dst.CloseWrite()
defer src.CloseRead()
io.Copy(dst, src)
}()
go func() {
defer src.CloseWrite()
defer dst.CloseRead()
io.Copy(src, dst)
}()
}()
}