This repository has been archived on 2024-07-06. You can view files and clone it, but cannot push or open issues or pull requests.
go-playit/tunnel/tcp_tunnel.go
Matheus Sampaio Queiroga 37207e9678
Stash code
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-06-01 16:03:52 -03:00

39 lines
812 B
Go

package tunnel
import (
"fmt"
"net"
"sirherobrine23.org/playit-cloud/go-playit/proto"
)
type TcpTunnel struct {
ClaimInstruction proto.ClaimInstructions
}
func (tcpTunnel *TcpTunnel) Connect() (*net.TCPConn, error) {
debug.Printf("Conecting to %q\n", tcpTunnel.ClaimInstruction.Address.String())
conn, err := net.DialTCP("tcp", nil, net.TCPAddrFromAddrPort(tcpTunnel.ClaimInstruction.Address))
if err != nil {
if conn != nil {
conn.Close()
}
return nil, err
}
_, err = conn.Write(tcpTunnel.ClaimInstruction.Token)
if err != nil {
conn.Close()
return nil, err
}
buff := make([]byte, 8)
size, err := conn.Read(buff)
if err != nil {
conn.Close()
return nil, err
} else if size != 8 {
conn.Close()
return nil, fmt.Errorf("invalid response reader size")
}
return conn, nil
}