Undefined: ctx was encountered while running go libp2p - go

The code is following,it is the an offical demo of go-libp2p.And I didn't encounter any other references or undefined errors
// if a remote peer has been passed on the command line, connect to it
// and send it 5 ping messages, otherwise wait for a signal to stop
if len(os.Args) > 1 {
addr, err := multiaddr.NewMultiaddr(os.Args[1])
if err != nil {
panic(err)
}
peer, err := peerstore.AddrInfoFromP2pAddr(addr)
if err != nil {
panic(err)
}
if err := node.Connect(ctx, *peer); err != nil {
panic(err)
}
fmt.Println("sending 5 ping messages to", addr)
The import is following:
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/libp2p/go-libp2p"
peerstore "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
multiaddr "github.com/multiformats/go-multiaddr"
)

Looks like you are following the "Getting Started" tutorial.
You'll need to import context and prior to the code block in your question, you'll need to create a context:
// create context
ctx:=context.Background()
// if a remote peer has been passed on the command line, connect to it
// and send it 5 ping messages, otherwise wait for a signal to stop
if len(os.Args) > 1 {
addr, err := multiaddr.NewMultiaddr(os.Args[1])
if err != nil {
panic(err)
}
peer, err := peerstore.AddrInfoFromP2pAddr(addr)
if err != nil {
panic(err)
}
if err := node.Connect(ctx, *peer); err != nil {
panic(err)
}
fmt.Println("sending 5 ping messages to", addr)

Related

Why does net.Conn.close() seem to be closing at the wrong time?

I'm trying to read and write some commands from a TCP client. I want to close a connection after the last function has been executed but for some reason, it seems like the server disconnects the connection in the middle of the function even when explicitly placed afterward.
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
"time"
)
func main() {
listener, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
}
go handleConn(conn)
conn.Close()
}
}
func handleConn(someconnection net.Conn) {
func1(someconnection)
func2(someconnection) //connection drops in the middle of executing this part
}
func func2(someconnection net.Conn) {
//send message(a string)
_, err := io.WriteString(someconnection, dosomething)
if err != nil {
log.Fatal(err)
}
//await reply
//send another message
_, err = io.WriteString(someconnection, dosomething)
if err != nil {
log.Fatal(err)
}
//await reply
//send another message, connection tends to close somewhere here
_, err = io.WriteString(someconnection, dosomething)
if err != nil {
log.Fatal(err)
}
//await,send
_, err = io.WriteString(someconnection, do something)
if err != nil {
log.Fatal(err)
}
//await, read and print message
c := bufio.NewReader(someconnection)
buff1 := make([]byte, maxclientmessagelength)
buff1, err = c.ReadBytes(delimiter)
fmt.Printf("\n%s\n", buff1)
_, err = io.WriteString(someconnection, dosomething)
if err != nil {
log.Fatal(err)
}
}
That means the client trying to communicate backward simply isn't able to communicate but the program runs to the end.
Update 1:
Made some progress by placing the deferred close statement to when the connection was first acquired.
func main() {
listener, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
}
defer conn.Close()
go handleConn(conn)
}}
Now it doesn't necessarily close within the second I hope it to close but at least it now runs all the way through.
Goroutines are asynchronous so after calling handleConn here:
go handleConn(conn)
conn.Close()
the main function continues to execute and closes the connection.
Try just calling the handleConn function regularly (without the go).
The conn.Close needs to de done AFTER handleConn has done its work. You could communicate the back to the main thread using channels but that would be too complex (and also block execution of main thread). This is how it should be done
func main() {
listener, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
}
go handleConn(conn)
// REMOVE BELOW LINE
// conn.Close()
}
}
Add conn.Close inside handleConn
func handleConn(someconnection net.Conn) {
// ADD BELOW LINE
defer someconnection.Close()
func1(someconnection)
func2(someconnection)
}
This makes sure conn.Close is called AFTER func1 and func2 are done executing

Sending binaries or strings by a client socket

I'm studying networks, and I'm doing a tcp server with Go. One of the challenges I'm studying is to send binaries or strings by a socket client to a server, save the server response to a txt, and compare it to the original data that was sent.
The problem is that the binaries do not arrive completely on the server.
Server
package main
import (
"fmt"
"io"
"log"
"net"
)
func main() {
l, err := net.Listen("tcp", ":8000")
if nil != err {
log.Println(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if nil != err {
log.Println(err)
continue
}
defer conn.Close()
go ConnHandler(conn)
}
}
func ConnHandler(conn net.Conn) {
recvBuf := make([]byte, 4096)
for {
n, err := conn.Read(recvBuf)
if nil != err {
if io.EOF == err {
log.Println(err)
return
}
log.Println(err)
return
}
if 0 < n {
data := recvBuf[:n]
fmt.Println(string(data))
}
}
}
Client
package main
import (
"fmt"
"log"
"net"
)
func main() {
conn, err := net.Dial("tcp", ":8000")
if nil != err {
log.Println(err)
}
var s string
fmt.Scanln(&s)
conn.Write([]byte(s))
conn.Close()
}
I'm generating the binaries using the command on linux:
head -c100000 /dev/urandom > binary_message.txt
I run the server:
./server > result.txt
And I send this data by the client using:
./client < binary_data.txt
In the end the file binary_data.txt have 98KB but the result .txt only has 0KB.
The problem is with scanning the binary from input. You didn't see it because the errors were ignored and not printed or otherwise handled. fmt.Scanln returns an error (so does the Write function). You should always check for possible errors happening.
I rewrote the client to load the file from disk itself as I don't think using stdin is a good fit for binary data.
package main
import (
"flag"
"io"
"log"
"net"
"os"
)
var fileName = flag.String("file", "", "file to send")
func main() {
flag.Parse()
conn, err := net.Dial("tcp", ":8000")
if nil != err {
log.Println(err)
}
defer conn.Close()
f, err := os.Open(*fileName)
if err != nil {
log.Println(err)
return
}
defer f.Close()
b := make([]byte, 1024)
for {
n, err := f.Read(b)
if err != nil {
if err == io.EOF {
log.Println("Done sending")
return
}
log.Println(err)
return
}
if _, err := conn.Write(b[:n]); err != nil {
log.Println(err)
return
}
}
}
You can use it with:
go run . -file=binary_message.txt
or if you have built the binary:
./client -file=binary_message.txt
I suggest you do the same for the server. Open a file for writing and write the binary data into that file. Use a flag to pass in the filename to write to. That will be cleaner than piping stdout to a file.

Operation timeout with SSH connection

I am trying to connect to a remote EC2-server through Go code using a PEM key provided by AWS. I am able to log in to the server through the command line using the PEM key.
I have done the following so far.
package main
import (
"io"
"io/ioutil"
"os"
"time"
"golang.org/x/crypto/ssh"
)
func publicKey(path string) ssh.AuthMethod {
key, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
panic(err)
}
return ssh.PublicKeys(signer)
}
func runCommand(cmd string, conn *ssh.Client) {
sess, err := conn.NewSession()
if err != nil {
panic(err)
}
defer sess.Close()
sessStdOut, err := sess.StdoutPipe()
if err != nil {
panic(err)
}
go io.Copy(os.Stdout, sessStdOut)
sessStderr, err := sess.StderrPipe()
if err != nil {
panic(err)
}
go io.Copy(os.Stderr, sessStderr)
err = sess.Run(cmd) // eg., /usr/bin/whoami
if err != nil {
panic(err)
}
}
func main() {
config := &ssh.ClientConfig{
User: "ec2-user",
Auth: []ssh.AuthMethod{
publicKey("mykey"),
},
Timeout: 15 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", "remote-server:22", config)
if err != nil {
panic(err.Error())
}
defer conn.Close()
runCommand("whoami", conn)
}
I keep getting the following error. What am I missing?
panic: ssh: handshake failed: read tcp "localhost"->"remotehost:22": read: operation timed out
In the above message I have replaced the IP addresses of the actual machines with bogus names.
I was able to resolve this since I was using an incorrect port to connect to :). I should be using port 22 for SSH, but was using some other service port.
Thanks.

golang simple server with interactive prompt

I am trying to build a simple CLI interface for my go process, I need to listen and serve on TCP port and get stats when connected to the port on TCP . I came across 'terminal' package to help with Password prompt, command history and other things. I tried to write up some code to achieve this. Although my client is not receiving any prompt or server is stuck on 'ReadLine' method. Any helpers?
package main
import (
"bufio"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"net"
_ "time"
)
func handleConnection(conn net.Conn) error {
fmt.Println("Handling new connection...")
defer func() {
fmt.Println("Closing connection...")
conn.Close()
}()
r := bufio.NewReader(conn)
w := bufio.NewWriter(conn)
rw := bufio.NewReadWriter(r, w)
term := terminal.NewTerminal(rw, "")
term.SetPrompt(string(term.Escape.Red) + "> " + string(term.Escape.Reset))
rePrefix := string(term.Escape.Cyan) + "Human says:" + string(term.Escape.Reset)
line := "welcome"
fmt.Fprintln(term, rePrefix, line)
for {
line, err := term.ReadLine()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
if line == "" {
continue
}
fmt.Fprintln(term, rePrefix, line)
}
}
func main() {
// Start listening to port 8888 for TCP connection
listener, err := net.Listen("tcp", ":8888")
if err != nil {
fmt.Println(err)
return
}
defer func() {
listener.Close()
fmt.Println("Listener closed")
}()
for {
// Get net.TCPConn object
conn, err := listener.Accept()
if err != nil {
fmt.Println(err)
break
}
go handleConnection(conn)
}
}

Golang - Filtering Stdout of Active SSH Session

I'm writing an SSH client in Go that connects to a switch and runs a list of configuration commands. So far, I am able to successfully connect to a switch, run the desired commands, and print the output of the session. The problem comes when the switch is expecting a \n, space, or "q" to be entered when a command's output is too long. For example:
switch#show int status
Port Name Status Vlan Duplex Speed Type
Gi1/0/1 notconnect 100 auto auto 10/100/1000BaseTX
Gi1/0/2 connected 915 a-full a-100 10/100/1000BaseTX
Gi1/0/3 notconnect 100 auto auto 10/100/1000BaseTX
Gi1/0/4 notconnect 100 auto auto 10/100/1000BaseTX
Gi1/0/5 notconnect 230 auto auto 10/100/1000BaseTX
...
Po1 sw-sww-100-sww-0-0 connected trunk a-full 10G
--More-- # Program hangs here; expecting a new line, space, or 'q'
The --More-- prompt is not actually printed to the screen, so simply checking if the current line of Stdout contains --More-- and sending a \n, space, or "q" does not work.
In addition to fixing this problem, I'd like to filter Stdout of the session so that the only thing printed is the output of each command. In other words, I don't want the switch's prompt to be printed to the terminal.
To sum up:
How to capture and print only the output of each command?
How to send a new line, space, or letter "q" when prompted?
Any help is appreciated. Here is my code:
package main
import (
"bufio"
"fmt"
"golang.org/x/crypto/ssh"
"io"
"log"
"os"
"time"
)
type Device struct {
Config *ssh.ClientConfig
Client *ssh.Client
Session *ssh.Session
Stdin io.WriteCloser
Stdout io.Reader
Stderr io.Reader
}
func (d *Device) Connect() error {
client, err := ssh.Dial("tcp", os.Args[1]+":22", d.Config)
if err != nil {
return err
}
session, err := client.NewSession()
if err != nil {
return err
}
sshIn, err := session.StdinPipe()
if err != nil {
return err
}
sshOut, err := session.StdoutPipe()
if err != nil {
return err
}
sshErr, err := session.StderrPipe()
if err != nil {
return err
}
d.Client = client
d.Session = session
d.Stdin = sshIn
d.Stdout = sshOut
d.Stderr = sshErr
return nil
}
func (d *Device) SendCommand(cmd string) error {
if _, err := io.WriteString(d.Stdin, cmd+"\r\n"); err != nil {
return err
}
return nil
}
func (d *Device) SendConfigSet(cmds []string) error {
for _, cmd := range cmds {
if _, err := io.WriteString(d.Stdin, cmd+"\r\n"); err != nil {
return err
}
time.Sleep(time.Second)
}
return nil
}
func (d *Device) PrintOutput() {
r := bufio.NewReader(d.Stdout)
for {
text, err := r.ReadString('\n')
fmt.Printf("%s", text)
if err == io.EOF {
break
}
}
}
func (d *Device) PrintErr() {
r := bufio.NewReader(d.Stderr)
for {
text, err := r.ReadString('\n')
fmt.Printf("%s", text)
if err == io.EOF {
break
}
}
}
func main() {
sshConf := ssh.Config{}
sshConf.Ciphers = append(sshConf.Ciphers, "aes128-cbc", "3des-cbc", "blowfish-cbc", "arcfour")
config := &ssh.ClientConfig{
Config: sshConf,
User: "mwalto7",
Auth: []ssh.AuthMethod{
ssh.Password("Lion$Tiger$Bear$"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Second * 5,
}
sw := &Device{Config: config}
fmt.Println("Connecting to ", os.Args[1])
if err := sw.Connect(); err != nil {
log.Fatal(err)
}
defer sw.Client.Close()
defer sw.Session.Close()
defer sw.Stdin.Close()
if err := sw.Session.Shell(); err != nil {
log.Fatal(err)
}
commands := []string{"show int status", "exit"}
if err := sw.SendConfigSet(commands); err != nil {
log.Fatal(err)
}
sw.Session.Wait()
sw.PrintOutput()
sw.PrintErr()
}
I fixed this problem by sending the command terminal length 0 just after connecting to the switch, before sending the rest of the commands. This disabled the —More— prompt by setting the switch’s terminal height to 0, allowing the whole output of a command to be displayed.

Resources